In OpenGL, can I draw a pixel that exactly at the coordinates (5, 5)?

xzhu picture xzhu · May 27, 2011 · Viewed 14.9k times · Source

By (5, 5) I mean exactly the fifth row and fifth column.

I found it very hard to draw things using screen coordinates, all the coordinates in OpenGL is relative, and usually ranging from -1.0 to 1.0. Why it is so serious to prevent programmers from using screen coordinates / window coordinates?

Answer

basszero picture basszero · May 27, 2011

The simplest way is probably to set the projection to match the pixel dimensions of the rendering space via glOrtho. Then vertices can be in pixel coordinates. The downside is that resizing the window could cause problems and you're mostly wasting the accelerated transforms.

Assuming a window that is 640x480:

// You can reverse the 0,480 arguments depending on you Y-axis 
// direction preference
glOrtho(0, 640, 0, 480, -1, 1);

Frame buffer objects and textures are another avenue but you'll have to create your own rasterization routines (draw line, circle, bitmap, etc). There are problaby libs for this.