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.