0

OpenGLでチェス盤を描こうとしています。glOrtho を使用して GL_MODELVIEW モードで描画すると、問題なく表示されます。ただし、gluPerspective を使用して GL_PROJECTION で表示しようとすると、黒い空の画面しか表示されません。射影行列が何も指していない必要がありますが、どこが間違っているのかわかりません。関連するコードは次のとおりです。

double dim=2.0;    
float fov=50;
double asp=1;

void chessboard() {

        // define the board
        float square_edge = 1;
        float border = 0.5;
        float board_thickness = 0.25;
        float board_corner = 4*square_edge+border;
        float board_width = 2*board_corner;

        glPushMatrix();

        glScalef(1/board_corner, 1/board_corner, 1/board_corner);

        GLfloat board_vertices[8][3] = {
            {-board_corner,  board_corner, 0.0},
            {-board_corner, -board_corner, 0.0},
            { board_corner, -board_corner, 0.0},
            { board_corner,  board_corner, 0.0},
            {-board_corner,  board_corner, board_thickness},
            {-board_corner, -board_corner, board_thickness},
            { board_corner, -board_corner, board_thickness},
            { board_corner,  board_corner, board_thickness}
        };

        float darkSquare[3] = {0,0,1};
        float lightSquare[3] = {1,1,1};

        glEnableClientState(GL_VERTEX_ARRAY);
        glVertexPointer(3, GL_FLOAT, 0, board_vertices);
        // this defines each of the six faces in counter clockwise vertex order
        GLubyte boardIndices[] = {0,3,2,1,2,3,7,6,0,4,7,3,1,2,6,5,4,5,6,7,0,1,5,4};

        glColor3f(0.3,0.3,0.3); //upper left square is always light

        glEnable(GL_POLYGON_OFFSET_FILL);
        glPolygonOffset(1.0, 1.0);

        glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, boardIndices);

        glPolygonOffset(0.0, 0.0);

        glBegin(GL_QUADS);
        // draw the individual squares on top of the board
        for(int x = -4; x < 4; x++) {
            for(int y = -4; y < 4; y++) {
                //set the color of the square
                if ( (x+y)%2 ) glColor3fv(darkSquare);
                else glColor3fv(lightSquare);

                glVertex3i(x*square_edge, y*square_edge, 0);
                glVertex3i(x*square_edge+square_edge, y*square_edge, 0);
                glVertex3i(x*square_edge+square_edge, y*square_edge+square_edge, 0);
                glVertex3i(x*square_edge, y*square_edge+square_edge, 0);
            }
        }
        glEnd();
        glDisable(GL_POLYGON_OFFSET_FILL);
        glPopMatrix();
    }

    void display()
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glEnable(GL_DEPTH_TEST);
    //    glMatrixMode(GL_MODELVIEW);
        glMatrixMode(GL_PROJECTION);

        glLoadIdentity();

        // Set the view angle
        glRotated(ph,1,0,0);
        glRotated(th,0,1,0);

        // Set the viewing matrix
    //    glOrtho(-dim, dim, dim, -dim, -dim, dim);
        gluLookAt(0,-0.6,0.6,0,0,0,0,0,1);
        gluPerspective(fov, asp, dim/4, dim);

        draw_board();

        glMatrixMode(GL_MODELVIEW);

        glFlush();
        glutSwapBuffers();
    }
4

1 に答える 1

4

最初に射影行列を設定してから、モデルビューのみを使用する必要があります。

// code in display()

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// use ortho or perspective

// switch to modelview
glMatrixMode(GL_MODELVIEW);

drawScene();

投影モードでは glRotate/glScale を使用しないでください...それはあなたを傷つけるだけです。

ここでこれに関する良いチュートリアルを参照してください

別のチュートリアル: lighthouse - glut - reshape。通常、Projection マトリックスは関数のみで設定されreshapeRedenrScene()関数のみで Modelview マトリックスが使用されます。

古い opengl を使用していることに注意してください。「最新の」opengl チュートリアルを探してみてください。

于 2013-09-30T16:49:54.313 に答える