I'm working on a test project to learn some basic OpenGL. Right now I'm trying to work on a "camera" with gluLookAt. My camera object is really simple, I have two vectors, one for the position and the other for the rotation (x, y and z on both). I'm having trouble with the target point gluLookAt receives, how can I find a target point using only the position and camera angle?
-
1\$\begingroup\$ What do you mean by finding a target point? Are your inputs: camPos, camOrientation(x,y,z) and targetPos(x,y,z)? You don't need a camera angle if you have its camOrientation vector.. Sorry, it's a tad ambiguous \$\endgroup\$– teodronCommented Apr 22, 2012 at 8:54
-
\$\begingroup\$ I think I meant Orientation instead of Rotation. I don't have the target, only the camera position angles on each axis, I'm trying to find a target with that. \$\endgroup\$– Luke B.Commented Apr 23, 2012 at 17:21
-
\$\begingroup\$ You have the position/eye for the glulookat. If you assume the camera looking towards (0,0,-1) and the up vector to be (0,1,0) as usually with OpenGL, then you must construct a matrix M from your angles (use Euler angles.. but beware of the infamous gimbal lock issue). Once you get the M, your direction is M * (0,0,-1), your up vector M*(0,1,0). To find the center/lookatpoint, add to your camPos the M*(0,0,-1) vector. There you go. This is a bad way to do it, but it's educational.. go for angle-axis and quaternions in the future. \$\endgroup\$– teodronCommented Apr 24, 2012 at 9:13
2 Answers
If you have desired rotation angles for your camera, then gluLookAt
is not really the appropriate function to use. Instead, use glRotate
three times, once for each axis of rotation (typically in roll, pitch, yaw order) using the negative of the desired camera angle on each axis, then glTranslate
with the negative of the camera position.
(Why the negatives? Because you're constructing a matrix to go from world space to camera space, so everything's opposite to the transformations that go from camera space to world space.)
The position passed to gluLookAt()
is relative to the origin. Since you have one point relative to another point, then get that point relative to the origin by subtracting the second point from it, i.e.
V1 - V2 = (v1.x-v2.x,v1.y-v2.y,v1.z-v2.z)
And pass the result to gluLookAt()
.
After calling gluLookat()
, call glTranslate()
to "move" the camera to its position vector.