glGetDoublev fails sometimes when called from another thread

Started by
1 comment, last by HyperHacker 17 years, 7 months ago
I'm using glGetDoublev() to grab an object's modelview matrix and multiply various things by it to move the object. At first this was working fine, but I moved the entire movement process to an SDL timer callback so things would move independent of frame rate. When I do this it seems glGetDoublev() returns all zeros and throws an invalid operation error. The only documented reason for this I can find is calling it between glBegin() and glEnd() but I added some checks to make sure that's not happening. What I don't understand is it works for some objects but fails for others. O_o Are these functions not multi-thread safe or what?
---------------------------------dofile('sig.lua')
Advertisement
None of OpenGL is thread-safe, you're only allowed to make gl calls from the thread that has the current GL context.

You really shouldn't have multiple threads querying GL, and you shouldn't be using it to store your game state either. Do the calculations yourself and/or save the results after you've calculated them and only ever push data into GL, not pull it out again.
Alright, well I'm not quite sure how to handle that... When I fetch the modelview for this purpose it's just glLoadIdentity(), a few glRotatef()s and then glGetDoublev() to get the matrix. I'm sure there's probably some way to just make up a matrix rather than reading it from GL but I'm not sure how.

Off topic, how do you draw spheres and other round things? Just fake it with a lot of triangles?

[edit] May as well include the actual code.

/*Retrieves the modelview matrix for the object given its rotation.*/void Object3D::UpdateModelView(){	//While we're here, clamp the rotation.	if(this->Rotation.X > 360.0) this->Rotation.X = fmod(this->Rotation.X, 360.0);	else if(this->Rotation.X < 0.0) this->Rotation.X = 360.0 + fmod(this->Rotation.X, 360.0); //Add because it's negative	if(this->Rotation.Y > 360.0) this->Rotation.Y = fmod(this->Rotation.Y, 360.0);	else if(this->Rotation.Y < 0.0) this->Rotation.Y = 360.0 + fmod(this->Rotation.Y, 360.0);	if(this->Rotation.Z > 360.0) this->Rotation.Z = fmod(this->Rotation.Z, 360.0);	else if(this->Rotation.Z < 0.0) this->Rotation.Z = 360.0 + fmod(this->Rotation.Z, 360.0);	glMatrixMode(GL_PROJECTION);	glPushMatrix();	glLoadIdentity();	glMatrixMode(GL_MODELVIEW);	glPushMatrix();	glLoadIdentity();	glRotatef(-this->Rotation.X, 1.0, 0.0, 0.0);	glRotatef(-this->Rotation.Y, 0.0, 1.0, 0.0);	glRotatef(-this->Rotation.Z, 0.0, 0.0, 1.0);	glGetDoublev(GL_MODELVIEW_MATRIX, this->Modelview[0]);	glPopMatrix();	glMatrixMode(GL_PROJECTION);	glPopMatrix();	glMatrixMode(GL_MODELVIEW);}
---------------------------------dofile('sig.lua')

This topic is closed to new replies.

Advertisement