Jump to content
  • Advertisement
Sign in to follow this  
deafchef

OpenGL ortho vs persp. GL_PROJECTION problem?

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

Extraneous information follows problem: I create two viewports, one is meant to hold several 3d objects, the other is meant to hold text (later to be buttons). The first persp. viewport always works. The second viewport, which takes up the right quarter of the screen, is given an ortho2d, but text loaded using glutbitmapcharacterfont() will fail to appear if I change to the projection matrix before setting the ortho. code follows> void render_string(void* font, const char* string) { char* p = (char*) string; while (*p) glutBitmapCharacter(font, *p++); } void display() { char str[128]; glClear(GL_COLOR_BUFFER_BIT); windowwidth = glutGet(GLUT_WINDOW_WIDTH); windowheight = glutGet(GLUT_WINDOW_HEIGHT); //1st viewport: "3D window" glViewport(0,0,(windowwidth * 3) / 4, windowheight); glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Reset The gluPerspective(45.0f,(GLfloat)((windowwidth * 3) / 4) / (GLfloat) windowheight, 0.1f, 100.0f); //Move away, better overview. glTranslatef(0.0, 0.0, -10.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glClear(GL_DEPTH_BUFFER_BIT); /* Draw some 3d shapes, call glrotate before each * with an incrementing counter to make them spin. */ //Second viewport: "Menu bodkins" glViewport((windowwidth * 3) / 4, 0, windowwidth / 4, windowheight); /* Removing this comment block results in nothing * appearing in the 2nd viewport glMatrixMode(GL_PROJECTION); glLoadIdentity(); */ gluOrtho2D((windowwidth * 3) / 4,windowwidth,0, windowheight); /* If this ortho is used instead of the one above, and the * glMatrixMode(GL_PROJECTION) and LoadIdentity() statements * are uncommented, text appears in THE SECOND VIEWPORT. gluOrtho2D(0,windowwidth,0,windowheight); */ glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glClear(GL_DEPTH_BUFFER_BIT); glColor3f(1.0,1.0,1.0); glBegin(GL_QUADS); glVertex2f(1.0,1.0); glVertex2f(1.0,-1.0); glVertex2f(-1.0,-1.0); glVertex2f(-1.0,1.0); glEnd(); sprintf(str, "A little text sample."); glRasterPos2f(0.0,(GLfloat)windowheight/2); render_string(GLUT_BITMAP_HELVETICA_12, str); glutSwapBuffers(); } As it is now, the code will render a white rectangle, which is part of the GL_QUAD square, but the rectangle is not cut off by the viewport, so I assume I have a perspective futz. As stated in the comments, if one does use the projection matrix, and sets the ortho to go from 0,0 to windowwidth,windowheight, the text will appear, but no rectangle (nor square) will appear. Also note that if one removes the LoadIdentity after setting MODELVIEW, one also sees the text, but no rectangle nor square. I'm pretty sure I'm botching the matrices and perspectives somehow, but after several hours of this, having tried what seems like every possibility, I am utterly codeblind. Any and all help and amusing observations would be welcome. Notes: I am new to opengl and hence suffer from a huge lack of reference and experience, but beg you bear with me. I'm using the nehe tuts (surprising, isn't it?), but since I already have a firm and structured idea, and the tuts don't go that way, I have to pick and patch quite a bit. This is inviting disaster, I know, but that's what forums and clever forum regulars are for, isn't it (shameless brown-nosing)? If somebody can find out what is causing this problem, I may not have to cause my computer serious structural harm, which would really be rather nice.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
but the rectangle is not cut off by the viewport

Don't have time to take a proper look, but I did notice this. Be careful here as glViewport does *not* ensure that no drawing will occur outside of the viewport.

Share this post


Link to post
Share on other sites
Thx for the replies, but it's not precisely what I'm looking for.As of now, I'm only looking for a reason why the following code results in no visible text.

display()
{
//...
glViewport((windowwidth * 3) / 4, 0,
windowwidth / 4, windowheight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D((windowwidth * 3) / 4,windowwidth,0, windowheight);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_DEPTH_BUFFER_BIT);

glColor3f(1.0,1.0,1.0);

sprintf(str, "Sample text.");
glRasterPos2f(0.0,(GLfloat)windowheight/2);
render_string(GLUT_BITMAP_HELVETICA_12, str);
}

WHY does gluOrtho2D (and normal glOrtho for that matter) work within the confines of the viewport? Setting the ortho to cover the entire window, not just the same area as the viewport, results in the text appearing in the viewport.
I want to be able to define a viewport, define an ortho over the SAME area, and be able to send the text i send there!
Any clues?

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!