# VDrift/vdrift

### Subversion checkout URL

You can clone with
or
.
Fetching contributors…

Cannot retrieve contributors at this time

96 lines (73 sloc) 2.626 kB
 #ifndef _CAMERA_H #define _CAMERA_H #include "mathvector.h" #include "quaternion.h" #include "coordinatesystem.h" #include ///base class for a camera class CAMERA { public: CAMERA(const std::string & camera_name) : name(camera_name), fov(0) {} virtual ~CAMERA() {} const std::string & GetName() const { return name; } void SetFOV(float value) { fov = std::max(40.0f, std::min(160.0f, value)); } float GetFOV() const { return fov; } virtual const MATHVECTOR & GetPosition() const { return position; } virtual const QUATERNION & GetOrientation() const { return rotation; } // reset position, orientation virtual void Reset(const MATHVECTOR & newpos, const QUATERNION & newquat) {}; // update position, orientation virtual void Update(const MATHVECTOR & newpos, const QUATERNION & newquat, float dt) {}; // move relative to current position, orientation virtual void Move(float dx, float dy, float dz) {}; // rotate relative to current position, orientation virtual void Rotate(float up, float left) {}; protected: const std::string name; MATHVECTOR position; QUATERNION rotation; float fov; }; inline float AngleBetween(MATHVECTOR vec1, MATHVECTOR vec2) { float dotprod = vec1.Normalize().dot(vec2.Normalize()); float angle = acos(dotprod); float epsilon = 1e-6; if (fabs(dotprod) <= epsilon) angle = M_PI * 0.5; if (dotprod >= 1.0-epsilon) angle = 0.0; if (dotprod <= -1.0+epsilon) angle = M_PI; return angle; } inline QUATERNION LookAt( MATHVECTOR eye, MATHVECTOR center, MATHVECTOR up) { QUATERNION rotation; MATHVECTOR forward(center - eye); forward = forward.Normalize(); MATHVECTOR side = (forward.cross(up)).Normalize(); MATHVECTOR realup = side.cross(forward); //rotate so the camera is pointing along the forward line float theta = AngleBetween(forward, direction::Forward); assert(theta == theta); if (fabs(theta) > 0.001) { MATHVECTOR axis = forward.cross(direction::Forward).Normalize(); rotation.Rotate(-theta, axis[0], axis[1], axis[2]); } //now rotate the camera so it's pointing up MATHVECTOR curup = direction::Up; rotation.RotateVector(curup); float rollangle = AngleBetween(realup, curup); if (curup.dot(side) > 0.0) { rollangle = 2 * M_PI - rollangle; } assert(rollangle == rollangle); MATHVECTOR axis = forward; rotation.Rotate(rollangle, axis[0], axis[1], axis[2]); return rotation; } #endif // _CAMERA_H
Something went wrong with that request. Please try again.