我必须使用glDrawPixels来实现光栅算法。
现在,我只是尝试获得一个简单的glDrawPixels工作的例子,但有一个问题。
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();上面的代码是我正在尝试工作的,根据我的理解,它应该简单地绘制一个512x512的红色方块。
然而,我得到的是底部的一行红色,其他的都是灰色的。
发布于 2012-02-19 08:15:46
您的第二个for()循环中断了--您从i开始,但只运行到width * 3,所以当使用i > 0时,它根本不会运行。
这里有一个更简单的方法:
GLbyte *p = image;
for (i = 0; i < height; i++) {
for (j = 0; j < width; j++) {
*p++ = 0xFF;
*p++ = 0x00;
*p++ = 0x00;
}
}发布于 2012-02-19 08:17:31
我看不出你的循环状况。(在第一行之后,j上的条件将始终为真,内部循环将不会执行。)一种更简单的方法是这样做:
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;
}
}https://stackoverflow.com/questions/9345392
复制相似问题