copenglglutopengl-compatglu

gluLookAt doesn't work with some parameters



I'm trying to move the camera along the XYZ axes.
To do this, I change the third parameter in the glulookat function and it behaves strangely.

Here is my entire code:

glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(
    0, 0, 1, //camera pos
    0, 0, 0, //camera target
    0.0, 1.0, 0.0
);

//draw grid

glutSwapBuffers();

If I write

gluLookAt(
0, 0, 1, //camera pos
0, 0, 0, //camera target
0.0, 1.0, 0.0
);

then everything works and draws a grid

But if I put

gluLookAt(
0, 0, 2, //camera pos
0, 0, 0, //camera target
0.0, 1.0, 0.0
);

then I don't see the grid.

Why is this happening and how to get rid of it?


Solution

  • The first argument of gluLookAt is the position of the camera. When you increase the distance between the camera and the object, the object is clipped by the near plane of the viewing volume.

    The perspective projection is invalid

    glFrustum(-100, 100, -100, 100, -100, 100);
    

    because at perspective projection the distance to the near and far plane has to be positive. glFrustum defines a Viewing frustum, where 0 < near < far:

    Change the near plane to solve the issue. For instance:

    glFrustum(-100, 100, -100, 100, 0.1, 100);