Search code examples
visual-c++clippingopengl-3

Increasing OpenGL's far clip plane distance


I'm trying to make a C++ OpenGL representation of our Solar System as a way to teach myself OpenGL, so please keep your answers simple. The problem I have is that planets are very far away, so everything else is beyond the clipping plane when viewing from any given planet. How do I move the clipping of C++ OpenGL 3.1 plane to, say, 2000000000? I'd prefer a simple code snippet if you can. I've looked up SO and forum posts about this, but they're either so old that nothing applies (using legacy APIs or just dead links), or so complex that I can't work out what they're saying.


Solution

  • Clipping planes are defined by the perspective projection matrix.

    If you use glFrustum, change the last argument passed to it to 2000000000.0.

    If you use your own matrix, set 10th element of your matrix array to:

    (2000000000.0+nearClippingPlane)/(nearClippingPlane-2000000000.0)

    (the formula is (far+near)/(near-far))

    and 14th to:

    (-4000000000.0*nearClippingPlane)/(2000000000.0-nearClippingPlane)

    (the formula is (-2.0*near*far)/(far-near))

    2000000000 is very big value, however, so Z-fighting may occur if you add details such as mountains.