]> Dogcows Code - chaz/yoink/blob - src/Moof/Camera.cc
8f313b7aaf7d83b15ee8a97dc8ec12562c8c40e3
[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& position)
37 {
38 mState.position = position;
39 }
40
41 void Camera::setRotation(const Quaternion& rotation)
42 {
43 mState.orientation = rotation;
44 }
45
46 void Camera::lookAt(const Vector3& point)
47 {
48 // FIXME this doesn't work as expected
49 cml::quaternion_rotation_aim_at(mState.orientation, mState.position, point,
50 Vector3(0.0, 1.0, 0.0));
51 }
52
53
54 void Camera::setProjection(const Matrix4& projection)
55 {
56 mProjection = projection;
57 }
58
59 void Camera::setProjection(Scalar fovy, Scalar aspect, Scalar abutting,
60 Scalar distant)
61 {
62 cml::matrix_perspective_yfov_RH(mProjection, fovy, aspect, abutting,
63 distant, cml::z_clip_neg_one);
64 }
65
66
67 void Camera::uploadToGL(Scalar alpha) const
68 {
69 calculate(alpha);
70
71 glMatrixMode(GL_PROJECTION);
72 glMultMatrix(mProjection.data());
73
74 glMatrixMode(GL_MODELVIEW);
75 glMultMatrix(mModelview.data());
76 }
77
78 void Camera::calculate(Scalar alpha) const
79 {
80 State3 state = getState(alpha);
81
82 cml::matrix_rotation_quaternion(mModelview, state.orientation);
83
84 Matrix4 translate;
85 cml::matrix_translation(translate, state.position);
86
87 mModelview *= translate;
88
89 mFrustum.init(mModelview, mProjection);
90 }
91
92
93 void Camera::update(Scalar t, Scalar dt)
94 {
95 RigidBody3::update(t, dt);
96 }
97
98 void Camera::draw(Scalar alpha) const
99 {
100 mSphere.draw(alpha);
101 }
102
103
104 void Camera::handleEvent(const Event& event)
105 {
106 const Scalar ds = 50.0;
107
108 switch (event.type)
109 {
110 case SDL_KEYDOWN:
111 if (event.key.keysym.sym == SDLK_RIGHT)
112 {
113 mState.position[0] -= ds;
114 }
115 else if (event.key.keysym.sym == SDLK_LEFT)
116 {
117 mState.position[0] += ds;
118 }
119 else if (event.key.keysym.sym == SDLK_UP)
120 {
121 mState.position[1] -= ds;
122 }
123 else if (event.key.keysym.sym == SDLK_DOWN)
124 {
125 mState.position[1] += ds;
126 }
127 else if (event.key.keysym.sym == SDLK_PAGEUP)
128 {
129 mState.position[2] += ds;
130 }
131 else if (event.key.keysym.sym == SDLK_PAGEDOWN)
132 {
133 mState.position[2] -= ds;
134 }
135 break;
136
137 case SDL_MOUSEMOTION:
138 {
139 Scalar xrel = cml::rad(Scalar(event.motion.xrel) / 6.0);
140 Scalar yrel = cml::rad(Scalar(event.motion.yrel) / 6.0);
141
142 Quaternion rotation = mState.orientation;
143
144 cml::quaternion_rotate_about_world_x(rotation, yrel);
145 //mRotation.normalize();
146 cml::quaternion_rotate_about_world_y(rotation, xrel);
147
148 rotation.normalize();
149 mState.orientation = rotation;
150 }
151 break;
152
153 case SDL_MOUSEBUTTONDOWN:
154 if (event.button.button == SDL_BUTTON_WHEELUP)
155 {
156 mState.position[2] -= ds;
157 }
158 else if (event.button.button == SDL_BUTTON_WHEELDOWN)
159 {
160 mState.position[2] -= ds;
161 }
162 break;
163 }
164 }
165
166
167 } // namespace Mf
168
169 /** vim: set ts=4 sw=4 tw=80: *************************************************/
170
This page took 0.035813 seconds and 3 git commands to generate.