c++copenglgldrawpixels

Having trouble useing glDrawPixels


I have to use glDrawPixels to implement a raster algorithm.

Right now I'm only trying to get a simple example of glDrawPixels working but having an issue.

GLint height, width, size = 0;
GLbyte *image = NULL;
int i,j=0;

width = 512;
height = 512;
size = width*height;
image = (GLbyte*)malloc(sizeof(GLbyte)*size*3);

for(i = 0; i < size*3; i=i+width*3){
    for(j = i; j < width*3; j=j+3){
        image[j] = 0xFF;
        image[j+1] = 0x00;
        image[j+2] = 0x00;
    }
}

glDrawPixels(width, height, GL_RGB, GL_BYTE, image);

free(image);
gluSwapBuffers();

Above is the code that I'm trying to get to work, from my understanding it should simply draw a 512x512 red square.

However what I get is one red row at the bottom and everything else is grey.


Solution

  • Your second for() loop is broken -- you're starting at i, but only going up to width * 3, so it doesn't run at all when i > 0.

    Here's a simpler approach:

    GLbyte *p = image;
    for (i = 0; i < height; i++) {
        for (j = 0; j < width; j++) {
            *p++ = 0xFF;
            *p++ = 0x00;
            *p++ = 0x00;
        }
    }