Search code examples
opengljoglprojectionclippingisometric

OpenGL isometric projection clipping


I am using this neat trick from another Stack Overflow question: True Isometric Projection with OpenGL to setup an isometric projection. If I draw just outline with lines everything is fine 1 but if I draw polygons they get clipped and so lines 2.

       Correct      Clipped

Code to setup projection:

    pmv.glMatrixMode(PMVMatrix.GL_PROJECTION);
    pmv.glLoadIdentity();
    float dist = (float)Math.sqrt(1 / 3.0f);
    pmv.gluLookAt(dist, dist, dist,
              0.0f,  0.0f,  0.0f,
              0.0f,  1.0f,  0.0f);

NOTE: I am using JOGL, so it might be a possible bug there; other than that, no culling, depth test enabled and depthrange for offsetting lines/polys.


Solution

  • By closer look at frustum near and far planes you can see they are set badly

        N: Plane[ [ 0.57735026, 0.57735026, 0.57735026 ], 0.0], 
        F: Plane[ [ -0.57735026, -0.57735026, -0.57735026 ], 1.9999998]], 
    

    solution is easy set ortho projection first with correct near/far values and then multiply it with lookat function matrix

        pmv.glMatrixMode(PMVMatrix.GL_PROJECTION);
        pmv.glLoadIdentity();
        pmv.glOrthof(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);
        float dist = (float)Math.sqrt(1 / 3.0f);
        pmv.gluLookAt(dist, dist, dist,
                  0.0f,  0.0f,  0.0f,
                  0.0f,  1.0f,  0.0f);
    

    results in:

        N: Plane[ [ -0.57735026, -0.57735026, -0.57735026 ], 1.9999998], 
        F: Plane[ [ 0.57735026, 0.57735026, 0.57735026 ], 0.0]],