Jump to content
  • Advertisement
Sign in to follow this  
chri5ty

OpenGL One thought about mixing 2d and 3d

This topic is 3267 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

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Its already been answered but all you do:

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

// draw things to ui

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

lightbringer did a bit more (loading identity moddelview matrix, disabling lighting etc) which is good if your gonna be drawing your ui.

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.

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!