Advertisement

2d pixel-perfect positioning

Started by July 10, 2003 10:46 AM
1 comment, last by Wavarian 21 years, 7 months ago
Not too happy right now, after rummaging through my code looking for an error I expected to have caused by my own fault, I come across a conversion inaccuracy:

// Note this is already in 2d ortho mode

glPushMatrix();

int x = 500;
int y = 30;

glTranslatef((float)x,(float)y,0.0f);

glBegin(GL_POINTS);
   glVertex2i(0,0);
glEnd();

glPopMatrix();
For some reason, (float)x is getting converted to 499.0f, and in turn, causes the pixel to be drawn at (499,30) instead of (500,30). However, if you change x to something like 300 or so, it gets converted properly, and the pixel gets drawn at (300,30). What can I do to fix this? Is there a better way to get pixel perfect positioning in 2d? Thanks for any help you might provide in advance.
If your card supports either OpenGL1.4 or the GL_ARB_window_pos extension, you can use the glWindowPos command which is pixel-exact but that''s limited for raster position (eg Bitmaps).

Otherwise there is an hint at the OpenGL.org FAQ : How do I draw 2D controls over my 3D rendering?
Advertisement
Thanks vincoof, the hints there helped me with my problem. All is looking alright now.

This topic is closed to new replies.

Advertisement