Mercurial > hg > Members > kono > Cerium
annotate Renderer/Engine/Camera.cc @ 751:8b45afad160f
add task/DataLoad task/DataUpdate
author | hiroki |
---|---|
date | Mon, 25 Jan 2010 19:25:40 +0900 |
parents | b7376415fa5f |
children | 4455e7b0caf3 |
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 |
149 for(int i = 0; i < 16; i++) { | |
150 real_matrix[i] = 0; | |
151 if (i % 5 == 0) { | |
152 real_matrix[i] = 1; | |
153 } | |
154 } | |
155 | |
156 | |
507 | 157 } |
158 | |
159 Camera::~Camera(void) | |
160 { | |
161 delete [] m_view; | |
162 delete [] m_pers; | |
163 delete [] m_screen; | |
164 } | |
165 | |
166 void | |
167 Camera::updateView(void) | |
168 { | |
169 float radx,rady,radz; | |
170 float cx[4], cy[4], cz[4], p[4]; | |
171 float tm[16]; | |
172 | |
173 radx = angle[0]*3.14/180; | |
174 rady = angle[1]*3.14/180; | |
175 radz = angle[2]*3.14/180; | |
176 | |
177 float sinx = sin(radx); | |
178 float cosx = cos(radx); | |
179 float siny = sin(rady); | |
180 float cosy = cos(rady); | |
181 float sinz = sin(radz); | |
182 float cosz = cos(radz); | |
183 | |
184 /* View Transform */ | |
185 tm[0] = cosz*cosy+sinz*sinx*siny; | |
186 tm[1] = sinz*cosx; | |
187 tm[2] = -cosz*siny+sinz*sinx*cosy; | |
188 tm[3] = 0.0f; | |
189 tm[4] = -sinz*cosy+cosz*sinx*siny; | |
190 tm[5] = cosz*cosx; | |
191 tm[6] = sinz*siny+cosz*sinx*cosy; | |
192 tm[7] = 0.0f; | |
193 tm[8] = cosx*siny; | |
194 tm[9] = -sinx; | |
195 tm[10] = cosx*cosy; | |
196 tm[11] = 0.0f; | |
197 tm[12] = 0.0f; | |
198 tm[13] = 0.0f; | |
199 tm[14] = 0.0f; | |
200 tm[15] = 1.0f; | |
201 | |
202 applyMatrix(cz, tm, zd); | |
203 applyMatrix(cy, tm, yd); | |
204 applyMatrix(p, tm, xyz); | |
205 | |
206 outerProduct(cx, cy, cz); | |
207 normalize(cx, cx); | |
208 normalize(cz, cz); | |
209 outerProduct(cy, cz, cx); | |
210 | |
211 m_view[ 0] = cx[0]; | |
212 m_view[ 1] = cy[0]; | |
213 m_view[ 2] = cz[0]; | |
214 m_view[ 3] = 0.0f; | |
215 | |
216 m_view[ 4] = cx[1]; | |
217 m_view[ 5] = cy[1]; | |
218 m_view[ 6] = cz[1]; | |
219 m_view[ 7] = 0.0f; | |
220 m_view[ 8] = cx[2]; | |
221 m_view[ 9] = cy[2]; | |
222 m_view[10] = cz[2]; | |
223 m_view[11] = 0.0f; | |
224 | |
225 m_view[12] = innerProduct(xyz, cx)*(-1); | |
226 m_view[13] = innerProduct(xyz, cy)*(-1); | |
227 m_view[14] = innerProduct(xyz, cz)*(-1); | |
228 m_view[15] = 1.0f; | |
229 } | |
230 | |
231 void | |
232 Camera::updatePerspective(float w, float h) | |
233 { | |
234 float sx, sy, sz; | |
235 float aspect = w/h; | |
236 | |
237 sy = (1.0f/tanf((fov/2.0f)*M_PI/180.0f)); | |
238 sx = sy/aspect; | |
239 //sz = far/(far+near); | |
240 sz = far/(far-near); | |
241 | |
242 m_pers[ 0] = sx; | |
243 m_pers[ 1] = 0.0f; | |
244 m_pers[ 2] = 0.0f; | |
245 m_pers[ 3] = 0.0f; | |
246 | |
247 m_pers[ 4] = 0.0f; | |
248 m_pers[ 5] = sy; | |
249 m_pers[ 6] = 0.0f; | |
250 m_pers[ 7] = 0.0f; | |
251 | |
252 m_pers[ 8] = 0.0f; | |
253 m_pers[ 9] = 0.0f; | |
254 m_pers[10] = sz; | |
255 m_pers[11] = 1.0f; | |
256 | |
257 m_pers[12] = 0.0f; | |
258 m_pers[13] = 0.0f; | |
259 m_pers[14] = -near*sz; | |
260 m_pers[15] = 0.0f; | |
261 } | |
262 | |
263 void | |
264 Camera::updateScreen(float _w, float _h) | |
265 { | |
266 float w = _w/2.0f; | |
267 float h = _h/2.0f; | |
268 | |
269 m_screen[ 0] = w; | |
270 m_screen[ 1] = 0.0f; | |
271 m_screen[ 2] = 0.0f; | |
272 m_screen[ 3] = 0.0f; | |
273 | |
274 m_screen[ 4] = 0.0f; | |
275 m_screen[ 5] = h; | |
276 m_screen[ 6] = 0.0f; | |
277 m_screen[ 7] = 0.0f; | |
278 | |
279 m_screen[ 8] = 0.0f; | |
280 m_screen[ 9] = 0.0f; | |
281 m_screen[10] = 1.0f; | |
282 m_screen[11] = 0.0f; | |
283 | |
284 m_screen[12] = w; | |
285 m_screen[13] = h; | |
286 m_screen[14] = 0.0f; | |
287 m_screen[15] = 1.0f; | |
288 } | |
289 | |
290 void | |
291 Camera::setCamera(float *pose) | |
292 { | |
293 //memcpy(xyz, &pose[12], sizeof(float)*4); | |
294 } | |
295 | |
296 void | |
297 Camera::update(float w, float h) | |
298 { | |
299 #if 1 | |
300 float tmp[16]; | |
301 | |
302 updateView(); | |
303 updatePerspective(w, h); | |
304 updateScreen(w, h); | |
305 | |
306 matrix4x4(tmp, this->m_pers, this->m_screen); | |
307 matrix4x4(this->matrix, this->m_view, tmp); | |
308 #else | |
309 get_matrix(matrix, angle, xyz, NULL); | |
310 #endif | |
311 } |