help w/ correcting rotation

Hey all,

Can anyone help w/ the following code. I’m trying to rotate an object using LEFT, RIGHT, UP, DOWN, PAGE UP, and PAGE DOWN keys. The work but my object doesn’t rotate as it should. If I press the LEFT key, I’d like it to turn left (which I think is a rotation on the Y axis). It turns left wierd like.

I’ve been using the NEHE tutorials, but I can’t tell what’s wrong here. Any help much appreciated.


private float xrot = 0.0f;
private float yrot = 0.0f;
private float zrot = 0.0f;
private float xspeed = 0.0f;
private float yspeed = 0.0f;
// i’ve tried many different values here.
private float angle = 10.0f;

public void display(GLDrawable drawable) {

    GL gl = drawable.getGL();
    GLU glu = drawable.getGLU();
    this.gldrawable = drawable;
                        
    gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
    gl.glLoadIdentity();
    gl.glColor3f(1.0f, 1.0f, 1.0f);
     
    // viewing transformation
    gl.glMatrixMode(GL.GL_MODELVIEW);
    // this is default
    //glu.gluLookAt(0.0, 0.0, 0.0, 0.0, 0.0, -100.0, 0.0, 1.0, 0.0);

    gl.glRotatef(angle, xrot, yrot, zrot);

    if (selectedFile) {
        model.draw();
    }
        
    gl.glFlush();
}


public void keyPressed(KeyEvent e) {                                                                               
    switch (e.getKeyCode()) {
                                                                            
        case KeyEvent.VK_PAGE_UP:
            zrot += 2.0f;
                                                                            
            break;
                                                                            
        case KeyEvent.VK_PAGE_DOWN:
            zrot -= 2.0f;
                                                                            
            break;
                                                                            
        case KeyEvent.VK_UP:
            xrot += 2.0f;
                                                                            
            break;
                                                                            
        case KeyEvent.VK_DOWN:
            xrot -= 2.0f;
                                                                            
            break;
                                                                            
        case KeyEvent.VK_RIGHT:
            yrot += 2.0f;

            break;
             
        case KeyEvent.VK_LEFT:
            yrot -= 2.0f;
             
            break;
    }
             
    this.gldrawable.display();
}

You should review your glRotate call and compare it with NeHe’s Lesson 7.

I would also strongly suggest you read the OpenGL Programming Guide (aka Redbook). A free, older version can be found at www.gamedev.net/download/redbook.pdf

As for the glRotate()-call. You shoul use 3 calls instead of one:


gl.glRotatef(xrot,1.0f,0.0f,0.0f);                                    // Rotate On The X Axis By xrot
gl.glRotatef(yrot,0.0f,1.0f,0.0f);                                    // Rotate On The Y Axis By yrot
gl.glRotatef(zrot,0.0f,0.0f,1.0f);                                    // Rotate On The Z Axis By zrot

The first argument is the angle to rotate, the other three form the vector, meaning the axis, to rotate around.