問題描述
我想在運行時更改應用的背景顏色.所以在按鈕點擊我第一次打電話:
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);
它什么也沒做!它保持當前的背景顏色 - 不會改變它.但是當我暫停我的應用程序并再次恢復它時,背景顏色會發生變化.
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.
我找到了一種方法.每一幀我首先調用 glClear
但我沒有調用 glClearColor
.因此,如果我在調用 glClear
之前先調用 glClearColor
每一幀,它就可以工作.但這對我來說仍然沒有意義,我想避免在每一幀調用 glClearColor
,我想如果我想改變顏色時調用一次就足夠了.
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.
推薦答案
您只能在擁有當前 OpenGL 上下文時進行 OpenGL 調用.當您使用 GLSurfaceView
時,上下文處理會為您處理好,所以這一切似乎都神奇地起作用了.直到出現問題,就像你的情況一樣.讓我仍然解釋一下幕后發生的事情,而不是只給你解決方案,以避免將來出現意外.
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.
在進行任何 OpenGL 調用之前,需要創建一個 OpenGL 上下文,并將其設置為當前上下文.在 Android 上,這使用 EGL API.GLSurfaceView
會為您處理,這一切都發生在您的渲染器上調用 onSurfaceCreated()
之前.因此,當調用 Renderer
實現中的方法時,您始終可以指望擁有當前上下文,而不必擔心它.
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.
然而,關鍵方面是當前上下文是每個線程的.GLSurfaceView
創建一個渲染線程,所有Renderer
方法都在這個線程中調用.
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.
這樣做的結果是您無法從其他線程進行 OpenGL 調用,因為它們沒有當前的 OpenGL 上下文.其中包括 UI 線程.這正是你試圖做的.如果您調用 glClearColor()
以響應按鈕單擊,則您處于 UI 線程中,并且您沒有當前的 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.
您已經找到的解決方法實際上可能是這種情況下最現實的解決方案.glClearColor()
應該是一個便宜的調用,所以在每個 glClear()
之前調用它并不重要.如果您需要采取的操作更昂貴,您還可以在值更改時設置一個布爾標志,然后如果設置了該標志,則僅在 onDrawFrame()
中進行相應的工作.
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.
這里還有另一個微妙但非常重要的方面:線程安全.一旦您在一個線程(UI 線程)中設置值并在另一個線程(渲染線程)中使用它們,這是您必須擔心的事情.假設背景顏色的 RGB 分量有 3 個值,并在 UI 線程中一一設置.渲染線程可能會在 UI 線程設置它們時使用這 3 個值,最終混合使用舊值和新值.
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.
為了說明這一切,我將使用您的示例,并勾勒出一個有效且線程安全的解決方案.所涉及的班級成員可能如下所示:
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 線程中設置值的位置:
Then where you set the value in the UI thread:
synchronized(mBackLock) {
mBackRed = ...;
mBackGreen = ...;
mBackBlue = ...;
mBackChanged = true;
}
并且在調用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);
}
注意對兩個線程共享的類成員的所有訪問是如何在鎖內的.在最后一個代碼片段中,還要注意顏色值是如何在使用之前復制到局部變量中的.對于這個簡單的示例,這可能太過分了,但我想說明應該盡可能簡短地持有鎖的一般目標.如果直接使用成員變量,則必須在鎖內調用 glClearColor()
.如果這是一個可能需要很長時間的操作,則 UI 線程無法更新值,并且可能會卡住一段時間以等待鎖定.
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
有一個 queueEvent()
方法,允許您傳入一個 Runnable
,然后在渲染線程中執行該方法.GLSurfaceView
文檔中有一個例子,所以我不會在這里拼出它的代碼.
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.
這篇關于glClearColor 無法正常工作(android opengl)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!