Dezachu
Dezachu

Reputation: 153

Rotate camera NOT scene without gluLookAt

I'm created a grid to represent ground to walk on.

I've got my arrow keys linked up to rotate/move the view:

switch(currentKey)
        {
        case sf::Key::Up:
            z_trans+=1;
            break;

        case sf::Key::Down:
            z_trans-=1;
            break;

        case sf::Key::Left:
            y_angle-=0.5;
            break;

        case sf::Key::Right:
            y_angle+=0.5;
            break;
        }

With z_trans being the translation in z, y_angle being the rotation in the y axis in glRotatef().

I can get my 'character' to walk forward and turn left and right, and then continue walking straight in the direction he is facing. The only problem is that as I get away from the origin (0,0,0), the rotations occur AROUND the origin, not the camera. So in if I'm 100 units away from the origin, my character is basically following the path of a 100 radius circle.

WITHOUT using gluLookAt, does anyone have any ideas how to implement this? Completely stumped, been sat here a good 2 hours trying to figure it out.

The full code:

float elapsedTime=Clock.GetElapsedTime();
    if(elapsedTime>REFRESH_RATE){
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity(); 
        glTranslatef(x_trans, y_trans, z_trans); // y_trans == -25.f to keep us above the plane
        glRotatef(x_angle,1,0,0);
        glRotatef(y_angle,0,1,0);
        glRotatef(z_angle,0,0,1);

        currentKey=sf::Key::Num9;

        if (Event.Type == sf::Event::KeyPressed) currentKey = Event.Key.Code;
        switch(currentKey)
        {
        case sf::Key::Up:
            z_trans+=1;
            break;

        case sf::Key::Down:
            z_trans-=1;
            break;

        case sf::Key::Left:
            y_angle-=0.5;
            break;

        case sf::Key::Right:
            y_angle+=0.5;
            break;
        }
        drawGrid();

        Clock.Reset();
    }

Upvotes: 0

Views: 1603

Answers (1)

JohnD
JohnD

Reputation: 1159

Rotate fist, then translate second?

edit: I just checked some of my existing code.

glLoadIdentity();
// Rotate around origin first
glRotatef(RotY,-1.0f ,0.0f, 0.0f);
glRotatef(RotX, 0.0f, 1.0f, 0.0f);
// Then move the camera
glTranslatef(-Player.x, -Player.y, -Player.z);

Upvotes: 0

Related Questions