]> Dogcows Code - chaz/yoink/blob - src/Moof/Camera.cc
initial port to win32
[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::uploadProjectionToGL() const
64 {
65 glMatrixMode(GL_PROJECTION);
66 glLoadMatrix(projection_.data());
67
68 glMatrixMode(GL_MODELVIEW);
69 }
70
71 void Camera::update(Scalar t, Scalar dt)
72 {
73 //pInterp_.update(dt);
74 //position_ = pInterp_.getState(0.0);
75
76 //calculateSecondary();
77 }
78
79
80 void Camera::lookAt(const Vector3& point)
81 {
82 cml::quaternion_rotation_aim_at(rotation_, position_, point,
83 Vector3(0.0, -1.0, 0.0));
84 calculateSecondary();
85 }
86
87 void Camera::handleEvent(const Event& event)
88 {
89 switch (event.type)
90 {
91 case SDL_KEYDOWN:
92 if (event.key.keysym.sym == SDLK_RIGHT)
93 {
94 Vector3 vec = position_;
95 vec[0] -= 50.0;
96 setPosition(vec);
97 }
98 else if (event.key.keysym.sym == SDLK_LEFT)
99 {
100 Vector3 vec = position_;
101 vec[0] += 50.0;
102 setPosition(vec);
103 }
104 else if (event.key.keysym.sym == SDLK_UP)
105 {
106 Vector3 vec = position_;
107 vec[1] -= 50.0;
108 setPosition(vec);
109 }
110 else if (event.key.keysym.sym == SDLK_DOWN)
111 {
112 Vector3 vec = position_;
113 vec[1] += 50.0;
114 setPosition(vec);
115 }
116 else if (event.key.keysym.sym == SDLK_PAGEUP)
117 {
118 Vector3 vec = position_;
119 vec[2] += 50.0;
120 setPosition(vec);
121 }
122 else if (event.key.keysym.sym == SDLK_PAGEDOWN)
123 {
124 Vector3 vec = position_;
125 vec[2] -= 50.0;
126 setPosition(vec);
127 }
128 break;
129
130 case SDL_MOUSEMOTION:
131 {
132 Scalar xrel = cml::rad(Scalar(event.motion.xrel) / 5.0);
133 Scalar yrel = cml::rad(Scalar(event.motion.yrel) / 5.0);
134
135 Quaternion rotation = rotation_;
136
137 cml::quaternion_rotate_about_world_x(rotation, yrel);
138 //rotation_.normalize();
139 cml::quaternion_rotate_about_world_y(rotation, xrel);
140 rotation.normalize();
141
142 setRotation(rotation);
143 break;
144 }
145
146 case SDL_MOUSEBUTTONDOWN:
147 if (event.button.button == SDL_BUTTON_WHEELUP)
148 {
149 Vector3 vec = position_;
150 vec[2] += 50.0;
151 setPosition(vec);
152 }
153 else if (event.button.button == SDL_BUTTON_WHEELDOWN)
154 {
155 Vector3 vec = position_;
156 vec[2] -= 50.0;
157 setPosition(vec);
158 }
159 break;
160 }
161
162 calculateSecondary();
163 }
164
165 void Camera::calculateSecondary()
166 {
167 cml::matrix_rotation_quaternion(modelview_, rotation_);
168
169 Matrix4 translate;
170 cml::matrix_translation(translate, position_);
171
172 //modelview_.transpose();
173 modelview_ *= translate;
174 //modelview_ = translate * modelview_;
175
176 frustum_.init(modelview_, projection_);
177 }
178
179
180 } // namespace Mf
181
182 /** vim: set ts=4 sw=4 tw=80: *************************************************/
183
This page took 0.03865 seconds and 4 git commands to generate.