1 |
bearsoft |
1.1 |
#include "Rotations.h"
|
2 |
|
|
#include "math.h"
|
3 |
|
|
|
4 |
|
|
|
5 |
|
|
Rotations::Rotations()
|
6 |
|
|
{
|
7 |
|
|
xangle=0;
|
8 |
|
|
yangle=0;
|
9 |
|
|
zangle=0;
|
10 |
|
|
}
|
11 |
|
|
|
12 |
|
|
Rotations::~Rotations(){}
|
13 |
|
|
|
14 |
|
|
void Rotations::copy_matrix( float rotate[4][4], float dest[4][4] )
|
15 |
|
|
{
|
16 |
|
|
dest[0][0]=rotate[0][0];
|
17 |
|
|
dest[0][1]=rotate[0][1];
|
18 |
|
|
dest[0][2]=rotate[0][2];
|
19 |
|
|
dest[0][3]=rotate[0][3];
|
20 |
|
|
|
21 |
|
|
dest[1][0]=rotate[1][0];
|
22 |
|
|
dest[1][1]=rotate[1][1];
|
23 |
|
|
dest[1][2]=rotate[1][2];
|
24 |
|
|
dest[1][3]=rotate[1][3];
|
25 |
|
|
|
26 |
|
|
dest[2][0]=rotate[2][0];
|
27 |
|
|
dest[2][1]=rotate[2][1];
|
28 |
|
|
dest[2][2]=rotate[2][2];
|
29 |
|
|
dest[2][3]=rotate[2][3];
|
30 |
|
|
|
31 |
|
|
dest[3][0]=rotate[3][0];
|
32 |
|
|
dest[3][1]=rotate[3][1];
|
33 |
|
|
dest[3][2]=rotate[3][2];
|
34 |
|
|
dest[3][3]=rotate[3][3];
|
35 |
|
|
}
|
36 |
|
|
|
37 |
|
|
|
38 |
|
|
void Rotations::inverse_matrix( float rotate[4][4] )
|
39 |
|
|
{
|
40 |
|
|
float d00,d01,d02,d03;
|
41 |
|
|
float d10,d11,d12,d13;
|
42 |
|
|
float d20,d21,d22,d23;
|
43 |
|
|
float d30,d31,d32,d33;
|
44 |
|
|
float m00,m01,m02,m03;
|
45 |
|
|
float m10,m11,m12,m13;
|
46 |
|
|
float m20,m21,m22,m23;
|
47 |
|
|
float m30,m31,m32,m33;
|
48 |
|
|
float d;
|
49 |
|
|
|
50 |
|
|
/////////////////////////////////////////////////////////////////////////
|
51 |
|
|
|
52 |
|
|
m00=rotate[0][0];
|
53 |
|
|
m01=rotate[0][1];
|
54 |
|
|
m02=rotate[0][2];
|
55 |
|
|
m03=rotate[0][3];
|
56 |
|
|
|
57 |
|
|
m10=rotate[1][0];
|
58 |
|
|
m11=rotate[1][1];
|
59 |
|
|
m12=rotate[1][2];
|
60 |
|
|
m13=rotate[1][3];
|
61 |
|
|
|
62 |
|
|
m20=rotate[2][0];
|
63 |
|
|
m21=rotate[2][1];
|
64 |
|
|
m22=rotate[2][2];
|
65 |
|
|
m23=rotate[2][3];
|
66 |
|
|
|
67 |
|
|
m30=rotate[3][0];
|
68 |
|
|
m31=rotate[3][1];
|
69 |
|
|
m32=rotate[3][2];
|
70 |
|
|
m33=rotate[3][3];
|
71 |
|
|
|
72 |
|
|
/////////////////////////////////////////////////////////////////////////
|
73 |
|
|
|
74 |
|
|
d00 = m11*m22*m33 + m12*m23*m31 + m13*m21*m32 - m31*m22*m13 - m32*m23*m11 - m33*m21*m12;
|
75 |
|
|
d01 = m10*m22*m33 + m12*m23*m30 + m13*m20*m32 - m30*m22*m13 - m32*m23*m10 - m33*m20*m12;
|
76 |
|
|
d02 = m10*m21*m33 + m11*m23*m30 + m13*m20*m31 - m30*m21*m13 - m31*m23*m10 - m33*m20*m11;
|
77 |
|
|
d03 = m10*m21*m32 + m11*m22*m30 + m12*m20*m31 - m30*m21*m12 - m31*m22*m10 - m32*m20*m11;
|
78 |
|
|
|
79 |
|
|
d10 = m01*m22*m33 + m02*m23*m31 + m03*m21*m32 - m31*m22*m03 - m32*m23*m01 - m33*m21*m02;
|
80 |
|
|
d11 = m00*m22*m33 + m02*m23*m30 + m03*m20*m32 - m30*m22*m03 - m32*m23*m00 - m33*m20*m02;
|
81 |
|
|
d12 = m00*m21*m33 + m01*m23*m30 + m03*m20*m31 - m30*m21*m03 - m31*m23*m00 - m33*m20*m01;
|
82 |
|
|
d13 = m00*m21*m32 + m01*m22*m30 + m02*m20*m31 - m30*m21*m02 - m31*m22*m00 - m32*m20*m01;
|
83 |
|
|
|
84 |
|
|
d20 = m01*m12*m33 + m02*m13*m31 + m03*m11*m32 - m31*m12*m03 - m32*m13*m01 - m33*m11*m02;
|
85 |
|
|
d21 = m00*m12*m33 + m02*m13*m30 + m03*m10*m32 - m30*m12*m03 - m32*m13*m00 - m33*m10*m02;
|
86 |
|
|
d22 = m00*m11*m33 + m01*m13*m30 + m03*m10*m31 - m30*m11*m03 - m31*m13*m00 - m33*m10*m01;
|
87 |
|
|
d23 = m00*m11*m32 + m01*m12*m30 + m02*m10*m31 - m30*m11*m02 - m31*m12*m00 - m32*m10*m01;
|
88 |
|
|
|
89 |
|
|
d30 = m01*m12*m23 + m02*m13*m21 + m03*m11*m22 - m21*m12*m03 - m22*m13*m01 - m23*m11*m02;
|
90 |
|
|
d31 = m00*m12*m23 + m02*m13*m20 + m03*m10*m22 - m20*m12*m03 - m22*m13*m00 - m23*m10*m02;
|
91 |
|
|
d32 = m00*m11*m23 + m01*m13*m20 + m03*m10*m21 - m20*m11*m03 - m21*m13*m00 - m23*m10*m01;
|
92 |
|
|
d33 = m00*m11*m22 + m01*m12*m20 + m02*m10*m21 - m20*m11*m02 - m21*m12*m00 - m22*m10*m01;
|
93 |
|
|
|
94 |
|
|
/////////////////////////////////////////////////////////////////////////
|
95 |
|
|
|
96 |
|
|
d = m00*d00 - m01*d01 + m02*d02 - m03*d03;
|
97 |
|
|
|
98 |
|
|
if(0.0 != d)
|
99 |
|
|
{
|
100 |
|
|
d = 1.0f/d;
|
101 |
|
|
|
102 |
|
|
rotate[0][0] = d00*d;
|
103 |
|
|
rotate[0][1] = -d10*d;
|
104 |
|
|
rotate[0][2] = d20*d;
|
105 |
|
|
rotate[0][3] = -d30*d;
|
106 |
|
|
|
107 |
|
|
rotate[1][0] = -d01*d;
|
108 |
|
|
rotate[1][1] = d11*d;
|
109 |
|
|
rotate[1][2] = -d21*d;
|
110 |
|
|
rotate[1][3] = d31*d;
|
111 |
|
|
|
112 |
|
|
rotate[2][0] = d02*d;
|
113 |
|
|
rotate[2][1] = -d12*d;
|
114 |
|
|
rotate[2][2] = d22*d;
|
115 |
|
|
rotate[2][3] = -d32*d;
|
116 |
|
|
|
117 |
|
|
rotate[3][0] = -d03*d;
|
118 |
|
|
rotate[3][1] = d13*d;
|
119 |
|
|
rotate[3][2] = -d23*d;
|
120 |
|
|
rotate[3][3] = d33*d;
|
121 |
|
|
}
|
122 |
|
|
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
|
126 |
|
|
void Rotations::makenormtab()
|
127 |
|
|
{
|
128 |
|
|
for ( int step=0 ; step<numberofpolys ; step++ )
|
129 |
|
|
{
|
130 |
|
|
// calc normal for the polygon
|
131 |
|
|
|
132 |
|
|
int pos1=step*3+0;
|
133 |
|
|
int pos2=step*3+1;
|
134 |
|
|
int pos3=step*3+2;
|
135 |
|
|
|
136 |
|
|
float uxt=vx[points[pos2]]-vx[points[pos1]];
|
137 |
|
|
float uyt=vy[points[pos2]]-vy[points[pos1]];
|
138 |
|
|
float uzt=vz[points[pos2]]-vz[points[pos1]];
|
139 |
|
|
|
140 |
|
|
float vxt=vx[points[pos3]]-vx[points[pos2]];
|
141 |
|
|
float vyt=vy[points[pos3]]-vy[points[pos2]];
|
142 |
|
|
float vzt=vz[points[pos3]]-vz[points[pos2]];
|
143 |
|
|
|
144 |
|
|
float x = uyt*vzt - uzt*vyt; // x normal
|
145 |
|
|
float y = uzt*vxt - uxt*vzt; // y normal
|
146 |
|
|
float z = uxt*vyt - uyt*vxt; // z normal
|
147 |
|
|
|
148 |
|
|
float dist=(float)sqrt(x*x+y*y+z*z); // distans till normal
|
149 |
|
|
|
150 |
|
|
vnx[step]=x;
|
151 |
|
|
vny[step]=y;
|
152 |
|
|
vnz[step]=z;
|
153 |
|
|
|
154 |
|
|
}
|
155 |
|
|
|
156 |
|
|
}
|
157 |
|
|
|
158 |
|
|
void Rotations::matmulmatrix44(float a[4][4] ,float b[4][4], float result[4][4])
|
159 |
|
|
{
|
160 |
|
|
int i,j,k;
|
161 |
|
|
|
162 |
|
|
float sum;
|
163 |
|
|
|
164 |
|
|
for ( i=0; i<4; i++ )
|
165 |
|
|
{
|
166 |
|
|
|
167 |
|
|
for ( j=0; j<4 ; j++ )
|
168 |
|
|
{
|
169 |
|
|
sum=0;
|
170 |
|
|
|
171 |
|
|
for ( k=0; k<4; k++ )
|
172 |
|
|
{
|
173 |
|
|
sum+=a[i][k]*b[k][j];
|
174 |
|
|
}
|
175 |
|
|
|
176 |
|
|
result[i][j]=sum;
|
177 |
|
|
}
|
178 |
|
|
|
179 |
|
|
}
|
180 |
|
|
|
181 |
|
|
}
|
182 |
|
|
|
183 |
|
|
|
184 |
|
|
void Rotations::clearmatric(float a[4][4])
|
185 |
|
|
{
|
186 |
|
|
a[0][0]=0;
|
187 |
|
|
a[1][0]=0;
|
188 |
|
|
a[2][0]=0;
|
189 |
|
|
a[3][0]=0;
|
190 |
|
|
|
191 |
|
|
a[0][1]=0;
|
192 |
|
|
a[1][1]=0;
|
193 |
|
|
a[2][1]=0;
|
194 |
|
|
a[3][1]=0;
|
195 |
|
|
|
196 |
|
|
a[0][2]=0;
|
197 |
|
|
a[1][2]=0;
|
198 |
|
|
a[2][2]=0;
|
199 |
|
|
a[3][2]=0;
|
200 |
|
|
|
201 |
|
|
a[0][3]=0;
|
202 |
|
|
a[1][3]=0;
|
203 |
|
|
a[2][3]=0;
|
204 |
|
|
a[3][3]=0;
|
205 |
|
|
|
206 |
|
|
a[0][0]=1;
|
207 |
|
|
a[1][1]=1;
|
208 |
|
|
a[2][2]=1;
|
209 |
|
|
a[3][3]=1;
|
210 |
|
|
}
|
211 |
|
|
|
212 |
|
|
void Rotations::objectspline(float x, float y, float z)
|
213 |
|
|
{
|
214 |
|
|
clearmatric( rotate );
|
215 |
|
|
clearmatric( rotate_x2 );
|
216 |
|
|
clearmatric( rotate_y2 );
|
217 |
|
|
clearmatric( rotate_z2 );
|
218 |
|
|
clearmatric( translate2 );
|
219 |
|
|
|
220 |
|
|
translate2[3][0]=(x/30);
|
221 |
|
|
translate2[3][1]=-(y/30);
|
222 |
|
|
translate2[3][2]=-10.0f;
|
223 |
|
|
|
224 |
|
|
xangle++;
|
225 |
|
|
yangle++;
|
226 |
|
|
zangle++;
|
227 |
|
|
|
228 |
|
|
// yangle=180.0f;
|
229 |
|
|
|
230 |
|
|
float tempxangle=xangle;
|
231 |
|
|
float tempyangle=yangle;
|
232 |
|
|
float tempzangle=zangle;
|
233 |
|
|
|
234 |
|
|
tempxangle=(float)3.14*tempxangle/180;
|
235 |
|
|
tempyangle=(float)3.14*tempyangle/180;
|
236 |
|
|
tempzangle=(float)3.14*tempzangle/180;
|
237 |
|
|
|
238 |
|
|
rotate_x2[1][1]=(float)cos(tempxangle);
|
239 |
|
|
rotate_x2[0][1]=(float)(sin(tempxangle));
|
240 |
|
|
rotate_x2[1][0]=(float)(-sin(tempxangle));
|
241 |
|
|
rotate_x2[0][0]=(float)cos(tempxangle);
|
242 |
|
|
|
243 |
|
|
rotate_y2[0][0]=(float)cos(tempyangle);
|
244 |
|
|
rotate_y2[0][2]=(float)(-sin(tempyangle));
|
245 |
|
|
rotate_y2[2][0]=(float)(sin(tempyangle));
|
246 |
|
|
rotate_y2[2][2]=(float)cos(tempyangle);
|
247 |
|
|
|
248 |
|
|
rotate_z2[1][1]=(float)cos(tempzangle);
|
249 |
|
|
rotate_z2[1][2]=(float)(sin(tempzangle));
|
250 |
|
|
rotate_z2[2][1]=(float)(-sin(tempzangle));
|
251 |
|
|
rotate_z2[2][2]=(float)cos(tempzangle);
|
252 |
|
|
}
|
253 |
|
|
|
254 |
|
|
void Rotations::objectsmulmatrix()
|
255 |
|
|
{
|
256 |
|
|
float temp[4][4]={ {1,0,0,0}, {0,1,0,0}, {0,0,1,0}, {0,0,0,1} };
|
257 |
|
|
float temp2[4][4]={ {1,0,0,0}, {0,1,0,0}, {0,0,1,0}, {0,0,0,1} };
|
258 |
|
|
|
259 |
|
|
matmulmatrix44( rotate_x2, rotate_z2, temp);
|
260 |
|
|
matmulmatrix44( temp, rotate_y2, temp2);
|
261 |
|
|
matmulmatrix44( temp2, translate2, rotate);
|
262 |
|
|
}
|
263 |
|
|
|
264 |
|
|
|
265 |
|
|
void Rotations::calculaterotation()
|
266 |
|
|
{
|
267 |
|
|
for ( int v=0 ; v<numberofvertices ; v++ )
|
268 |
|
|
{
|
269 |
|
|
if ( visiblyvertices[v] != 0 )
|
270 |
|
|
{
|
271 |
|
|
x[v]=vx[v]*rotate[0][0]+vy[v]*rotate[1][0]+vz[v]*rotate[2][0]+rotate[3][0];
|
272 |
|
|
y[v]=vx[v]*rotate[0][1]+vy[v]*rotate[1][1]+vz[v]*rotate[2][1]+rotate[3][1];
|
273 |
|
|
z[v]=vx[v]*rotate[0][2]+vy[v]*rotate[1][2]+vz[v]*rotate[2][2]+rotate[3][2];
|
274 |
|
|
z[v]=-z[v];
|
275 |
|
|
}
|
276 |
|
|
}
|
277 |
|
|
}
|
278 |
|
|
|
279 |
|
|
|
280 |
|
|
void Rotations::calculate_inverse_rotation()
|
281 |
|
|
{
|
282 |
|
|
for ( int v=0 ; v<numberofvertices ; v++ )
|
283 |
|
|
{
|
284 |
|
|
ix[v]=-translate2[3][0]*inverse_rotate[0][0]+-translate2[3][1]*inverse_rotate[1][0]+-translate2[3][2]*inverse_rotate[2][0];
|
285 |
|
|
iy[v]=-translate2[3][0]*inverse_rotate[0][1]+-translate2[3][1]*inverse_rotate[1][1]+-translate2[3][2]*inverse_rotate[2][1];
|
286 |
|
|
iz[v]=-translate2[3][0]*inverse_rotate[0][2]+-translate2[3][1]*inverse_rotate[1][2]+-translate2[3][2]*inverse_rotate[2][2];
|
287 |
|
|
}
|
288 |
|
|
}
|
289 |
|
|
|
290 |
|
|
void Rotations::backfacecull()
|
291 |
|
|
{
|
292 |
|
|
for ( int v=0 ; v<numberofvertices ; v++ )
|
293 |
|
|
{
|
294 |
|
|
visiblyvertices[v]=0;
|
295 |
|
|
}
|
296 |
|
|
|
297 |
|
|
for ( int oo=0 ; oo<numberofpolys ; oo++ )
|
298 |
|
|
{
|
299 |
|
|
int p=0;
|
300 |
|
|
|
301 |
|
|
for ( int r=0; r<3 ; r++ )
|
302 |
|
|
{
|
303 |
|
|
if (check_vertices_is_visibly(oo, r))
|
304 |
|
|
{
|
305 |
|
|
p++;
|
306 |
|
|
}
|
307 |
|
|
}
|
308 |
|
|
|
309 |
|
|
if ( p == 3 )
|
310 |
|
|
{
|
311 |
|
|
visiblypolys[oo]=1;
|
312 |
|
|
|
313 |
|
|
for ( int step=0; step<3 ; step++ )
|
314 |
|
|
{
|
315 |
|
|
visiblyvertices[points[oo*3+step]]++;
|
316 |
|
|
}
|
317 |
|
|
|
318 |
|
|
}
|
319 |
|
|
else
|
320 |
|
|
{
|
321 |
|
|
visiblypolys[oo]=0;
|
322 |
|
|
}
|
323 |
|
|
}
|
324 |
|
|
}
|
325 |
|
|
|
326 |
|
|
|
327 |
|
|
bool Rotations::check_vertices_is_visibly( int oo, int step)
|
328 |
|
|
{
|
329 |
|
|
bool sant=false;
|
330 |
|
|
|
331 |
|
|
float gx=vnx[oo]*(ix[points[oo*3+step]]-vx[points[oo*3+step]]);
|
332 |
|
|
float gy=vny[oo]*(iy[points[oo*3+step]]-vy[points[oo*3+step]]);
|
333 |
|
|
float gz=vnz[oo]*(iz[points[oo*3+step]]-vz[points[oo*3+step]]);
|
334 |
|
|
|
335 |
|
|
float g=gx+gy+gz;
|
336 |
|
|
|
337 |
|
|
if ( g > 0 )
|
338 |
|
|
{
|
339 |
|
|
sant=true;
|
340 |
|
|
}
|
341 |
|
|
|
342 |
|
|
return sant;
|
343 |
|
|
}
|
344 |
|
|
|
345 |
|
|
void Rotations::initbackfacecull()
|
346 |
|
|
{
|
347 |
|
|
copy_matrix(rotate,inverse_rotate);
|
348 |
|
|
inverse_matrix(inverse_rotate);
|
349 |
|
|
calculate_inverse_rotation();
|
350 |
|
|
}
|