comparison Renderer/Engine/Camera.cc @ 0:04e28d8d3c6f

first commit
author Daiki KINJYO <e085722@ie.u-ryukyu.ac.jp>
date Mon, 08 Nov 2010 01:23:25 +0900
parents
children b5b462ac9b3b
comparison
equal deleted inserted replaced
-1:000000000000 0:04e28d8d3c6f
1 #include <math.h>
2 #include "SceneGraphRoot.h"
3 #include "Camera.h"
4 #include "sys.h"
5 #include "Scheduler.h"
6 #include "TaskManager.h"
7
8 //static SceneGraphRoot *sgroot;
9
10 #if 1
11 static void
12 camera_move(SceneGraphPtr _node, void *sgroot_, int screen_w, int screen_h)
13 {
14 SceneGraphRoot *sgroot = (SceneGraphRoot *)sgroot_;
15 Pad *pad = sgroot->getController();
16 CameraPtr node = (CameraPtr)_node;
17
18 #if 0
19 if (pad->right.isHold()) {
20 node->xyz[0] += 10.0f;
21 } else if (pad->left.isHold()) {
22 node->xyz[0] -= 10.0f;
23 }
24
25 if (pad->up.isPush() || pad->up.isHold()) {
26 node->xyz[1] -= 2.0f;
27 } else if (pad->down.isPush() || pad->down.isHold()) {
28 node->xyz[1] += 2.0f;
29 }
30 #endif
31
32 /*
33 * ここに show_dma_wait 表示をいれようか
34 */
35
36 /*
37 if (pad->r1.isPush()) {
38 show_time(manager);
39 }
40 */
41
42 if (pad->r1.isPush() || pad->r1.isHold()) {
43 node->xyz[2] += 10.0f;
44 } else if (pad->l1.isPush() || pad->l1.isHold()) {
45 node->xyz[2] -= 10.0f;
46 }
47
48 if (pad->r2.isHold()) {
49 if (node->zd[0] <= 1.0f) {
50 node->zd[0] += 0.02f;
51 }
52 if (node->zd[2] >= 0.0f) {
53 node->zd[2] -= 0.02f;
54 }
55 } else if (pad->l2.isHold()) {
56 if (node->zd[0] > -1.0f) {
57 node->zd[0] -= -0.02f;
58 }
59 if (node->zd[2] >= 0.0f) {
60 node->zd[2] -= 0.02f;
61 }
62 } else {
63 node->zd[0] = 0.0f;
64 node->zd[2] = 1.0f;
65 }
66
67 }
68 #endif
69
70 #if 0
71 static void
72 Camera::camera_move(SceneGraphPtr _node, int screen_w, int screen_h)
73 {
74 Pad *pad = sgroot->getController();
75 CameraPtr node = (CameraPtr)_node;
76
77 if (pad->r1.isPush() || pad->r1.isHold()) {
78 node->xyz[2] += 10.0f;
79 } else if (pad->l1.isPush() || pad->l1.isHold()) {
80 node->xyz[2] -= 10.0f;
81 }
82
83 if (pad->r2.isHold()) {
84 if (node->zd[0] <= 1.0f) {
85 node->zd[0] += 0.02f;
86 }
87 if (node->zd[2] >= 0.0f) {
88 node->zd[2] -= 0.02f;
89 }
90 } else if (pad->l2.isHold()) {
91 if (node->zd[0] > -1.0f) {
92 node->zd[0] -= -0.02f;
93 }
94 if (node->zd[2] >= 0.0f) {
95 node->zd[2] -= 0.02f;
96 }
97 } else {
98 node->zd[0] = 0.0f;
99 node->zd[2] = 1.0f;
100 }
101
102 }
103 #endif
104
105 static void
106 camera_collision(SceneGraphPtr node, void *sgroot_,int screen_w, int screen_h,
107 SceneGraphPtr tree)
108 {
109 }
110
111 /**
112 * @param w Width of screen
113 * @param h Height of screen
114 */
115 Camera::Camera(float w, float h, SceneGraphRoot *sgroot_)
116 {
117 name = (char*)"Camera";
118 sgroot = sgroot_;
119
120 fov = 60.0f;
121 near = 0.0f;
122 far = 1000.0f;
123
124 zd[0] = 0.0f;
125 zd[1] = 0.0f;
126 zd[2] = 1.0f;
127 zd[3] = 1.0f;
128
129 yd[0] = 0.0f;
130 yd[1] = 1.0f;
131 yd[2] = 0.0f;
132 yd[3] = 1.0f;
133
134 // Screen の真ん中を初期値とする
135 xyz[0] = w/2.0f;
136 xyz[1] = h/2.0f;
137
138 // min(w, h) がちょうど一杯見えるような z の位置の計算
139 xyz[2] = -(((xyz[1] < xyz[0]) ? xyz[1] : xyz[0])/tanf((fov/2.0f)*M_PI/180.0f));
140 //xyz[2] = -200.0f;
141 xyz[3] = 1.0f;
142
143 m_view = new float[16];
144 m_pers = new float[16];
145 m_screen = new float[16];
146
147 this->set_move_collision(camera_move, camera_collision, (void *)sgroot);
148
149 #if SPE_CREATE_POLYGON
150
151 #endif
152
153 for(int i = 0; i < 16; i++) {
154 real_matrix[i] = 0;
155 if (i % 5 == 0) {
156 real_matrix[i] = 1;
157 }
158 }
159
160 }
161
162 Camera::~Camera(void)
163 {
164 delete [] m_view;
165 delete [] m_pers;
166 delete [] m_screen;
167 }
168
169 void
170 Camera::updateView(void)
171 {
172 float radx,rady,radz;
173 float cx[4], cy[4], cz[4], p[4];
174 float tm[16];
175
176 radx = angle[0]*3.14/180;
177 rady = angle[1]*3.14/180;
178 radz = angle[2]*3.14/180;
179
180 float sinx = sin(radx);
181 float cosx = cos(radx);
182 float siny = sin(rady);
183 float cosy = cos(rady);
184 float sinz = sin(radz);
185 float cosz = cos(radz);
186
187 /* View Transform */
188 tm[0] = cosz*cosy+sinz*sinx*siny;
189 tm[1] = sinz*cosx;
190 tm[2] = -cosz*siny+sinz*sinx*cosy;
191 tm[3] = 0.0f;
192 tm[4] = -sinz*cosy+cosz*sinx*siny;
193 tm[5] = cosz*cosx;
194 tm[6] = sinz*siny+cosz*sinx*cosy;
195 tm[7] = 0.0f;
196 tm[8] = cosx*siny;
197 tm[9] = -sinx;
198 tm[10] = cosx*cosy;
199 tm[11] = 0.0f;
200 tm[12] = 0.0f;
201 tm[13] = 0.0f;
202 tm[14] = 0.0f;
203 tm[15] = 1.0f;
204
205 applyMatrix(cz, tm, zd);
206 applyMatrix(cy, tm, yd);
207 applyMatrix(p, tm, xyz);
208
209 outerProduct(cx, cy, cz);
210 normalize(cx, cx);
211 normalize(cz, cz);
212 outerProduct(cy, cz, cx);
213
214 m_view[ 0] = cx[0];
215 m_view[ 1] = cy[0];
216 m_view[ 2] = cz[0];
217 m_view[ 3] = 0.0f;
218
219 m_view[ 4] = cx[1];
220 m_view[ 5] = cy[1];
221 m_view[ 6] = cz[1];
222 m_view[ 7] = 0.0f;
223 m_view[ 8] = cx[2];
224 m_view[ 9] = cy[2];
225 m_view[10] = cz[2];
226 m_view[11] = 0.0f;
227
228 m_view[12] = innerProduct(xyz, cx)*(-1);
229 m_view[13] = innerProduct(xyz, cy)*(-1);
230 m_view[14] = innerProduct(xyz, cz)*(-1);
231 m_view[15] = 1.0f;
232 }
233
234 void
235 Camera::updatePerspective(float w, float h)
236 {
237 float sx, sy, sz;
238 float aspect = w/h;
239
240 sy = (1.0f/tanf((fov/2.0f)*M_PI/180.0f));
241 sx = sy/aspect;
242 //sz = far/(far+near);
243 sz = far/(far-near);
244
245 m_pers[ 0] = sx;
246 m_pers[ 1] = 0.0f;
247 m_pers[ 2] = 0.0f;
248 m_pers[ 3] = 0.0f;
249
250 m_pers[ 4] = 0.0f;
251 m_pers[ 5] = sy;
252 m_pers[ 6] = 0.0f;
253 m_pers[ 7] = 0.0f;
254
255 m_pers[ 8] = 0.0f;
256 m_pers[ 9] = 0.0f;
257 m_pers[10] = sz;
258 m_pers[11] = 1.0f;
259
260 m_pers[12] = 0.0f;
261 m_pers[13] = 0.0f;
262 m_pers[14] = -near*sz;
263 m_pers[15] = 0.0f;
264 }
265
266 void
267 Camera::updateScreen(float _w, float _h)
268 {
269 float w = _w/2.0f;
270 float h = _h/2.0f;
271
272 m_screen[ 0] = w;
273 m_screen[ 1] = 0.0f;
274 m_screen[ 2] = 0.0f;
275 m_screen[ 3] = 0.0f;
276
277 m_screen[ 4] = 0.0f;
278 m_screen[ 5] = h;
279 m_screen[ 6] = 0.0f;
280 m_screen[ 7] = 0.0f;
281
282 m_screen[ 8] = 0.0f;
283 m_screen[ 9] = 0.0f;
284 m_screen[10] = 1.0f;
285 m_screen[11] = 0.0f;
286
287 m_screen[12] = w;
288 m_screen[13] = h;
289 m_screen[14] = 0.0f;
290 m_screen[15] = 1.0f;
291 }
292
293 void
294 Camera::setCamera(float *pose)
295 {
296 //memcpy(xyz, &pose[12], sizeof(float)*4);
297 }
298
299 void
300 Camera::update(float w, float h)
301 {
302 #if 1
303 float tmp[16];
304
305 updateView();
306 updatePerspective(w, h);
307 updateScreen(w, h);
308
309 matrix4x4(tmp, this->m_pers, this->m_screen);
310 matrix4x4(this->matrix, this->m_view, tmp);
311 #else
312 get_matrix(matrix, angle, xyz, NULL);
313 #endif
314 }