X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FCamera.cc;h=8f313b7aaf7d83b15ee8a97dc8ec12562c8c40e3;hp=6b6cfa1ac8572a26f6d8a558177813d9a342e434;hb=a31d65a998121df0651c57bfb68782e2a07d2e2f;hpb=31d52677b38d935297d132bdbd956c655cd3feee diff --git a/src/Moof/Camera.cc b/src/Moof/Camera.cc index 6b6cfa1..8f313b7 100644 --- a/src/Moof/Camera.cc +++ b/src/Moof/Camera.cc @@ -33,148 +33,134 @@ namespace Mf { -void Camera::setPosition(const Vector3& point) +void Camera::setPosition(const Vector3& position) { - position_ = point; - calculateSecondary(); - //Vector3 coeff[2] = {position_, point}; - //pInterp_.init(coeff, 0.1); + mState.position = position; } + void Camera::setRotation(const Quaternion& rotation) { - rotation_ = 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) { - projection_ = projection; + mProjection = projection; } void Camera::setProjection(Scalar fovy, Scalar aspect, Scalar abutting, Scalar distant) { - cml::matrix_perspective_yfov_RH(projection_, fovy, aspect, abutting, + cml::matrix_perspective_yfov_RH(mProjection, fovy, aspect, abutting, distant, cml::z_clip_neg_one); - calculateSecondary(); } -void Camera::uploadToGL() const +void Camera::uploadToGL(Scalar alpha) const { + calculate(alpha); + glMatrixMode(GL_PROJECTION); - glMultMatrix(projection_.data()); + glMultMatrix(mProjection.data()); glMatrixMode(GL_MODELVIEW); - glMultMatrix(modelview_.data()); + glMultMatrix(mModelview.data()); } -void Camera::update(Scalar t, Scalar dt) +void Camera::calculate(Scalar alpha) const { - //pInterp_.update(dt); - //position_ = pInterp_.getState(0.0); + State3 state = getState(alpha); - //calculateSecondary(); + cml::matrix_rotation_quaternion(mModelview, state.orientation); + + Matrix4 translate; + cml::matrix_translation(translate, state.position); + + mModelview *= translate; + + mFrustum.init(mModelview, mProjection); } -void Camera::lookAt(const Vector3& point) +void Camera::update(Scalar t, Scalar dt) +{ + RigidBody3::update(t, dt); +} + +void Camera::draw(Scalar alpha) const { - cml::quaternion_rotation_aim_at(rotation_, position_, point, - Vector3(0.0, -1.0, 0.0)); - calculateSecondary(); + 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) { - Vector3 vec = position_; - vec[0] -= 50.0; - setPosition(vec); + mState.position[0] -= ds; } else if (event.key.keysym.sym == SDLK_LEFT) { - Vector3 vec = position_; - vec[0] += 50.0; - setPosition(vec); + mState.position[0] += ds; } else if (event.key.keysym.sym == SDLK_UP) { - Vector3 vec = position_; - vec[1] -= 50.0; - setPosition(vec); + mState.position[1] -= ds; } else if (event.key.keysym.sym == SDLK_DOWN) { - Vector3 vec = position_; - vec[1] += 50.0; - setPosition(vec); + mState.position[1] += ds; } else if (event.key.keysym.sym == SDLK_PAGEUP) { - Vector3 vec = position_; - vec[2] += 50.0; - setPosition(vec); + mState.position[2] += ds; } else if (event.key.keysym.sym == SDLK_PAGEDOWN) { - Vector3 vec = position_; - vec[2] -= 50.0; - setPosition(vec); + mState.position[2] -= ds; } break; case SDL_MOUSEMOTION: { - Scalar xrel = cml::rad(Scalar(event.motion.xrel) / 5.0); - Scalar yrel = cml::rad(Scalar(event.motion.yrel) / 5.0); + Scalar xrel = cml::rad(Scalar(event.motion.xrel) / 6.0); + Scalar yrel = cml::rad(Scalar(event.motion.yrel) / 6.0); - Quaternion rotation = rotation_; + Quaternion rotation = mState.orientation; - cml::quaternion_rotate_about_world_x(rotation, yrel); - //rotation_.normalize(); - cml::quaternion_rotate_about_world_y(rotation, xrel); - rotation.normalize(); + cml::quaternion_rotate_about_world_x(rotation, yrel); + //mRotation.normalize(); + cml::quaternion_rotate_about_world_y(rotation, xrel); - setRotation(rotation); - break; + rotation.normalize(); + mState.orientation = rotation; } + break; case SDL_MOUSEBUTTONDOWN: if (event.button.button == SDL_BUTTON_WHEELUP) { - Vector3 vec = position_; - vec[2] += 50.0; - setPosition(vec); + mState.position[2] -= ds; } else if (event.button.button == SDL_BUTTON_WHEELDOWN) { - Vector3 vec = position_; - vec[2] -= 50.0; - setPosition(vec); + mState.position[2] -= ds; } break; } - - calculateSecondary(); -} - -void Camera::calculateSecondary() -{ - cml::matrix_rotation_quaternion(modelview_, rotation_); - - Matrix4 translate; - cml::matrix_translation(translate, position_); - - //modelview_.transpose(); - modelview_ *= translate; - //modelview_ = translate * modelview_; - - frustum_.init(modelview_, projection_); }