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