# Model Translations

I am sure this is a common problem with OpenGL newbies. I am having some difficulty with the glRotatef and glTranslatef functions I hope someone can help me with. I want to allow the user to rotate (with left-mouse button) and move the Model (with right-mouse button). When I comment out either the rotations or translates separately they work fine, but when used together I get basically a rotation with some periodic jittering.

A snippet of my code follows:

``````public void draw() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
// Rotate around the X, Y, and Z axes
glRotatef(angleX, 1.0f, 0.0f, 0.0f);
glRotatef(angleY, 0.0f, 1.0f, 0.0f);
gl.glRotatef(angleZ, 0.0f, 0.0f, 1.0f);

// Move the Model
glTranslatef(xTrans, 0.0f, 0.0f);
glTranslatef(0.0f, yTrans, 0.0f);
glTranslatef(0.0f, 0.0f, 1.0f);

// Draw Model here
drawCube();

glPopMatrix();
glFlush();
}
``````

The xTrans, yTrans, angleX, angleY, and angleZ have been previously calculated from coordinates from the mouse. I think the problem may be the order of the Matrix transformations? Can anyone give me some hints as to why this is happening?

Put glTranslatef first.

And, why you won’t combine 3 of your translates into one: “glTranslatef(xTrans, yTrans, 1.0f)”;?

No clue why you get jittering, though.

You’re right, I don’t know why I didn’t just put the glTranslatef into one function. However, I get no translation when I place the glTranslatef. Perhaps the way I am calculating the rotates and translates is not correct. The following is a sample of my code:

``````// Constructor
public myClass() {
mouseRButtonDown = false;
view_rotx = 20.0f;
view_roty = 30.0f;
view_rotz = 0.0f;
xTrans = yTrans = 0.0f;
xTransInit = yTransInit = 0.0f;
}

public void mousePressed(MouseEvent e) {
prevMouseX = e.getX();
prevMouseY = e.getY();
if ((e.getModifiers() & e.BUTTON3_MASK) != 0) {
mouseRButtonDown = true;
Rectangle bounds = e.getComponent().getBounds();
xTransInit = ((e.getX()/(float)bounds.getMaxX())*2.0f) - 1.0f;
yTransInit = ((((float)bounds.getMaxY() - e.getY())/(float) bounds.getMaxY()) * 2.0f) - 1.0f;
}
}

public void mouseReleased(MouseEvent e) {
if ((e.getModifiers() & e.BUTTON3_MASK) != 0) {
mouseRButtonDown = false;
}
}

public void mouseDragged(MouseEvent e) {
int x = e.getX();
int y = e.getY();
Dimension size = e.getComponent().getSize();

float thetaY = 360.0f * ( (float)(x-prevMouseX)/(float)size.width);
float thetaX = 360.0f * ( (float)(prevMouseY-y)/(float)size.height);

prevMouseX = x;
prevMouseY = y;

view_rotx += thetaX;
view_roty += thetaY;

// Calculate the Change for MouseMovement
if (mouseRButtonDown) {
Rectangle bounds = e.getComponent().getBounds();

float xVal = ((e.getX()/(float)bounds.getMaxX())*2.0f) - 1.0f;
float yVal = ((((float)bounds.getMaxY() - e.getY())/(float)bounds.getMaxY())*2.0f) - 1.0f;

xTrans -= (xTransInit - xVal);
yTrans -= (yTransInit - yVal);

xTransInit = xVal;
yTransInit = yVal;
}

}

``````

I have no idea why the translations are not showing but the rotations are. BTW, I am using Java JOGL but the bindings to OpenGL are basically one-to-one.

Thanks.

Does anybody know if the code I have posted has any kind of obvious problem with the way I am calculating the rotations? I am not so sure about the Z-Rotational value.

Thanks.