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……它只会伤害你。

在这里看到一个很好的教程

另一个教程:灯塔 - 过剩 - 重塑。通常仅在函数中设置投影矩阵reshape,然后在RedenrScene()函数中仅使用模型视图矩阵。

请注意,您正在使用旧的 opengl 并尝试寻找“现代”的 opengl 教程。

于 2013-09-30T16:49:54.313 回答