Mercurial > hg > Members > kono > Cerium
annotate Renderer/Engine/Camera.cc @ 1032:8892d0ea7985
merge
author | Yutaka_Kinjyo |
---|---|
date | Tue, 16 Nov 2010 14:59:28 +0900 |
parents | 2683838396c6 |
children | 6a80ca9a65d9 |
rev | line source |
---|---|
507 | 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 | |
656
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
8 //static SceneGraphRoot *sgroot; |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
9 |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
10 #if 1 |
507 | 11 static void |
656
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
12 camera_move(SceneGraphPtr _node, void *sgroot_, int screen_w, int screen_h) |
507 | 13 { |
656
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
14 SceneGraphRoot *sgroot = (SceneGraphRoot *)sgroot_; |
507 | 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 } | |
656
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
68 #endif |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
69 |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
70 #if 0 |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
71 static void |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
72 Camera::camera_move(SceneGraphPtr _node, int screen_w, int screen_h) |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
73 { |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
74 Pad *pad = sgroot->getController(); |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
75 CameraPtr node = (CameraPtr)_node; |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
76 |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
77 if (pad->r1.isPush() || pad->r1.isHold()) { |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
78 node->xyz[2] += 10.0f; |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
79 } else if (pad->l1.isPush() || pad->l1.isHold()) { |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
80 node->xyz[2] -= 10.0f; |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
81 } |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
82 |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
83 if (pad->r2.isHold()) { |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
84 if (node->zd[0] <= 1.0f) { |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
85 node->zd[0] += 0.02f; |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
86 } |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
87 if (node->zd[2] >= 0.0f) { |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
88 node->zd[2] -= 0.02f; |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
89 } |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
90 } else if (pad->l2.isHold()) { |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
91 if (node->zd[0] > -1.0f) { |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
92 node->zd[0] -= -0.02f; |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
93 } |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
94 if (node->zd[2] >= 0.0f) { |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
95 node->zd[2] -= 0.02f; |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
96 } |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
97 } else { |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
98 node->zd[0] = 0.0f; |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
99 node->zd[2] = 1.0f; |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
100 } |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
101 |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
102 } |
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
103 #endif |
507 | 104 |
105 static void | |
656
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
106 camera_collision(SceneGraphPtr node, void *sgroot_,int screen_w, int screen_h, |
507 | 107 SceneGraphPtr tree) |
108 { | |
109 } | |
110 | |
111 /** | |
112 * @param w Width of screen | |
113 * @param h Height of screen | |
114 */ | |
656
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
115 Camera::Camera(float w, float h, SceneGraphRoot *sgroot_) |
507 | 116 { |
117 name = (char*)"Camera"; | |
656
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
118 sgroot = sgroot_; |
507 | 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 | |
656
d0b8860c17f8
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
147 this->set_move_collision(camera_move, camera_collision, (void *)sgroot); |
748 | 148 |
861 | 149 #if SPE_CREATE_POLYGON |
150 | |
151 #endif | |
152 | |
748 | 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 | |
507 | 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 } |