# 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 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 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.

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627657
• Total Posts
2978471
• ### Similar Content

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• 10
• 12
• 22
• 13
• 33