OpenGL object distortion in perspective view

140 Views Asked by At

Do you guys have any idea what is the cause of this problem? It looks fine in orthographic view.

Orthographic View:

enter image description here

Perspective View:

enter image description here

Code that might be related:

//Global variable
float tx = 0, tz = 0, tSpeed = 1.0;         
bool isOrtho = true;                
float ONear = -20.0, OFar = 20.0;   
float PNear = 1.0, PFar = 41.0;     
float ptx = 0, pty = 0, ptSpeed = 0.1;  
float pry = 0, prSpeed = 1.0;           

void projection() {
    glMatrixMode(GL_PROJECTION);    //refer to projection matrix
    glLoadIdentity();               //reset projection matrix

    glTranslatef(ptx, pty, 0.0);    //translation for projection
    glRotatef(pry, 0.0, 1.0, 0.0);  //rotate for projection
    if (isOrtho) {
        //Ortho View
        glOrtho(-20.0, 20.0, -20.0, 20.0, ONear, OFar); //Ortho view
    }
    else {
        //Perspective view
        gluPerspective(45, 1.0, -1.0, 1.0);
        glFrustum(-20.0, 20.0, -20.0, 20.0, PNear, PFar);
    }
}

void display(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);
    projection();

    glMatrixMode(GL_MODELVIEW);
    lighting();
    drawRobot();
}
1

There are 1 best solutions below

0
BDL On

The glu docs for gluPerspective state:

zNear
Specifies the distance from the viewer to the near clipping plane (always positive).

(emphasis mine).

Your code gluPerspective(45, 1.0, -1.0, 1.0); passes -1.0 for zNear, which is not in the allowed range of values.