Yusha Yusha - 2 months ago 8
Java Question

How to rotate an image about the Z Axis in JOGL without using the glRotateF built-in functions

How can I rotate an image about the Z axis in JOGL without using

glRotatef
function? I don't understand how to get z' since there is no
e.getZ()
function. How might one calculate a
z_prime
? Right now when I try to rotate about the z axis, it like rotates it about the z axis in the beggining and then begins rotating it about the y axis and then back to the z (which makes sense because i have
z_rot += y_prime
. How can I just rotate it about the z and calculate a z'?

public void transform(float[] vertices_in, float[] vertices_out){

// perform your transformation

int length = vertices_in.length;
float[] transformMatrix =
{
r11, r12, r13, tx,
r21, r22, r23, ty,
r31, r32, r33, tz,
0, 0, 0, 1
};




// Fill in the empty verticesout vector
for(int i = 0; i < vertices_in.length; i++)
{
vertices_out[i] = vertices_in[i];
}
// loop through and set the new matrix (after translation)
// because
// 1 0 0 0
// 0 1 0 0
// 0 0 1 0

// X Rotation
for(int i = 0; i < vertices_out.length; i += 3)
{
vertices_out[i+1] = vertices_out[i+1] * (float)Math.cos(xRot) - vertices_out[i+2] * (float)Math.sin(xRot); //New Y
vertices_out[i+2] = vertices_out[i+1] * (float)Math.sin(xRot) + vertices_out[i+2] * (float)Math.cos(xRot); //New Z
}

// Y Rotation
for(int i = 0; i < vertices_out.length; i += 3)
{
vertices_out[i] = vertices_out[i] * (float)Math.cos(yRot) + vertices_out[i+2] * (float)Math.sin(yRot);
vertices_out[i+2] = vertices_out[i]*-(float)Math.sin(yRot) + vertices_out[i+2]*(float)Math.cos(yRot);
}

// Z Rotation
for(int i = 0; i < vertices_out.length; i += 3)
{
vertices_out[i] = vertices_out[i] * (float)Math.cos(zRot) - vertices_out[i+1] * (float)Math.sin(zRot);
vertices_out[i+1] = vertices_out[i] * (float)Math.sin(zRot) + vertices_out[i+1] * (float)Math.cos(zRot);
}

// Translation & Scaling
for(int i = 0; i < vertices_in.length; i+=3)
{
vertices_out[i] = vertices_out[i] * transformMatrix[0] + transformMatrix[3]; //x'
vertices_out[i+1] = vertices_out[i+1] * transformMatrix[5] + transformMatrix[7]; //y'
vertices_out[i+2] = vertices_out[i+2] * transformMatrix[10] + transformMatrix[11]; //z'
}

}

@Override
public void mouseDragged(MouseEvent e)
{
if(rotating)
{
float XX = (e.getX()-windowWidth*0.5f)*orthoX/windowWidth;
float YY = -(e.getY()-windowHeight*0.5f)*orthoX/windowWidth;

float x_prime = (StartXX - XX);
float y_prime = (StartYY - YY);

if(rightMouseClick)
{
zRot += y_prime/50;

}
else
{
xRot += y_prime/50;
yRot += x_prime/50;
StartYY = YY;
StartXX = XX;
}

}
}

Answer

All these rotations are using an inconsistent intermediate state. E.g (I just substituted for shorter variable names).

y = y * (float)Math.cos(xRot) - z * (float)Math.sin(xRot); //New Y
z = y * (float)Math.sin(xRot) + z * (float)Math.cos(xRot); //New Z

The first line is correct. However, the second line already uses the new y when it should use the old one. Hence, you have to save the old variable somewhere to make it work. The same applies to all other rotations.

The rest of the code looks ok.