Setting raster position in OpenGL

December 1, 2005 on 2:18 am | In Computer, Graphics | No Comments

When writing an OpenGL application, there are many occasions you want to draw texts or pixels in a window. To do that, you need to set the current raster position in the window coordinates, and the following is a typical code to achive it:

void drawPixelsOnScreen(const int x, const int y)
{
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    gluOrtho2D(0, windowWidth, 0, windowHeight);
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();
    glRasterPos2i(x, y);
    glDrawPixels(...);
    glPopMatrix();
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
}

There are so many state changes involved in this code just to draw texts/pixels. However, I just found today that I can set the current raster position in the window coordinates with glWindowPos*() which is introduced in OpenGL 1.4. Then the above code will become:

void drawPixelsOnScreen(const int x, const int y)
{
    glWindowPos2i(x, y);
    glDrawPixels(...);
}

It’s much more simple. I should have known this earlier.

Powered by WordPress with Pool theme design by Borja Fernandez.
Entries and comments feeds. Valid XHTML and CSS. ^Top^