PDA

View Full Version : Ortho/2D question



stodge
03-13-2003, 01:12 PM
I'm back re-writing some of the code in my engine and I something just occurred to me. When I draw each GUI element, I'm setting up the matrices and setting the Ortho mode:



void hbGuiSplashScreen::Render()
{
HBfloat texMinX, texMinY, texMaxX, texMaxY;

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

mMaterial->GetTextureCoordinates(texMinX, texMinY, texMaxX, texMaxY);

glEnable(GL_TEXTURE_2D);
glBindTexture( GL_TEXTURE_2D, mMaterial->GetTexture());

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();

// Initialise the model matrix.
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

glOrtho(0, 640, 0, 480, -1.0, 1.0);

glDisable(GL_DEPTH_TEST);

glBegin(GL_QUADS);

glTexCoord2f(texMinX, texMaxY);
glVertex2f(mPosX, mPosY);

glTexCoord2f(texMaxX, texMaxY);
glVertex2f(mPosX+mSizeX, mPosY);

glTexCoord2f(texMaxX, texMinY);
glVertex2f(mPosX+mSizeX, mPosY+mSizeY);

glTexCoord2f(texMinX, texMinY);
glVertex2f(mPosX, mPosY+mSizeY);

glEnd();

glEnable(GL_DEPTH_TEST);

// Restore the projection matrix.
glMatrixMode(GL_PROJECTION);
glPopMatrix();

glMatrixMode(GL_MODELVIEW);
glPopMatrix();

glDisable(GL_TEXTURE_2D);

glDisable(GL_BLEND);
}


I was wondering if this was the inefficient way of doing it. Do you guys do this or do the following:


Start 3D Frame
Render 3D World
End 3D Frame
Start 2D Frame (set up matrices and set ortho)
Render 2D Objects (GUI)
End 2D Frame

Is there a "best" way of doing this?

Thanks

Deiussum
03-14-2003, 05:28 AM
The only thing I see odd about that code is that you are using glOrtho on the GL_MODELVIEW matrix. It should be done on the GL_PROJECTION matrix instead. If you were to use ligting or fogging the calculations will be messed up the way it stands now.

Also, if your glOrtho doesn't change, you could just set it in the resize event and not worry about setting it every frame. Likewise, you wouldn't need to do a push/pop matrix on it. Usually a push/pop on the projection matrix is done for things like rendering elements in a perpspective view, and then switching to an ortho view for other things.

Edit:
Re-read your post and it seems that you are doing this as a rendering after your perspective stuff. My comments on doing the glOrtho in the GL_PROJECTION matrix still stands, though. One other thought is if you have a lot of these GUI elements, you could do the projection matrix stuff outside of these functions and render all the GUI stuff at the same time.

For example:




glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, 640, 0, 480, -1, 1);
glMatrixMode(GL_MODELVIEW);
foreach guiobject
guiobject.Render();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);


[This message has been edited by Deiussum (edited 03-14-2003).]