2
votes

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();

}
2
Did you read the man page? opengl.org/sdk/docs/man/xhtml/gluPerspective.xml . Also you're doing integer division for aspect, you need to do float division instead, otherwise it will round 500/300 to 1. (500.f/300.f)Tim

2 Answers

2
votes

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.

0
votes

Like Luca already said, near and far define the range of visible objects. But most important, never use a value <= 0 for the near or far value (which you currently do), as this will result in a singular projection matrix (with whatever strange render outcome that will result in).

Use some reasonably small value for the near clipping plane, but not too small as this will result in a bad depth buffer precision. Use something reasonable, for example when modeling a normal real world, a near distance of 10cm is usually a good choice (whatever amount of generic units this means in the actual application), as you won't get your eyes that much nearer any object. But it depends on your application, like the far value.

And of course, like Tim said, at the moment your aspect ratio is 1.0, since you do integer division.