gluLookAt(z=0) objects z=0 but they r not visible

Hello,

I have written a simple program to learn camera, perspective,projection and model_view matrix.

In this program :

  • I am setting the camera to look at (0,0,0) :x=0,y=0,z=0
    and placing the camera at (0,0,-20) : x=0,y=0,z=-20

  • first I place all the objects at z=0
    But they are not visible.
    They should be visible bcz camera is looking at z=0 and
    all the objects are at z=0

  • as soon as I place objects at z=-2 they get visible.

Here is my code :

#include <stdio.h>
#include <glut.h>
int fov = -2;

void handleResize(int w, int h){
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,double(w)/double(h),1, 100 );
printf("%d
",fov);
gluLookAt(0,0,0,0,0,-20,0,1,0);
}

void handleKeypress(unsigned char key,int x, int y){

switch(key){
    case 27:
        exit(0);
    case 56:
        fov+=2;
        glutPostRedisplay();
        break;
    case 50:
        fov-=2;
        glutPostRedisplay();
        break;
}
printf("Key: %d : Z: %d

",key,fov);
}
void init(){
glEnable(GL_DEPTH_TEST);
}

void drawScene(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f,0.0f,float(fov));

glBegin(GL_POINTS);
glVertex3f(20.0f, 20.0f, 0.0f);
glEnd();
glBegin(GL_QUADS);
glVertex3f(2.0f, 2.0f, 0.0f);
glVertex3f(-2.0f, 2.0f, 0.0f);
glVertex3f(-2.0f, -2.0f, 0.0f);
glVertex3f(2.0f, -2.0f, 0.0f);
glEnd();
glutSwapBuffers();

}

void main (int argc, char** argv){

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH);
glutInitWindowSize(400,400);
glutCreateWindow("Chayan Vinayak's OpenGL tutorial ");
init();
glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
glutMainLoop();

}

No, you didn’t ask OpenGL to do what you said you did.

Here you positioned the camera “at” 0,0,0 in WORLD-SPACE “looking at” 0,0,-20 with 0,1,0 being the camera up vector. So you’re looking down the -Z axis in WORLD-SPACE.

Your clip planes in EYE-SPACE are at Z=-1 and Z=-100 (that is, 1 unit and 100 units in front of you). So what you can see in WORLD-SPACE is in the -100 <= Z <= -1 range.

  • as soon as I place objects at z=-2 they get visible.

Yup :wink:

You also need to insert a:


glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); 

before your gluLookAt. VIEWING matrices go on the MODELVIEW stack, not the PROJECTION matrix stack. If you want more info here, see:

http://www.sjbaker.org/steve/omniv/projection_abuse.html