Moving the camera to the left and right based on mouse movement

Viewed 89

I know this has been asked before, but I have yet to find an answer that works in my case. Basically, I want the camera to move left and right based on the mouse cursor position. The more the mouse is to the left, the more the camera turns to the left. So it should be possible to turn around and move in the reverse direction. How do I do this?

This is my camera position:

GLfloat cameraPosition[] = { 0.0, 0.0, 3.5 };
GLfloat lx = 0.0; GLfloat ly = 0.0;

This is my projection matrix:

    // set to projection mode 
    glMatrixMode(GL_PROJECTION);

    // clear any previous transformations
    glLoadIdentity();

    // set the perspective 
    gluPerspective(45, (float)windowWidth / (float)windowHeight, 0.1, 20);

In the myDisplay function, this how I set the camera position:

    glMatrixMode(GL_MODELVIEW);

    glLoadIdentity();

    // set the camera position
    gluLookAt(cameraPosition[0], cameraPosition[1], cameraPosition[2],
        lx, ly, cameraPosition[2] - 100,
        0, 1, 0);

What should I do in the glutPassiveMotionFunc function?

1 Answers

Most probably you need to do something like this

glRotatef(-yAngle, 0.0f, 1.0, 0.0f);
glRotatef(-xAngle, 1.0f, 0.0f, 0.0f);
glTranslatef(cameraPosition[0], cameraPosition[1], cameraPosition[2])

instead of gluLookAt(). Try it out, maybe it will solve your problem.

Related