简体   繁体   中英

Perspective projection (misunderstood gluPerspective)

I want to clarify things with gluPerspective near and far parameters, I know that they define the range in z axis for all objects - so objects closer/away than near/far will be clipped by the clipping algorithms. And when lets say near = 0.1, and far = 100*winWid, we are not seeing anything because objects are behind of the viewer (and camera by default is at (0.0, 0.0, 0.0) plus openGL user coordinates system is right handed), so then we call (see code below) translate(0.0, 0.0, -winWid) to move back by -z axis objects to place them in front of the camera. But if we set far = -100*winWid; everything works same as with positive far value.

So what's being changed when far is negative ??

Why in that case nothing is clipped too ??

#include <gl/glut.h>
#include <math.h>    

const float winWid = 1000.0f;
const float winHei = 800.0f;      
GLfloat cube_side = 200.0f;
GLfloat ALPHA = 0.7f;

void render();    
void updateDisplay()
{
    render(cubeAngle, rotx, roty, rotz);
}    

void drawCube(const GLfloat& a)
{
    glBegin(GL_QUADS);
      //    back face   
      glColor4f(0.0f, 1.0f, 0.0f, ALPHA);
      glVertex3f(0.0f, 0.0f, 0.0f);
      glVertex3f(0.0f, a, 0.0f);
      glVertex3f(a, a, 0.0f);
      glVertex3f(a, 0.0f, 0.0f); 
      //  and other cube faces here ...
    glEnd();
}      

void render()
{       
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    
    glPushMatrix();    
        drawCube(cube_side);    
    glPopMatrix();  
    glutSwapBuffers();
} 

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_ALPHA);
    glutInitWindowSize(winWid, winHei);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("window");    
    glutDisplayFunc(updateDisplay);     
    glEnable(GL_DEPTH_TEST);                                    //  depth buffer setup
    glEnable(GL_BLEND);                                         //  transparency setup
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);              

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();   

    gluPerspective(55.0f, winWid/winHei, 0.1f, 100*winWid);
    glMatrixMode(GL_MODELVIEW); 
    glTranslatef(0.0f, 0.0f, -winWid);                          //  move back to see drawing objects
    glRotatef(75.0f, -1.0f, 0.0f, 0.0f);                            //  make z+ axis point up to emphasize 3D (wihout this rotate z+ points towards the viewer)

    glutMainLoop();
    return 0;
}

Negative far-plane values are not supported by gluPerspective. The documentation states:

zFar: Specifies the distance from the viewer to the far clipping plane (always positive) . (source)

By default, the camera in OpenGL looks along the negative z-axis. So the visible area is [-near, -far] in world coordinates. In your code example, the object is located at z=-1000, while the visible range is from [-0.01, -100*1000], which means that the object is clearly in view.

One additional thing to mention is the depth-buffer precision: This is mainly defined by the range given by nearPlane and farPlane. Assuming, that you have a precision of 16-bit (can be more or less depending on the setup), one can store 2^16 different depth values. This means with your setup, objects can be relative far away from each other and will still be treated as being at the same depth. You may think about whether this huge depth range is really necessary for the application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM