Search code examples
copenglglut

Use values instead of -1...1 for OpenGL drawing shapes?


If I wanted to draw a plane in OpenGL, I would do something like the below:

glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 1.0);
glVertex3f(0.5, -0.5, 0.5);
glVertex3f(0.5, 0.5, 0.5);
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f(-0.5, -0.5, 0.5);
glEnd();

This draws a white plane that covers 50% of the canvas (from -0.5 to 0.5 on two axes). I want to use numbers instead, however. I don't want to use -1 to 1, but instead something like 0 to n, where n is the dimension of my canvas. For the above example, something like 250 to 750 on two axes on a 1000 pixel canvas rather than -0.5 to 0.5.


Solution

  • That's what the transformation matrices are for. In your case you'd set a ortho projection matrix with the limits as you desire. In your example case

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, 1000, 0, 1000, -1, 1);
    

    would set up a viewing volume so that the boundaries are at 0,0 for the lower left corner and 1000,1000 for the upper right.

    Note that this (and the code you've given) use the old, deprecated fixed function pipeline. You should drop that in favour of a shader based approach.