Search code examples
javaopengljoglperspectiveperspectivecamera

How do I use gluPerspective() in jogl?


I have an init() method, and I am trying to create a Perspective rendering. Below is the code I have so far, but the numbers that I'm passing to gluPerspective(fovy, aspect, zNear, zFar) are wrong. I think fovy is the field of view (60 degrees), and aspect is the aspect ratio (width / height), but I don't know what zNear and zFar are.

public void init(GLAutoDrawable gld) {
    //We will  use the default ViewPort

    GL gl = gld.getGL();
    glu = new GLU();

    gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);


    glu.gluLookAt(
            25, 15, 0, // eye
            25, 15, 30, // at
            0, 1, 0 // up
            );



    // Set up camera for Orthographic projection:
    gl.glMatrixMode(GL.GL_PROJECTION);
    gl.glLoadIdentity();
    glu.gluPerspective(60, 500/300, 0.0, 60.0);

    gl.glMatrixMode(GL.GL_MODELVIEW);
    gl.glLoadIdentity();

}

Solution

  • Near and far values specify the range in which objects are visible (they define the near and far clipping planes). Indeed objects more far than 60.0 will not be visible using that perspective.

    As Tim as already commented, it's bettere to explicitly write floating point values (i.e. 500.0f/300.0f).

    But worse, you setup the look-at matrix before setting it to identity (assuming that at the beginning of the method the matrix mode is GL.GL_MODELVIEW). Probably it is better the following sequence:

    gl.glMatrixMode(GL.GL_MODELVIEW);
    gl.glLoadIdentity();
    glu.gluLookAt(...);
    

    Maybe you need to investigate more about the camera abstraction.