glClearColor 无法正常工作(android opengl)
我想在运行时更改应用的背景颜色.所以在按钮点击我第一次打电话:
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.
相关文章