]> Dogcows Code - chaz/yoink/blobdiff - src/moof/camera.cc
the massive refactoring effort
[chaz/yoink] / src / moof / camera.cc
diff --git a/src/moof/camera.cc b/src/moof/camera.cc
new file mode 100644 (file)
index 0000000..4a4e4bf
--- /dev/null
@@ -0,0 +1,152 @@
+
+/*]  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 moof {
+
+
+void camera::position(const vector3& position)
+{
+       state_.position = position;
+}
+
+void camera::rotation(const quaternion& rotation)
+{
+       state_.orientation = rotation;
+}
+
+void camera::look_at(const vector3& point)
+{
+       // FIXME: this doesn't work as expected
+       quaternion_rotation_aim_at(state_.orientation,
+                                                                       state_.position, point,
+                                                                       vector3(0.0, 1.0, 0.0));
+}
+
+
+void camera::projection(const matrix4& projection)
+{
+       projection_ = projection;
+}
+
+void camera::projection(scalar fovy, scalar aspect, scalar abutting,
+                                                  scalar distant)
+{
+       matrix_perspective_yfov_RH(projection_, fovy, aspect, abutting,
+                                                                       distant, z_clip_neg_one);
+}
+
+
+void camera::upload_to_gl(scalar alpha) const
+{
+       calculate(alpha);
+
+       glMatrixMode(GL_PROJECTION);
+       glMultMatrix(projection_.data());
+
+       glMatrixMode(GL_MODELVIEW);
+       glMultMatrix(modelview_.data());
+}
+
+void camera::calculate(scalar alpha) const
+{
+       state3 state = rigid_body3::state(alpha);
+
+       matrix_rotation_quaternion(modelview_, state.orientation);
+
+       matrix4 translate;
+       matrix_translation(translate, state.position);
+
+       modelview_ *= translate;
+
+       frustum_.init(modelview_, projection_);
+}
+
+
+void camera::update(scalar t, scalar dt)
+{
+       rigid_body3::update(t, dt);
+}
+
+void camera::draw(scalar alpha) const
+{
+       sphere_.draw(alpha);
+}
+
+
+void camera::handle_event(const event& event)
+{
+       const scalar ds = 50.0;
+
+       switch (event.type)
+       {
+               case SDL_KEYDOWN:
+                       if (event.key.keysym.sym == SDLK_RIGHT)
+                       {
+                               state_.position[0] -= ds;
+                       }
+                       else if (event.key.keysym.sym == SDLK_LEFT)
+                       {
+                               state_.position[0] += ds;
+                       }
+                       else if (event.key.keysym.sym == SDLK_UP)
+                       {
+                               state_.position[1] -= ds;
+                       }
+                       else if (event.key.keysym.sym == SDLK_DOWN)
+                       {
+                               state_.position[1] += ds;
+                       }
+                       else if (event.key.keysym.sym == SDLK_PAGEUP)
+                       {
+                               state_.position[2] += ds;
+                       }
+                       else if (event.key.keysym.sym == SDLK_PAGEDOWN)
+                       {
+                               state_.position[2] -= ds;
+                       }
+                       break;
+
+               case SDL_MOUSEMOTION:
+                       {
+                               scalar xrel = rad(scalar(event.motion.xrel) / 6.0);
+                               scalar yrel = rad(scalar(event.motion.yrel) / 6.0);
+
+                               quaternion rotation = state_.orientation;
+
+                               quaternion_rotate_about_world_x(rotation, yrel);
+                               //mRotation.normalize();
+                               quaternion_rotate_about_world_y(rotation, xrel);
+
+                               rotation.normalize();
+                               state_.orientation = rotation;
+                       }
+                       break;
+
+               case SDL_MOUSEBUTTONDOWN:
+                       if (event.button.button == SDL_BUTTON_WHEELUP)
+                       {
+                               state_.position[2] -= ds;
+                       }
+                       else if (event.button.button == SDL_BUTTON_WHEELDOWN)
+                       {
+                               state_.position[2] -= ds;
+                       }
+                       break;
+       }
+}
+
+
+} // namespace moof
+
This page took 0.024994 seconds and 4 git commands to generate.