有一个简单的程序实际上来自一个更大的程序,把它放在一边,我试着做一个简单的像素读/写Opengl
。
第一部分涉及读取像素颜色成功,但涉及写入像素然后读取它的第二个操作失败!。这是演示问题的程序:
#include <iostream>
#include <glut.h>
using namespace std;
void init(void)
{
glClearColor(1.0,0.0,0.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,100.0,0.0,100.0);
}
void displayfnc()
{
glClear(GL_COLOR_BUFFER_BIT);
unsigned char current[3];
int r,g,b;
//x=0 to 6 is right
glBegin (GL_POINTS);
glColor3f (0.0,0.0,1.0);
glVertex2i (6,0);
glEnd();
//works well.
glReadPixels(6,0, 1, 1 , GL_RGB , GL_UNSIGNED_BYTE ,current);
r = current[0];
g = current[1];
b = current[2];
cout<<"read from x=6::::"<<"r:"<<r<<"g:"<<g<<"b:"<<b<<endl;
//x =7 and other isnt right
glBegin (GL_POINTS);
glColor3f (0.0,0.0,1.0);
glVertex2i (7,0);
glEnd();
//the problem is here in the reading.why this happen?
glReadPixels(7,0, 1, 1 , GL_RGB , GL_UNSIGNED_BYTE ,current);
r = current[0];
g = current[1];
b = current[2];
cout<<"Read from x=7::::"<<"r:"<<r<<"g:"<<g<<"b:"<<b<<endl;
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(100,100);
glutInitWindowPosition(0, 0);
glutCreateWindow("BoundaryFill");
init();
glutDisplayFunc(displayfnc);
glutMainLoop();
return 0;
}
这是输出:
是什么导致第二次读取过程失败?