]> Dogcows Code - chaz/yoink/blobdiff - src/Moof/Camera.cc
tcp socket disconnecting by remote
[chaz/yoink] / src / Moof / Camera.cc
index cdebc7a7b735b7e681e3b28f041f576aa9166d7f..d7f1fe2d34c20752aa2667c9e8723c0472ee7ca7 100644 (file)
 
-/*******************************************************************************
-
- 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 <iostream>
-#include <Moof/Camera.hh>
-#include <Moof/OpenGL.hh>
+/*]  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& point)
+void Camera::setPosition(const Vector3& position)
 {
-       position_ = point;
-       //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 near, Scalar far)
+void Camera::setProjection(Scalar fovy, Scalar aspect, Scalar abutting,
+                                                  Scalar distant)
 {
-       cml::matrix_perspective_yfov_RH(projection_, fovy, aspect, near, far,
-                       cml::z_clip_neg_one);
-
-       calculateSecondary();
+       cml::matrix_perspective_yfov_RH(mProjection, fovy, aspect, abutting,
+                                                                       distant, cml::z_clip_neg_one);
 }
 
 
-void Camera::uploadProjectionToGL() const
+void Camera::uploadToGL(Scalar alpha) const
 {
+       calculate(alpha);
+
        glMatrixMode(GL_PROJECTION);
-       glLoadMatrix(projection_.data());
+       glMultMatrix(mProjection.data());
 
        glMatrixMode(GL_MODELVIEW);
+       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);
+
+       cml::matrix_rotation_quaternion(mModelview, state.orientation);
+
+       Matrix4 translate;
+       cml::matrix_translation(translate, state.position);
 
-       //calculateSecondary();
+       mModelview *= translate;
+
+       mFrustum.init(mModelview, mProjection);
 }
 
 
-void Camera::lookAt(const Vector3& point)
+void Camera::update(Scalar t, Scalar dt)
 {
-       quaternion_rotation_aim_at(rotation_, position_, point);
+       RigidBody3::update(t, dt);
 }
 
-void Camera::adjustFromInput(const Event& event)
+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 ||
-                                       event.key.keysym.sym == SDLK_d)
+                       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 ||
-                                       event.key.keysym.sym == SDLK_a)
+                       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 ||
-                                       event.key.keysym.sym == SDLK_w)
+                       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 ||
-                                       event.key.keysym.sym == SDLK_s)
+                       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)
                        {
-                               //position_[2] -= 50.0;
-                               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;
 
-                       quaternion_rotate_about_world_x(rotation, yrel);
-                       //rotation_.normalize();
-                       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()
-{
-       matrix_rotation_quaternion(modelview_, rotation_);
-
-       Matrix4 translate;
-       matrix_translation(translate, position_);
-
-       //modelview_ = translate * modelview_;
-       modelview_ *= translate;
-
-       frustum_.init(modelview_, projection_);
 }
 
 
 } // namespace Mf
 
-/** vim: set ts=4 sw=4 tw=80: *************************************************/
-
This page took 0.022461 seconds and 4 git commands to generate.