Sign in to follow this  
gatofedorento

OpenGL Lock states in openGL

Recommended Posts

Hello, I'm making a level editor and I came across a design doubt.

Like in UDK, I want my editor to change from fill the polygons to wireframes or to points in real time with just the press of a button, that's easy to do in openGL, the problem is that when i change to wireframes I want to be able to disable textures, color, etc.

In my implementation when the draw function of an object is called first it sets all the states it needs to (textures, color, etc) and then draws, I want to be able to lock these states so even though the draw functions calls them nothing happens, it's easy to do, I just need to cover the openGL functions with some of my own.

My questions are: is there a better way to do this? Am I falling in other things in my design about each draw functions sets up the states?

Share this post


Link to post
Share on other sites
I would say that this is a design problem by letting objects set up and draw themselves, yes.

The problem is that, unless you're doing very trivial state changes, you will have problems with that design. An object should not set up and draw itself. What you end up with is code at many different places that have to be kept in sync. For this design to work in your case, you need the wire-frame and state locking code at every place where this wire-frame and state locking must have effect. Every single place where you draw something has to be aware of it; possibly even places that are not supposed to be affected by this wire-frame and state locking mechanism so that they can revert to standard rendering if you're in the wrong state.

State management like this will explode to everywhere in your code for every non-trivial piece of state management if you let objects set up and draw themselves, because they now have to be aware of every state that directly affect them, and possibly even indirectly affect them.

Instead have a single point in your code that handles OpenGL. Instead of having object set up and draw themselves, have the object provide this single point with what is needed to draw the object (a vertex array for example) what states are required to draw it (for example a structure containing states like what shaders, colors, textures, materials, blending states and so on that describes how to draw the model). You now have a single point that can override an object's requested state for normal drawing with a basic color-less wire-frame setup when your application is in wire-frame mode. Your objects don't even have to be aware of wire-frame mode in the first place.

Share this post


Link to post
Share on other sites
I think you misunderstood ( or maybe I misunderstood your explanation, sorry =( ) part of what I wrote.

But I got your point, it is indeed a bad design choice, and what you told me is a better way to do things, but anyway i will try to explain what I wanted to do in pseudo code and compare to your design:

[code]Renderer -> disableTextures
Renderer -> disableColor
Renderer -> renderWireframe
Renderer -> lockTexturesState
Renderer -> lockColorState
//at this point the wireframes will be ready to use without color and textures, now let's see what happens in the draw function of one of my object, let's say a terrain
Terrain -> enableTexture //this won't take effect because of the lock
Terrain -> setTexture //the texture will be set, but it won't do anything, because the texture is disabled
Terrain -> draw the mesh //which will be a wireframe
//now that the terrain is drawn I can unlock the states for the other elements in my screen to draw normally, let's say GUI for example
Renderer-> unlockTextures
Renderer -> unlockColor
GUI -> enableTextures
GUI -> setTexture
GUI -> draw //now everything will be draw normally
[/code]

Instead of what I did up here every object in my scene could have a function that sets the states and another function that draws the mesh, and depending of the mode I have only the renderer would know what to do, and the code would be somehting like this:

[CODE]
if(state == FILL){
Renderer -> setFillMode
foreach(Element in Elements){
Element -> setupState
Element -> drawMesh
}
}
else if(state == WIREFRAME){
Renderer -> setWireFrameMode
foreach(Element in Elements){
Element -> drawMesh
}
}
[/CODE]

I think the last piece of pseudo code seems indeed more clean and I wouldn't need the locks, which would avoid the programmer of causing state bugs.

Thanks for the help and sorry for the long post, I want to make sure I make good design choices, so I don't have the need to make huge refactorings later.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this