0
votes

I'm trying to draw a chess board in OpenGL. When I draw it in GL_MODELVIEW mode using glOrtho, it displays just fine. However, when I try to display it in GL_PROJECTION using gluPerspective, all I get is a black empty screen. I must have my projection matrix pointing at nothing, but I can't figure out where I am going wrong. Here is the relevant code:

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
Aside: If you're trying to learn OpenGL then moving to the modern shader-based pipeline is better.legends2k

1 Answers

4
votes

You should set projection matrix first, then use only modelview:

// code in display()

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

// switch to modelview
glMatrixMode(GL_MODELVIEW);

drawScene();

do not use glRotate/glScale when in projection mode... it can only hurt you.

see here a good tutorial about this

another tutorial: lighthouse - glut - reshape. Usually Projection matrix is setup in reshape function only, then in RedenrScene() function only Modelview matrix is used.

Note that you are using old opengl and try to look for "modern" opengl tutorials.