Jump to content
  • Advertisement
Sign in to follow this  
TheBirdMan

2D interface in a 3D environment

This topic is 4406 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 working on a 3D engine and i came to the problem of making the user interface. At the moment i have figured out that i can do it by switching the projection mode to perspective, drawing the 3D objects then switching to orthographic and drawing the 2D ones. This works but i was wondering if this will be a big performance downgrade. Maybe there's some other way to do it? I searched the net for answers but couldn't find anything usefull:) The code looks something like this:
void Render2D()
{
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0,viewWidth,0,viewHeight,0,1);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

        //Here i draw the 2D objects
}

void Render3D()
{
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(90.0f, (GLfloat)viewWidth / (GLfloat)viewHeight, 0.1f, 100.0f);
	gluLookAt(camPos[0],camPos[1],camPos[2],
		  camTrg[0],camTrg[1],camTrg[2],
		  camPos[0],camPos[1]+1,camPos[2]);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

        //Here i draw the 3D objects
}


void Render()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	Render3D();
	Render2D();


	glFlush();
}

I skipped all the unnecessary details.

Share this post


Link to post
Share on other sites
Advertisement
That's the way it's usually done.

As for performance, you shouldn't worry about it unless you notice a significant difference in performance between rendering with the 2D interface and rendering without it (in most cases the cost of rendering the 2D interface will be minimal compared to the cost of rendering the game world).

Share this post


Link to post
Share on other sites
Just a small point, if you're running in fullscreen mode, then this line:

gluPerspective(90.0f, (GLfloat)viewWidth / (GLfloat)viewHeight, 0.1f, 100.0f);

would be better off if the viewWidth/viewHeight was a variable somewhere without adding unnecessary overhead to your render3D function. Also, if you're developing on Windows (can't speak for other OSes), and are using double buffering, then this line is not needed:

glFlush();

as swapping the buffers explicitly flushes all OGL commands. :)

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!