Mercurial > hg > Game > Cerium
annotate Renderer/Engine/matrix_calc.cc @ 1798:3babb36ac459 draft
array(bmskip table) allocate size change 256 to 256*sizeof(int)
author | Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 05 Dec 2013 16:41:38 +0900 |
parents | e51127dbd63c |
children |
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 | |
9 transMatrix(float *m0, float *m1, float *v) | |
10 { | |
11 memcpy(m0, m1, sizeof(float)*16); | |
12 | |
13 m0[12] = m1[12] + v[0]; | |
14 m0[13] = m1[13] + v[1]; | |
15 m0[14] = m1[14] + v[2]; | |
16 m0[15] = m1[15] + v[3]; | |
17 } | |
18 | |
19 void | |
20 unitMatrix(float *m) | |
21 { | |
22 bzero(m, sizeof(float)*16); | |
23 | |
24 m[0] = 1.0f; | |
25 m[5] = 1.0f; | |
26 m[10] = 1.0f; | |
27 m[15] = 1.0f; | |
28 } | |
29 | |
1292
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
30 float |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
31 determinant(float *m) { |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
32 |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
33 float det = 1.0f; |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
34 float buf; |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
35 int n = 4; // 4次 |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
36 |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
37 float det_m[16]; |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
38 for (int i = 0; i < 16; i++) det_m[i] = m[i]; |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
39 |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
40 // 三角行列を作成 |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
41 for (int i = 0; i < n; i++) { |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
42 for (int j = 0; j < n; j++) { |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
43 if ( i < j ) { |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
44 buf = det_m[j*n+i] / det_m[i*n+i]; |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
45 for (int k = 0; k < n; k++) { |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
46 det_m[j*n+k] -= det_m[i*n+k] * buf; |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
47 } |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
48 } |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
49 } |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
50 } |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
51 |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
52 // 対角部分の積 |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
53 for (int i = 0; i < n; i++) { |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
54 det *= det_m[i*n+i]; |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
55 } |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
56 |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
57 return det; |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
58 } |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
59 |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
60 |
539 | 61 void |
1276
e92f00ed2fc0
apply view matrix to normal vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1226
diff
changeset
|
62 inverseMatrix(float *m0, float *m1) |
539 | 63 { |
64 float m[16]; | |
65 | |
66 for (int i = 0; i < 4; i++) { | |
67 for (int j = 0; j < 4; j++) { | |
1254 | 68 m0[i*4+j] = m[i*4+j] = m1[j*4+i]; |
539 | 69 } |
70 } | |
71 | |
1254 | 72 m0[12] = -(m1[12]*m[0] + m1[13]*m[1] + m1[14]*m[2]); |
73 m0[13] = -(m1[12]*m[4] + m1[13]*m[5] + m1[14]*m[6]); | |
74 m0[14] = -(m1[12]*m[8] + m1[13]*m[9] + m1[14]*m[10]); | |
75 m0[3] = m0[7] = m0[11] = 0.0f; | |
76 m0[15] = 1.0f; | |
539 | 77 |
78 } | |
79 | |
80 /** | |
81 * マトリックス m にベクトル v1 を右から乗算して、v0に与える | |
82 * @param[out] v0 output vector (float[4]) | |
83 * @param[in] v1 input vector (float[4]) | |
84 * @param[in] m matrix (float[16]) | |
85 */ | |
86 void | |
87 applyMatrix(float *v0, float *m, float *v1) | |
88 { | |
89 for (int i = 0; i < 4; i++) { | |
90 v0[i] = v1[0]*m[i] + v1[1]*m[i+4] + v1[2]*m[i+8] + v1[3]*m[i+12]; | |
91 } | |
92 } | |
93 | |
1254 | 94 |
1351
e51127dbd63c
operate directly on matrix. not work yet.
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1292
diff
changeset
|
95 void |
e51127dbd63c
operate directly on matrix. not work yet.
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1292
diff
changeset
|
96 copyMatrix(float *m0, float *m1) |
e51127dbd63c
operate directly on matrix. not work yet.
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1292
diff
changeset
|
97 { |
e51127dbd63c
operate directly on matrix. not work yet.
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1292
diff
changeset
|
98 for (int i = 0; i < 16; i++) { |
e51127dbd63c
operate directly on matrix. not work yet.
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1292
diff
changeset
|
99 m0[i] = m1[i]; |
e51127dbd63c
operate directly on matrix. not work yet.
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1292
diff
changeset
|
100 } |
e51127dbd63c
operate directly on matrix. not work yet.
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1292
diff
changeset
|
101 } |
e51127dbd63c
operate directly on matrix. not work yet.
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1292
diff
changeset
|
102 |
e51127dbd63c
operate directly on matrix. not work yet.
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1292
diff
changeset
|
103 |
539 | 104 /** |
105 * ベクトルの正規化 | |
106 * | |
107 * @param[out] v0 output vector | |
108 * @param[in] v1 input vector | |
109 */ | |
110 void | |
111 normalize(float *v0, float *v1) | |
112 { | |
113 float norm, dnorm; | |
114 | |
115 norm = sqrt(v1[0]*v1[0] + v1[1]*v1[1] + v1[2]*v1[2]); | |
116 if (norm > 0) { | |
117 dnorm = 1.0/norm; | |
118 v0[0] = v1[0]*dnorm; | |
119 v0[1] = v1[1]*dnorm; | |
120 v0[2] = v1[2]*dnorm; | |
121 v0[3] = v1[3]*dnorm; | |
122 } | |
123 } | |
124 | |
125 /** | |
126 * ベクトルの減算 v0 = v1 - v2 | |
127 */ | |
128 void | |
129 subVector(float *v0, float *v1, float *v2) | |
130 { | |
131 v0[0] = v1[0] - v2[0]; | |
132 v0[1] = v1[1] - v2[1]; | |
133 v0[2] = v1[2] - v2[2]; | |
134 v0[3] = v1[3] - v2[3]; | |
135 } | |
136 | |
137 /** | |
138 * ベクトルの外積 v0 = v1 x v2 | |
139 */ | |
140 void | |
141 outerProduct(float *v0, float *v1, float *v2) | |
142 { | |
143 v0[0] = v1[1] * v2[2] - v1[2] * v2[1]; | |
144 v0[1] = v1[2] * v2[0] - v1[0] * v2[2]; | |
145 v0[2] = v1[0] * v2[1] - v1[1] * v2[0]; | |
146 v0[3] = 0; | |
147 } | |
148 | |
149 void | |
150 transposeMatrix(float *m0, float *m1) | |
151 { | |
152 float t[16]; | |
153 | |
1254 | 154 // こういう小細工よりベクタ使った方が良いんだが |
539 | 155 for (int i = 0; i < 4; i++) { |
1254 | 156 for (int j = i+1; j < 4; j++) { |
157 float tmp = t[j*4+i]; | |
539 | 158 t[i*4+j] = m1[j*4+i]; |
1254 | 159 m1[j*4+i] = tmp; |
539 | 160 } |
161 } | |
162 } | |
163 | |
164 /** | |
165 * ベクトルの内積 f = v0 * v1 | |
166 */ | |
167 float | |
168 innerProduct(float *v0, float *v1) | |
169 { | |
170 return (v0[0]*v1[0] + v0[1]*v1[1] + v0[2]*v1[2]); | |
171 } | |
172 | |
1094
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
173 /** |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
174 * xyz = xyz1 * xyz2 |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
175 */ |
539 | 176 void matrix4x4(float *xyz, float *xyz1, float *xyz2) //xyz[16] |
177 { | |
1351
e51127dbd63c
operate directly on matrix. not work yet.
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1292
diff
changeset
|
178 |
539 | 179 for(int t=0; t<16; t+=4) |
180 { | |
181 for(int i=0; i<4; i++) | |
182 { | |
183 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]; | |
184 } | |
185 } | |
186 } | |
187 | |
1292
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
188 /** |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
189 * xyz = xyz * xyz1 |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
190 */ |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
191 void mulMatrix4x4(float *xyz, float *xyz1) //xyz[16] |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
192 { |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
193 |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
194 float tmp[16]; |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
195 for(int i = 0; i<16; i++) { |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
196 tmp[i] = xyz[i]; |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
197 } |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
198 |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
199 for(int t=0; t<16; t+=4) { |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
200 for(int i=0; i<4; i++) { |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
201 xyz[t+i] = tmp[t]*xyz1[i] + tmp[t+1]*xyz1[4+i] + tmp[t+2]*xyz1[8+i] + tmp[t+3]*xyz1[12+i]; |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
202 } |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
203 } |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
204 } |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
205 |
90efd2aac2cb
add matrix test and debug light vector
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1277
diff
changeset
|
206 |
1256 | 207 void matrix4x4R(float xyz[16], float xyz1[16], float xyz2[16]) |
1254 | 208 { |
209 for(int t=0; t<16; t+=4) | |
210 { | |
211 for(int i=0; i<4; i++) | |
212 { | |
213 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]; | |
214 } | |
215 } | |
216 } | |
217 | |
1047
f87218eed9fc
broken texure ( h/w != 2^n ) protection
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
677
diff
changeset
|
218 /** |
1094
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
219 * c_xyz を中心に sacle 倍する |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
220 */ |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
221 void |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
222 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
|
223 { |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
224 float xyz2[16] = { |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
225 scale[0], 0, 0, scale[0]*(-c_xyz[0]) + c_xyz[0], |
1098 | 226 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
|
227 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
|
228 0, 0, 0, 1 |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
229 |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
230 }; |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
231 float xyz1[16] = { |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
232 xyz[0], xyz[1], xyz[2], xyz[3], |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
233 xyz[4], xyz[5], xyz[6], xyz[7], |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
234 xyz[8], xyz[9], xyz[10], xyz[11], |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
235 xyz[12], xyz[13], xyz[14], xyz[15]}; |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
236 |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
237 for(int t=0; t<16; t+=4) |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
238 { |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
239 for(int i=0; i<4; i++) |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
240 { |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
241 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
|
242 } |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
243 } |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
244 } |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
245 |
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
246 /** |
1050 | 247 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
|
248 行ったものを matrix に代入する |
f87218eed9fc
broken texure ( h/w != 2^n ) protection
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
677
diff
changeset
|
249 */ |
f87218eed9fc
broken texure ( h/w != 2^n ) protection
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
677
diff
changeset
|
250 void |
1094
f10ec9bbd3f6
separate scale matrix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1051
diff
changeset
|
251 get_matrix( float *matrix, float *rxyz, float *txyz, float *stack) |
539 | 252 { |
253 float radx,rady,radz; | |
1254 | 254 radx = rxyz[0]*M_PI/180; |
255 rady = rxyz[1]*M_PI/180; | |
256 radz = rxyz[2]*M_PI/180; | |
539 | 257 |
258 float sinx = sin(radx); | |
259 float cosx = cos(radx); | |
260 float siny = sin(rady); | |
261 float cosy = cos(rady); | |
262 float sinz = sin(radz); | |
263 float cosz = cos(radz); | |
264 | |
1050 | 265 float m1[16]; |
266 float *m = stack? m1 : matrix; | |
267 | |
539 | 268 /* View Transform */ |
1051 | 269 m[0] = cosz*cosy+sinz*sinx*siny; |
270 m[1] = sinz*cosx; | |
271 m[2] = -cosz*siny+sinz*sinx*cosy; | |
1050 | 272 m[3] = 0; |
1051 | 273 m[4] = -sinz*cosy+cosz*sinx*siny; |
274 m[5] = cosz*cosx; | |
275 m[6] = sinz*siny+cosz*sinx*cosy; | |
1050 | 276 m[7] = 0; |
1051 | 277 m[8] = cosx*siny; |
278 m[9] = -sinx; | |
279 m[10] = cosx*cosy; | |
1050 | 280 m[11] = 0; |
281 m[12] = txyz[0]; | |
282 m[13] = txyz[1]; | |
283 m[14] = txyz[2]; | |
284 m[15] = 1; | |
539 | 285 |
286 if(stack) | |
287 { | |
288 matrix4x4(matrix, m, stack); | |
289 } | |
290 | |
291 } | |
292 | |
1165 | 293 void |
294 get_matrix_scale( float *matrix, float *rxyz, float *txyz, float *scale, float *stack) | |
295 { | |
296 float radx,rady,radz; | |
1254 | 297 radx = rxyz[0]*M_PI/180; |
298 rady = rxyz[1]*M_PI/180; | |
299 radz = rxyz[2]*M_PI/180; | |
1165 | 300 |
301 float sinx = sin(radx)*scale[0]; | |
302 float cosx = cos(radx)*scale[0]; | |
303 float siny = sin(rady)*scale[1]; | |
304 float cosy = cos(rady)*scale[1]; | |
305 float sinz = sin(radz)*scale[2]; | |
306 float cosz = cos(radz)*scale[2]; | |
307 | |
308 float m1[16]; | |
309 float *m = stack? m1 : matrix; | |
310 | |
311 /* View Transform */ | |
312 m[0] = cosz*cosy+sinz*sinx*siny; | |
313 m[1] = sinz*cosx; | |
314 m[2] = -cosz*siny+sinz*sinx*cosy; | |
315 m[3] = 0; | |
316 m[4] = -sinz*cosy+cosz*sinx*siny; | |
317 m[5] = cosz*cosx; | |
318 m[6] = sinz*siny+cosz*sinx*cosy; | |
319 m[7] = 0; | |
320 m[8] = cosx*siny; | |
321 m[9] = -sinx; | |
322 m[10] = cosx*cosy; | |
323 m[11] = 0; | |
324 m[12] = txyz[0]; | |
325 m[13] = txyz[1]; | |
326 m[14] = txyz[2]; | |
327 m[15] = 1; | |
328 | |
329 if(stack) | |
330 { | |
331 matrix4x4(matrix, m, stack); | |
332 } | |
333 | |
334 } | |
335 | |
1254 | 336 void |
1256 | 337 rotate_matrix( float m[16], float rxyz[4] ) |
1254 | 338 { |
339 float radx,rady,radz; | |
340 radx = rxyz[0]*M_PI/180; | |
341 rady = rxyz[1]*M_PI/180; | |
342 radz = rxyz[2]*M_PI/180; | |
343 | |
344 float sinx = sin(radx); | |
345 float cosx = cos(radx); | |
346 float siny = sin(rady); | |
347 float cosy = cos(rady); | |
348 float sinz = sin(radz); | |
349 float cosz = cos(radz); | |
350 | |
351 /* View Transform */ | |
352 m[0] = cosz*cosy+sinz*sinx*siny; | |
353 m[1] = sinz*cosx; | |
354 m[2] = -cosz*siny+sinz*sinx*cosy; | |
355 m[3] = 0; | |
356 m[4] = -sinz*cosy+cosz*sinx*siny; | |
357 m[5] = cosz*cosx; | |
358 m[6] = sinz*siny+cosz*sinx*cosy; | |
359 m[7] = 0; | |
360 m[8] = cosx*siny; | |
361 m[9] = -sinx; | |
362 m[10] = cosx*cosy; | |
363 m[11] = 0; | |
364 m[12] = 0; | |
365 m[13] = 0; | |
366 m[14] = 0; | |
367 m[15] = 1; | |
368 } | |
369 | |
539 | 370 void rotate_x(float *xyz, float r) |
371 { | |
1254 | 372 float rad = r*M_PI/180; |
539 | 373 |
374 xyz[0] = xyz[0]; | |
375 xyz[1] = xyz[1]*cos(rad) - xyz[2]*sin(rad); | |
376 xyz[2] = xyz[1]*sin(rad) + xyz[2]*cos(rad); | |
377 } | |
378 | |
379 void rotate_y(float *xyz, float r) | |
380 { | |
1254 | 381 float rad = r*M_PI/180; |
539 | 382 |
383 xyz[0] = xyz[0]*cos(rad) + xyz[2]*sin(rad); | |
384 xyz[1] = xyz[1]; | |
385 xyz[2] = -xyz[0]*sin(rad) + xyz[2]*cos(rad); | |
386 } | |
387 | |
388 void rotate_z(float *xyz, float r) | |
389 { | |
1254 | 390 float rad = r*M_PI/180; |
539 | 391 |
392 xyz[0] = xyz[0]*cos(rad) - xyz[1]*sin(rad); | |
393 xyz[1] = xyz[0]*sin(rad) + xyz[1]*cos(rad); | |
394 xyz[2] = xyz[2]; | |
395 } | |
396 | |
397 void rotate(float *xyz, float *matrix) | |
398 { | |
399 float abc[4]; | |
400 abc[0] = xyz[0]; | |
401 abc[1] = xyz[1]; | |
402 abc[2] = xyz[2]; | |
403 abc[3] = xyz[3]; | |
404 | |
405 for(int i=0; i<4; i++) | |
406 { | |
407 //xyz[i] = abc[0]*rot[i] + abc[1]*rot[i+4] + abc[2]*rot[i+8] + abc[3]*rot[i+12]; | |
408 xyz[i] = abc[0]*matrix[i] + abc[1]*matrix[i+4] + abc[2]*matrix[i+8] + abc[3]*matrix[i+12]; | |
409 } | |
410 } | |
411 | |
412 | |
413 void translate(float *xyz, float x, float y, float z) | |
414 { | |
415 xyz[0] += x; | |
416 xyz[1] += y; | |
417 xyz[2] += z; | |
418 } | |
677
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
419 |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
420 /** |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
421 * ベクトルに行列を乗算する |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
422 * @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
|
423 * @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
|
424 */ |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
425 void |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
426 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
|
427 { |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
428 float t[4]; |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
429 |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
430 t[0] = v[0]; |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
431 t[1] = v[1]; |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
432 t[2] = v[2]; |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
433 t[3] = v[3]; |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
434 |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
435 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
|
436 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
|
437 } |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
438 } |
24054155368c
add Load light info for some spe
yutaka@henri.cr.ie.u-ryukyu.ac.jp
parents:
539
diff
changeset
|
439 |
1048
5e62924bd7d9
add ScaleXY (not for allExecute...)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1047
diff
changeset
|
440 void |
1050 | 441 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
|
442 { |
5e62924bd7d9
add ScaleXY (not for allExecute...)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1047
diff
changeset
|
443 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
|
444 m[i] *= x; |
5e62924bd7d9
add ScaleXY (not for allExecute...)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1047
diff
changeset
|
445 m[i+4] *= y; |
1050 | 446 m[i+8] *= z; |
1048
5e62924bd7d9
add ScaleXY (not for allExecute...)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1047
diff
changeset
|
447 } |
5e62924bd7d9
add ScaleXY (not for allExecute...)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1047
diff
changeset
|
448 } |
5e62924bd7d9
add ScaleXY (not for allExecute...)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1047
diff
changeset
|
449 |