/*] Copyright (c) 2009-2010, Charles McGarvey [************************** **] All rights reserved. * * vi:ts=4 sw=4 tw=75 * * Distributable under the terms and conditions of the 2-clause BSD license; * see the file COPYING for a complete text of the license. * **************************************************************************/ #include "Camera.hh" #include "OpenGL.hh" namespace Mf { void Camera::setPosition(const Vector3& position) { mState.position = position; } void Camera::setRotation(const Quaternion& rotation) { mState.orientation = rotation; } void Camera::lookAt(const Vector3& point) { // FIXME this doesn't work as expected cml::quaternion_rotation_aim_at(mState.orientation, mState.position, point, Vector3(0.0, 1.0, 0.0)); } void Camera::setProjection(const Matrix4& projection) { mProjection = projection; } void Camera::setProjection(Scalar fovy, Scalar aspect, Scalar abutting, Scalar distant) { cml::matrix_perspective_yfov_RH(mProjection, fovy, aspect, abutting, distant, cml::z_clip_neg_one); } void Camera::uploadToGL(Scalar alpha) const { calculate(alpha); glMatrixMode(GL_PROJECTION); glMultMatrix(mProjection.data()); glMatrixMode(GL_MODELVIEW); glMultMatrix(mModelview.data()); } void Camera::calculate(Scalar alpha) const { State3 state = getState(alpha); cml::matrix_rotation_quaternion(mModelview, state.orientation); Matrix4 translate; cml::matrix_translation(translate, state.position); mModelview *= translate; mFrustum.init(mModelview, mProjection); } void Camera::update(Scalar t, Scalar dt) { RigidBody3::update(t, dt); } void Camera::draw(Scalar alpha) const { mSphere.draw(alpha); } void Camera::handleEvent(const Event& event) { const Scalar ds = 50.0; switch (event.type) { case SDL_KEYDOWN: if (event.key.keysym.sym == SDLK_RIGHT) { mState.position[0] -= ds; } else if (event.key.keysym.sym == SDLK_LEFT) { mState.position[0] += ds; } else if (event.key.keysym.sym == SDLK_UP) { mState.position[1] -= ds; } else if (event.key.keysym.sym == SDLK_DOWN) { mState.position[1] += ds; } else if (event.key.keysym.sym == SDLK_PAGEUP) { mState.position[2] += ds; } else if (event.key.keysym.sym == SDLK_PAGEDOWN) { mState.position[2] -= ds; } break; case SDL_MOUSEMOTION: { Scalar xrel = cml::rad(Scalar(event.motion.xrel) / 6.0); Scalar yrel = cml::rad(Scalar(event.motion.yrel) / 6.0); Quaternion rotation = mState.orientation; cml::quaternion_rotate_about_world_x(rotation, yrel); //mRotation.normalize(); cml::quaternion_rotate_about_world_y(rotation, xrel); rotation.normalize(); mState.orientation = rotation; } break; case SDL_MOUSEBUTTONDOWN: if (event.button.button == SDL_BUTTON_WHEELUP) { mState.position[2] -= ds; } else if (event.button.button == SDL_BUTTON_WHEELDOWN) { mState.position[2] -= ds; } break; } } } // namespace Mf