I am writing a simple program that uses perspective projection and I have a bunch of objects drawn in my scene. For perspective projection I am using the following code:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluLookAt(eyePosX, eyePosY, eyePosZ, centerPosX, centerPosY, centerPosZ, 0.0, 1.0, 0.0);
glFrustum(frustumLeft,frustumRight,frustumBottom,frustumTop,frustumNear,frustumFar);
When I have an object drawn with a certain offset on the X axis that does not inside into the frustum, the object is stil drawn, but is elongated and not culled by the frustum.
What are the coordinates of the 8 points in the XYZ space with respect to eyePosX/Y/Z and frustumLeft/Right/Bottom/Top/Near/Far?
How can I tell OpenGL to perform the culling of the objects that are not inside the frustum?
There are two possibilities. The first is that you really didn’t mean to do what this code does. The second is that you did mean it, but don’t fully understand what you’ve done.
Let’s cover the first one now. The look-at matrix should never go inside the
GL_PROJECTIONmatrix. Also, the look-at matrix always comes after the projection matrix. These should always be true unless you’re doing something special.Which leads to the second. If you really intend to rotate and offset the post-projective space, then you cannot expect geometry to be culled against the frustum. Why?
Because OpenGL doesn’t do frustum culling. It culls against whatever post-T&L vertex positions you provide. If you rotate the view outside of the frustum, then that’s what gets drawn. OpenGL doesn’t draw what isn’t visible; if you change the view post-projection so that things that wouldn’t have been visible are visible now, then you’ve changed what is and is not visible.