0

So I'm playing around with OpenGL and drawing bitmaps and it seems at times glRasterPos is off by a pixel. When I get the GL_CURRENT_RASTERPOSITION I can see that rouding (floor) is causing this error. For example when I set the x-pos to 110 it results in 109.9998 which leads the bitmap to be drawn on pixel 109. Any ideas why I can't draw to pixel 110? See example and output below:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluOrtho2D(0, 800, 800, 0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glViewport(0, 0, 800, 800);

glRasterPos2f (100.0, 12);
glGetFloatv(GL_CURRENT_RASTER_POSITION, pos);
printf("%f\n",pos[0]);

glRasterPos2f (110.0, 30.0);
glGetFloatv(GL_CURRENT_RASTER_POSITION, pos);
printf("%f\n",pos[0]);

glRasterPos2f (111.0, 45);
glGetFloatv(GL_CURRENT_RASTER_POSITION, pos);
printf("%f\n",pos[0]);


Console Output:
    100.000000
    109.999992
    111.000015

UPDATE: It seems to have to do with the Ortho2D and the glViewport values I am using.

SirRoot
  • 249
  • 4
  • 17

2 Answers2

0

Try using glRasterPos2i instead and specify integer coordinates.

Andreas Haferburg
  • 5,189
  • 3
  • 37
  • 63
  • Won't help, since this will just change the API to accept integers. But those integers will still run (as floats) through the same pipeline that causes those rounding errors. – Christian Rau Jun 25 '13 at 13:31
0

It seems to have to do with the Ortho2D and the glViewport values I am using.

In fact it does. While your current glOrtho/glViewport configuration lets you specify coordinates directly in window (viewport) space and thus as pixels, they are still converted into normalized device coordinates ([-1,1]) and back when running through the pipeline. So this is the point where you get in rounding errors.

See this question and its answers for some thoughts about using OpenGL for pixel-perfect rendering and why this is not as easy as it may seem at first. Though I don't completely know how this actually applies to the deprecated pixel drawing functions (which nobody ever uses anyway).

Community
  • 1
  • 1
Christian Rau
  • 45,360
  • 10
  • 108
  • 185