# OpenGL ortho vs persp. GL_PROJECTION problem?

This topic is 5176 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
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 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?

1. 1
Rutin
42
2. 2
3. 3
4. 4
5. 5

• 9
• 27
• 20
• 14
• 14
• ### Forum Statistics

• Total Topics
633387
• Total Posts
3011608
• ### Who's Online (See full list)

There are no registered users currently online

×