GennSev
GennSev

Reputation: 1646

Multiple Viewports on OpenGL

I'm trying to make a racecar game, and I'm having a problem on showing multiple viewports on the screen. I wish to put on the down right corner of the screen a small "mini-map" (that would acctually be a different view from the main camera scene) but somewhy, the second viewport just does not appears, and all I can see is the view of the main camera.

Here follows my code as is:

    void mainRender() {
        updateState();

        setViewport(0, windowWidth, 0, windowHeight);
        setWindow();
        renderScene();


        setViewport(0, windowWidth, 0, windowHeight/2);
        renderScene();
        gluLookAt(0,88,0,
                  1 ,0 ,0,
                  0.0,1.0,0.0);



        Sleep(30);
    }

    void setWindow() {
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(45.0f,(GLfloat)windowWidth/(GLfloat)windowHeight,0.1f, 100.0f);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        gluLookAt(posX,posY + 0.025 * std::abs(sin(headPosAux*PI/180)),posZ,
        posX + sin(roty*PI/180),posY + 0.025 * std::abs(sin(headPosAux*PI/180)) + cos(rotx*PI/180),posZ -cos(roty*PI/180),
        0.0,1.0,0.0);

    }

    void setViewport(GLint left, GLint right, GLint bottom, GLint top){
        glViewport(left, bottom, right - left, top - bottom);
     }


    int main(int argc, char **argv) {
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH);
        glutInitWindowSize(windowWidth,windowHeight);
        glutInitWindowPosition(windowXPos,windowYPos);

        /**
        Store main window id so that glui can send it redisplay events
        */
        mainWindowId = glutCreateWindow("TF");

        glutDisplayFunc(mainRender);

        glutReshapeFunc(onWindowReshape);

        /**
        Register keyboard events handlers
        */
        glutKeyboardFunc(onKeyDown);
        glutKeyboardUpFunc(onKeyUp);



        mainInit();

        glutMainLoop();

        return 0;
    }

Upvotes: 0

Views: 5162

Answers (2)

Tim
Tim

Reputation: 35933

I don't draw in the second viewport, I just change the position of the camera, so it can look to the map from upwards and it will look like a minimap (that's what I use the lookAt function for).

I think you've got some serious misunderstanding of how opengl works. Just changing the gluLookAt won't do anything. If you want to view the scene from a different angle, you have to redraw the entire scene once you've changed the viewport. If you want to show a minimap then you should draw your whole scene normally, set the viewport to the minimap, set the overhead camera, and then draw the whole scene again.

Upvotes: 3

Tim
Tim

Reputation: 35933

You need to start using glGetError.

Your left and right argument to your function is mixed up. You're passing left as width/2, and right as 0. This is generating a negative width, and thus an error. Your height also looks negative.

Upvotes: 1

Related Questions