Open source Java projects: Java Binding for OpenGL (JOGL)

A low-level Java API for 3D graphics

1 2 3 4 5 6 7 8 9 Page 5
Page 5 of 9

The display() event method is always called after reshape(). After telling OpenGL to clear the drawable's surface to the background color, and to clear the depth buffer in preparation for another round of depth testing, this method initializes the viewpoint, viewing direction, and what direction is up (positive Y or negative Y axis, for example) via the following method:

public void gluLookAt(double eyeX, double eyeY, double eyeZ,
                      double centerX, double centerY, double centerZ,
                      double upX, double upY, double upZ)
  • eyeX, eyeY, and eyeZ establish the viewpoint
  • centerX, centerY, and centerZ establish the viewing direction via a reference point
  • upX, upY, and upZ establish the up vector

This method, which you can think of as setting up an imaginary camera, is one of many useful OpenGL Utilities (GLU) Library methods that exist in the javax.media.opengl.glu.GLU class. Before its invocation, a GLU instance must be created by invoking its public GLU() constructor -- you can call this constructor from outside GLEventListener methods.

I've chosen a viewpoint that positions the camera one unit in front of either the triangle (front view) or the square (back view). At this distance, the triangle completely fills the viewport (front view) and the square occupies 1/16th of the viewport (back view) -- I still want to see part of the triangle when viewing from the back, which is why the square is so much smaller.

The display() method goes on to render a triangle followed by a square into OpenGL's back buffer. Because the square is rendered last, the depth buffer ensures that the square (which is located behind the triangle when viewed from the front) will not be rendered in front of the triangle (when viewed from the same direction). After display() exits, JOGL swaps the back and front buffers, and the scene appears.

Assuming the original setup and a Windows platform, compile JOGLDemo1.java via the following command line:

javac -cp "c:\jogl\jogl.jar" JOGLDemo1.java

Run this application via the command line below:

java -cp "c:\jogl\jogl.jar;c:\jogl\gluegen-rt.jar;." -Djava.library.path="c:\jogl" JOGLDemo1

Figure 3 reveals the resulting scene.

The default smooth shading model ensures that colors are interpolated between the three vertices.
Figure 3. The default smooth shading model ensures that colors are interpolated between the three vertices.

Let's observe the scene from the back. This time, include the back argument, as follows:

java -cp "c:\jogl\jogl.jar;c:\jogl\gluegen-rt.jar;." -Djava.library.path="c:\jogl" JOGLDemo1 back

Figure 4 reveals the resulting scene.

The square is no longer eclipsed by the triangle..
Figure 4. The square is no longer eclipsed by the triangle.
Related:
1 2 3 4 5 6 7 8 9 Page 5
Page 5 of 9