X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=src%2Fmoof%2Fcamera.cc;fp=src%2Fmoof%2Fcamera.cc;h=4a4e4bf71a2b6f711e100627d8d03221d9489f13;hb=831f04d4bc19a390415ac0bbac4331c7a65509bc;hp=0000000000000000000000000000000000000000;hpb=299af4f2047e767e5d79501c26444473bda64c64;p=chaz%2Fyoink diff --git a/src/moof/camera.cc b/src/moof/camera.cc new file mode 100644 index 0000000..4a4e4bf --- /dev/null +++ b/src/moof/camera.cc @@ -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 +