Mercurial > hg > Members > kono > Cerium
annotate Renderer/Engine/sys.cc @ 1048:40cde8c1a6cd default tip
add ScaleXY (not for allExecute...)
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 08 Dec 2010 06:22:15 +0900 |
parents | 6a80ca9a65d9 |
children |
rev | line source |
---|---|
507 | 1 #include <stdlib.h> |
2 #include <iostream> | |
3 #include <string.h> | |
4 #include <math.h> | |
5 #include "sys.h" | |
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 | |
138 void matrix4x4(float *xyz, float *xyz1, float *xyz2) //xyz[16] | |
139 { | |
140 for(int t=0; t<16; t+=4) | |
141 { | |
142 for(int i=0; i<4; i++) | |
143 { | |
144 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]; | |
145 } | |
146 } | |
147 } | |
148 | |
1047
6a80ca9a65d9
broken texure ( h/w != 2^n ) protection
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
678
diff
changeset
|
149 /** |
6a80ca9a65d9
broken texure ( h/w != 2^n ) protection
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
678
diff
changeset
|
150 stack 上の変換行列に、相対的に、rxyz の回転、txyz の平行移動を |
6a80ca9a65d9
broken texure ( h/w != 2^n ) protection
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
678
diff
changeset
|
151 行ったものを matrix に代入する |
6a80ca9a65d9
broken texure ( h/w != 2^n ) protection
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
678
diff
changeset
|
152 */ |
6a80ca9a65d9
broken texure ( h/w != 2^n ) protection
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
678
diff
changeset
|
153 void |
6a80ca9a65d9
broken texure ( h/w != 2^n ) protection
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
678
diff
changeset
|
154 get_matrix( float *matrix, float *rxyz, float *txyz, float *stack) |
507 | 155 { |
156 float radx,rady,radz; | |
157 radx = rxyz[0]*3.14/180; | |
158 rady = rxyz[1]*3.14/180; | |
159 radz = rxyz[2]*3.14/180; | |
160 | |
161 float sinx = sin(radx); | |
162 float cosx = cos(radx); | |
163 float siny = sin(rady); | |
164 float cosy = cos(rady); | |
165 float sinz = sin(radz); | |
166 float cosz = cos(radz); | |
167 | |
168 /* View Transform */ | |
169 matrix[0] = cosz*cosy+sinz*sinx*siny; | |
170 matrix[1] = sinz*cosx; | |
171 matrix[2] = -cosz*siny+sinz*sinx*cosy; | |
172 matrix[3] = 0; | |
173 matrix[4] = -sinz*cosy+cosz*sinx*siny; | |
174 matrix[5] = cosz*cosx; | |
175 matrix[6] = sinz*siny+cosz*sinx*cosy; | |
176 matrix[7] = 0; | |
177 matrix[8] = cosx*siny; | |
178 matrix[9] = -sinx; | |
179 matrix[10] = cosx*cosy; | |
180 matrix[11] = 0; | |
181 matrix[12] = txyz[0]; | |
182 matrix[13] = txyz[1]; | |
183 matrix[14] = txyz[2]; | |
184 matrix[15] = 1; | |
185 | |
186 float m[16]; | |
187 | |
188 for(int i=0; i<16; i++) | |
189 { | |
190 m[i] = matrix[i]; | |
191 } | |
192 | |
193 if(stack) | |
194 { | |
195 matrix4x4(matrix, m, stack); | |
196 } | |
197 | |
198 } | |
199 | |
200 void rotate_x(float *xyz, float r) | |
201 { | |
202 float rad = r*3.14/180; | |
203 | |
204 xyz[0] = xyz[0]; | |
205 xyz[1] = xyz[1]*cos(rad) - xyz[2]*sin(rad); | |
206 xyz[2] = xyz[1]*sin(rad) + xyz[2]*cos(rad); | |
207 } | |
208 | |
209 void rotate_y(float *xyz, float r) | |
210 { | |
211 float rad = r*3.14/180; | |
212 | |
213 xyz[0] = xyz[0]*cos(rad) + xyz[2]*sin(rad); | |
214 xyz[1] = xyz[1]; | |
215 xyz[2] = -xyz[0]*sin(rad) + xyz[2]*cos(rad); | |
216 } | |
217 | |
218 void rotate_z(float *xyz, float r) | |
219 { | |
220 float rad = r*3.14/180; | |
221 | |
222 xyz[0] = xyz[0]*cos(rad) - xyz[1]*sin(rad); | |
223 xyz[1] = xyz[0]*sin(rad) + xyz[1]*cos(rad); | |
224 xyz[2] = xyz[2]; | |
225 } | |
226 | |
227 void rotate(float *xyz, float *matrix) | |
228 { | |
229 float abc[4]; | |
230 abc[0] = xyz[0]; | |
231 abc[1] = xyz[1]; | |
232 abc[2] = xyz[2]; | |
233 abc[3] = xyz[3]; | |
234 | |
235 for(int i=0; i<4; i++) | |
236 { | |
237 //xyz[i] = abc[0]*rot[i] + abc[1]*rot[i+4] + abc[2]*rot[i+8] + abc[3]*rot[i+12]; | |
238 xyz[i] = abc[0]*matrix[i] + abc[1]*matrix[i+4] + abc[2]*matrix[i+8] + abc[3]*matrix[i+12]; | |
239 } | |
240 } | |
241 | |
242 | |
243 void translate(float *xyz, float x, float y, float z) | |
244 { | |
245 xyz[0] += x; | |
246 xyz[1] += y; | |
247 xyz[2] += z; | |
248 } | |
678
e201be3f6897
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
249 |
e201be3f6897
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
250 /** |
e201be3f6897
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
251 * ベクトルに行列を乗算する |
e201be3f6897
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
252 * @param[out] v vector (float[4]) |
e201be3f6897
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
253 * @param[in] m matrix (float[16]) |
e201be3f6897
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
254 */ |
e201be3f6897
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
255 void |
e201be3f6897
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
256 ApplyMatrix(float *v, float *m) |
e201be3f6897
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
257 { |
e201be3f6897
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
258 float t[4]; |
e201be3f6897
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
259 |
e201be3f6897
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
260 t[0] = v[0]; |
e201be3f6897
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
261 t[1] = v[1]; |
e201be3f6897
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
262 t[2] = v[2]; |
e201be3f6897
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
263 t[3] = v[3]; |
e201be3f6897
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
264 |
e201be3f6897
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
265 for (int i = 0; i < 4; i++) { |
e201be3f6897
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
266 v[i] = t[0]*m[i] + t[1]*m[i+4] + t[2]*m[i+8] + t[3]*m[i+12]; |
e201be3f6897
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
267 } |
e201be3f6897
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
268 } |
e201be3f6897
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
507
diff
changeset
|
269 |
1048
40cde8c1a6cd
add ScaleXY (not for allExecute...)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1047
diff
changeset
|
270 void |
40cde8c1a6cd
add ScaleXY (not for allExecute...)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1047
diff
changeset
|
271 ScaleMatrix(float *m, float v) |
40cde8c1a6cd
add ScaleXY (not for allExecute...)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1047
diff
changeset
|
272 { |
40cde8c1a6cd
add ScaleXY (not for allExecute...)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1047
diff
changeset
|
273 for(int i=0;i<16;i++) |
40cde8c1a6cd
add ScaleXY (not for allExecute...)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1047
diff
changeset
|
274 m[i] *= v; |
40cde8c1a6cd
add ScaleXY (not for allExecute...)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1047
diff
changeset
|
275 } |
40cde8c1a6cd
add ScaleXY (not for allExecute...)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1047
diff
changeset
|
276 |
40cde8c1a6cd
add ScaleXY (not for allExecute...)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1047
diff
changeset
|
277 void |
40cde8c1a6cd
add ScaleXY (not for allExecute...)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1047
diff
changeset
|
278 ScaleMatrixXY(float *m, float x, float y) |
40cde8c1a6cd
add ScaleXY (not for allExecute...)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1047
diff
changeset
|
279 { |
40cde8c1a6cd
add ScaleXY (not for allExecute...)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1047
diff
changeset
|
280 for(int i=0;i<3;i++) { |
40cde8c1a6cd
add ScaleXY (not for allExecute...)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1047
diff
changeset
|
281 m[i] *= x; |
40cde8c1a6cd
add ScaleXY (not for allExecute...)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1047
diff
changeset
|
282 m[i+4] *= y; |
40cde8c1a6cd
add ScaleXY (not for allExecute...)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1047
diff
changeset
|
283 } |
40cde8c1a6cd
add ScaleXY (not for allExecute...)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1047
diff
changeset
|
284 } |
40cde8c1a6cd
add ScaleXY (not for allExecute...)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1047
diff
changeset
|
285 |