]> Dogcows Code - chaz/yoink/blob - src/Moof/Camera.cc
cdebc7a7b735b7e681e3b28f041f576aa9166d7f
[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 <iostream>
30 #include <Moof/Camera.hh>
31 #include <Moof/OpenGL.hh>
32
33
34 namespace Mf {
35
36
37 void Camera::setPosition(const Vector3& point)
38 {
39 position_ = point;
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 near, Scalar far)
55 {
56 cml::matrix_perspective_yfov_RH(projection_, fovy, aspect, near, far,
57 cml::z_clip_neg_one);
58
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 quaternion_rotation_aim_at(rotation_, position_, point);
83 }
84
85 void Camera::adjustFromInput(const Event& event)
86 {
87 switch (event.type)
88 {
89 case SDL_KEYDOWN:
90 if (event.key.keysym.sym == SDLK_RIGHT ||
91 event.key.keysym.sym == SDLK_d)
92 {
93 Vector3 vec = position_;
94 vec[0] -= 50.0;
95 setPosition(vec);
96 }
97 else if (event.key.keysym.sym == SDLK_LEFT ||
98 event.key.keysym.sym == SDLK_a)
99 {
100 Vector3 vec = position_;
101 vec[0] += 50.0;
102 setPosition(vec);
103 }
104 else if (event.key.keysym.sym == SDLK_UP ||
105 event.key.keysym.sym == SDLK_w)
106 {
107 Vector3 vec = position_;
108 vec[1] -= 50.0;
109 setPosition(vec);
110 }
111 else if (event.key.keysym.sym == SDLK_DOWN ||
112 event.key.keysym.sym == SDLK_s)
113 {
114 Vector3 vec = position_;
115 vec[1] += 50.0;
116 setPosition(vec);
117 }
118 else if (event.key.keysym.sym == SDLK_PAGEUP)
119 {
120 Vector3 vec = position_;
121 vec[2] += 50.0;
122 setPosition(vec);
123 }
124 else if (event.key.keysym.sym == SDLK_PAGEDOWN)
125 {
126 //position_[2] -= 50.0;
127 Vector3 vec = position_;
128 vec[2] -= 50.0;
129 setPosition(vec);
130 }
131 break;
132
133 case SDL_MOUSEMOTION:
134 {
135 Scalar xrel = cml::rad(Scalar(event.motion.xrel) / 5.0);
136 Scalar yrel = cml::rad(Scalar(event.motion.yrel) / 5.0);
137
138 Quaternion rotation = rotation_;
139
140 quaternion_rotate_about_world_x(rotation, yrel);
141 //rotation_.normalize();
142 quaternion_rotate_about_world_y(rotation, xrel);
143 rotation.normalize();
144
145 setRotation(rotation);
146 break;
147 }
148
149 case SDL_MOUSEBUTTONDOWN:
150 if (event.button.button == SDL_BUTTON_WHEELUP)
151 {
152 Vector3 vec = position_;
153 vec[2] += 50.0;
154 setPosition(vec);
155 }
156 else if (event.button.button == SDL_BUTTON_WHEELDOWN)
157 {
158 Vector3 vec = position_;
159 vec[2] -= 50.0;
160 setPosition(vec);
161 }
162 break;
163 }
164
165 calculateSecondary();
166 }
167
168 void Camera::calculateSecondary()
169 {
170 matrix_rotation_quaternion(modelview_, rotation_);
171
172 Matrix4 translate;
173 matrix_translation(translate, position_);
174
175 //modelview_ = translate * modelview_;
176 modelview_ *= translate;
177
178 frustum_.init(modelview_, projection_);
179 }
180
181
182 } // namespace Mf
183
184 /** vim: set ts=4 sw=4 tw=80: *************************************************/
185
This page took 0.035541 seconds and 3 git commands to generate.