Sign in to follow this  

Glut Mixing 2d and 3d

This topic is 3314 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 everyone! I am currently using Glut in an application that required that I mix 2d and 3d together. Right now I can switch between the two, and resize my window using the code below. However the problem is that when I resize the window it stretches the 3d part. I also need the 2d layer to be in sync with the 3d layer at all times. How can I accomplish this? I need the 3d to stay relative to the 2d when I resize my window, and the window should not stretch the 3d. I have a feeling I have to use orthographic projection for the 3d as well, but I am not quite sure if that's what needs to be done. Thanks for any future insight, I hope I explained my situation well enough. (here's my code if it helps to see what I am using.)
void glEnable2D(){
	GLint iViewport[4];

	// Get a copy of the viewport
	glGetIntegerv( GL_VIEWPORT, iViewport );

	// Save a copy of the projection matrix so that we can restore it
	// when it's time to do 3D rendering again.
	glMatrixMode( GL_PROJECTION );
	glPushMatrix();
	glLoadIdentity();

	// Set up the orthographic projection
	glOrtho( iViewport[0], iViewport[0]+iViewport[2],
			 iViewport[1]+iViewport[3], iViewport[1], -1, 1 );
	glMatrixMode( GL_MODELVIEW );
	glPushMatrix();
	glLoadIdentity();

	// Make sure depth testing and lighting are disabled for 2D rendering until
	// we are finished rendering in 2D
	glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_LIGHTING_BIT );
	glDisable( GL_DEPTH_TEST );
	glDisable( GL_LIGHTING );
}
void glDisable2D()
{
	glPopAttrib();
	glMatrixMode( GL_PROJECTION );
	glPopMatrix();
	glMatrixMode( GL_MODELVIEW );
	glPopMatrix();
}
static void resize(int width, int height){ //change 800 to width and 600 to height
    GLdouble ASPECTRATIO = (GLdouble)width/(GLdouble)height;
    glViewport(0, 0, width, height);              /* Set the viewport */
    glMatrixMode(GL_PROJECTION);                  /* Select the projection matrix */
    glLoadIdentity();				/* Reset The Projection Matrix */

    gluPerspective(45.0f,(GLfloat)ASPECTRATIO,0.1f,200.0f);  /* Calculate The Aspect Ratio Of The Window FIRST 1*/

    glMatrixMode(GL_MODELVIEW);                   /* Switch back to the modelview matrix */

    glClearAccum(0.0, 0.0, 0.0, 1.0);
    glClear(GL_ACCUM_BUFFER_BIT);

}

Share this post


Link to post
Share on other sites
Thanks for your reply, I attempted to just change the aspect ratio in my resize function to...


GLdouble ASPECTRATIO = 1;



However I still received stretching on window resize. As I think about it more and more I think I need to have the 3d use orthogonal view.

It's going to be a game where buildings will be 3d models, and characters and other things will be 2d. Right now I can position 2d objects using the standard 0-800, 0-600 coordinate system. So I need a way to position 3d buildings relative to the 2d stuff easily, but at the same time orthogonal would remove the right angles wouldn't it? So the buildings would just be seen top down regardless of their distance away from the viewport?

Is anything like this possible?

Share this post


Link to post
Share on other sites

This topic is 3314 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this