透视OpenGL

时间:2013-09-30 16:42:12

标签: opengl perspective

我正在尝试在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();
    }

1 个答案:

答案 0 :(得分:4)

您应首先设置投影矩阵,然后仅使用模型视图:

// code in display()

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

// switch to modelview
glMatrixMode(GL_MODELVIEW);

drawScene();

在投影模式下不要使用glRotate / glScale ......它只能伤害你。

请参阅here a good tutorial about this

另一个教程:lighthouse - glut - reshape。通常,Projection矩阵仅在reshape函数中设置,然后在RedenrScene()函数中仅使用Modelview矩阵。

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