X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FCamera.cc;fp=src%2FMoof%2FCamera.cc;h=26e24ba358fbfd7494517a66409e75c5150f8b59;hp=0000000000000000000000000000000000000000;hb=29e3d45f7bbbf31eadf793c41ff2b3d9c47b7539;hpb=16d1a05b0777e97a45c48e2874aa4e5cc791282e diff --git a/src/Moof/Camera.cc b/src/Moof/Camera.cc new file mode 100644 index 0000000..26e24ba --- /dev/null +++ b/src/Moof/Camera.cc @@ -0,0 +1,166 @@ + +/******************************************************************************* + + Copyright (c) 2009, Charles McGarvey + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +*******************************************************************************/ + +#include +#include + + +namespace Mf { + + +void Camera::setPosition(const Vector3& point) +{ + //position_ = point; + Vector3 coeff[2] = {position_, point}; + pInterp_.init(coeff, 0.1); +} +void Camera::setRotation(const Quaternion& rotation) +{ + rotation_ = rotation; + //srcRotation_ = rotation_; + //dstRotation_ = rotation; + //tInterp_ = 0.0; +} + +void Camera::update(Scalar t, Scalar dt) +{ + pInterp_.update(dt); + position_ = pInterp_.getState(0.0); + + //tInterp_ += dt * 10.0; + //rotation_ = cml::slerp(srcRotation_, dstRotation_, cml::clamp(tInterp_, 0.0, 1.0)); + //rotation_.normalize(); + + calculateSecondary(); +} + + +void Camera::lookAt(const Vector3& point) +{ + quaternion_rotation_aim_at(rotation_, position_, point); +} + +void Camera::adjustFromInput(const Event& event) +{ + switch (event.type) + { + case SDL_KEYDOWN: + if (event.key.keysym.sym == SDLK_RIGHT || + event.key.keysym.sym == SDLK_d) + { + Vector3 vec = position_; + vec[0] -= 50.0; + setPosition(vec); + } + else if (event.key.keysym.sym == SDLK_LEFT || + event.key.keysym.sym == SDLK_a) + { + Vector3 vec = position_; + vec[0] += 50.0; + setPosition(vec); + } + else if (event.key.keysym.sym == SDLK_UP || + event.key.keysym.sym == SDLK_w) + { + Vector3 vec = position_; + vec[1] -= 50.0; + setPosition(vec); + } + else if (event.key.keysym.sym == SDLK_DOWN || + event.key.keysym.sym == SDLK_s) + { + Vector3 vec = position_; + vec[1] += 50.0; + setPosition(vec); + } + else if (event.key.keysym.sym == SDLK_PAGEUP) + { + Vector3 vec = position_; + vec[2] += 50.0; + setPosition(vec); + } + else if (event.key.keysym.sym == SDLK_PAGEDOWN) + { + //position_[2] -= 50.0; + Vector3 vec = position_; + vec[2] -= 50.0; + setPosition(vec); + } + break; + + case SDL_MOUSEMOTION: + { + Scalar xrel = cml::rad(Scalar(event.motion.xrel) / 5.0); + Scalar yrel = cml::rad(Scalar(event.motion.yrel) / 5.0); + + Quaternion rotation = rotation_; + + quaternion_rotate_about_world_x(rotation, yrel); + //rotation_.normalize(); + quaternion_rotate_about_world_y(rotation, xrel); + rotation.normalize(); + + setRotation(rotation); + break; + } + + case SDL_MOUSEBUTTONDOWN: + if (event.button.button == SDL_BUTTON_WHEELUP) + { + Vector3 vec = position_; + vec[2] += 50.0; + setPosition(vec); + } + else if (event.button.button == SDL_BUTTON_WHEELDOWN) + { + Vector3 vec = position_; + vec[2] -= 50.0; + setPosition(vec); + } + break; + } + + calculateSecondary(); +} + +void Camera::calculateSecondary() +{ + matrix_rotation_quaternion(transformation_, rotation_); + + Matrix4 translate; + matrix_translation(translate, position_); + + //transformation_ = translate * transformation_; + transformation_ *= translate; +} + + +} // namespace Mf + +/** vim: set ts=4 sw=4 tw=80: *************************************************/ +