Question

I have a car draw at (0,0) and some obstacles set up but right now my main concern is switching from glPerspective to glOrtho and vice-versa. All that i get when i switch from perspective to ortho is a black screen.

void myinit(){
    glClearColor(0.0,0.0,0.0,0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60,ww/wh,1,100);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(-5,5,3,backcarx,topcarx,0,0,0,1);
}

void menu(int id){
    /*menu selects if you want to view it in ortho or perspective*/
    if(id == 1){
        glClear(GL_DEPTH_BUFFER_BIT);
        glViewport(0,0,ww,wh);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(-2,100,-2,100,-1,1);
        glMatrixMode(GL_MODELVIEW);
        glutPostRedisplay();
    }
    if(id == 2){
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(60,ww/wh,1,100);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        viewx = backcarx - 10;
        viewy = backcary - 10;
        gluLookAt(viewx,viewy,viewz,backcarx,topcarx,0,0,0,1);
    }
}

i've tried using the clear depth buffer and still doesnt work.

Was it helpful?

Solution

Your mental roadblock lies here:

void myinit(){
    glClearColor(0.0,0.0,0.0,0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60,ww/wh,1,100);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(-5,5,3,backcarx,topcarx,0,0,0,1);
}

OpenGL is not initialized! Yes, there are operations done only occasionally, like loading textures. But in general OpenGL is a state based drawing system. Which means, that you set all your drawing related stuff right before you use it. That code in the "myinit" function actually belongs into the display function. And once you have it there, it get's trivial to do the switching: Set a enumerating variable and setup the right projection at before drawing depending on the value of the variable.

As a general rule: OpenGL drawing operations – and setting transformation state belongs into that set of operations – belong into drawing code only. Hence your "menu" function makes no sense at all. It's an event handler. Event handlers deal with input, they don't generate output. In a event handler you change the value of variables and then you flag that output is to be performed.


Update due to comment:

typedef enum ProjectionType_t { Ortho, Perspective } ProjectionType;

ProjectionType projection;

void onMenu(int entry)
{
    swtich(entry) {
    case ...:
        projection = Ortho; break;
    case ...:
        projection = Perspective; break;
    }

    glutPostRedisplay();
}

void display(void)
{
    glClear(...);

    glViewport(...);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    switch(projection) {
    case Ortho:
        glOrtho(...); break;

    case Perspective:
        gluPerspective(...); break;
    }    

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    ...

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top