1 | /********************************************************************** <BR>
|
---|
2 | This file is part of Crack dot Com's free source code release of
|
---|
3 | Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
|
---|
4 | information about compiling & licensing issues visit this URL</a>
|
---|
5 | <PRE> If that doesn't help, contact Jonathan Clark at
|
---|
6 | golgotha_source@usa.net (Subject should have "GOLG" in it)
|
---|
7 | ***********************************************************************/
|
---|
8 |
|
---|
9 | #ifndef __MATRIX_HPP_
|
---|
10 | #define __MATRIX_HPP_
|
---|
11 |
|
---|
12 | #include <string.h>
|
---|
13 | #include "math/point.hh"
|
---|
14 |
|
---|
15 | #ifdef _WINDOWS
|
---|
16 | //#define G1_WIN32_TRANSFORM_ASM
|
---|
17 | #endif
|
---|
18 |
|
---|
19 | class i4_transform_class
|
---|
20 | {
|
---|
21 | public:
|
---|
22 | i4_3d_vector x,y,z,t;
|
---|
23 |
|
---|
24 | i4_3d_vector& operator[](int pos) const
|
---|
25 | {
|
---|
26 | return (i4_3d_vector&)(((i4_3d_vector*)(&x))[pos]);
|
---|
27 | }
|
---|
28 |
|
---|
29 | i4_transform_class() {}
|
---|
30 | i4_transform_class(const i4_transform_class &m)
|
---|
31 | {
|
---|
32 | memcpy(this,&m,sizeof(i4_transform_class));
|
---|
33 | }
|
---|
34 | i4_transform_class& operator=(const i4_transform_class &m)
|
---|
35 | {
|
---|
36 | memcpy(this,&m,sizeof(i4_transform_class));
|
---|
37 |
|
---|
38 | return *this;
|
---|
39 | }
|
---|
40 |
|
---|
41 | i4_transform_class& multiply(const i4_transform_class &a, const i4_transform_class &b);
|
---|
42 | i4_transform_class& identity()
|
---|
43 | {
|
---|
44 | return uniscale(i4_float(1));
|
---|
45 | }
|
---|
46 | i4_transform_class& translate(const i4_3d_vector& offset);
|
---|
47 | i4_transform_class& mult_translate(const i4_3d_vector& offset);
|
---|
48 |
|
---|
49 | i4_transform_class& translate(i4_float tx, i4_float ty, i4_float tz)
|
---|
50 | {
|
---|
51 | return translate(i4_3d_vector(tx,ty,tz));
|
---|
52 | }
|
---|
53 | i4_transform_class& mult_translate(i4_float tx, i4_float ty, i4_float tz)
|
---|
54 | {
|
---|
55 | return mult_translate(i4_3d_vector(tx,ty,tz));
|
---|
56 | }
|
---|
57 |
|
---|
58 | i4_transform_class& rotate_x_y_z(i4_float x, i4_float y, i4_float z,i4_bool use_lookup=i4_F);
|
---|
59 |
|
---|
60 | i4_transform_class& rotate_z(i4_angle th);
|
---|
61 | i4_transform_class& mult_rotate_z(i4_angle th);
|
---|
62 | i4_transform_class& rotate_y(i4_angle th);
|
---|
63 | i4_transform_class& mult_rotate_y(i4_angle th);
|
---|
64 | i4_transform_class& rotate_x(i4_angle th);
|
---|
65 | i4_transform_class& mult_rotate_x(i4_angle th);
|
---|
66 | i4_transform_class& scale(const i4_3d_vector& scale);
|
---|
67 |
|
---|
68 | i4_transform_class& mult_scale(const i4_3d_vector& scale)
|
---|
69 | {
|
---|
70 | x *= scale.x;
|
---|
71 | y *= scale.y;
|
---|
72 | z *= scale.z;
|
---|
73 |
|
---|
74 | return *this;
|
---|
75 | }
|
---|
76 | i4_transform_class& scale(i4_float tx, i4_float ty, i4_float tz)
|
---|
77 | {
|
---|
78 | return scale(i4_3d_vector(tx,ty,tz));
|
---|
79 | }
|
---|
80 | i4_transform_class& mult_scale(i4_float tx, i4_float ty, i4_float tz)
|
---|
81 | {
|
---|
82 | return mult_scale(i4_3d_vector(tx,ty,tz));
|
---|
83 | }
|
---|
84 | i4_transform_class& uniscale(i4_float scale)
|
---|
85 | {
|
---|
86 | x.set( scale, i4_float(0), i4_float(0) );
|
---|
87 | y.set( i4_float(0), scale, i4_float(0) );
|
---|
88 | z.set( i4_float(0), i4_float(0), scale );
|
---|
89 | t.set( i4_float(0), i4_float(0), i4_float(0) );
|
---|
90 |
|
---|
91 | return *this;
|
---|
92 | }
|
---|
93 | i4_transform_class& mult_uniscale(i4_float scale)
|
---|
94 | {
|
---|
95 | x *= scale;
|
---|
96 | y *= scale;
|
---|
97 | z *= scale;
|
---|
98 |
|
---|
99 | return *this;
|
---|
100 | }
|
---|
101 |
|
---|
102 | // this version of multiple store the result in this
|
---|
103 | i4_transform_class& multiply(const i4_transform_class &b)
|
---|
104 | {
|
---|
105 | i4_transform_class tmp(*this);
|
---|
106 | multiply(tmp, b);
|
---|
107 |
|
---|
108 | return *this;
|
---|
109 | }
|
---|
110 |
|
---|
111 | i4_3d_vector& transform(const i4_3d_vector& src, i4_3d_vector& dst)
|
---|
112 | {
|
---|
113 | #ifdef G1_WIN32_TRANSFORM_ASM
|
---|
114 | trans_4x3(src, dst);
|
---|
115 | #else
|
---|
116 | dst.x = x.x*src.x + y.x*src.y + z.x*src.z + t.x;
|
---|
117 | dst.y = x.y*src.x + y.y*src.y + z.y*src.z + t.y;
|
---|
118 | dst.z = x.z*src.x + y.z*src.y + z.z*src.z + t.z;
|
---|
119 | #endif
|
---|
120 | return dst;
|
---|
121 | }
|
---|
122 |
|
---|
123 | i4_3d_vector& transform_3x3(const i4_3d_vector& src, i4_3d_vector& dst)
|
---|
124 | {
|
---|
125 | #ifdef G1_WIN32_TRANSFORM_ASM
|
---|
126 | trans_3x3(src, dst);
|
---|
127 | #else
|
---|
128 | dst.x = x.x*src.x + y.x*src.y + z.x*src.z;
|
---|
129 | dst.y = x.y*src.x + y.y*src.y + z.y*src.z;
|
---|
130 | dst.z = x.z*src.x + y.z*src.y + z.z*src.z;
|
---|
131 | #endif
|
---|
132 | return dst;
|
---|
133 | }
|
---|
134 |
|
---|
135 | i4_3d_vector& inverse_transform(const i4_3d_vector& src, i4_3d_vector& dst) const
|
---|
136 | {
|
---|
137 | i4_3d_vector tmp = src;
|
---|
138 |
|
---|
139 | tmp -= t;
|
---|
140 | dst.x = tmp.dot(x);
|
---|
141 | dst.y = tmp.dot(y);
|
---|
142 | dst.z = tmp.dot(z);
|
---|
143 |
|
---|
144 | return dst;
|
---|
145 | }
|
---|
146 |
|
---|
147 | i4_3d_vector& inverse_transform_3x3(const i4_3d_vector &src, i4_3d_vector &dst) const
|
---|
148 | {
|
---|
149 | i4_3d_vector tmp = src;
|
---|
150 |
|
---|
151 | dst.x = tmp.dot(x);
|
---|
152 | dst.y = tmp.dot(y);
|
---|
153 | dst.z = tmp.dot(z);
|
---|
154 |
|
---|
155 | return dst;
|
---|
156 | }
|
---|
157 |
|
---|
158 | #ifdef G1_WIN32_TRANSFORM_ASM
|
---|
159 | void trans_4x3(const i4_3d_vector &src_vector, i4_3d_vector &dst_vector)
|
---|
160 | {
|
---|
161 | _asm
|
---|
162 | {
|
---|
163 | mov eax, this
|
---|
164 | mov ecx, src_vector
|
---|
165 | mov edx, dst_vector
|
---|
166 |
|
---|
167 | ;optimized transformation: 34 cycles
|
---|
168 | fld dword ptr [ecx+0] ;starts & ends on cycle 0
|
---|
169 | fmul dword ptr [eax+0] ;starts on cycle 1
|
---|
170 | fld dword ptr [ecx+0] ;starts & ends on cycle 2
|
---|
171 | fmul dword ptr [eax+4] ;starts on cycle 3
|
---|
172 | fld dword ptr [ecx+0] ;starts & ends on cycle 4
|
---|
173 | fmul dword ptr [eax+8] ;starts on cycle 5
|
---|
174 | fld dword ptr [ecx+4] ;starts & ends on cycle 6
|
---|
175 | fmul dword ptr [eax+12] ;starts on cycle 7
|
---|
176 | fld dword ptr [ecx+4] ;starts & ends on cycle 8
|
---|
177 | fmul dword ptr [eax+16] ;starts on cycle 9
|
---|
178 | fld dword ptr [ecx+4] ;starts & ends on cycle 10
|
---|
179 | fmul dword ptr [eax+20] ;starts on cycle 11
|
---|
180 | fxch st(2) ;no cost
|
---|
181 | faddp st(5),st(0) ;starts on cycle 12
|
---|
182 | faddp st(3),st(0) ;starts on cycle 13
|
---|
183 | faddp st(1),st(0) ;starts on cycle 14
|
---|
184 | fld dword ptr [ecx+8] ;starts & ends on cycle 15
|
---|
185 | fmul dword ptr [eax+24] ;starts on cycle 16
|
---|
186 | fld dword ptr [ecx+8] ;starts & ends on cycle 17
|
---|
187 | fmul dword ptr [eax+28] ;starts on cycle 18
|
---|
188 | fld dword ptr [ecx+8] ;starts & ends on cycle 19
|
---|
189 | fmul dword ptr [eax+32] ;starts on cycle 20
|
---|
190 | fxch st(2) ;no cost
|
---|
191 | faddp st(5),st(0) ;starts on cycle 21
|
---|
192 | faddp st(3),st(0) ;starts on cycle 22
|
---|
193 | faddp st(1),st(0) ;starts on cycle 23
|
---|
194 | fxch st(2) ;no cost
|
---|
195 | fadd dword ptr [eax+36] ;starts on cycle 24
|
---|
196 | fxch st(1) ;starts on cycle 25
|
---|
197 | fadd dword ptr [eax+40] ;starts on cycle 26
|
---|
198 | fxch st(2) ;no cost
|
---|
199 | fadd dword ptr [eax+44] ;starts on cycle 27
|
---|
200 | fxch st(1) ;no cost
|
---|
201 | fstp dword ptr [edx+0] ;starts on cycle 28, ends on cycle 29
|
---|
202 | fstp dword ptr [edx+8] ;starts on cycle 30, ends on cycle 31
|
---|
203 | fstp dword ptr [edx+4] ;starts on cycle 32, ends on cycle 33
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | void trans_3x3(const i4_3d_vector &src_vector, i4_3d_vector &dst_vector)
|
---|
208 | {
|
---|
209 | _asm
|
---|
210 | {
|
---|
211 | mov eax, this
|
---|
212 | mov ecx, src_vector
|
---|
213 | mov edx, dst_vector
|
---|
214 |
|
---|
215 | ;optimized transformation: 34 cycles
|
---|
216 | fld dword ptr [ecx+0] ;starts & ends on cycle 0
|
---|
217 | fmul dword ptr [eax+0] ;starts on cycle 1
|
---|
218 | fld dword ptr [ecx+0] ;starts & ends on cycle 2
|
---|
219 | fmul dword ptr [eax+4] ;starts on cycle 3
|
---|
220 | fld dword ptr [ecx+0] ;starts & ends on cycle 4
|
---|
221 | fmul dword ptr [eax+8] ;starts on cycle 5
|
---|
222 | fld dword ptr [ecx+4] ;starts & ends on cycle 6
|
---|
223 | fmul dword ptr [eax+12] ;starts on cycle 7
|
---|
224 | fld dword ptr [ecx+4] ;starts & ends on cycle 8
|
---|
225 | fmul dword ptr [eax+16] ;starts on cycle 9
|
---|
226 | fld dword ptr [ecx+4] ;starts & ends on cycle 10
|
---|
227 | fmul dword ptr [eax+20] ;starts on cycle 11
|
---|
228 | fxch st(2) ;no cost
|
---|
229 | faddp st(5),st(0) ;starts on cycle 12
|
---|
230 | faddp st(3),st(0) ;starts on cycle 13
|
---|
231 | faddp st(1),st(0) ;starts on cycle 14
|
---|
232 | fld dword ptr [ecx+8] ;starts & ends on cycle 15
|
---|
233 | fmul dword ptr [eax+24] ;starts on cycle 16
|
---|
234 | fld dword ptr [ecx+8] ;starts & ends on cycle 17
|
---|
235 | fmul dword ptr [eax+28] ;starts on cycle 18
|
---|
236 | fld dword ptr [ecx+8] ;starts & ends on cycle 19
|
---|
237 | fmul dword ptr [eax+32] ;starts on cycle 20
|
---|
238 | fxch st(2) ;no cost
|
---|
239 | faddp st(5),st(0) ;starts on cycle 21
|
---|
240 | faddp st(3),st(0) ;starts on cycle 22
|
---|
241 | faddp st(1),st(0) ;starts on cycle 23
|
---|
242 | fxch st(2) ;no cost
|
---|
243 | fstp dword ptr [edx+0]
|
---|
244 | fstp dword ptr [edx+4]
|
---|
245 | fstp dword ptr [edx+8]
|
---|
246 | }
|
---|
247 | }
|
---|
248 | #endif
|
---|
249 | };
|
---|
250 |
|
---|
251 | #endif
|
---|
252 |
|
---|