Mercurial > hg > Game > Cerium
annotate Renderer/Engine/Camera.cc @ 898:302ebfc75a79 draft
merge
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 16 Jul 2010 19:00:49 +0900 |
parents | fda7b71c3cc6 |
children | e9a64c42e5d0 |
rev | line source |
---|---|
539 | 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 | |
653
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
8 //static SceneGraphRoot *sgroot; |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
9 |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
10 #if 1 |
539 | 11 static void |
653
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
12 camera_move(SceneGraphPtr _node, void *sgroot_, int screen_w, int screen_h) |
539 | 13 { |
653
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
14 SceneGraphRoot *sgroot = (SceneGraphRoot *)sgroot_; |
539 | 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 } | |
653
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
68 #endif |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
69 |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
70 #if 0 |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
71 static void |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
72 Camera::camera_move(SceneGraphPtr _node, int screen_w, int screen_h) |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
73 { |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
74 Pad *pad = sgroot->getController(); |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
75 CameraPtr node = (CameraPtr)_node; |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
76 |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
77 if (pad->r1.isPush() || pad->r1.isHold()) { |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
78 node->xyz[2] += 10.0f; |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
79 } else if (pad->l1.isPush() || pad->l1.isHold()) { |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
80 node->xyz[2] -= 10.0f; |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
81 } |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
82 |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
83 if (pad->r2.isHold()) { |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
84 if (node->zd[0] <= 1.0f) { |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
85 node->zd[0] += 0.02f; |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
86 } |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
87 if (node->zd[2] >= 0.0f) { |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
88 node->zd[2] -= 0.02f; |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
89 } |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
90 } else if (pad->l2.isHold()) { |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
91 if (node->zd[0] > -1.0f) { |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
92 node->zd[0] -= -0.02f; |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
93 } |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
94 if (node->zd[2] >= 0.0f) { |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
95 node->zd[2] -= 0.02f; |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
96 } |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
97 } else { |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
98 node->zd[0] = 0.0f; |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
99 node->zd[2] = 1.0f; |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
100 } |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
101 |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
102 } |
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
103 #endif |
539 | 104 |
105 static void | |
653
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
106 camera_collision(SceneGraphPtr node, void *sgroot_,int screen_w, int screen_h, |
539 | 107 SceneGraphPtr tree) |
108 { | |
109 } | |
110 | |
111 /** | |
112 * @param w Width of screen | |
113 * @param h Height of screen | |
114 */ | |
653
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
115 Camera::Camera(float w, float h, SceneGraphRoot *sgroot_) |
539 | 116 { |
117 name = (char*)"Camera"; | |
653
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
118 sgroot = sgroot_; |
539 | 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 | |
653
7a311860a76e
remove global variable "sgroot" , add SgChange.{cc, h} SgMain.cc SgRootChange.{cc, h}
hiroki@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
147 this->set_move_collision(camera_move, camera_collision, (void *)sgroot); |
747 | 148 |
861 | 149 #if SPE_CREATE_POLYGON |
150 | |
151 sg_matrix = (float*)malloc(sizeof(float)*32); | |
152 matrix = sg_matrix; | |
153 real_matrix = sg_matrix + 16; | |
154 | |
155 #endif | |
156 | |
747 | 157 for(int i = 0; i < 16; i++) { |
158 real_matrix[i] = 0; | |
159 if (i % 5 == 0) { | |
160 real_matrix[i] = 1; | |
161 } | |
162 } | |
163 | |
539 | 164 } |
165 | |
166 Camera::~Camera(void) | |
167 { | |
168 delete [] m_view; | |
169 delete [] m_pers; | |
170 delete [] m_screen; | |
171 } | |
172 | |
173 void | |
174 Camera::updateView(void) | |
175 { | |
176 float radx,rady,radz; | |
177 float cx[4], cy[4], cz[4], p[4]; | |
178 float tm[16]; | |
179 | |
180 radx = angle[0]*3.14/180; | |
181 rady = angle[1]*3.14/180; | |
182 radz = angle[2]*3.14/180; | |
183 | |
184 float sinx = sin(radx); | |
185 float cosx = cos(radx); | |
186 float siny = sin(rady); | |
187 float cosy = cos(rady); | |
188 float sinz = sin(radz); | |
189 float cosz = cos(radz); | |
190 | |
191 /* View Transform */ | |
192 tm[0] = cosz*cosy+sinz*sinx*siny; | |
193 tm[1] = sinz*cosx; | |
194 tm[2] = -cosz*siny+sinz*sinx*cosy; | |
195 tm[3] = 0.0f; | |
196 tm[4] = -sinz*cosy+cosz*sinx*siny; | |
197 tm[5] = cosz*cosx; | |
198 tm[6] = sinz*siny+cosz*sinx*cosy; | |
199 tm[7] = 0.0f; | |
200 tm[8] = cosx*siny; | |
201 tm[9] = -sinx; | |
202 tm[10] = cosx*cosy; | |
203 tm[11] = 0.0f; | |
204 tm[12] = 0.0f; | |
205 tm[13] = 0.0f; | |
206 tm[14] = 0.0f; | |
207 tm[15] = 1.0f; | |
208 | |
209 applyMatrix(cz, tm, zd); | |
210 applyMatrix(cy, tm, yd); | |
211 applyMatrix(p, tm, xyz); | |
212 | |
213 outerProduct(cx, cy, cz); | |
214 normalize(cx, cx); | |
215 normalize(cz, cz); | |
216 outerProduct(cy, cz, cx); | |
217 | |
218 m_view[ 0] = cx[0]; | |
219 m_view[ 1] = cy[0]; | |
220 m_view[ 2] = cz[0]; | |
221 m_view[ 3] = 0.0f; | |
222 | |
223 m_view[ 4] = cx[1]; | |
224 m_view[ 5] = cy[1]; | |
225 m_view[ 6] = cz[1]; | |
226 m_view[ 7] = 0.0f; | |
227 m_view[ 8] = cx[2]; | |
228 m_view[ 9] = cy[2]; | |
229 m_view[10] = cz[2]; | |
230 m_view[11] = 0.0f; | |
231 | |
232 m_view[12] = innerProduct(xyz, cx)*(-1); | |
233 m_view[13] = innerProduct(xyz, cy)*(-1); | |
234 m_view[14] = innerProduct(xyz, cz)*(-1); | |
235 m_view[15] = 1.0f; | |
236 } | |
237 | |
238 void | |
239 Camera::updatePerspective(float w, float h) | |
240 { | |
241 float sx, sy, sz; | |
242 float aspect = w/h; | |
243 | |
244 sy = (1.0f/tanf((fov/2.0f)*M_PI/180.0f)); | |
245 sx = sy/aspect; | |
246 //sz = far/(far+near); | |
247 sz = far/(far-near); | |
248 | |
249 m_pers[ 0] = sx; | |
250 m_pers[ 1] = 0.0f; | |
251 m_pers[ 2] = 0.0f; | |
252 m_pers[ 3] = 0.0f; | |
253 | |
254 m_pers[ 4] = 0.0f; | |
255 m_pers[ 5] = sy; | |
256 m_pers[ 6] = 0.0f; | |
257 m_pers[ 7] = 0.0f; | |
258 | |
259 m_pers[ 8] = 0.0f; | |
260 m_pers[ 9] = 0.0f; | |
261 m_pers[10] = sz; | |
262 m_pers[11] = 1.0f; | |
263 | |
264 m_pers[12] = 0.0f; | |
265 m_pers[13] = 0.0f; | |
266 m_pers[14] = -near*sz; | |
267 m_pers[15] = 0.0f; | |
268 } | |
269 | |
270 void | |
271 Camera::updateScreen(float _w, float _h) | |
272 { | |
273 float w = _w/2.0f; | |
274 float h = _h/2.0f; | |
275 | |
276 m_screen[ 0] = w; | |
277 m_screen[ 1] = 0.0f; | |
278 m_screen[ 2] = 0.0f; | |
279 m_screen[ 3] = 0.0f; | |
280 | |
281 m_screen[ 4] = 0.0f; | |
282 m_screen[ 5] = h; | |
283 m_screen[ 6] = 0.0f; | |
284 m_screen[ 7] = 0.0f; | |
285 | |
286 m_screen[ 8] = 0.0f; | |
287 m_screen[ 9] = 0.0f; | |
288 m_screen[10] = 1.0f; | |
289 m_screen[11] = 0.0f; | |
290 | |
291 m_screen[12] = w; | |
292 m_screen[13] = h; | |
293 m_screen[14] = 0.0f; | |
294 m_screen[15] = 1.0f; | |
295 } | |
296 | |
297 void | |
298 Camera::setCamera(float *pose) | |
299 { | |
300 //memcpy(xyz, &pose[12], sizeof(float)*4); | |
301 } | |
302 | |
303 void | |
304 Camera::update(float w, float h) | |
305 { | |
306 #if 1 | |
307 float tmp[16]; | |
308 | |
309 updateView(); | |
310 updatePerspective(w, h); | |
311 updateScreen(w, h); | |
312 | |
313 matrix4x4(tmp, this->m_pers, this->m_screen); | |
314 matrix4x4(this->matrix, this->m_view, tmp); | |
315 #else | |
316 get_matrix(matrix, angle, xyz, NULL); | |
317 #endif | |
318 } |