Mercurial > hg > Game > Cerium
annotate Renderer/Engine/matrix_calc.cc @ 1094:f10ec9bbd3f6 draft
separate scale matrix
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 25 Dec 2010 18:38:53 +0900 |
parents | 2a291e6ac2fc |
children | 1ef561eb1ef5 |
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 | |
37 inversMatrix(float *m0, float *m1) | |
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], |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
160 0, scale[1], 1, scale[1]*(-c_xyz[1]) + c_xyz[1], |
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; | |
188 radx = rxyz[0]*3.14/180; | |
189 rady = rxyz[1]*3.14/180; | |
190 radz = rxyz[2]*3.14/180; | |
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 | |
227 void rotate_x(float *xyz, float r) | |
228 { | |
229 float rad = r*3.14/180; | |
230 | |
231 xyz[0] = xyz[0]; | |
232 xyz[1] = xyz[1]*cos(rad) - xyz[2]*sin(rad); | |
233 xyz[2] = xyz[1]*sin(rad) + xyz[2]*cos(rad); | |
234 } | |
235 | |
236 void rotate_y(float *xyz, float r) | |
237 { | |
238 float rad = r*3.14/180; | |
239 | |
240 xyz[0] = xyz[0]*cos(rad) + xyz[2]*sin(rad); | |
241 xyz[1] = xyz[1]; | |
242 xyz[2] = -xyz[0]*sin(rad) + xyz[2]*cos(rad); | |
243 } | |
244 | |
245 void rotate_z(float *xyz, float r) | |
246 { | |
247 float rad = r*3.14/180; | |
248 | |
249 xyz[0] = xyz[0]*cos(rad) - xyz[1]*sin(rad); | |
250 xyz[1] = xyz[0]*sin(rad) + xyz[1]*cos(rad); | |
251 xyz[2] = xyz[2]; | |
252 } | |
253 | |
254 void rotate(float *xyz, float *matrix) | |
255 { | |
256 float abc[4]; | |
257 abc[0] = xyz[0]; | |
258 abc[1] = xyz[1]; | |
259 abc[2] = xyz[2]; | |
260 abc[3] = xyz[3]; | |
261 | |
262 for(int i=0; i<4; i++) | |
263 { | |
264 //xyz[i] = abc[0]*rot[i] + abc[1]*rot[i+4] + abc[2]*rot[i+8] + abc[3]*rot[i+12]; | |
265 xyz[i] = abc[0]*matrix[i] + abc[1]*matrix[i+4] + abc[2]*matrix[i+8] + abc[3]*matrix[i+12]; | |
266 } | |
267 } | |
268 | |
269 | |
270 void translate(float *xyz, float x, float y, float z) | |
271 { | |
272 xyz[0] += x; | |
273 xyz[1] += y; | |
274 xyz[2] += z; | |
275 } | |
677
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
276 |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
277 /** |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
278 * ベクトルに行列を乗算する |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
279 * @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
|
280 * @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
|
281 */ |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
282 void |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
283 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
|
284 { |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
285 float t[4]; |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
286 |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
287 t[0] = v[0]; |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
288 t[1] = v[1]; |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
289 t[2] = v[2]; |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
290 t[3] = v[3]; |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
291 |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
292 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
|
293 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
|
294 } |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
295 } |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
296 |
1048
5e62924bd7d9
add ScaleXY (not for allExecute...)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1047
diff
changeset
|
297 void |
1050 | 298 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
|
299 { |
5e62924bd7d9
add ScaleXY (not for allExecute...)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1047
diff
changeset
|
300 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
|
301 m[i] *= x; |
5e62924bd7d9
add ScaleXY (not for allExecute...)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1047
diff
changeset
|
302 m[i+4] *= y; |
1050 | 303 m[i+8] *= z; |
1048
5e62924bd7d9
add ScaleXY (not for allExecute...)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1047
diff
changeset
|
304 } |
5e62924bd7d9
add ScaleXY (not for allExecute...)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1047
diff
changeset
|
305 } |
5e62924bd7d9
add ScaleXY (not for allExecute...)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1047
diff
changeset
|
306 |