-
Notifications
You must be signed in to change notification settings - Fork 1
/
3dFMath.c
322 lines (294 loc) · 8.31 KB
/
3dFMath.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*****************************************************************************
* file: "FixedMath.c"
*
* purpose: Fixed point vector, matrix and certain trigonometric
* operations required by "3d.c" functions.
*
* ©1990 Mark M. Owen. All rights reserved.
*****************************************************************************
*/
#include "xvt.h"
#include "3d.h"
#include "3dFMath.h"
Fixed
#if XVT_CC_PROTO
x2fix( FPType *d) /* X2Fix */
#else
x2fix(d) /* X2Fix */
FPType *d; /* X2Fix */
#endif
{
return *d;
}
/*****************************************************************************
*
* Function: FxTan
*
* purpose: calculate the tangent of an angle.
*
* warning: the angle is assumed to be expressed in degrees. conversion
* to radians is performed here.
*
*****************************************************************************
*/
Fixed
#if XVT_CC_PROTO
FxTan(Fixed a)
#else
FxTan(a)
Fixed a;
#endif
{
return(fixdiv(frac2fix(fracsin(Rad(a))),frac2fix(fraccos(Rad(a)))));
}
/*****************************************************************************
*
* Function: vScalarMultiply
*
* purpose: multiply the components of a vector by some factor.
*
* warning: only the first three cells of each vector are used.
*
*****************************************************************************
*/
void
#if XVT_CC_PROTO
vScalarMultiply(Fixed factor,Vector *vect)
#else
vScalarMultiply(factor, vect)
Fixed factor;
Vector *vect;
#endif
{
register int i;
for(i=0;i<=2;i++)
vect->v[i] = fixmul(vect->v[i], factor);
}
/*****************************************************************************
*
* Function: vAdd
*
* purpose: adds corresponding cells of two vectors giving a third.
*
* warning: only the first three cells of each vector are used.
*
*****************************************************************************
*/
void
#if XVT_CC_PROTO
vAdd(Vector *a,Vector *b,Vector *c)
#else
vAdd(a, b, c)
Vector *a, *b, *c;
#endif
{
register int i;
for (i=0;i<=2;i++)
c->v[i] = a->v[i] + b->v[i];
}
/*****************************************************************************
*
* Function: vVectorProduct aka: CrossProduct
*
* purpose: using two vectors calculates a third which is mutually
* perpendicular to the first two vectors.
*
* method: |a x b| = |a||b| sin theta
*
* warning: only the first three cells of each vector are used.
*
*****************************************************************************
*/
void
#if XVT_CC_PROTO
vVectorProduct(Vector *a,Vector *b,Vector *c)
#else
vVectorProduct(a, b, c)
Vector *a, *b, *c;
#endif
{
Vector vt;
vt.v[0] = fixmul(a->v[1],b->v[2])-fixmul(b->v[1],a->v[2]);
vt.v[1] = fixmul(b->v[0],a->v[2])-fixmul(a->v[0],b->v[2]);
vt.v[2] = fixmul(a->v[0],b->v[1])-fixmul(b->v[0],a->v[1]);
*c = vt;
}
/*****************************************************************************
*
* Function: mIdentity
*
* purpose: generates a 4x4 identity matrix.
*
* methods: a global matrix is constructed of the following form:
*
* 1 0 0 0
* 0 1 0 0
* 0 0 1 0
* 0 0 0 1
*
*****************************************************************************
*/
void
#if XVT_CC_PROTO
mIdentity(Matrix3D *m)
#else
mIdentity(m)
Matrix3D *m;
#endif
{
register int i,j;
for (i=0;i<=3;i++)
for (j=0;j<=3;j++)
if (i == j)
m->v[i][j] = 1.00;
else
m->v[i][j] = 0.00;
}
/*****************************************************************************
*
* Function: mTranspose
*
* purpose: exchanges the rows and columns of a 4x4 matrix.
*
*****************************************************************************
*/
void
#if XVT_CC_PROTO
mTranspose (Matrix3D *m)
#else
mTranspose (m)
Matrix3D *m;
#endif
{
register int i,j;
Matrix3D mt;
for (i=0;i<=3;i++)
for (j=0;j<=3;j++)
mt.v[i][j] = m->v[j][i];
memcpy ( m, &mt, sizeof(Matrix3D));
}
/*****************************************************************************
*
* Function: mScalarMultiply
*
* purpose: multiplies corresponding cells of one matrix by those of
* another giving those of a third.
*
*****************************************************************************
*/
void
#if XVT_CC_PROTO
mScalarMultiply(Fixed factor,Matrix3D *m)
#else
mScalarMultiply(factor, m)
Fixed factor;
Matrix3D *m;
#endif
{
register int i,j;
for (i=0;i<=2;i++)
for (j=0;j<=2;j++)
m->v[i][j] = fixmul(m->v[i][j], factor);
}
/*****************************************************************************
*
* Function: mAdd
*
* purpose: adds one matrix to another giving a third.
*
*****************************************************************************
*/
void
#if XVT_CC_PROTO
mAdd(Matrix3D *a,Matrix3D *b,Matrix3D *c)
#else
mAdd(a, b, c)
Matrix3D *a, *b, *c;
#endif
{
Matrix3D mt;
register int i,j;
for (i=0;i<=2;i++)
for (j=0;j<=2;j++)
mt.v[i][j] = a->v[i][j]+b->v[i][j];
*c = mt;
}
/*****************************************************************************
*
* Function: mScalarMultiply
*
* purpose: concatenates one matrix to another giving a third.
*
* methods: standard algebraic multiplication of two 4x4 matrices.
* differs from mMult4x4 in terms of speed. mMult4x4 is
* faster, but not as general in form.
*
*****************************************************************************
*/
void
#if XVT_CC_PROTO
mMultiply(Matrix3D *a,Matrix3D *b,Matrix3D *c)
#else
mMultiply(a, b, c)
Matrix3D *a, *b, *c;
#endif
{
Matrix3D mt;
register int i,j,k;
for (i=0;i<=3;i++)
for (j=0;j<=3;j++)
{ mt.v[i][j] = 0.0;
for (k=0;k<=3;k++)
mt.v[i][j] += fixmul(a->v[i][k],b->v[k][j]);
}
*c = mt;
}
/*****************************************************************************
*
* Function: mMult4x4
*
* purpose: concatenates one matrix to another giving a third.
*
* methods: standard algebraic multiplication of two 4x4 matrices.
* differs from mMultiply in terms of speed. mMult4x4 is
* faster, but not very general.
*****************************************************************************
*/
void mMult4x4(a,b,c)
register Matrix3D *a,*b,*c;
{
Matrix3D t;
t.v[0][0] =fixmul(a->v[0][0],b->v[0][0])+fixmul(a->v[0][1],b->v[1][0])
+fixmul(a->v[0][2],b->v[2][0])+fixmul(a->v[0][3],b->v[3][0]);
t.v[1][0] =fixmul(a->v[1][0],b->v[0][0])+fixmul(a->v[1][1],b->v[1][0])
+fixmul(a->v[1][2],b->v[2][0])+fixmul(a->v[1][3],b->v[3][0]);
t.v[2][0] =fixmul(a->v[2][0],b->v[0][0])+fixmul(a->v[2][1],b->v[1][0])
+fixmul(a->v[2][2],b->v[2][0])+fixmul(a->v[2][3],b->v[3][0]);
t.v[3][0] =fixmul(a->v[3][0],b->v[0][0])+fixmul(a->v[3][1],b->v[1][0])
+fixmul(a->v[3][2],b->v[2][0])+fixmul(a->v[3][3],b->v[3][0]);
t.v[0][1] =fixmul(a->v[0][0],b->v[0][1])+fixmul(a->v[0][1],b->v[1][1])
+fixmul(a->v[0][2],b->v[2][1])+fixmul(a->v[0][3],b->v[3][1]);
t.v[1][1] =fixmul(a->v[1][0],b->v[0][1])+fixmul(a->v[1][1],b->v[1][1])
+fixmul(a->v[1][2],b->v[2][1])+fixmul(a->v[1][3],b->v[3][1]);
t.v[2][1] =fixmul(a->v[2][0],b->v[0][1])+fixmul(a->v[2][1],b->v[1][1])
+fixmul(a->v[2][2],b->v[2][1])+fixmul(a->v[2][3],b->v[3][1]);
t.v[3][1] =fixmul(a->v[3][0],b->v[0][1])+fixmul(a->v[3][1],b->v[1][1])
+fixmul(a->v[3][2],b->v[2][1])+fixmul(a->v[3][3],b->v[3][1]);
t.v[0][2] =fixmul(a->v[0][0],b->v[0][2])+fixmul(a->v[0][1],b->v[1][2])
+fixmul(a->v[0][2],b->v[2][2])+fixmul(a->v[0][3],b->v[3][2]);
t.v[1][2] =fixmul(a->v[1][0],b->v[0][2])+fixmul(a->v[1][1],b->v[1][2])
+fixmul(a->v[1][2],b->v[2][2])+fixmul(a->v[1][3],b->v[3][2]);
t.v[2][2] =fixmul(a->v[2][0],b->v[0][2])+fixmul(a->v[2][1],b->v[1][2])
+fixmul(a->v[2][2],b->v[2][2])+fixmul(a->v[2][3],b->v[3][2]);
t.v[3][2] =fixmul(a->v[3][0],b->v[0][2])+fixmul(a->v[3][1],b->v[1][2])
+fixmul(a->v[3][2],b->v[2][2])+fixmul(a->v[3][3],b->v[3][2]);
t.v[0][3] =fixmul(a->v[0][0],b->v[0][3])+fixmul(a->v[0][1],b->v[1][3])
+fixmul(a->v[0][2],b->v[2][3])+fixmul(a->v[0][3],b->v[3][3]);
t.v[1][3] =fixmul(a->v[1][0],b->v[0][3])+fixmul(a->v[1][1],b->v[1][3])
+fixmul(a->v[1][2],b->v[2][3])+fixmul(a->v[1][3],b->v[3][3]);
t.v[2][3] =fixmul(a->v[2][0],b->v[0][3])+fixmul(a->v[2][1],b->v[1][3])
+fixmul(a->v[2][2],b->v[2][3])+fixmul(a->v[2][3],b->v[3][3]);
t.v[3][3] =fixmul(a->v[3][0],b->v[0][3])+fixmul(a->v[3][1],b->v[1][3])
+fixmul(a->v[3][2],b->v[2][3])+fixmul(a->v[3][3],b->v[3][3]);
*c = t;
}