Jump to content
  • Advertisement
Sign in to follow this  
DarkSpeed

OpenGL Encapsulating OpenGL functionality in class functions

This topic is 4143 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is probably an obvious question, but I can't seem to find any answer to it, so bear with me. I'm deriving an OpenGL render class from a base interface containing the usual suspects - setting matrices, lighting and so on, but have a problem maintaining OpenGL states across class function calls. I've outlined the problem in the pseudocode below. class::SetLight(light_details) { glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); // ... other light-enabling here - position, diffuse, etc. } class::Draw(obj_details) { glMatrixMode(GL_MODELVIEW); glLoadMatrix(my_modelview_matrix); // ... draw with obj_details. } class_two::render() { render_obj->SetLight(); render_obj->Draw(); } ////////////// class::Draw(obj_details) { glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); // ... light-setting glMatrixMode(GL_MODELVIEW); glLoadMatrix(my_modelview_matrix); // ... draw. } class_two::render() { render_obj->Draw(); } /////////////// In the second instance, there's no problem, and the object is rendered and lit as it is supposed to be. However, if I try to split the functionality into two functions as in the first instance, the lighting just isn't enabled any more, even though I've run the debugger to ensure the correct program flow. I get the same problem with setting the current projection matrix; I set the projection matrix in one function, then come to render in another function, by which point the projection matrix seems to have been "forgotten" - no such problems if I set the projection matrix followed by the render call in the same function. I'd understood OpenGL to be a global state machine and indeed, there's no problem in (for example) standard C with setting the projection matrix in a resize function; the examples in the Red Book show just that... it just seems to be something I'm missing in C++. If this isn't a standard problem, let me know so that I can check project settings or anything else rather than let my post sink without reply - and thanks for reading.

Share this post


Link to post
Share on other sites
Advertisement
Very strange, indeed!

If the program flow is correct, I can't think of any reason why this would happen. Maybe you are using some kind of threading? Only one thread can issue OpenGL commands at any time (try placing a call to wgl/glxMakeCurrent).

Otherwise, you could use some program like glDebugger to shed some light on what's really happening.

Share this post


Link to post
Share on other sites
Thanks Fiddler. I used gDEBugger, which showed me that a stray wglMakeCurrent was being called *after* I issued the one-shot projection matrix/lighting calls etc. Repositioning the wglMakeCurrent means that the calls are taking effect. Thanks again!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!