In OpenGL I'm trying to rotate a camera around a point, with camera being distance r from the point and facing the point when it rotates. In other words, I want the camera to move along the circumference of a circle at a radius r from the center, with the camera facing the center at any point along the circumference.
Lets say that in 3d space the center of the circle is (3, 0, 3);
I've tried:
// move to center of circle glTranslatef(-3, 0, -3) // move a distance away from the circle glTranslatef(0, 0, r); // rotate along the y "up" axis glRotatef(CameraAngle, 0, 1, 0);
where CameraAngle is the degrees being moved around the circle.
My end result is the camera is still rotating along the origin, not the center of the circle. Can anyone help me fix this problem? Thanks!
To rotate around a different point, the formula: X = cx + (x-cx)*cosA - (y-cy)*sinA, Y = cy + (x-cx)*sinA + (y-cy)*cosA, cx, cy is centre coordinates, A is the angle of rotation. The OpenGL function is glRotatef (A, x, y, z). 3. Scaling: Scaling refers to zooming in and out an object on different scales across axes.
As far as OpenGL is concerned, there is no camera. More specifically, the camera is always located at the eye space coordinate (0.0, 0.0, 0.0). To give the appearance of moving the camera, your OpenGL application must move the scene with the inverse of the camera transformation by placing it on the MODELVIEW matrix.
Pan. Moves the view up, down, left and right. To pan the view, hold down Shift and drag MMB in the 3D Viewport. For discrete steps, use the hotkeys Ctrl - Numpad8 , Ctrl - Numpad2 , Ctrl - Numpad4 and Ctrl - Numpad6 as with orbiting.
You need to either:
or:
gluLookAt
to keep the camera pointing at the center of the circle(*) rotation functions normally rotate about the origin. To rotate around another point P
you have to:
it is a little confusing, but i think you should:
// move camera a distance r away from the center glTranslatef(0, 0, -r); // rotate glRotatef(angley, 0, 1, 0); glRotatef(anglex, 1, 0, 0); // move to center of circle glTranslatef(-cx, -cy, -cz)
note the order must NOT be changed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With