Search code examples
copenglmatrixrotationglu

Rotate about Y-Axis gluLookAt


I am trying to rotate the viewer about the y-axis. I have a function called tranform_eye() which will calculate the next position of eyex, eyey and eyez after each update.

Can anyone help me figure out how to calculate the values for eyex, eyey and eyez?

My Code:

float eyex = 5;
float eyey = 5;
float eyez = 5;

void display() {

    transform_eye();

    glMatrixMode(GL_PROJECTION);     // To operate on model-view matrix
    glLoadIdentity();
    gluPerspective(40.0, 1.0, 1.0, 10000.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    gluLookAt(eyex, eyey, eyez,
              0.0, 0.0, 0.0,
              0.0, 1.0, 0.0);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color and depth buffers

    drawTriangles();

    glutSwapBuffers();  // Swap the front and back frame buffers (double buffering)
}

void transform(){
    /// calculate new eyex, y z.
}

Solution

  • Applying the math from e.g. this answer gives us:

    void transform()
    {
        float theta = 0.01; //angle in radians to rotate every frame
        float cosTheta = cos(theta);
        float sinTheta = sin(theta);
        float newX = cosTheta * eyeX + sinTheta * eyeZ;
        eyeZ = -sinTheta * eyeX + cosTheta * eyeZ;
        eyeX = newX;
    }