OpenGL is perhaps most directly used with C/C++. JOGL serves two purposes. First, it provides "wrapper functions" via JNI so you can call OpenGL functions from Java. Second, it extends classes in Java.awt to allow for the creation of OpenGL windows. (From C/C++, this is typically done with a GUI library such as GLUT, Tck/Tk, Motif, or the MFC in Visual C++ in Windows).
The use of OpenGL via JOGL is quite simple, and can be briefly summarized as:
glBegin(GL_POLYGON);
glVertex3f(0.25, 0.25, 0.0);
glVertex3f(0.75, 0.25, 0.0);
glVertex3f(0.75, 0.75, 0.0);
glVertex3f(0.25, 0.75, 0.0);
glEnd();
would be written in Java as:
gl.glBegin(GL.GL_POLYGON);
gl.glVertex3f(0.25f, 0.25f, 0.0f);
gl.glVertex3f(0.75f, 0.25f, 0.0f);
gl.glVertex3f(0.75f, 0.75f, 0.0f);
gl.glVertex3f(0.25f, 0.75f, 0.0f);
gl.glEnd();
Notice the explicit type specifications (the f's) in the
arguments to gl.glVertex3f(). As a result, it's probably
easier to use double for everything:
gl.glBegin(GL.GL_POLYGON);
gl.glVertex3d(0.25, 0.25, 0.0);
gl.glVertex3d(0.75, 0.25, 0.0);
gl.glVertex3d(0.75, 0.75, 0.0);
gl.glVertex3d(0.25, 0.75, 0.0);
gl.glEnd();
There are also some differences in argument structure, in particular when
passing arrays to OpenGL functions. For instance, when using the vector
versions of vertex functions, like gl.glVertex3dv(), or
gl.glColor3dv(), you need to add a second argument with the
value zero:
double[] p = { 1.0, 1.0, 2.0 };
gl.glVertex3dv(p, 0);
or when specifying a clipping plane,
a third argument of zero:
double[] eqn = { 0.0, 1.0, 0.0, 1.5 };
gl.glClipPlane(GL.GL_CLIP_PLANE0, eqn, 0);
The skeleton code you'll be using defines gl and (when needed) glu.
NOTE: You will probably not need to use this material at all, unless you are doing something unusual (or, if you are doing something the hard way). So, if you're using this material, ask for help!
It's easy to get the current matrix on the matrix stack using glGetDoublev, multiplying it back (later) using glMultMatrixd. See the code below for an example, and also for example code that converts back and forth between the one-dimensional array format that OpenGL uses, and a Matrix4d (it's a bit messy since the row/column order is switched).
double[] curmat = new double[16];
// Get the current matrix on the MODELVIEW stack
gl.glGetDoublev(GL.GL_MODELVIEW_MATRIX, curmat, 0);
// Note: If you want to multiply the matrix 'curmat' onto the
// matrix stack at a later time, just use this:
// gl.glMultMatrixd(curmat, 0);
// Print out the contents of this matrix in OpenGL format
System.out.println("as an OpenGL matrix");
for (int row = 0; row < 4; row++)
for (int col = 0; col < 4; col++)
// OpenGL uses column-major order for storage
System.out.format("%7.3f%c", curmat[row+col*4], col==3 ? '\n':' ');
// Convert this matrix to vecmath Matrix4d
// (which uses row-major order, the conversion is done using transpose)
Matrix4d cMat = new Matrix4d(curmat);
cMat.transpose();
// Print out the contents of the matrix as a Matrix4d
System.out.println("as a Matrix4d");
for (int row = 0; row < 4; row++)
for (int col = 0; col < 4; col++)
System.out.format("%7.3f%c", cMat.getElement(row, col), col==3 ? '\n':' ');
// We can convert this Matrix4d back to an OpenGL matrix
// (in this case this has no effect...)
// Put Matrix4d back in array in OpenGL order
for (int row = 0; row < 4; row++)
for (int col = 0; col < 4; col++)
curmat[row+col*4] = cMat.getElement(row, col);
// Print out the contents of this matrix in OpenGL format
System.out.println("back again to an OpenGL matrix");
for (int row = 0; row < 4; row++)
for (int col = 0; col < 4; col++)
// OpenGL uses column-major order for storage
System.out.format("%7.3f%c", curmat[row+col*4], col==3 ? '\n':' ');
System.out.println();
428 Home