Mercurial > hg > Game > Cerium
annotate TaskManager/Test/test_render/Camera.cpp @ 226:fe6a440c5ff3 draft
fix Makefile
author | gongo@gendarme.cr.ie.u-ryukyu.ac.jp |
---|---|
date | Wed, 11 Feb 2009 11:02:45 +0900 |
parents | 0351818cf0fe |
children | e7faaf516be1 |
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 | |
226 | 67 |
68 fov = 60.0f; | |
69 near = 0.0f; | |
70 far = 1000.0f; | |
71 | |
219
335ea3665fcd
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
72 zd[0] = 0.0f; |
335ea3665fcd
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
73 zd[1] = 0.0f; |
335ea3665fcd
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
74 zd[2] = 1.0f; |
335ea3665fcd
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
75 zd[3] = 1.0f; |
212 | 76 |
219
335ea3665fcd
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
77 yd[0] = 0.0f; |
335ea3665fcd
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
78 yd[1] = 1.0f; |
335ea3665fcd
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
79 yd[2] = 0.0f; |
335ea3665fcd
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
80 yd[3] = 1.0f; |
212 | 81 |
221 | 82 // Screen の真ん中を初期値とする |
83 xyz[0] = w/2.0f; | |
84 xyz[1] = h/2.0f; | |
85 | |
86 // min(w, h) がちょうど一杯見えるような z の位置の計算 | |
87 xyz[2] = -(((xyz[1] < xyz[0]) ? xyz[1] : xyz[0])/tanf((fov/2.0f)*M_PI/180.0f)); | |
222 | 88 //xyz[2] = -200.0f; |
221 | 89 xyz[3] = 1.0f; |
226 | 90 |
91 m_view = new float[16]; | |
92 m_pers = new float[16]; | |
93 m_screen = new float[16]; | |
207 | 94 |
95 this->set_move_collision(camera_move, camera_collision); | |
96 } | |
97 | |
221 | 98 Camera::~Camera(void) |
99 { | |
100 delete [] m_view; | |
101 delete [] m_pers; | |
102 delete [] m_screen; | |
103 } | |
104 | |
212 | 105 void |
221 | 106 Camera::updateView(void) |
212 | 107 { |
219
335ea3665fcd
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
108 float radx,rady,radz; |
221 | 109 float cx[4], cy[4], cz[4], p[4]; |
110 float tm[16]; | |
219
335ea3665fcd
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
111 |
335ea3665fcd
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
112 radx = angle[0]*3.14/180; |
335ea3665fcd
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
113 rady = angle[1]*3.14/180; |
335ea3665fcd
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
114 radz = angle[2]*3.14/180; |
335ea3665fcd
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
115 |
335ea3665fcd
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
116 float sinx = sin(radx); |
335ea3665fcd
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
117 float cosx = cos(radx); |
335ea3665fcd
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
118 float siny = sin(rady); |
335ea3665fcd
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
119 float cosy = cos(rady); |
335ea3665fcd
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
120 float sinz = sin(radz); |
335ea3665fcd
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
121 float cosz = cos(radz); |
212 | 122 |
219
335ea3665fcd
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
123 /* View Transform */ |
221 | 124 tm[0] = cosz*cosy+sinz*sinx*siny; |
125 tm[1] = sinz*cosx; | |
126 tm[2] = -cosz*siny+sinz*sinx*cosy; | |
127 tm[3] = 0.0f; | |
128 tm[4] = -sinz*cosy+cosz*sinx*siny; | |
129 tm[5] = cosz*cosx; | |
130 tm[6] = sinz*siny+cosz*sinx*cosy; | |
131 tm[7] = 0.0f; | |
132 tm[8] = cosx*siny; | |
133 tm[9] = -sinx; | |
134 tm[10] = cosx*cosy; | |
135 tm[11] = 0.0f; | |
136 tm[12] = 0.0f; | |
137 tm[13] = 0.0f; | |
138 tm[14] = 0.0f; | |
139 tm[15] = 1.0f; | |
219
335ea3665fcd
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
140 |
221 | 141 applyMatrix(cz, tm, zd); |
142 applyMatrix(cy, tm, yd); | |
143 applyMatrix(p, tm, xyz); | |
219
335ea3665fcd
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
144 |
335ea3665fcd
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
145 outerProduct(cx, cy, cz); |
221 | 146 normalize(cx, cx); |
147 normalize(cz, cz); | |
148 outerProduct(cy, cz, cx); | |
149 | |
150 m_view[ 0] = cx[0]; | |
151 m_view[ 1] = cy[0]; | |
152 m_view[ 2] = cz[0]; | |
153 m_view[ 3] = 0.0f; | |
154 | |
155 m_view[ 4] = cx[1]; | |
156 m_view[ 5] = cy[1]; | |
157 m_view[ 6] = cz[1]; | |
158 m_view[ 7] = 0.0f; | |
159 | |
160 m_view[ 8] = cx[2]; | |
161 m_view[ 9] = cy[2]; | |
162 m_view[10] = cz[2]; | |
163 m_view[11] = 0.0f; | |
164 | |
165 m_view[12] = innerProduct(xyz, cx)*(-1); | |
166 m_view[13] = innerProduct(xyz, cy)*(-1); | |
167 m_view[14] = innerProduct(xyz, cz)*(-1); | |
168 m_view[15] = 1.0f; | |
212 | 169 } |
170 | |
171 void | |
221 | 172 Camera::updatePerspective(float w, float h) |
207 | 173 { |
221 | 174 float sx, sy, sz; |
175 float aspect = w/h; | |
176 | |
177 sy = (1.0f/tanf((fov/2.0f)*M_PI/180.0f)); | |
178 sx = sy/aspect; | |
179 //sz = far/(far+near); | |
180 sz = far/(far-near); | |
181 | |
182 m_pers[ 0] = sx; | |
183 m_pers[ 1] = 0.0f; | |
184 m_pers[ 2] = 0.0f; | |
185 m_pers[ 3] = 0.0f; | |
212 | 186 |
221 | 187 m_pers[ 4] = 0.0f; |
188 m_pers[ 5] = sy; | |
189 m_pers[ 6] = 0.0f; | |
190 m_pers[ 7] = 0.0f; | |
207 | 191 |
221 | 192 m_pers[ 8] = 0.0f; |
193 m_pers[ 9] = 0.0f; | |
194 m_pers[10] = sz; | |
195 m_pers[11] = 1.0f; | |
207 | 196 |
221 | 197 m_pers[12] = 0.0f; |
198 m_pers[13] = 0.0f; | |
199 m_pers[14] = -near*sz; | |
200 m_pers[15] = 0.0f; | |
207 | 201 } |
202 | |
203 void | |
221 | 204 Camera::updateScreen(float _w, float _h) |
212 | 205 { |
206 float w = _w/2.0f; | |
207 float h = _h/2.0f; | |
208 | |
221 | 209 m_screen[ 0] = w; |
210 m_screen[ 1] = 0.0f; | |
211 m_screen[ 2] = 0.0f; | |
212 m_screen[ 3] = 0.0f; | |
212 | 213 |
221 | 214 m_screen[ 4] = 0.0f; |
215 m_screen[ 5] = h; | |
216 m_screen[ 6] = 0.0f; | |
217 m_screen[ 7] = 0.0f; | |
212 | 218 |
221 | 219 m_screen[ 8] = 0.0f; |
220 m_screen[ 9] = 0.0f; | |
221 m_screen[10] = 1.0f; | |
222 m_screen[11] = 0.0f; | |
212 | 223 |
221 | 224 m_screen[12] = w; |
225 m_screen[13] = h; | |
226 m_screen[14] = 0.0f; | |
227 m_screen[15] = 1.0f; | |
207 | 228 } |
229 | |
230 void | |
219
335ea3665fcd
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
231 Camera::setCamera(float *pose) |
335ea3665fcd
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
232 { |
221 | 233 //memcpy(xyz, &pose[12], sizeof(float)*4); |
219
335ea3665fcd
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
234 } |
335ea3665fcd
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
235 |
335ea3665fcd
allExecute する度に SceneGraph をコピーしていく様に変更
gongo@gendarme.local
parents:
215
diff
changeset
|
236 void |
221 | 237 Camera::update(float w, float h) |
207 | 238 { |
221 | 239 #if 1 |
212 | 240 float tmp[16]; |
207 | 241 |
221 | 242 updateView(); |
243 updatePerspective(w, h); | |
244 updateScreen(w, h); | |
212 | 245 |
221 | 246 matrix4x4(tmp, this->m_pers, this->m_screen); |
247 matrix4x4(this->matrix, this->m_view, tmp); | |
215
59f4129a9562
fix SceneGraph Constructor, Destructor
gongo@gendarme.local
parents:
212
diff
changeset
|
248 #else |
207 | 249 get_matrix(matrix, angle, xyz, NULL); |
215
59f4129a9562
fix SceneGraph Constructor, Destructor
gongo@gendarme.local
parents:
212
diff
changeset
|
250 #endif |
207 | 251 } |