# OpenGL One thought about mixing 2d and 3d

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

## Recommended Posts

Hello, I'm quite a beginner with OpenGL and I'm asking myself various questions, before plunging into implementation. Today's question was: In order to easily draw a 2D interface in front of a 3D scene, is it possible to save a GL_PROJECTION matrix and load it as a GL_MODELVIEW matrix? The 2D elements I want to draw should have the coordinates like (x,y,K), where K is a fixed value, and is the distance to the near plane of the view frustum. Sounds either complicated, either grammatically incorrect (which is why I have to practice my English further :-) ). (And as a side note, I would like to know how to limit the fps to the monitor refresh rate, without using a timer; Google will keep giving me the opposite answer :D).

##### Share on other sites
Quote:
 In order to easily draw a 2D interface in front of a 3D scene, is it possible to save a GL_PROJECTION matrix and load it as a GL_MODELVIEW matrix?

Cant get what you mean. If you are going to draw a 2D GUI you should set an ortho projection matrix. Then first draw the 3D stuff and then set an ortho matrix and draw the 2D stuff over everything, using textured quads, with some transparency or so. And you dont need to specify the z position for this 2D stuff, you dont want them to intersect the 3D stuff do you?

And you want to compute frames per second without using a timer??? Id like to know how to do this =)

##### Share on other sites
For 2D, like xissburg said, use glOrtho. No need to mess with matrices directly. What you describe (load projection matrix into modelview) is technically possible but I don't think it will work like you expect (in other words, it would be nonsense). Instead, call something like this before/after drawing your UI:

	/**	* Sets OpenGL to render in flat 2D (no perspective) on top of current scene.	* Pushes current projection and modelview, and disables depth testing and lighting.	*	* @see #setOrthoOff()	*/	public void setOrthoOn(double width, double height)	{				GL11.glDisable(GL11.GL_DEPTH_TEST);		GL11.glDisable(GL11.GL_LIGHTING);		GL11.glEnable(GL11.GL_BLEND);		GL11.glEnable(GL11.GL_TEXTURE_2D);		GL11.glMatrixMode(GL11.GL_PROJECTION);		GL11.glPushMatrix();		GL11.glLoadIdentity();		GL11.glOrtho(0,width,0,height,-1,1);		GL11.glMatrixMode(GL11.GL_MODELVIEW);		GL11.glPushMatrix();		GL11.glLoadIdentity();	}		/**	* Turns 2D mode off. pops projection and modelview matricies, and	* re-enables depth testing and lighting.	*	* @see #setOrthoOn()	*/	public void setOrthoOff()	{		GL11.glMatrixMode(GL11.GL_PROJECTION);		GL11.glPopMatrix();		GL11.glMatrixMode(GL11.GL_MODELVIEW);		GL11.glPopMatrix();		GL11.glEnable(GL11.GL_DEPTH_TEST);		GL11.glEnable(GL11.GL_LIGHTING);		GL11.glDisable(GL11.GL_BLEND);		GL11.glDisable(GL11.GL_TEXTURE_2D);	}

For vsync, you can try going into your adapter's device settings in control panel and turning on vsync for your own PC at least. Google tells me there's a windows-only OpenGL extension for doing this programatically, too.

##### Share on other sites
Thank you for the replies. So basically, I have to call setOrthoOn and setOrthoOff at the end of the scene, before, and respectively, after drawing the user interface. I'll write it down for future use :)

##### Share on other sites

//start orthogonal (stick it in a function as in the other reply)
glMatrixMode(GL_PROJECTION)
glPushMatrix()
glMatrixMode(GL_MODELVIEW)

// draw things to ui

// end orthogonal (stick it in an ortho off function maybe)
glMatrixMode(GL_PROJECTION)
glPopMatrix();
glMatrixMode(GL_MODELVIEW);

Perhaps a better idea than just turning things on/off is to use glPushAttrib in the "on" method, disable things and use glPopAttrib int he "off" method (which will revert their state to what they were before turning things on/off). After all, if somethign is off anyway, we turn it off again (safe enough) but then we turn it on afterwards which we might not want.

I'm think push/pop attrib might be removed in opengl 3.0 though (not sure) so perhaps best to just avoid it anyway.

1. 1
2. 2
frob
16
3. 3
4. 4
5. 5
Rutin
10

• 19
• 13
• 14
• 76
• 22
• ### Forum Statistics

• Total Topics
632138
• Total Posts
3004359

×