]> Dogcows Code - chaz/yoink/blob - src/Moof/Camera.cc
fixed layer bugs; generalized octree
[chaz/yoink] / src / Moof / Camera.cc
1
2 /*******************************************************************************
3
4 Copyright (c) 2009, Charles McGarvey
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 *******************************************************************************/
28
29 #include "Camera.hh"
30 #include "OpenGL.hh"
31
32
33 namespace Mf {
34
35
36 void Camera::setPosition(const Vector3& point)
37 {
38 position_ = point;
39 calculateSecondary();
40 //Vector3 coeff[2] = {position_, point};
41 //pInterp_.init(coeff, 0.1);
42 }
43 void Camera::setRotation(const Quaternion& rotation)
44 {
45 rotation_ = rotation;
46 }
47
48
49 void Camera::setProjection(const Matrix4& projection)
50 {
51 projection_ = projection;
52 }
53
54 void Camera::setProjection(Scalar fovy, Scalar aspect, Scalar abutting,
55 Scalar distant)
56 {
57 cml::matrix_perspective_yfov_RH(projection_, fovy, aspect, abutting,
58 distant, cml::z_clip_neg_one);
59 calculateSecondary();
60 }
61
62
63 void Camera::uploadToGL() const
64 {
65 glMatrixMode(GL_PROJECTION);
66 glMultMatrix(projection_.data());
67
68 glMatrixMode(GL_MODELVIEW);
69 glMultMatrix(modelview_.data());
70 }
71
72 void Camera::update(Scalar t, Scalar dt)
73 {
74 //pInterp_.update(dt);
75 //position_ = pInterp_.getState(0.0);
76
77 //calculateSecondary();
78 }
79
80
81 void Camera::lookAt(const Vector3& point)
82 {
83 cml::quaternion_rotation_aim_at(rotation_, position_, point,
84 Vector3(0.0, -1.0, 0.0));
85 calculateSecondary();
86 }
87
88 void Camera::handleEvent(const Event& event)
89 {
90 switch (event.type)
91 {
92 case SDL_KEYDOWN:
93 if (event.key.keysym.sym == SDLK_RIGHT)
94 {
95 Vector3 vec = position_;
96 vec[0] -= 50.0;
97 setPosition(vec);
98 }
99 else if (event.key.keysym.sym == SDLK_LEFT)
100 {
101 Vector3 vec = position_;
102 vec[0] += 50.0;
103 setPosition(vec);
104 }
105 else if (event.key.keysym.sym == SDLK_UP)
106 {
107 Vector3 vec = position_;
108 vec[1] -= 50.0;
109 setPosition(vec);
110 }
111 else if (event.key.keysym.sym == SDLK_DOWN)
112 {
113 Vector3 vec = position_;
114 vec[1] += 50.0;
115 setPosition(vec);
116 }
117 else if (event.key.keysym.sym == SDLK_PAGEUP)
118 {
119 Vector3 vec = position_;
120 vec[2] += 50.0;
121 setPosition(vec);
122 }
123 else if (event.key.keysym.sym == SDLK_PAGEDOWN)
124 {
125 Vector3 vec = position_;
126 vec[2] -= 50.0;
127 setPosition(vec);
128 }
129 break;
130
131 case SDL_MOUSEMOTION:
132 {
133 Scalar xrel = cml::rad(Scalar(event.motion.xrel) / 5.0);
134 Scalar yrel = cml::rad(Scalar(event.motion.yrel) / 5.0);
135
136 Quaternion rotation = rotation_;
137
138 cml::quaternion_rotate_about_world_x(rotation, yrel);
139 //rotation_.normalize();
140 cml::quaternion_rotate_about_world_y(rotation, xrel);
141 rotation.normalize();
142
143 setRotation(rotation);
144 break;
145 }
146
147 case SDL_MOUSEBUTTONDOWN:
148 if (event.button.button == SDL_BUTTON_WHEELUP)
149 {
150 Vector3 vec = position_;
151 vec[2] += 50.0;
152 setPosition(vec);
153 }
154 else if (event.button.button == SDL_BUTTON_WHEELDOWN)
155 {
156 Vector3 vec = position_;
157 vec[2] -= 50.0;
158 setPosition(vec);
159 }
160 break;
161 }
162
163 calculateSecondary();
164 }
165
166 void Camera::calculateSecondary()
167 {
168 cml::matrix_rotation_quaternion(modelview_, rotation_);
169
170 Matrix4 translate;
171 cml::matrix_translation(translate, position_);
172
173 //modelview_.transpose();
174 modelview_ *= translate;
175 //modelview_ = translate * modelview_;
176
177 frustum_.init(modelview_, projection_);
178 }
179
180
181 } // namespace Mf
182
183 /** vim: set ts=4 sw=4 tw=80: *************************************************/
184
This page took 0.041066 seconds and 4 git commands to generate.