Well, I bit the bullet and decided to try to write my own matrix math function. Right now I’m only doing it for the x-axis until I can get it working. It’s pretty fugly, but I think the math is right. The issue is that nothing displays, so I think I’m doing it wrong with the OpenGL functions somewhere.
Here’s my rotateX and related declarations:
private float[] xMatrix = {0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0};
.
.
.
private float[] rotationMatrixX = {1.0f, 0, 0, 0,
0, (float)Math.cos(degreesX), (float)Math.sin(degreesX), 0,
0,-((float)Math.sin(degreesX)), (float)Math.cos(degreesX), 0,
0, 0, 0, 1.0f};
.
.
.
public void rotateX()
{
float[] tempMatrixX = new float[16];
tempMatrixX[0] = (xMatrix[0] * rotationMatrixX[0]) + (xMatrix[4] * rotationMatrixX[1]) + (xMatrix[8] * rotationMatrixX[2]) + (xMatrix[12] * rotationMatrixX[3]);
tempMatrixX[1] = (xMatrix[1] * rotationMatrixX[0]) + (xMatrix[5] * rotationMatrixX[1]) + (xMatrix[9] * rotationMatrixX[2]) + (xMatrix[13] * rotationMatrixX[3]);
tempMatrixX[2] = (xMatrix[2] * rotationMatrixX[0]) + (xMatrix[6] * rotationMatrixX[1]) + (xMatrix[10] * rotationMatrixX[2]) + (xMatrix[14] * rotationMatrixX[3]);
tempMatrixX[3] = (xMatrix[3] * rotationMatrixX[0]) + (xMatrix[7] * rotationMatrixX[1]) + (xMatrix[11] * rotationMatrixX[2]) + (xMatrix[15] * rotationMatrixX[3]);
tempMatrixX[4] = (xMatrix[0] * rotationMatrixX[4]) + (xMatrix[4] * rotationMatrixX[5]) + (xMatrix[8] * rotationMatrixX[6]) + (xMatrix[12] * rotationMatrixX[7]);
tempMatrixX[5] = (xMatrix[1] * rotationMatrixX[4]) + (xMatrix[5] * rotationMatrixX[5]) + (xMatrix[9] * rotationMatrixX[6]) + (xMatrix[13] * rotationMatrixX[7]);
tempMatrixX[6] = (xMatrix[2] * rotationMatrixX[4]) + (xMatrix[6] * rotationMatrixX[5]) + (xMatrix[10] * rotationMatrixX[6]) + (xMatrix[14] * rotationMatrixX[7]);
tempMatrixX[7] = (xMatrix[3] * rotationMatrixX[4]) + (xMatrix[7] * rotationMatrixX[5]) + (xMatrix[11] * rotationMatrixX[6]) + (xMatrix[15] * rotationMatrixX[7]);
tempMatrixX[8] = (xMatrix[0] * rotationMatrixX[8]) + (xMatrix[4] * rotationMatrixX[9]) + (xMatrix[8] * rotationMatrixX[10]) + (xMatrix[12] * rotationMatrixX[11]);
tempMatrixX[9] = (xMatrix[1] * rotationMatrixX[8]) + (xMatrix[5] * rotationMatrixX[9]) + (xMatrix[9] * rotationMatrixX[10]) + (xMatrix[13] * rotationMatrixX[11]);
tempMatrixX[10] = (xMatrix[2] * rotationMatrixX[8]) + (xMatrix[6] * rotationMatrixX[9]) + (xMatrix[10] * rotationMatrixX[10]) + (xMatrix[14] * rotationMatrixX[11]);
tempMatrixX[11] = (xMatrix[3] * rotationMatrixX[8]) + (xMatrix[7] * rotationMatrixX[9]) + (xMatrix[11] * rotationMatrixX[10]) + (xMatrix[15] * rotationMatrixX[11]);
tempMatrixX[12] = (xMatrix[0] * rotationMatrixX[12]) + (xMatrix[4] * rotationMatrixX[13]) + (xMatrix[8] * rotationMatrixX[14]) + (xMatrix[12] * rotationMatrixX[15]);
tempMatrixX[13] = (xMatrix[1] * rotationMatrixX[12]) + (xMatrix[5] * rotationMatrixX[13]) + (xMatrix[9] * rotationMatrixX[14]) + (xMatrix[13] * rotationMatrixX[15]);
tempMatrixX[14] = (xMatrix[2] * rotationMatrixX[12]) + (xMatrix[6] * rotationMatrixX[13]) + (xMatrix[10] * rotationMatrixX[14]) + (xMatrix[14] * rotationMatrixX[15]);
tempMatrixX[15] = (xMatrix[3] * rotationMatrixX[12]) + (xMatrix[7] * rotationMatrixX[13]) + (xMatrix[11] * rotationMatrixX[14]) + (xMatrix[15] * rotationMatrixX[15]);
for(int i = 0; i < xMatrix.length; i++)
{
xMatrix[i] = tempMatrixX[i];
}
}
//this is called in the main display() method
public void finalizeRotation(GL gl)
{
gl.glMultMatrixf(FloatBuffer.wrap(xMatrix));
}
Here’s my display function:
public void display(GLAutoDrawable drawable)
{
GL gl = drawable.getGL();
// Clear the drawing area
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
// Reset the current matrix to the "identity"
gl.glLoadIdentity();
gl.glPushMatrix();
cube.finalizeTranslate(gl);
cube.finalizeRotation(gl);
cube.finalizeScaling(gl);
//Here's where we start drawing. ORDER MATTERS.
//In case you want to do textures or something, you need to draw
//everything in counter-clockwise order.
// Draw A Quad
gl.glBegin(GL.GL_QUADS);
gl.glColor4f(0.5f, 0.5f, 1.0f, 0.2f); // Set the current drawing color to light blue
cube.stackMaker(gl);
// Done Drawing The Quad
gl.glEnd();
// Flush all drawing operations to the graphics card
gl.glFlush();
gl.glPopMatrix();
}