久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

glClearColor 無(wú)法正常工作(android opengl)

glClearColor not working correct (android opengl)(glClearColor 無(wú)法正常工作(android opengl))
本文介紹了glClearColor 無(wú)法正常工作(android opengl)的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我想在運(yùn)行時(shí)更改應(yīng)用的背景顏色.所以在按鈕點(diǎn)擊我第一次打電話:

I want to change the background color of my app on runtime. So on button click I first call:

GLES20.glClearColor(color[0], color[1], color[2], color[3]);

然后我打電話:

GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);

它什么也沒(méi)做!它保持當(dāng)前的背景顏色 - 不會(huì)改變它.但是當(dāng)我暫停我的應(yīng)用程序并再次恢復(fù)它時(shí),背景顏色會(huì)發(fā)生變化.

And it does nothing! It keeps the current background color - doesnt change it. But when I then pause my app and resume it again the background color is changed.

我找到了一種方法.每一幀我首先調(diào)用 glClear 但我沒(méi)有調(diào)用 glClearColor.因此,如果我在調(diào)用 glClear 之前先調(diào)用 glClearColor 每一幀,它就可以工作.但這對(duì)我來(lái)說(shuō)仍然沒(méi)有意義,我想避免在每一幀調(diào)用 glClearColor,我想如果我想改變顏色時(shí)調(diào)用一次就足夠了.

I found out a way to do it. Each frame i first call glClear but I dident call glClearColor. So if I first call glClearColor each frame before I call glClear it works. But this still doesnt make sense to me, I wanted to avoid calling glClearColor at each frame, thought it would be enough if I call it once when I want to change the color.

推薦答案

您只能在擁有當(dāng)前 OpenGL 上下文時(shí)進(jìn)行 OpenGL 調(diào)用.當(dāng)您使用 GLSurfaceView 時(shí),上下文處理會(huì)為您處理好,所以這一切似乎都神奇地起作用了.直到出現(xiàn)問(wèn)題,就像你的情況一樣.讓我仍然解釋一下幕后發(fā)生的事情,而不是只給你解決方案,以避免將來(lái)出現(xiàn)意外.

You can only make OpenGL calls while you have a current OpenGL context. When you use GLSurfaceView, context handling is taken care for you, so it all just magically seems to work. Until something goes wrong, like in your case. Instead of giving you only the solution, let me still explain what happens under the hood, to avoid future surprises.

在進(jìn)行任何 OpenGL 調(diào)用之前,需要?jiǎng)?chuàng)建一個(gè) OpenGL 上下文,并將其設(shè)置為當(dāng)前上下文.在 Android 上,這使用 EGL API.GLSurfaceView 會(huì)為您處理,這一切都發(fā)生在您的渲染器上調(diào)用 onSurfaceCreated() 之前.因此,當(dāng)調(diào)用 Renderer 實(shí)現(xiàn)中的方法時(shí),您始終可以指望擁有當(dāng)前上下文,而不必?fù)?dān)心它.

Before you can make any OpenGL calls, an OpenGL context needs to be created, and be set as the current context. On Android, this uses the EGL API. GLSurfaceView handles that for you, and this all happens before onSurfaceCreated() is called on your renderer. So when the methods on your Renderer implementation are called, you can always count on having a current context, without ever having to worry about it.

然而,關(guān)鍵方面是當(dāng)前上下文是每個(gè)線程的.GLSurfaceView創(chuàng)建一個(gè)渲染線程,所有Renderer方法都在這個(gè)線程中調(diào)用.

The crucial aspect is however that the current context is per thread. GLSurfaceView creates a rendering thread, and all the Renderer methods are invoked in this thread.

這樣做的結(jié)果是您無(wú)法從其他線程進(jìn)行 OpenGL 調(diào)用,因?yàn)樗鼈儧](méi)有當(dāng)前的 OpenGL 上下文.其中包括 UI 線程.這正是你試圖做的.如果您調(diào)用 glClearColor() 以響應(yīng)按鈕單擊,則您處于 UI 線程中,并且您沒(méi)有當(dāng)前的 OpenGL 上下文.

The consequence of this is that you cannot make OpenGL calls from other threads, because they do not have a current OpenGL context. Which includes the UI thread. This is exactly what you were attempting to do. If you make the glClearColor() call in response to a button click, you are in the UI thread, and you do not have a current OpenGL context.

您已經(jīng)找到的解決方法實(shí)際上可能是這種情況下最現(xiàn)實(shí)的解決方案.glClearColor() 應(yīng)該是一個(gè)便宜的調(diào)用,所以在每個(gè) glClear() 之前調(diào)用它并不重要.如果您需要采取的操作更昂貴,您還可以在值更改時(shí)設(shè)置一個(gè)布爾標(biāo)志,然后如果設(shè)置了該標(biāo)志,則僅在 onDrawFrame() 中進(jìn)行相應(yīng)的工作.

The workaround you already found might in fact be the most realistic solution in this case. glClearColor() should be a cheap call, so making it before every glClear() will not be significant. If the action you needed to take were more expensive, you could also set a boolean flag when the value changed, and then only do the corresponding work in onDrawFrame() if the flag is set.

這里還有另一個(gè)微妙但非常重要的方面:線程安全.一旦您在一個(gè)線程(UI 線程)中設(shè)置值并在另一個(gè)線程(渲染線程)中使用它們,這是您必須擔(dān)心的事情.假設(shè)背景顏色的 RGB 分量有 3 個(gè)值,并在 UI 線程中一一設(shè)置.渲染線程可能會(huì)在 UI 線程設(shè)置它們時(shí)使用這 3 個(gè)值,最終混合使用舊值和新值.

There's another subtle but very important aspect here: thread safety. As soon as you set values in one thread (UI thread) and use them in another thread (rendering thread), this is something you have to worry about. Say if you have 3 values for the RGB components of the background color, and you set them in the UI thread one by one. It's possible that the rendering thread uses the 3 values while the UI thread is setting them, ending up with a mix of old and new values.

為了說(shuō)明這一切,我將使用您的示例,并勾勒出一個(gè)有效且線程安全的解決方案.所涉及的班級(jí)成員可能如下所示:

To illustrate all of this, I'll use your example, and sketch out a working and thread safe solution. The class members involved could look like this:

float mBackRed, mBackGreen, mBackBlue;
boolean mBackChanged;
Object mBackLock = new Object();

然后在 UI 線程中設(shè)置值的位置:

Then where you set the value in the UI thread:

synchronized(mBackLock) {
    mBackRed = ...;
    mBackGreen = ...;
    mBackBlue = ...;
    mBackChanged = true;
}

并且在調(diào)用glClear()之前的onDrawFrame()方法中:

And in the onDrawFrame() method before calling glClear():

Boolean changed = false;
float backR = 0.0f, backG = 0.0f, backB = 0.0f;
synchronized(mBackLock) {
    if (mBackChanged) {
        changed = true;
        backR = mBackRed;
        backG = mBackGreen;
        backB = mBackBlue;
        mBackChanged = false;
    }
}

if (changed) {
    glClearColor(backR, backG, backB, 0.0f);
}

注意對(duì)兩個(gè)線程共享的類成員的所有訪問(wèn)是如何在鎖內(nèi)的.在最后一個(gè)代碼片段中,還要注意顏色值是如何在使用之前復(fù)制到局部變量中的.對(duì)于這個(gè)簡(jiǎn)單的示例,這可能太過(guò)分了,但我想說(shuō)明應(yīng)該盡可能簡(jiǎn)短地持有鎖的一般目標(biāo).如果直接使用成員變量,則必須在鎖內(nèi)調(diào)用 glClearColor().如果這是一個(gè)可能需要很長(zhǎng)時(shí)間的操作,則 UI 線程無(wú)法更新值,并且可能會(huì)卡住一段時(shí)間以等待鎖定.

Note how all access to the class members shared by the two threads is inside a lock. In the last code fragment, also note how the color values are copied to local variables before being used. This may be going too far for this simple example, but I wanted to illustrate the general goal that the lock should be held as briefly as possible. If you use the member variables directly, you would have to make the glClearColor() call inside the lock. If this is an operation that could take a long time, the UI thread could not update the values, and might be stuck for a while waiting for the lock.

還有另一種使用鎖的方法.GLSurfaceView 有一個(gè) queueEvent() 方法,允許您傳入一個(gè) Runnable,然后在渲染線程中執(zhí)行該方法.GLSurfaceView 文檔中有一個(gè)例子,所以我不會(huì)在這里拼出它的代碼.

There is an alternate approach to using a lock. GLSurfaceView has a queueEvent() method that allows you to pass in a Runnable that will then be executed in the rendering thread. There is an example for this in the GLSurfaceView documentation, so I won't spell out code for it here.

這篇關(guān)于glClearColor 無(wú)法正常工作(android opengl)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動(dòng)生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫(kù))
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對(duì)象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 极品电影院 | 日韩二区| 91精品国产综合久久久久久丝袜 | 亚洲电影一区二区三区 | 国产不卡视频在线 | 成人免费视频一区二区 | 亚洲欧美成人在线 | 日韩中文在线视频 | 一区二区av | 白浆在线 | 国产精品久久午夜夜伦鲁鲁 | 国产美女视频一区 | 91精品在线播放 | 久久免费精品 | 日韩中文久久 | 亚洲一区二区三区四区五区午夜 | 免费国产网站 | 欧美日韩中文字幕在线 | 亚洲va在线va天堂va狼色在线 | 亚洲国产aⅴ成人精品无吗 欧美激情欧美激情在线五月 | 亚洲视频在线观看 | 免费一区 | 欧美小视频在线观看 | 一级毛片成人免费看a | 久久久www成人免费无遮挡大片 | 国产精品伦一区二区三级视频 | 国产精品日韩在线观看一区二区 | 亚洲精品一区二区三区蜜桃久 | 亚洲一区二区三区在线视频 | 国产精品久久久久久久久久久久久 | 成人黄页在线观看 | 米奇狠狠鲁| 91视视频在线观看入口直接观看 | 美女在线视频一区二区三区 | 精品二 | 一区二区视屏 | 国产成人福利在线观看 | 中文字幕在线观看国产 | 久草视频在 | 国产精品久久久久久久久久三级 | 九一精品|