Question

i have drawn three boxes each same size but having different distance from camera. these boxes ought to be perceived as decreasing in size as they are moving away from the camera. how do i acheive this illusion of distance.

//these are the three planes for boxes

  // first plane
  gl.glVertex3i(0, 30, 30);
  gl.glVertex3i(10, 30, 30);
  gl.glVertex3i(10, 20, 30);
  gl.glVertex3i(0, 20, 30);

  //2nd Plane
  gl.glVertex3i(20, 20, 37);
  gl.glVertex3i(30, 20, 37);
  gl.glVertex3i(30, 10, 37);
  gl.glVertex3i(20, 10, 37);

  //3rd Plane
  gl.glVertex3i(40, 10, 45);
  gl.glVertex3i(50, 10, 45);
  gl.glVertex3i(50, 0, 45);
  gl.glVertex3i(40, 0, 45);

//and this is eye at up code.

gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
glu.gluLookAt(
              35, 15,  10, 
              25, 15, 30, 
      0,  1,  0   
              );

gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrtho(-50.0, 50.0, -30.0, 30.0, 0.0, 60.0);
Was it helpful?

Solution

You need to use perspective projection, instead of orthographic projection.

Instead of calling

gl.glOrtho(-50.0, 50.0, -30.0, 30.0, 0.0, 60.0);

You should be able to replace that line with

GLU glu = new GLU();
glu.gluPerspective(60.0, 4.0/3.0, 1.0, 100.0);

The arguments I provided might not be correct for your program, so you might need to adjust them.

The arguments, in order, are: fovy, aspect, zNear and zFar.

From the manpage:

fovy: Specifies the field of view angle, in degrees, in the y direction.

aspect: Specifies the aspect ratio that determines the field of view in the x direction. The aspect ratio is the ratio of x (width) to y (height).

zNear: Specifies the distance from the viewer to the near clipping plane (always positive).

zFar: Specifies the distance from the viewer to the far clipping plane (always positive).

The GLU class is located here

import javax.media.opengl.glu.GLU
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top