]> Dogcows Code - chaz/yoink/blob - src/moof/camera.cc
remove some unused stlplus modules
[chaz/yoink] / src / moof / camera.cc
1
2 /*] Copyright (c) 2009-2011, Charles McGarvey [*****************************
3 **] All rights reserved.
4 *
5 * Distributable under the terms and conditions of the 2-clause BSD license;
6 * see the file COPYING for a complete text of the license.
7 *
8 *****************************************************************************/
9
10 #include "camera.hh"
11 #include "opengl.hh"
12
13
14 namespace moof {
15
16
17 void camera::position(const vector3& position)
18 {
19 state_.position = position;
20 }
21
22 void camera::rotation(const quaternion& rotation)
23 {
24 state_.orientation = rotation;
25 }
26
27 void camera::look_at(const vector3& point)
28 {
29 // FIXME: this doesn't work as expected
30 quaternion_rotation_aim_at(state_.orientation, state_.position, point,
31 vector3(0.0, 1.0, 0.0));
32 }
33
34 void camera::projection(const matrix4& projection)
35 {
36 projection_ = projection;
37 }
38
39 void camera::projection(scalar fovy,
40 scalar aspect, scalar abutting, scalar distant)
41 {
42 matrix_perspective_yfov_RH(projection_, fovy, aspect, abutting,
43 distant, z_clip_neg_one);
44 }
45
46 void camera::upload_to_gl(scalar alpha) const
47 {
48 calculate(alpha);
49
50 glMatrixMode(GL_PROJECTION);
51 glMultMatrix(projection_.data());
52
53 glMatrixMode(GL_MODELVIEW);
54 glMultMatrix(modelview_.data());
55 }
56
57 void camera::calculate(scalar alpha) const
58 {
59 state3 state = rigid_body3::state(alpha);
60
61 matrix_rotation_quaternion(modelview_, state.orientation);
62
63 matrix4 translate;
64 matrix_translation(translate, state.position);
65
66 modelview_ *= translate;
67
68 frustum_.init(modelview_, projection_);
69 }
70
71 void camera::update(scalar t, scalar dt)
72 {
73 rigid_body3::update(t, dt);
74 }
75
76 void camera::draw(scalar alpha) const
77 {
78 sphere_.draw(alpha);
79 }
80
81 void camera::handle_event(const event& event)
82 {
83 const scalar ds = 50.0;
84
85 switch (event.type)
86 {
87 case SDL_KEYDOWN:
88 switch (event.key.keysym.sym)
89 {
90 case SDLK_RIGHT:
91 state_.position[0] -= ds; break;
92 case SDLK_LEFT:
93 state_.position[0] += ds; break;
94 case SDLK_UP:
95 state_.position[1] -= ds; break;
96 case SDLK_DOWN:
97 state_.position[1] += ds; break;
98 case SDLK_PAGEUP:
99 state_.position[2] += ds; break;
100 case SDLK_PAGEDOWN:
101 state_.position[2] -= ds; break;
102 default:
103 break;
104 }
105 break;
106
107 case SDL_MOUSEMOTION:
108 {
109 scalar xrel = rad(scalar(event.motion.xrel) / 6.0);
110 scalar yrel = rad(scalar(event.motion.yrel) / 6.0);
111
112 quaternion rotation = state_.orientation;
113
114 quaternion_rotate_about_world_x(rotation, yrel);
115 //mRotation.normalize();
116 quaternion_rotate_about_world_y(rotation, xrel);
117
118 rotation.normalize();
119 state_.orientation = rotation;
120 break;
121 }
122
123 case SDL_MOUSEBUTTONDOWN:
124 switch (event.button.button)
125 {
126 case SDL_BUTTON_WHEELUP:
127 state_.position[2] -= ds;
128 break;
129 case SDL_BUTTON_WHEELDOWN:
130 state_.position[2] += ds;
131 break;
132 }
133 }
134 }
135
136
137 } // namespace moof
138
This page took 0.036982 seconds and 4 git commands to generate.