OpenGL updating uniforms on Mac OSX
I'm programming a nice little game that uses shader generated simplex noise for displaying on the fly computed random terrain.
开发者_如何转开发I'm using Objective-C and Xcode 4 and I have gotten everything to run nicely using a subclass of NSOpenGLView. The subclass first compiles the shader and then renders a quad with the noise texture. The program has no problems running this at an acceptable speed (60Hz).
The subclass of NSOpenGLView
uses an NSRunLoop
to to fire a selector which in turn calls the drawRect:(NSRect)dirtyRect
. This is done every frame.
Now, I want the shader to use a uniform that is updated each frame. The shader should be able to react to a variable change that might occur every frame thus I'm trying to update the uniform at this frequency. The update of the uniform is done in the drawRect:(NSRect)dirtyRect function.
I am partially successful. The screen updates exactly as I'd like for the first 30 frames, then it stops updating the uniform even though I have a call to glUniform1f() and NSLog right next to each other and the NSLog always fires..!
The strange part is that if a hold space pressed (or any other key for that matter) the uniform is updated as it should be.
Clearly I am missing something here in regards to how OSX or OpenGL or something else handles uniforms.
An explanation of what might be ailing me would be appreciated but a pointer to where I can find information about this will suffice.
Update: After fiddling with glGetError()
and glGetUniform*()
I've noticed that the program works as intended when left alone. However, when I use the trackpad for input the uniform is reset to 0.000 while the rest of the program shows no errors.
First, have you tried calling glGetError()
right after glUniform1f()
to see what comes out?
I have a very limited knowledge of Mac OS programming (I did some iOS programming two years ago and forgot most of it since then) so the following is a wild guess.
Are you sure drawRect:(NSRect)dirtyRect
is called by the same thread as the thread that owns the OpenGL context? As far as I know, OpenGL is not thread safe, so it could be that your glUniform1f()
attempts are called from a different thread thus being unable to do anything.
精彩评论