/*! -------------------------------------------------------------------- \file Camera.cpp \brief Class to a encapsulate a camera \author James Kuffner \date 2002-09-20 -------------------------------------------------------------------- */ #ifndef IGNORE_DEPEND #include #include #endif #include "camera.h" #include #include "m4x4.h" #include "playership.h" //#include //! initialize singleton static variable Camera *Camera::s_pCamera = NULL; //! default camera setup //! default camera position const float DEFAULT_EYE[3] = { 0, 10, 20 }; const float DEFAULT_TARGET[3] = { 0, 0, 0 }; const float DEFAULT_UP[3] = { 0, 1, 0 }; //! default camera location CameraState Camera::s_defaultState; //! vector copy inline void COPY_VECTOR(float dest[3], const float src[3]) { dest[0] = src[0]; dest[1] = src[1]; dest[2] = src[2]; } //--------------------------------------------------------------------- // Camera // // Method: Constructor // //--------------------------------------------------------------------- Camera::Camera() { _fov = DEFAULT_FOV; _nearClip = DEFAULT_NEAR_CLIP; _farClip = DEFAULT_FAR_CLIP; // initialize default camera state COPY_VECTOR(s_defaultState.eye, DEFAULT_EYE); COPY_VECTOR(s_defaultState.target, DEFAULT_TARGET); COPY_VECTOR(s_defaultState.up, DEFAULT_UP); // look at params _state = s_defaultState; cout << "Constructing camera" << endl; } //--------------------------------------------------------------------- // Camera // Method: SetViewingParams // //! function to alter the camera viewing parameters // //--------------------------------------------------------------------- void Camera::SetViewingParams(float fov, float nearClip, float farClip) { assert(fov > 0); assert(nearClip > 0); assert(farClip > 0); _fov = fov; _nearClip = nearClip; _farClip = farClip; } //--------------------------------------------------------------------- // Camera // Method: SetViewingParams // //! set the aspect ratio // //--------------------------------------------------------------------- void Camera::SetAspectRatio(float aspect) { assert(aspect > 0); _aspectRatio = aspect; } //--------------------------------------------------------------------- // Camera // // Method: SetGLViewTransforms // //! load the viewing transformations on the matrix stacks // //--------------------------------------------------------------------- void Camera::SetGLViewTransforms() { enum { X = 0, Y, Z }; // update camera viewing transforms glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(_fov, _aspectRatio, _nearClip, _farClip); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // gluLookAt(_state.eye[X], _state.eye[Y], _state.eye[Z], // _state.target[X], _state.target[Y], _state.target[Z], // _state.up[X], _state.up[Y], _state.up[Z]); Math3d::M4x4 mat = thePlayer.GetRotationMat(); Math3d::M3d pos = thePlayer.GetPosition(); #if 1 Math3d::M3d dir = thePlayer.GetDirection(); // cout << "Dir: " << dir << endl; Math3d::M3d up = Math3d::M3d(0.0, 1.0, 0.0); up = mat * up; // cout << up << endl; Math3d::M3d from = pos; Math3d::M3d look = pos + dir; //cout << "From: " << from << endl; //cout << "Look: " << look << endl; //cout << "Up: " << up << endl; gluLookAt(from[0], from[1], from[2], look[0], look[1], look[2], up[0], up[1], up[2]); #else //Math3d::M3d direction = thePlayer.GetDirection(); //Math3d::M3d loc = pos + direction*5; //cout << mat << endl; // glTranslatef(loc[0], loc[1], loc[2]); // cout << loc << endl; glLoadMatrixd(mat); #endif // cout << pos << endl; //cout << "lookin" << endl; }