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