0

実際にはより大きなプログラムからの単純なプログラムがありますが、これはさておき、単純なピクセルの読み取り/書き込みを実行しようとしましたOpengl
ピクセルの色の読み取りを含む最初のセクションは成功しますが、ピクセルの書き込みと読み取りを含む 2 番目の操作は失敗します。問題を示すプログラムは次のとおりです。

#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;
}

出力は次のとおりです。

プログラム出力

2 番目の読み取り手順が失敗する原因は何ですか?

4

1 に答える 1

0

少なくとも私のシステムglutInitWindowSize(100,100)では、単なる提案です。 あなたの呼び出しが想定しているものではなく、glutGet(GLUT_WINDOW_WIDTH)実際には を返します。104100gluOrtho2D()

代わりに動的プロジェクションを使用してみてください。

#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 に答える