]> Dogcows Code - chaz/yoink/blob - src/Moof/Camera.cc
26e24ba358fbfd7494517a66409e75c5150f8b59
[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
32
33 namespace Mf {
34
35
36 void Camera::setPosition(const Vector3& point)
37 {
38 //position_ = point;
39 Vector3 coeff[2] = {position_, point};
40 pInterp_.init(coeff, 0.1);
41 }
42 void Camera::setRotation(const Quaternion& rotation)
43 {
44 rotation_ = rotation;
45 //srcRotation_ = rotation_;
46 //dstRotation_ = rotation;
47 //tInterp_ = 0.0;
48 }
49
50 void Camera::update(Scalar t, Scalar dt)
51 {
52 pInterp_.update(dt);
53 position_ = pInterp_.getState(0.0);
54
55 //tInterp_ += dt * 10.0;
56 //rotation_ = cml::slerp(srcRotation_, dstRotation_, cml::clamp(tInterp_, 0.0, 1.0));
57 //rotation_.normalize();
58
59 calculateSecondary();
60 }
61
62
63 void Camera::lookAt(const Vector3& point)
64 {
65 quaternion_rotation_aim_at(rotation_, position_, point);
66 }
67
68 void Camera::adjustFromInput(const Event& event)
69 {
70 switch (event.type)
71 {
72 case SDL_KEYDOWN:
73 if (event.key.keysym.sym == SDLK_RIGHT ||
74 event.key.keysym.sym == SDLK_d)
75 {
76 Vector3 vec = position_;
77 vec[0] -= 50.0;
78 setPosition(vec);
79 }
80 else if (event.key.keysym.sym == SDLK_LEFT ||
81 event.key.keysym.sym == SDLK_a)
82 {
83 Vector3 vec = position_;
84 vec[0] += 50.0;
85 setPosition(vec);
86 }
87 else if (event.key.keysym.sym == SDLK_UP ||
88 event.key.keysym.sym == SDLK_w)
89 {
90 Vector3 vec = position_;
91 vec[1] -= 50.0;
92 setPosition(vec);
93 }
94 else if (event.key.keysym.sym == SDLK_DOWN ||
95 event.key.keysym.sym == SDLK_s)
96 {
97 Vector3 vec = position_;
98 vec[1] += 50.0;
99 setPosition(vec);
100 }
101 else if (event.key.keysym.sym == SDLK_PAGEUP)
102 {
103 Vector3 vec = position_;
104 vec[2] += 50.0;
105 setPosition(vec);
106 }
107 else if (event.key.keysym.sym == SDLK_PAGEDOWN)
108 {
109 //position_[2] -= 50.0;
110 Vector3 vec = position_;
111 vec[2] -= 50.0;
112 setPosition(vec);
113 }
114 break;
115
116 case SDL_MOUSEMOTION:
117 {
118 Scalar xrel = cml::rad(Scalar(event.motion.xrel) / 5.0);
119 Scalar yrel = cml::rad(Scalar(event.motion.yrel) / 5.0);
120
121 Quaternion rotation = rotation_;
122
123 quaternion_rotate_about_world_x(rotation, yrel);
124 //rotation_.normalize();
125 quaternion_rotate_about_world_y(rotation, xrel);
126 rotation.normalize();
127
128 setRotation(rotation);
129 break;
130 }
131
132 case SDL_MOUSEBUTTONDOWN:
133 if (event.button.button == SDL_BUTTON_WHEELUP)
134 {
135 Vector3 vec = position_;
136 vec[2] += 50.0;
137 setPosition(vec);
138 }
139 else if (event.button.button == SDL_BUTTON_WHEELDOWN)
140 {
141 Vector3 vec = position_;
142 vec[2] -= 50.0;
143 setPosition(vec);
144 }
145 break;
146 }
147
148 calculateSecondary();
149 }
150
151 void Camera::calculateSecondary()
152 {
153 matrix_rotation_quaternion(transformation_, rotation_);
154
155 Matrix4 translate;
156 matrix_translation(translate, position_);
157
158 //transformation_ = translate * transformation_;
159 transformation_ *= translate;
160 }
161
162
163 } // namespace Mf
164
165 /** vim: set ts=4 sw=4 tw=80: *************************************************/
166
This page took 0.041184 seconds and 3 git commands to generate.