# OpenGL Always drawing on top of everything else in 2D

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

## Recommended Posts

I am making a 2D game using OpenGL. For debugging, i want to be able to draw text and shapes on top of everything else that is drawn on the scene regardless of when the drawing calls are made. For example, in my AI or player system i want to display the player's position and other data as text on the screen. But, for simplicity's sake, i would like to be able to do this when such data is calculated, which is usually *before* the scene is drawn. I would like to know how i can do this. I thought i could just set the z position so that it is drawn in front of everything, but it didn't work. I clear the screen (glClear) before i do anything in each main loop, i.e. before i update physics and all that. So i should be able to draw even before i start any other drawing, right? I can post any specific code if needed but you can see it all here.

##### Share on other sites
You have 3 options:
1. think again whether you really can't make another function which is called when the engine draws on the gui layer (or some layer that is after the usual things)
2. make GUI controls and just put the value in them at the time of calculation
3. enable z-buffer and draw your text in the layer that's closer to the camera..

##### Share on other sites
1. think again whether you really can't make another function which is called when the engine draws on the gui layer (or some layer that is after the usual things)

Yes, i could (and currently am) drawing debugging text in the player object's draw() function. But, as with general logging to a file, i would like to be able to make the calls from anywhere in the code. It's mostly for convenience but also because sometimes the data i want to display might not be available at the time when i draw everything (i.e. displaying a temporary variable).

2. make GUI controls and just put the value in them at the time of calculation

That's a good idea. I already have a GUI framework, I could have a label that sits in the top left corner and is drawn last. Then i could just set the text any time to whatever i want. The background would be transparent, so when there is no text, you don't see anything.
This solution still wouldn't allow me to draw lines and shapes so i could, for example, draw a line from the player representing it's velocity.

3. enable z-buffer and draw your text in the layer that's closer to the camera..

That's what i was trying to do but i didn't know how. Can you please explain it to me?
If i do this, will it affect performance? It will only be used for debugging so i guess could turn it off again when it's not needed.

##### Share on other sites
The only thing I can suggest is to just keep track of everything you need to display for debugging, and then render it in a seperate step after everything else. If there are only a set number of different things you need to render, you could do something like:
// pseudo-codeclass Debug{        // add some sort of list in here   public:        // push text element on end of list        void Text(int x, int y, std::string, ...);                // push arrow element on end of list        void Arrow(int x, int y, int dst_x, int dst_y);                // clears the list when you want to start with new info, or        // if you update the debug info each frame.        void ClearList();                // draw list from front to back        void Draw();}

Then when you need any of these, you simply call the respective function (Text() to write to the screen, etc.), and call the Draw() function to render it all after you've rendered the rest of the scene. This way, it can seem like you're calling commands as and when, and you don't need to worry about them being drawn over by other parts of the scene

##### Share on other sites
Yeah, i think i'll just go with the simpler idea of using a GUI control that i can write text to. It won't allow me to draw any lines or shapes but if i really need to do that i can do it in the respective object's draw() function.

##### Share on other sites
Why not render all your debug outputs to a FBO and then, after you rendered everything, apply this texture on top of the current display ?

##### Share on other sites
Quote:
 Original post by iliakWhy not render all your debug outputs to a FBO and then, after you rendered everything, apply this texture on top of the current display ?

Because that would be too much work for some simple debugging text/shapes. I have gone with the GUI label idea and am currently implementing it.

Thanks for all your help guys.

##### Share on other sites
Quote:
 Original post by XTAL256Yeah, i think i'll just go with the simpler idea of using a GUI control that i can write text to. It won't allow me to draw any lines or shapes but if i really need to do that i can do it in the respective object's draw() function.
You can still do lines and stuff, as in webwraith's idea, you just cache all the "drawLine" calls until the "GUI" class's draw method is called.

##### Share on other sites
If you aren't using the stencil buffer for anything else, you may be able to use it for this purpose. Basically, draw your debug layer elements with depth check disabled and writing to the stencil buffer. When you're drawing the game graphics, use stencil check to make sure you're not overwriting the debug layer.

(But yeah, if you have a GUI system it's probably best to just make this stuff GUI objects)

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
13
5. 5

• 12
• 9
• 9
• 17
• 21
• ### Forum Statistics

• Total Topics
632601
• Total Posts
3007344

×