camera question
hi,
im quite new to opengl and have this problem.
ive implemented a camera in my program which uses the arrow keys to move the forward/backwards and rotates left/right. i have an object on my screen and was wandering how i can get the camera to rotate left/right around the object because right now the camera only rotates relative to the 'eye' of the camera.
thanks
That's a different type of movement. If you only have one object, you could rotate the object instead of the camera. Otherwise:
Variables:
AngleX, AngleY, AngleZ - Angles of how to look at the object
ObjX, ObjY, ObjZ - Object position in your world
Radius - Radius of rotation around your object
Code:
glLoadIdentity
glTranslate(0, 0, -Radius)
glRotate(AngleY, 0, 1, 0)
glRotate(AngleX, 1, 0, 0)
glRotate(AngleZ, 0, 0, 1)
DrawYourObjectAtHere
glTranslate(-ObjX, -ObjY, -ObjZ)
DrawRestOfWorldAsIfWereAt000Position
To "move" the camera, change the angles and the radius.
Variables:
AngleX, AngleY, AngleZ - Angles of how to look at the object
ObjX, ObjY, ObjZ - Object position in your world
Radius - Radius of rotation around your object
Code:
glLoadIdentity
glTranslate(0, 0, -Radius)
glRotate(AngleY, 0, 1, 0)
glRotate(AngleX, 1, 0, 0)
glRotate(AngleZ, 0, 0, 1)
DrawYourObjectAtHere
glTranslate(-ObjX, -ObjY, -ObjZ)
DrawRestOfWorldAsIfWereAt000Position
To "move" the camera, change the angles and the radius.
Quote:Original post by gseed101
ive implemented a camera in my program which uses the arrow keys to move the forward/backwards and rotates left/right. i have an object on my screen and was wandering how i can get the camera to rotate left/right around the object because right now the camera only rotates relative to the 'eye' of the camera.
It depends how you're doing your current camera movement... suppose you extract the local orientation vectors from camera's transformation matrix. Then all you need, is to build a 'look at' matrix for your camera to rotate it correctly at the end of each 'tick'
// matrix made out of 4 vector4's: x, y, z and t(ranslation)matrix4::set_lookAt( const vector3& Eye, const vector3& Target ) { // Eye can be either current camera position or new spot for it // Target is the spot to look at vector3 direction = Target - Eye; direction.normalize(); vector3 right; if( is_zero( direction.x ) && is_zero( direction.z ) ) right = cross( direction, vector3::unit_x ); else right = cross( direction, vector3::unit_y ); right.normalize(); vector3 up = cross( right, direction ); set_identity(); x = right; y = up; z = -direction; t = Eye;}
now, if you use such matrix to place/rotate your camera, because at each frame your camera is re-aligned to face the target, the regular left-right movement becomes in fact orbitting around the target... the forward/backward keys bring the camera closer/farther from the target, and up/down work in similar manner moving your camera along arc 'above' the target. And when you want to detach camera from target, just disable the building of look-at matrix in camera code.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement