Reputation: 90
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.
Upvotes: 0
Views: 1443
Reputation: 26345
Your loop conditions look off to me. (After the first row, the condition on j will always be true, and the inner loop won't execute.) An easier way to do it would be to do something like this:
for (y = 0; y < height; y++)
{
// Go to the start of the next row
GLbyte* rowStart = image + (width * 3) * y;
GLbyte* row = rowStart;
for (x = 0; x < width; x++)
{
row [ x * 3 ] = 0xFF;
row [ (x * 3) + 1 ] = 0x00;
row [ (x * 3) + 2 ] = 0x00;
}
}
Upvotes: 1
Reputation:
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;
}
}
Upvotes: 1