glOrtho

BlitzMax Forums/OpenGL Module/glOrtho

Jim Teeuwen(Posted 2006) [#1]
lo peeps. im using opengl to do some 2D stuff.
The thing thats been keeping me busy all day is the way to properly map the screen x,y coordinates to the sprites displayed onscreen.

Everytime I thought I got it right, the whole viewport seemed to be offset by 5-10 pixels in some direction, making the positioning of sprites a pain in the ass.

I just figured out that passing that offset to the glOrtho command 'fixes' the problem.

Function ehResizeWindow( obj:Stage )
	If( obj.Height = 0 ) Then obj.Height = 1;
	glViewport(0, 0, obj.Width, obj.Height );
	glMatrixMode( GL_PROJECTION );
	glLoadIdentity();
	glOrtho( 5, obj.StageWidth + 5, obj.StageHeight - 5, -5, 0 ,10 );
	glMatrixMode( GL_MODELVIEW );
	glLoadIdentity();
End Function


just using: glOrtho( 0, obj.StageWidth, obj.StageHeight, 0, 0 ,10 ); causes all the sprites I try to place, to be offset by 5 pixels on both x and y axis.

Note that in my test runs, the resolution used was the same for glViewport, glOrtho and the OpenGL window itself. Allways 800,600. Yet the weird offset seems to be there all the time, enless I enter the offset as shown in the function above.

that code means that the display itself is offset 5 pixels to the right and 5 pixels to the top, compared to the window itself, yet everything displays correctly now.

Any idea why this happens?


Jim Teeuwen(Posted 2006) [#2]
Another thing. When using the above, if I position a sprite on a fixed position from the beginning, it's all OK, but when i start positioning it - by having it move along with my mousepointer for instance - I have to offset it's x and y position by +5 and -2.5 respectively, to actually have it at the exact right spot.

Could all this stuff have anything to do with floating point issues during the calculations with Integer screen coordinates and float vertex coordinates?