Mercurial > hg > Members > kono > Cerium
annotate TaskManager/Test/test_render/Camera.cpp @ 362:a64a6c34868f
vacuum -sg 15
author | e065746@localhost.localdomain |
---|---|
date | Fri, 17 Jul 2009 23:02:07 +0900 |
parents | b89ba1d96fff |
children | a4f690f44842 |
rev | line source |
---|---|
207 | 1 #include <math.h> |
2 #include "SceneGraphRoot.h" | |
3 #include "Camera.h" | |
4 #include "sys.h" | |
316 | 5 #include "Scheduler.h" |
327
7c6d4b101956
add test_render/show_time.{cpp,h} {spe,task}/ShowTime.{cpp,h} but incomplete
aaa
parents:
316
diff
changeset
|
6 #include "show_time.h" |
207 | 7 |
8 static void | |
9 camera_move(SceneGraphPtr _node, int screen_w, int screen_h) | |
10 { | |
11 Pad *pad = sgroot->getController(); | |
12 CameraPtr node = (CameraPtr)_node; | |
13 | |
222 | 14 #if 0 |
221 | 15 if (pad->right.isHold()) { |
16 node->xyz[0] += 10.0f; | |
17 } else if (pad->left.isHold()) { | |
18 node->xyz[0] -= 10.0f; | |
207 | 19 } |
20 | |
21 if (pad->up.isPush() || pad->up.isHold()) { | |
222 | 22 node->xyz[1] -= 2.0f; |
207 | 23 } else if (pad->down.isPush() || pad->down.isHold()) { |
222 | 24 node->xyz[1] += 2.0f; |
207 | 25 } |
222 | 26 #endif |
207 | 27 |
316 | 28 /* |
29 * ここに show_dma_wait 表示をいれようか | |
30 */ | |
31 | |
327
7c6d4b101956
add test_render/show_time.{cpp,h} {spe,task}/ShowTime.{cpp,h} but incomplete
aaa
parents:
316
diff
changeset
|
32 if (pad->r1.isPush()) { |
7c6d4b101956
add test_render/show_time.{cpp,h} {spe,task}/ShowTime.{cpp,h} but incomplete
aaa
parents:
316
diff
changeset
|
33 show_time(); |
7c6d4b101956
add test_render/show_time.{cpp,h} {spe,task}/ShowTime.{cpp,h} but incomplete
aaa
parents:
316
diff
changeset
|
34 } |
316 | 35 |
212 | 36 if (pad->r1.isPush() || pad->r1.isHold()) { |
207 | 37 node->xyz[2] += 10.0f; |
212 | 38 } else if (pad->l1.isPush() || pad->l1.isHold()) { |
207 | 39 node->xyz[2] -= 10.0f; |
40 } | |
221 | 41 |
42 if (pad->r2.isHold()) { | |
43 if (node->zd[0] <= 1.0f) { | |
44 node->zd[0] += 0.02f; | |
45 } | |
46 if (node->zd[2] >= 0.0f) { | |
47 node->zd[2] -= 0.02f; | |
48 } | |
49 } else if (pad->l2.isHold()) { | |
50 if (node->zd[0] > -1.0f) { | |
51 node->zd[0] -= -0.02f; | |
52 } | |
53 if (node->zd[2] >= 0.0f) { | |
54 node->zd[2] -= 0.02f; | |
55 } | |
56 } else { | |
57 node->zd[0] = 0.0f; | |
58 node->zd[2] = 1.0f; | |
59 } | |
60 | |
207 | 61 } |
62 | |
63 static void | |
64 camera_collision(SceneGraphPtr node, int screen_w, int screen_h, | |
65 SceneGraphPtr tree) | |
66 { | |
67 } | |
68 | |
221 | 69 /** |
70 * @param w Width of screen | |
71 * @param h Height of screen | |
72 */ | |
73 Camera::Camera(float w, float h) | |
207 | 74 { |
227 | 75 name = (char*)"Camera"; |
207 | 76 |
226 | 77 |
78 fov = 60.0f; | |
79 near = 0.0f; | |
80 far = 1000.0f; | |
81 | |
219
0f1ff7b06157
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
82 zd[0] = 0.0f; |
0f1ff7b06157
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
83 zd[1] = 0.0f; |
0f1ff7b06157
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
84 zd[2] = 1.0f; |
0f1ff7b06157
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
85 zd[3] = 1.0f; |
212 | 86 |
219
0f1ff7b06157
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
87 yd[0] = 0.0f; |
0f1ff7b06157
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
88 yd[1] = 1.0f; |
0f1ff7b06157
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
89 yd[2] = 0.0f; |
0f1ff7b06157
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
90 yd[3] = 1.0f; |
212 | 91 |
221 | 92 // Screen の真ん中を初期値とする |
93 xyz[0] = w/2.0f; | |
94 xyz[1] = h/2.0f; | |
95 | |
96 // min(w, h) がちょうど一杯見えるような z の位置の計算 | |
97 xyz[2] = -(((xyz[1] < xyz[0]) ? xyz[1] : xyz[0])/tanf((fov/2.0f)*M_PI/180.0f)); | |
222 | 98 //xyz[2] = -200.0f; |
221 | 99 xyz[3] = 1.0f; |
226 | 100 |
101 m_view = new float[16]; | |
102 m_pers = new float[16]; | |
103 m_screen = new float[16]; | |
207 | 104 |
105 this->set_move_collision(camera_move, camera_collision); | |
106 } | |
107 | |
221 | 108 Camera::~Camera(void) |
109 { | |
110 delete [] m_view; | |
111 delete [] m_pers; | |
112 delete [] m_screen; | |
113 } | |
114 | |
212 | 115 void |
221 | 116 Camera::updateView(void) |
212 | 117 { |
219
0f1ff7b06157
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
118 float radx,rady,radz; |
221 | 119 float cx[4], cy[4], cz[4], p[4]; |
120 float tm[16]; | |
219
0f1ff7b06157
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
121 |
0f1ff7b06157
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
122 radx = angle[0]*3.14/180; |
0f1ff7b06157
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
123 rady = angle[1]*3.14/180; |
0f1ff7b06157
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
124 radz = angle[2]*3.14/180; |
0f1ff7b06157
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
125 |
0f1ff7b06157
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
126 float sinx = sin(radx); |
0f1ff7b06157
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
127 float cosx = cos(radx); |
0f1ff7b06157
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
128 float siny = sin(rady); |
0f1ff7b06157
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
129 float cosy = cos(rady); |
0f1ff7b06157
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
130 float sinz = sin(radz); |
0f1ff7b06157
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
131 float cosz = cos(radz); |
212 | 132 |
219
0f1ff7b06157
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
133 /* View Transform */ |
221 | 134 tm[0] = cosz*cosy+sinz*sinx*siny; |
135 tm[1] = sinz*cosx; | |
136 tm[2] = -cosz*siny+sinz*sinx*cosy; | |
137 tm[3] = 0.0f; | |
138 tm[4] = -sinz*cosy+cosz*sinx*siny; | |
139 tm[5] = cosz*cosx; | |
140 tm[6] = sinz*siny+cosz*sinx*cosy; | |
141 tm[7] = 0.0f; | |
142 tm[8] = cosx*siny; | |
143 tm[9] = -sinx; | |
144 tm[10] = cosx*cosy; | |
145 tm[11] = 0.0f; | |
146 tm[12] = 0.0f; | |
147 tm[13] = 0.0f; | |
148 tm[14] = 0.0f; | |
149 tm[15] = 1.0f; | |
219
0f1ff7b06157
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
150 |
221 | 151 applyMatrix(cz, tm, zd); |
152 applyMatrix(cy, tm, yd); | |
153 applyMatrix(p, tm, xyz); | |
219
0f1ff7b06157
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
154 |
0f1ff7b06157
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
155 outerProduct(cx, cy, cz); |
221 | 156 normalize(cx, cx); |
157 normalize(cz, cz); | |
158 outerProduct(cy, cz, cx); | |
159 | |
160 m_view[ 0] = cx[0]; | |
161 m_view[ 1] = cy[0]; | |
162 m_view[ 2] = cz[0]; | |
163 m_view[ 3] = 0.0f; | |
164 | |
165 m_view[ 4] = cx[1]; | |
166 m_view[ 5] = cy[1]; | |
167 m_view[ 6] = cz[1]; | |
168 m_view[ 7] = 0.0f; | |
169 m_view[ 8] = cx[2]; | |
170 m_view[ 9] = cy[2]; | |
171 m_view[10] = cz[2]; | |
172 m_view[11] = 0.0f; | |
173 | |
174 m_view[12] = innerProduct(xyz, cx)*(-1); | |
175 m_view[13] = innerProduct(xyz, cy)*(-1); | |
176 m_view[14] = innerProduct(xyz, cz)*(-1); | |
177 m_view[15] = 1.0f; | |
212 | 178 } |
179 | |
180 void | |
221 | 181 Camera::updatePerspective(float w, float h) |
207 | 182 { |
221 | 183 float sx, sy, sz; |
184 float aspect = w/h; | |
185 | |
186 sy = (1.0f/tanf((fov/2.0f)*M_PI/180.0f)); | |
187 sx = sy/aspect; | |
188 //sz = far/(far+near); | |
189 sz = far/(far-near); | |
190 | |
191 m_pers[ 0] = sx; | |
192 m_pers[ 1] = 0.0f; | |
193 m_pers[ 2] = 0.0f; | |
194 m_pers[ 3] = 0.0f; | |
212 | 195 |
221 | 196 m_pers[ 4] = 0.0f; |
197 m_pers[ 5] = sy; | |
198 m_pers[ 6] = 0.0f; | |
199 m_pers[ 7] = 0.0f; | |
207 | 200 |
221 | 201 m_pers[ 8] = 0.0f; |
202 m_pers[ 9] = 0.0f; | |
203 m_pers[10] = sz; | |
204 m_pers[11] = 1.0f; | |
207 | 205 |
221 | 206 m_pers[12] = 0.0f; |
207 m_pers[13] = 0.0f; | |
208 m_pers[14] = -near*sz; | |
209 m_pers[15] = 0.0f; | |
207 | 210 } |
211 | |
212 void | |
221 | 213 Camera::updateScreen(float _w, float _h) |
212 | 214 { |
215 float w = _w/2.0f; | |
216 float h = _h/2.0f; | |
217 | |
221 | 218 m_screen[ 0] = w; |
219 m_screen[ 1] = 0.0f; | |
220 m_screen[ 2] = 0.0f; | |
221 m_screen[ 3] = 0.0f; | |
212 | 222 |
221 | 223 m_screen[ 4] = 0.0f; |
224 m_screen[ 5] = h; | |
225 m_screen[ 6] = 0.0f; | |
226 m_screen[ 7] = 0.0f; | |
212 | 227 |
221 | 228 m_screen[ 8] = 0.0f; |
229 m_screen[ 9] = 0.0f; | |
230 m_screen[10] = 1.0f; | |
231 m_screen[11] = 0.0f; | |
212 | 232 |
221 | 233 m_screen[12] = w; |
234 m_screen[13] = h; | |
235 m_screen[14] = 0.0f; | |
236 m_screen[15] = 1.0f; | |
207 | 237 } |
238 | |
239 void | |
219
0f1ff7b06157
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
240 Camera::setCamera(float *pose) |
0f1ff7b06157
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
241 { |
221 | 242 //memcpy(xyz, &pose[12], sizeof(float)*4); |
219
0f1ff7b06157
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
243 } |
0f1ff7b06157
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
244 |
0f1ff7b06157
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
245 void |
221 | 246 Camera::update(float w, float h) |
207 | 247 { |
221 | 248 #if 1 |
212 | 249 float tmp[16]; |
207 | 250 |
221 | 251 updateView(); |
252 updatePerspective(w, h); | |
253 updateScreen(w, h); | |
212 | 254 |
221 | 255 matrix4x4(tmp, this->m_pers, this->m_screen); |
256 matrix4x4(this->matrix, this->m_view, tmp); | |
215
7ca6a2ef5be9
fix SceneGraph Constructor, Destructor
gongo@gendarme.local
parents:
212
diff
changeset
|
257 #else |
207 | 258 get_matrix(matrix, angle, xyz, NULL); |
215
7ca6a2ef5be9
fix SceneGraph Constructor, Destructor
gongo@gendarme.local
parents:
212
diff
changeset
|
259 #endif |
207 | 260 } |