簡體   English   中英

glReadPixels在opengl中讀取錯誤的顏色

[英]glReadPixels Reads wrong color in opengl

有一個簡單的程序實際上來自更大的程序,放在一邊,我試圖在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;
}

這是輸出:

程序輸出

是什么導致第二次讀取過程失敗?

至少在我的系統上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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM