1

I'm using OpenGL in Qt through the QGLWidget, and I'm trying to implement some basic mouse and keyboard-driven interaction.

So for instance, the keyboard arrow keys will be used to "pan" the scene, the mouse wheel to zoom in the scene, and the mouse should be used to rotate the model.

I have figured out how to implement the panning and the zooming, but I have trouble with implementing the rotation.

This is what I have so far:

void MyGLWidget::paintGL() {
    glLoadIdentity();
    gluLookAt(0+camDelta[0],0+camDelta[1],-100+camDelta[2],centerCoords[0]+lookAtDelta[0],centerCoords[1]+lookAtDelta[1],centerCoords[2]+lookAtDelta[2],0,1,0);
    // draw stuff here
}

So basically I set the initial gluLookAt parameters, and then I added two arrays, float camDelta[3] and float lookAtDelta[3], to track the change in the camera position and where it's looking in response to the user interacting with the scene.

For the mouse wheel, I do:

void MyGLWidget::wheelEvent(QWheelEvent *event) {
    camDelta[2] += (event->delta() / 8.0 / 15.0) * WHEEL_DELTA;
    lookAtDelta[2] += (event->delta() / 8.0 / 15.0) * WHEEL_DELTA;
    updateGL();
}

Similarly in MyGLWidget::keyPressEvent, I modify the the deltas' [0] field for panning left and right, and the deltas' [1] for panning up and down.

So my question is , how do I implement something similar for supporting rotation with the mouse? I'm kind of confused, because I'm guessing I'll have to somehow change not only where the camera is looking but also the up vector, but I don't really have a clear idea on how to do that.

houbysoft
  • 32,532
  • 24
  • 103
  • 156
  • Hi Houbysoft, i am also looking for panning functionality in android.[i need to move the GLSurfaceview using touch event]. if you got any idea now , pls give any sample code. it would be helpful for me to complete my work.. my sample code here.http://www.mediafire.com/?09f9q51xnqhq2l7.which – harikrishnan Jul 06 '13 at 18:11

2 Answers2

3

What I have done for things like this is called the orbital camera model.

You keep track of the camera target position (or look at), and 3 other parameters: azimuth, declination (or inclination), and distance (or radius). Azimuth is the rotation of the camera around the target horizontally (as if the target was on the ground, and you were walking around it in a circle while looking at it). Declination is the vertical rotation. Distance is, well, the distance between the target and camera.

These are also called spherical coordinates:

http://mathworld.wolfram.com/SphericalCoordinates.html

You want the equations like this:

x = r * sin(phi) * cos(theta)
y = r * sin(phi) * sin(theta)
z = r * cos(phi)

Where r is the distance, theta is the azimuth, and phi is the declination. This gives you the offset to go from the camera lookat position to the camera position.

So here's what you do. Keep the camera target position, but not the camera position. When you pan the camera, move the target position. When you rotate the camera, adjust the azimuth and declination. When you zoom the camera, adjust the distance. Then, every frame before you call gluLookAt, compute the camera position using the above formula. You just add that x, y, and z to the target position.

  • Hi ALan, i am also looking for panning functionality in android.[i need to move the GLSurfaceview using touch event]. if you have any idea now , pls give any sample code. it would be helpful for me to complete my task.i am struggle here. my sample code here.http://www.mediafire.com/?09f9q51xnqhq2l7.which – harikrishnan Jul 06 '13 at 18:13
0

The best approach for this is to use Quaternions. Like the complex numbers are quite useful to rotate in 2D, quaternions are perfect for 3D rotation.

The standard 'rotate around x, y, z axis' can work, it will behave strange and not as expected.

You can get a nice tutorial about all this at: http://content.gpwiki.org/index.php/OpenGL:Tutorials:Using_Quaternions_to_represent_rotation

Ivan
  • 640
  • 5
  • 9
  • Hi lvan, i am also looking for panning functionality in android.[i need to move the GLSurfaceview using touch event]. if you have any idea now , pls give any sample code. it would be helpful for me to complete my task.i am struggle here. my sample code here.http://www.mediafire.com/?09f9q51xnqhq2l7.which – harikrishnan Jul 06 '13 at 18:13