Mercurial > hg > Game > Cerium
annotate Renderer/Engine/matrix_calc.cc @ 1276:e92f00ed2fc0 draft
apply view matrix to normal vector
author | Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 22 Nov 2011 19:26:18 +0900 |
parents | 636dfdc30176 |
children | 4ba9b622073d |
rev | line source |
---|---|
539 | 1 #include <stdlib.h> |
2 #include <iostream> | |
3 #include <string.h> | |
4 #include <math.h> | |
1050 | 5 #include "matrix_calc.h" |
539 | 6 using namespace std; |
7 | |
8 void noMoreMemory() | |
9 { | |
10 cout << "can't allocate memory\n"; | |
11 exit(1); | |
12 } | |
13 | |
14 void | |
15 transMatrix(float *m0, float *m1, float *v) | |
16 { | |
17 memcpy(m0, m1, sizeof(float)*16); | |
18 | |
19 m0[12] = m1[12] + v[0]; | |
20 m0[13] = m1[13] + v[1]; | |
21 m0[14] = m1[14] + v[2]; | |
22 m0[15] = m1[15] + v[3]; | |
23 } | |
24 | |
25 void | |
26 unitMatrix(float *m) | |
27 { | |
28 bzero(m, sizeof(float)*16); | |
29 | |
30 m[0] = 1.0f; | |
31 m[5] = 1.0f; | |
32 m[10] = 1.0f; | |
33 m[15] = 1.0f; | |
34 } | |
35 | |
36 void | |
1276
e92f00ed2fc0
apply view matrix to normal vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1226
diff
changeset
|
37 inverseMatrix(float *m0, float *m1) |
539 | 38 { |
39 float m[16]; | |
40 | |
41 for (int i = 0; i < 4; i++) { | |
42 for (int j = 0; j < 4; j++) { | |
43 m[i*4+j] = m1[j*4+i]; | |
44 } | |
45 } | |
46 | |
47 m[12] = -(m1[12]*m[0] + m1[13]*m[1] + m1[14]*m[2]); | |
48 m[13] = -(m1[12]*m[4] + m1[13]*m[5] + m1[14]*m[6]); | |
49 m[14] = -(m1[12]*m[8] + m1[13]*m[9] + m1[14]*m[10]); | |
50 m[3] = m[7] = m[11] = 0.0f; | |
51 m[15] = 1.0f; | |
52 | |
53 memcpy(m0, m, sizeof(float)*16); | |
54 } | |
55 | |
56 /** | |
57 * マトリックス m にベクトル v1 を右から乗算して、v0に与える | |
58 * @param[out] v0 output vector (float[4]) | |
59 * @param[in] v1 input vector (float[4]) | |
60 * @param[in] m matrix (float[16]) | |
61 */ | |
62 void | |
63 applyMatrix(float *v0, float *m, float *v1) | |
64 { | |
65 for (int i = 0; i < 4; i++) { | |
66 v0[i] = v1[0]*m[i] + v1[1]*m[i+4] + v1[2]*m[i+8] + v1[3]*m[i+12]; | |
67 } | |
68 } | |
69 | |
70 /** | |
71 * ベクトルの正規化 | |
72 * | |
73 * @param[out] v0 output vector | |
74 * @param[in] v1 input vector | |
75 */ | |
76 void | |
77 normalize(float *v0, float *v1) | |
78 { | |
79 float norm, dnorm; | |
80 | |
81 norm = sqrt(v1[0]*v1[0] + v1[1]*v1[1] + v1[2]*v1[2]); | |
82 if (norm > 0) { | |
83 dnorm = 1.0/norm; | |
84 v0[0] = v1[0]*dnorm; | |
85 v0[1] = v1[1]*dnorm; | |
86 v0[2] = v1[2]*dnorm; | |
87 v0[3] = v1[3]*dnorm; | |
88 } | |
89 } | |
90 | |
91 /** | |
92 * ベクトルの減算 v0 = v1 - v2 | |
93 */ | |
94 void | |
95 subVector(float *v0, float *v1, float *v2) | |
96 { | |
97 v0[0] = v1[0] - v2[0]; | |
98 v0[1] = v1[1] - v2[1]; | |
99 v0[2] = v1[2] - v2[2]; | |
100 v0[3] = v1[3] - v2[3]; | |
101 } | |
102 | |
103 /** | |
104 * ベクトルの外積 v0 = v1 x v2 | |
105 */ | |
106 void | |
107 outerProduct(float *v0, float *v1, float *v2) | |
108 { | |
109 v0[0] = v1[1] * v2[2] - v1[2] * v2[1]; | |
110 v0[1] = v1[2] * v2[0] - v1[0] * v2[2]; | |
111 v0[2] = v1[0] * v2[1] - v1[1] * v2[0]; | |
112 v0[3] = 0; | |
113 } | |
114 | |
115 void | |
116 transposeMatrix(float *m0, float *m1) | |
117 { | |
118 float t[16]; | |
119 | |
120 for (int i = 0; i < 4; i++) { | |
121 for (int j = 0; j < 4; j++) { | |
122 t[i*4+j] = m1[j*4+i]; | |
123 } | |
124 } | |
125 | |
126 memcpy(m0, t, sizeof(float)*16); | |
127 } | |
128 | |
129 /** | |
130 * ベクトルの内積 f = v0 * v1 | |
131 */ | |
132 float | |
133 innerProduct(float *v0, float *v1) | |
134 { | |
135 return (v0[0]*v1[0] + v0[1]*v1[1] + v0[2]*v1[2]); | |
136 } | |
137 | |
1094
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
138 /** |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
139 * xyz = xyz1 * xyz2 |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
140 */ |
539 | 141 void matrix4x4(float *xyz, float *xyz1, float *xyz2) //xyz[16] |
142 { | |
143 for(int t=0; t<16; t+=4) | |
144 { | |
145 for(int i=0; i<4; i++) | |
146 { | |
147 xyz[t+i] = xyz1[t]*xyz2[i] + xyz1[t+1]*xyz2[4+i] + xyz1[t+2]*xyz2[8+i] + xyz1[t+3]*xyz2[12+i]; | |
148 } | |
149 } | |
150 } | |
151 | |
1047
f87218eed9fc
broken texure ( h/w != 2^n ) protection
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
677
diff
changeset
|
152 /** |
1094
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
153 * c_xyz を中心に sacle 倍する |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
154 */ |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
155 void |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
156 scale_matrix(float *xyz, float *scale, float *c_xyz) |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
157 { |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
158 float xyz2[16] = { |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
159 scale[0], 0, 0, scale[0]*(-c_xyz[0]) + c_xyz[0], |
1098 | 160 0, scale[1], 0, scale[1]*(-c_xyz[1]) + c_xyz[1], |
1094
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
161 0, 0, scale[2], scale[2]*(-c_xyz[2]) + c_xyz[2], |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
162 0, 0, 0, 1 |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
163 |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
164 }; |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
165 float xyz1[16] = { |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
166 xyz[0], xyz[1], xyz[2], xyz[3], |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
167 xyz[4], xyz[5], xyz[6], xyz[7], |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
168 xyz[8], xyz[9], xyz[10], xyz[11], |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
169 xyz[12], xyz[13], xyz[14], xyz[15]}; |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
170 |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
171 for(int t=0; t<16; t+=4) |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
172 { |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
173 for(int i=0; i<4; i++) |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
174 { |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
175 xyz[t+i] = xyz1[t]*xyz2[i] + xyz1[t+1]*xyz2[4+i] + xyz1[t+2]*xyz2[8+i] + xyz1[t+3]*xyz2[12+i]; |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
176 } |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
177 } |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
178 } |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
179 |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
180 /** |
1050 | 181 stack 上の変換行列に、相対的に、rxyz の回転、txyz の平行移動、scale の拡大を |
1047
f87218eed9fc
broken texure ( h/w != 2^n ) protection
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
677
diff
changeset
|
182 行ったものを matrix に代入する |
f87218eed9fc
broken texure ( h/w != 2^n ) protection
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
677
diff
changeset
|
183 */ |
f87218eed9fc
broken texure ( h/w != 2^n ) protection
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
677
diff
changeset
|
184 void |
1094
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
185 get_matrix( float *matrix, float *rxyz, float *txyz, float *stack) |
539 | 186 { |
187 float radx,rady,radz; | |
1226
636dfdc30176
new API for SceneGraph
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1165
diff
changeset
|
188 radx = rxyz[0]*3.141562/180; |
636dfdc30176
new API for SceneGraph
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1165
diff
changeset
|
189 rady = rxyz[1]*3.141562/180; |
636dfdc30176
new API for SceneGraph
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1165
diff
changeset
|
190 radz = rxyz[2]*3.141562/180; |
539 | 191 |
192 float sinx = sin(radx); | |
193 float cosx = cos(radx); | |
194 float siny = sin(rady); | |
195 float cosy = cos(rady); | |
196 float sinz = sin(radz); | |
197 float cosz = cos(radz); | |
198 | |
1050 | 199 float m1[16]; |
200 float *m = stack? m1 : matrix; | |
201 | |
539 | 202 /* View Transform */ |
1051 | 203 m[0] = cosz*cosy+sinz*sinx*siny; |
204 m[1] = sinz*cosx; | |
205 m[2] = -cosz*siny+sinz*sinx*cosy; | |
1050 | 206 m[3] = 0; |
1051 | 207 m[4] = -sinz*cosy+cosz*sinx*siny; |
208 m[5] = cosz*cosx; | |
209 m[6] = sinz*siny+cosz*sinx*cosy; | |
1050 | 210 m[7] = 0; |
1051 | 211 m[8] = cosx*siny; |
212 m[9] = -sinx; | |
213 m[10] = cosx*cosy; | |
1050 | 214 m[11] = 0; |
215 m[12] = txyz[0]; | |
216 m[13] = txyz[1]; | |
217 m[14] = txyz[2]; | |
218 m[15] = 1; | |
539 | 219 |
220 if(stack) | |
221 { | |
222 matrix4x4(matrix, m, stack); | |
223 } | |
224 | |
225 } | |
226 | |
1165 | 227 void |
228 get_matrix_scale( float *matrix, float *rxyz, float *txyz, float *scale, float *stack) | |
229 { | |
230 float radx,rady,radz; | |
231 radx = rxyz[0]*3.14/180; | |
232 rady = rxyz[1]*3.14/180; | |
233 radz = rxyz[2]*3.14/180; | |
234 | |
235 float sinx = sin(radx)*scale[0]; | |
236 float cosx = cos(radx)*scale[0]; | |
237 float siny = sin(rady)*scale[1]; | |
238 float cosy = cos(rady)*scale[1]; | |
239 float sinz = sin(radz)*scale[2]; | |
240 float cosz = cos(radz)*scale[2]; | |
241 | |
242 float m1[16]; | |
243 float *m = stack? m1 : matrix; | |
244 | |
245 /* View Transform */ | |
246 m[0] = cosz*cosy+sinz*sinx*siny; | |
247 m[1] = sinz*cosx; | |
248 m[2] = -cosz*siny+sinz*sinx*cosy; | |
249 m[3] = 0; | |
250 m[4] = -sinz*cosy+cosz*sinx*siny; | |
251 m[5] = cosz*cosx; | |
252 m[6] = sinz*siny+cosz*sinx*cosy; | |
253 m[7] = 0; | |
254 m[8] = cosx*siny; | |
255 m[9] = -sinx; | |
256 m[10] = cosx*cosy; | |
257 m[11] = 0; | |
258 m[12] = txyz[0]; | |
259 m[13] = txyz[1]; | |
260 m[14] = txyz[2]; | |
261 m[15] = 1; | |
262 | |
263 if(stack) | |
264 { | |
265 matrix4x4(matrix, m, stack); | |
266 } | |
267 | |
268 } | |
269 | |
539 | 270 void rotate_x(float *xyz, float r) |
271 { | |
272 float rad = r*3.14/180; | |
273 | |
274 xyz[0] = xyz[0]; | |
275 xyz[1] = xyz[1]*cos(rad) - xyz[2]*sin(rad); | |
276 xyz[2] = xyz[1]*sin(rad) + xyz[2]*cos(rad); | |
277 } | |
278 | |
279 void rotate_y(float *xyz, float r) | |
280 { | |
281 float rad = r*3.14/180; | |
282 | |
283 xyz[0] = xyz[0]*cos(rad) + xyz[2]*sin(rad); | |
284 xyz[1] = xyz[1]; | |
285 xyz[2] = -xyz[0]*sin(rad) + xyz[2]*cos(rad); | |
286 } | |
287 | |
288 void rotate_z(float *xyz, float r) | |
289 { | |
290 float rad = r*3.14/180; | |
291 | |
292 xyz[0] = xyz[0]*cos(rad) - xyz[1]*sin(rad); | |
293 xyz[1] = xyz[0]*sin(rad) + xyz[1]*cos(rad); | |
294 xyz[2] = xyz[2]; | |
295 } | |
296 | |
297 void rotate(float *xyz, float *matrix) | |
298 { | |
299 float abc[4]; | |
300 abc[0] = xyz[0]; | |
301 abc[1] = xyz[1]; | |
302 abc[2] = xyz[2]; | |
303 abc[3] = xyz[3]; | |
304 | |
305 for(int i=0; i<4; i++) | |
306 { | |
307 //xyz[i] = abc[0]*rot[i] + abc[1]*rot[i+4] + abc[2]*rot[i+8] + abc[3]*rot[i+12]; | |
308 xyz[i] = abc[0]*matrix[i] + abc[1]*matrix[i+4] + abc[2]*matrix[i+8] + abc[3]*matrix[i+12]; | |
309 } | |
310 } | |
311 | |
312 | |
313 void translate(float *xyz, float x, float y, float z) | |
314 { | |
315 xyz[0] += x; | |
316 xyz[1] += y; | |
317 xyz[2] += z; | |
318 } | |
677
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
319 |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
320 /** |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
321 * ベクトルに行列を乗算する |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
322 * @param[out] v vector (float[4]) |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
323 * @param[in] m matrix (float[16]) |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
324 */ |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
325 void |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
326 ApplyMatrix(float *v, float *m) |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
327 { |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
328 float t[4]; |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
329 |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
330 t[0] = v[0]; |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
331 t[1] = v[1]; |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
332 t[2] = v[2]; |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
333 t[3] = v[3]; |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
334 |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
335 for (int i = 0; i < 4; i++) { |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
336 v[i] = t[0]*m[i] + t[1]*m[i+4] + t[2]*m[i+8] + t[3]*m[i+12]; |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
337 } |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
338 } |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
339 |
1048
5e62924bd7d9
add ScaleXY (not for allExecute...)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1047
diff
changeset
|
340 void |
1050 | 341 ScaleMatrixXYZ(float *m, float x, float y, float z) |
1048
5e62924bd7d9
add ScaleXY (not for allExecute...)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1047
diff
changeset
|
342 { |
5e62924bd7d9
add ScaleXY (not for allExecute...)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1047
diff
changeset
|
343 for(int i=0;i<3;i++) { |
5e62924bd7d9
add ScaleXY (not for allExecute...)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1047
diff
changeset
|
344 m[i] *= x; |
5e62924bd7d9
add ScaleXY (not for allExecute...)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1047
diff
changeset
|
345 m[i+4] *= y; |
1050 | 346 m[i+8] *= z; |
1048
5e62924bd7d9
add ScaleXY (not for allExecute...)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1047
diff
changeset
|
347 } |
5e62924bd7d9
add ScaleXY (not for allExecute...)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1047
diff
changeset
|
348 } |
5e62924bd7d9
add ScaleXY (not for allExecute...)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1047
diff
changeset
|
349 |