Reputation: 29
public Cube(int x, int y, int z, int x2, int y2, int z2){
int tmpX = x;
int tmpY = y;
int tmpZ = z;
if(x > x2){x = x2; x2 = tmpX;}
if(y > y2){y = y2; y2 = tmpY;}
if(z > z2){z = z2; z2 = tmpZ;}
int centerX = x2 - x;
int centerY = y2 - y;
int centerZ = z2 - z;
GL11.glTranslatef(centerX, centerY, centerZ);
GL11.glRotatef(rot, 0f, 0f, 1f);
GL11.glBegin(GL11.GL_QUADS);
//front
color(255, 0, 0);
v3d(x, y, z);
v3d(x, y2, z);
v3d(x2, y2, z);
v3d(x2, y, z);
//top
color(0, 255, 0);
v3d(x, y, z);
v3d(x2, y, z);
v3d(x2, y, z2);
v3d(x, y, z2);
//back
color(0, 0, 255);
v3d(x2, y2, z2);
v3d(x, y2, z2);
v3d(x, y, z2);
v3d(x2, y, z2);
//bottom
color(255, 255, 0);
v3d(x, y2, z);
v3d(x2, y2, z);
v3d(x2, y2, z2);
v3d(x, y2, z2);
//left
color(255, 0, 255);
v3d(x, y, z);
v3d(x, y2, z);
v3d(x, y2, z2);
v3d(x, y, z2);
//right
color(0, 255, 255);
v3d(x2, y, z);
v3d(x2, y2, z);
v3d(x2, y2, z2);
v3d(x2, y, z2);
GL11.glEnd();
GL11.glRotatef(-rot, 0f, 0f, 1f);
GL11.glTranslatef(-centerX, -centerY, -centerZ);
rot += 0.75f;
if(rot > 360){
rot -= 360;
}
}
I don't get why this isn't rotating around the Z axis just around the cube object itself, instead it appears to be just rotating around 0,0,0 in the matrix.
I also tried this for the centering code:
int xWidth = x2 - x;
int yWidth = y2 - y;
int zWidth = z2 - z;
int centerX = x + (xWidth / 2);
int centerY = y + (yWidth / 2);
int centerZ = z + (zWidth / 2);
But the first one made more sense after testing some math (15 - 5 = 10, 15 + 7.5 = 12.5). Any ideas?
Upvotes: 2
Views: 322
Reputation: 26023
I think that you may be trying to transfer your learning of mathematical transformations too directly to OpenGL. Interpreting what you've written literally, you're translating away from the origin, applying the rotation, and then attempting to undo both of those towards the bottom, in reverse order. It looks like you're trying to "undo" the transformations for some other transformation to happen later.
When we talk about transformations mathematically this makes sense, but in code we have a nice shortcut in glPushMatrix()
and glPopMatrix()
, where you can use both to "wrap" your transformations in a specific scope. It is very similar to a function scope, or a loop scope -- whatever transformations happen within this block won't influence anything outside of that scope.
The good:
GL11.glTranslatef(centerX, centerY, centerZ);
GL11.glRotatef(rot, 0f, 0f, 1f);
This applies your transformations to centerX, centerY, and centerZ, and then applies your rotation about the z-axis.
The bad:
GL11.glRotatef(-rot, 0f, 0f, 1f);
GL11.glTranslatef(-centerX, -centerY, -centerZ);
The glRotatef
and glTranslatef
here are unnecessary and can be commented out or removed.
This is how you would define the scope of a single "object" using your code:
GL11.glPushMatrix();
// Apply transformations here
GL11.glBegin(GL11.GL_QUADS);
// Draw object here
GL11.glEnd();
GL11.glPopMatrix();
So if you want to include other objects that won't be influenced by the transformation and rotation of your cube, you would wrap those transformations in a Push/Pop block. Here's another question that covers pushing and popping a bit better than I have here.
Upvotes: 1