Archived

This topic is now archived and is closed to further replies.

gluOrtho2d not doing its job?

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

In the OGL Red book it states that when using gluortho2d, the screen turns into graph paper, where each pixel cooresponds to the unit on the graph paper. So if I make a square 20 units in length/width at the corner of the screen:
              
glBegin(GL_LINES);
    glVertex2s(0,0);
    glVertex2s(0,19);

    glVertex2s(0,19);
    glVertex2s(19,19);
		
    glVertex2s(19,19);
    glVertex2s(19,0);

    glVertex2s(19,0);
    glVertex2s(0,0);
glEnd(); 
  
(yes i know you could use LINE_STRIP or LINE_LOOP but they ALL give the same output) This should make this square just as i expect, right? wrong... I get something more like this:
  
xxxxxxxxxxxxx
x            x
x            x
x            x
x            x
x            x
x            x
x            x
xxxxxxxxxxxxxx
  
what happened to the upper right corner?! It worked the other corners, so why might it cut off that pixel? I adjusted the value by 1 pixel/unit and it looks correct but that is NOT a good way to fix the problem as it might bring up other issues in the future. Thanks for your replies Edit: stupid spaces my gl setup routine:
        
void InitGL(void)
{
	/* Our shading model. */
    glShadeModel( GL_SMOOTH );

	/* Set the clear color. */
    glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );

	/* Set viewing values */
	glViewport( 0, 0, 640, 480 );
	glMatrixMode( GL_PROJECTION );
	glLoadIdentity();
	gluOrtho2D(0.0, 640.0, 0.0, 480.0);
	glMatrixMode( GL_MODELVIEW );
	
}
  
no, i havent tried gl ortho, however i DID use glRect(0,0,20,20) and it produced the correct sized filled rect but I still need to know why my routine is NOT showing correctly. thanks. Edit: same results when using glOrtho(0.0, 640.0, 0.0, 480.0, -1.0, 1.0); Edit: GL_QUADS produces the same results as glRect (meaning it worked). Any other suggestions out there? Edited by - xtrmntr on February 21, 2002 3:22:19 PM

Share this post


Link to post
Share on other sites
You have a problem with vertex coordinates vs pixel coordinates.

You use the projection matrix to setup an orthographic projection, with lower left corner at (0,0), and upper left corner at (640, 480). But take a look at this picture. The coordinate (0,0) does not correspond to the center of the lower left pixel, but the corner of that pixel

  
| | | | |
4 -------------------
| | | | |
3 -------------------
| | | | |
2 -------------------
| x | x | | |
1 -------------------
| x | x | | |
0 -------------------
0 1 2 3 4

x = center of pixel


As you can see, with cooridnate (1, 1), you don''t hit the pixel with coordinate (1, 1), but the corner between the four pixels marked as ''x''. Which one of these pixels are chosen?

The problem you have, is that you think you hit the pixel with coordinate (19, 19), but infact you hit the corner of the four pixels (18, 18), (18, 19), (19, 18) and (19, 19). In this case, OpenGL happen to choose the wrong pixel. That is, the wrong pixels in your eyes.

To solve the problem with pixel coordinates, you need to offset the coordinates by one half. Well, if you offset the cooridnates by one half, other pixel operations will hit corners instead of the center, so you should offset by 0.375.

I sugegst you use the following for direct vertex coordinate to pixel coordinate mapping.
  
glViewport( 0, 0, 640, 480 );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
glMatrixMode( GL_MODELVIEW );
glTranslatef(0.375, 0.375, 0);

Share this post


Link to post
Share on other sites
well, i will try your suggestion when i get to my computer at work but for some reason when i run the program on my home computer, it reacts the way it should. I''m guessing the problem is the video card. My work comp is a p4 something with a viper v770 32mb card and my home comp is a p4 1.7 with a gf3. Another odd note is that if i do full screen with my work comp it shows white as light blue but white when in windowed mode. weird...

Share this post


Link to post
Share on other sites