0

有一个简单的程序实际上来自一个更大的程序,把它放在一边,我试着做一个简单的像素读/写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;
}

这是输出:

程序输出

是什么导致第二次读取过程失败?

4

1 回答 1

0

至少在我的系统glutInitWindowSize(100,100)上只是一个建议。 glutGet(GLUT_WINDOW_WIDTH)实际上返回104,而不是100您的gluOrtho2D()电话假设。

尝试使用动态投影:

#include <GL/glut.h>
#include <iostream>

using namespace std;

void displayfnc()
{
    glClearColor(1.0,0.0,0.0,0.0); 
    glClear(GL_COLOR_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    double w = glutGet( GLUT_WINDOW_WIDTH );
    double h = glutGet( GLUT_WINDOW_HEIGHT );
    gluOrtho2D(0.0,w,0.0,h);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    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");
    glutDisplayFunc(displayfnc);
    glutMainLoop();
    return 0;
}
于 2013-04-12T22:00:12.143 回答