1 ;THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
2 ;SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
3 ;END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
4 ;ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
5 ;IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
6 ;SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
7 ;FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
8 ;CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
9 ;AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
10 ;COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
12 ; $Source: /cvs/cvsroot/d2x/maths/vecmata.asm,v $
15 ; $Date: 2001-10-19 09:34:02 $
17 ; Source for vector/matrix library
19 ; $Log: not supported by cvs2svn $
20 ; Revision 1.1.1.1 2001/01/19 03:29:58 bradleyb
23 ; Revision 1.1.1.1 1999/06/14 22:13:33 donut
24 ; Import of d1x 1.37 source.
26 ; Revision 1.54 1995/01/31 00:14:50 matt
27 ; Took out int3 from dotprod overflow, since it saturates now
29 ; Revision 1.53 1994/12/14 18:29:33 matt
30 ; Made dotprod overflow check stay in, and return saturated value
32 ; Revision 1.52 1994/12/14 12:34:13 matt
33 ; Disabled now-unused vector_2_matrix_norm()
35 ; Revision 1.51 1994/12/13 16:55:13 matt
36 ; Ripped out optimization from last version, which was bogus
38 ; Revision 1.50 1994/12/13 14:55:18 matt
39 ; Use quick normalize in a couple of places where it was safe to do so
41 ; Revision 1.49 1994/12/13 14:44:12 matt
42 ; Added vm_vector_2_matrix_norm()
44 ; Revision 1.48 1994/12/13 13:26:49 matt
45 ; Fixed overflow check
47 ; Revision 1.47 1994/12/03 15:39:54 matt
48 ; Gracefully handle some vector_2_matrix problems
50 ; Revision 1.46 1994/11/19 17:15:05 matt
51 ; Assemble out some code not used in DESCENT
53 ; Revision 1.45 1994/11/17 11:41:05 matt
54 ; Put handling in extract_angles_from_matrix to deal with bad matrices
56 ; Revision 1.44 1994/11/16 11:48:10 matt
57 ; Added error checking to vm_extract_angles_matrix()
59 ; Revision 1.43 1994/09/19 22:00:10 matt
60 ; Fixed register trash
62 ; Revision 1.42 1994/09/11 19:23:05 matt
63 ; Added vm_vec_normalized_dir_quick()
65 ; Revision 1.41 1994/08/14 13:28:38 matt
66 ; Put in check for zero-length vector in extract angles
68 ; Revision 1.40 1994/07/19 18:52:53 matt
69 ; Added vm_vec_normalize_quick() and vm_vec_copy_normalize_quick()
71 ; Revision 1.39 1994/06/16 18:24:22 matt
72 ; Added vm_vec_mag_quick()
74 ; Revision 1.38 1994/06/10 23:18:38 matt
75 ; Added new code for vm_vec_ang_2_matrix() which may be better, but may
78 ; Revision 1.37 1994/05/22 18:17:29 mike
79 ; Optimize vm_vec_dist_quick, using jns in place of abs_eax.
81 ; Revision 1.36 1994/05/19 12:07:04 matt
82 ; Fixed globals and macros and added a constant
84 ; Revision 1.35 1994/05/19 09:19:00 matt
85 ; Made vm_vec_normalized_dir() return mag of vector
87 ; Revision 1.34 1994/05/18 22:28:01 matt
88 ; Added function vm_vec_normalized_dir()
89 ; Added C macros IS_ZERO_VEC(), vm_vec_zero(), and vm_set_identity()
90 ; Added C global static vars vmd_zero_vector & vmd_identity_matrix
92 ; Revision 1.33 1994/05/18 21:44:16 matt
94 ; vm_extract_angles_vector()
95 ; vm_extract_angles_vector_normalized()
96 ; vm_vec_copy_normalize()
98 ; Revision 1.32 1994/05/13 12:41:51 matt
99 ; Added new function, vm_vec_dist_quick(), which does an approximation.
101 ; Revision 1.31 1994/05/04 17:41:31 mike
102 ; Comment out debug_brk on null vector.
104 ; Revision 1.30 1994/04/15 21:41:31 matt
105 ; Check for foward vector straigt up in extract angles routine
107 ; Revision 1.29 1994/03/30 15:45:05 matt
108 ; Added two functions, vm_vec_scale_add() & vm_vec_scale_add2()
110 ; Revision 1.28 1994/02/26 19:23:35 matt
111 ; Do an int3 when we get a null vector when computing surface normal
113 ; Revision 1.27 1994/02/10 18:29:45 matt
114 ; Changed 'if DEBUG_ON' to 'ifndef NDEBUG'
116 ; Revision 1.26 1994/02/10 18:28:55 matt
117 ; Fixed bugs in extract angles function
119 ; Revision 1.25 1994/01/31 22:46:07 matt
120 ; Added vm_extract_angles_matrix() function
122 ; Revision 1.24 1994/01/30 19:29:55 matt
123 ; Put in debug_brk when vm_vec_2_matrix got zero-length vector
125 ; Revision 1.23 1994/01/25 15:27:59 matt
126 ; Added debugging check for dotprod overflow
128 ; Revision 1.22 1994/01/24 11:52:59 matt
129 ; Added checking for dest==src for several functions where this is not allowed
131 ; Revision 1.21 1994/01/19 23:13:02 matt
132 ; Fixed bug in vm_vec_ang_2_matrix()
134 ; Revision 1.20 1994/01/04 12:33:43 mike
135 ; Prevent divide overflow in vm_vec_scale2
137 ; Revision 1.19 1993/12/21 19:46:26 matt
138 ; Added function vm_dist_to_plane()
140 ; Revision 1.18 1993/12/13 17:26:23 matt
141 ; Added vm_vec_dist()
143 ; Revision 1.17 1993/12/02 12:43:39 matt
144 ; New functions: vm_vec_copy_scale(), vm_vec_scale2()
146 ; Revision 1.16 1993/10/29 22:39:29 matt
147 ; Changed matrix order, making direction vectors the rows
149 ; Revision 1.15 1993/10/29 18:06:01 matt
150 ; Fixed vm_vector_2_matrix() bug when forward vector was straight down
152 ; Revision 1.14 1993/10/26 18:51:26 matt
153 ; Fixed some register trashes in vm_vec_ang_2_matrix()
155 ; Revision 1.13 1993/10/25 11:49:37 matt
156 ; Made vm_vec_delta_ang() take optional forward vector to return signed delta
158 ; Revision 1.12 1993/10/20 01:09:42 matt
159 ; Added vm_vec_delta_ang(), vm_vec_delta_ang_norm(), and vm_vec_ang_2_matrix()
161 ; Revision 1.11 1993/10/17 17:03:08 matt
162 ; vm_vector_2_matrix() now takes optional right vector
164 ; Revision 1.10 1993/10/10 18:11:42 matt
165 ; Changed angles_2_matrix so that heading & bank rotate in the
166 ; correct directions.
168 ; Revision 1.9 1993/09/30 16:17:59 matt
169 ; Fixed bug in vector_2_matrix() by adding another normalize
171 ; Revision 1.8 1993/09/29 10:51:58 matt
172 ; Fixed bad register trashes in crossprod, perp, & normal
174 ; Revision 1.7 1993/09/28 12:16:46 matt
175 ; Fixed bugs in cross product
176 ; Added func vm_vector_2_matrix()
178 ; Revision 1.6 1993/09/24 21:19:37 matt
179 ; Added vm_vec_avg() and vm_vec_avg4()
181 ; Revision 1.5 1993/09/20 18:15:07 matt
182 ; Trap zero-length vectors in vm_vec_normalize(), vm_vec_perp(), and vm_vec_normal()
184 ; Revision 1.4 1993/09/20 14:56:43 matt
185 ; Fixed bug in vm_vec_normal(), made that routine normalize the results,
186 ; and added new function vm_vec_perp().
188 ; Revision 1.3 1993/09/20 10:12:06 mike
191 ; Revision 1.2 1993/09/17 11:10:33 matt
192 ; Added vm_vec_add2() and vm_vec_sub2(), which take 2 args (dest==src0)
194 ; Revision 1.1 1993/09/16 20:10:24 matt
205 ; Cater for linux ELF compilers which don't prefix underscores...
207 %define _vmd_zero_vector vmd_zero_vector
208 %define _vmd_identity_matrix vmd_identity_matrix
210 %define _vm_vec_add vm_vec_add
211 %define _vm_vec_sub vm_vec_sub
212 %define _vm_vec_add2 vm_vec_add2
213 %define _vm_vec_sub2 vm_vec_sub2
214 %define _vm_vec_avg vm_vec_avg
215 %define _vm_vec_scale vm_vec_scale
216 %define _vm_vec_copy_scale vm_vec_copy_scale
217 %define _vm_vec_scale2 vm_vec_scale2
218 %define _vm_vec_scale_add vm_vec_scale_add
219 %define _vm_vec_scale_add2 vm_vec_scale_add2
220 %define _vm_vec_mag vm_vec_mag
221 %define _vm_vec_dist vm_vec_dist
222 %define _vm_vec_mag_quick vm_vec_mag_quick
223 %define _vm_vec_dist_quick vm_vec_dist_quick
224 %define _vm_vec_normalize vm_vec_normalize
225 %define _vm_vec_normalize_quick vm_vec_normalize_quick
226 %define _vm_vec_normalized_dir vm_vec_normalized_dir
227 %define _vm_vec_normalized_dir_quick vm_vec_normalized_dir_quick
228 %define _vm_vec_copy_normalize vm_vec_copy_normalize
229 %define _vm_vec_copy_normalize_quick vm_vec_copy_normalize_quick
230 %define _vm_vec_dotprod vm_vec_dotprod
231 %define _vm_vec_crossprod vm_vec_crossprod
232 %define _vm_vec_perp vm_vec_perp
233 %define _vm_vec_normal vm_vec_normal
234 %define _vm_vec_rotate vm_vec_rotate
235 %define _vm_vec_delta_ang vm_vec_delta_ang
236 %define _vm_vec_delta_ang_norm vm_vec_delta_ang_norm
237 %define _vm_vector_2_matrix vm_vector_2_matrix
238 %define _vm_vec_ang_2_matrix vm_vec_ang_2_matrix
239 %define _vm_angles_2_matrix vm_angles_2_matrix
240 %define _vm_transpose_matrix vm_transpose_matrix
241 %define _vm_copy_transpose_matrix vm_copy_transpose_matrix
242 %define _vm_matrix_x_matrix vm_matrix_x_matrix
247 ;temporary vectors for surface normal calculation
257 ;sine & cosine values for angles_2_matrix
265 global _vmd_zero_vector,_vmd_identity_matrix
267 ;These should never be changed!
271 _vmd_identity_matrix:
278 extern fix_sincos_asm
281 ; calling convention is arguments on stack from right to left,
282 ; eax,ecx,edx possibly destroyed, ebx,esi,edi,ebp preserved,
283 ; caller removes arguments.
290 global _vm_vec_copy_scale
291 global _vm_vec_scale2
292 global _vm_vec_scale_add
293 global _vm_vec_scale_add2
296 global _vm_vec_mag_quick
297 global _vm_vec_dist_quick
298 global _vm_vec_normalize
299 global _vm_vec_normalize_quick
300 global _vm_vec_normalized_dir
301 global _vm_vec_normalized_dir_quick
302 global _vm_vec_copy_normalize
303 global _vm_vec_copy_normalize_quick
304 global _vm_vec_dotprod
305 global _vm_vec_crossprod
307 global _vm_vec_normal
308 global _vm_angles_2_matrix
309 global _vm_vec_rotate
310 global _vm_vec_delta_ang
311 global _vm_vec_delta_ang_norm
312 global _vm_transpose_matrix
313 global _vm_copy_transpose_matrix
314 global _vm_matrix_x_matrix
315 global _vm_vector_2_matrix
316 global _vm_vec_ang_2_matrix
349 ; offsets in fixang struct (packed)
363 ;vector offsets in matrix
368 ; vec *vm_vec_add(vec *dest, vec *src1, vec *src2);
386 ; vec *vm_vec_sub(vec *dest, vec *src1, vec *src2);
404 ; vec *vm_vec_add2(vec *dest, vec *src);
418 ; vec *vm_vec_sub2(vec *dest, vec *src);
432 ; vec *vm_vec_avg(vec *dest, vec *src1, vec *src2);
451 ; vec *vm_vec_scale(vec *dest, fix scale);
469 ; vec *vm_vec_copy_scale(vec *dest, vec *src, fix scale);
490 ; vec *vm_vec_scale_add(vec *dest, vec *src1, vec *src2, fix scale);
492 ; dest=src1+src2*scale
515 ; vec *vm_vec_scale_add2(vec *dest, vec *src, fix scale);
536 ; vec *vm_vec_scale2(vec *dest, fix n, fix d);
561 ;compute magnitude of vector. takes esi=vector, returns eax=mag
588 ;compute the distance between two points. (does sub and mag)
610 call quad_sqrt_asm ; asm version, takes eax,edx
617 ;computes an approximation of the magnitude of a vector
618 ;uses dist = largest + next_largest*3/8 + smallest*3/16
642 mag_quick_eax_ebx_ecx:
647 no_swap_ab: cmp ebx,ecx
654 do_add: sar ebx,2 ; b*1/4
656 add ebx,ecx ; b*1/4 + c*1/8
657 add eax,ebx ;a + b*1/4 + c*1/8
658 sar ebx,1 ; b*1/8 + c*1/16
659 add eax,ebx ;a + b*3/4 + c*3/16
666 ;computes an approximation of the distance between two points.
667 ;uses dist = largest + next_largest*3/8 + smallest*3/16
694 jmp mag_quick_eax_ebx_ecx
697 ;return the normalized direction vector between two points
698 ;dest = normalized(end - start).
699 ;takes edi=dest, esi=endpoint, ebx=startpoint. Returns mag of dir vec
700 ;NOTE: the order of the parameters matches the vector subtraction
701 _vm_vec_normalized_dir:
733 mov ecx,eax ;mag in ecx
752 ;normalize a vector in place.
760 jmp vm_vec_copy_normalize_nopar
762 ;normalize a vector. takes edi=dest, esi=vector
763 ;returns ecx=mag of source vec
764 _vm_vec_copy_normalize:
770 vm_vec_copy_normalize_nopar:
789 mov ecx,eax ;mag in ecx
807 ;normalize a vector in place.
809 _vm_vec_normalize_quick:
814 jmp vm_vec_copy_normalize_quick_nopar
816 ;save as vm_vec_normalized_dir, but with quick sqrt
817 ;takes dest, endpoint, startpoint. Returns mag of dir vec
818 _vm_vec_normalized_dir_quick:
832 jmp vm_vec_copy_normalize_quick_nopar
836 _vm_vec_copy_normalize_quick:
842 vm_vec_copy_normalize_quick_nopar:
844 call _vm_vec_mag_quick
847 mov ecx,eax ;mag in ecx
865 ;compute dot product of two vectors. takes esi,edi=vectors, returns eax=dotprod
890 ;ifndef NDEBUG ;check for overflow
891 ;always do overflow check, and return saturated value
892 sar edx,16 ;get real sign from high word
894 cdq ;get sign of our result
895 cmp bx,dx ;same sign?
897 ;;debug_brk 'overflow in vm_vec_dotprod'
899 or ebx,ebx ;check desired sign
910 ;computes cross product of two vectors.
911 ;Note: this magnitude of the resultant vector is the
912 ;product of the magnitudes of the two source vectors. This means it is
913 ;quite easy for this routine to overflow and underflow. Be careful that
915 ; takes dest, src vectors
927 break_if e,'crossprod: dest==src0'
929 break_if e,'crossprod: dest==src1'
941 %ifndef NDEBUG ;check for overflow
943 cdq ;get sign of result
944 shr ebx,16 ;get high 16 of quad result
945 cmp dx,bx ;sign extension the same?
946 break_if ne,'overflow in crossprod'
959 %ifndef NDEBUG ;check for overflow
961 cdq ;get sign of result
962 shr ebx,16 ;get high 16 of quad result
963 cmp dx,bx ;sign extension the same?
964 break_if ne,'overflow in crossprod'
977 %ifndef NDEBUG ;check for overflow
979 cdq ;get sign of result
980 shr ebx,16 ;get high 16 of quad result
981 cmp dx,bx ;sign extension the same?
982 break_if ne,'overflow in crossprod'
986 mov eax,ebp ;return dest in eax
995 ;computes surface normal from three points. takes ebx=dest, eax,esi,edi=vecs
996 ;returns eax=dest. Result vector is normalized.
998 push dword [esp+16+00];src2
999 push dword [esp+12+04];src1
1000 push dword [esp+08+08];src0
1001 push dword [esp+04+12];dest
1002 call _vm_vec_perp ;get unnormalized
1005 call _vm_vec_normalize
1010 ;make sure a vector is reasonably sized to go into a cross product
1011 ;takes vector in esi
1012 ;trashes eax,ebx,cl,edx
1025 xor cl,cl ;init shift count
1027 test ebx,0fffc0000h ;too big
1029 check_4_down: test ebx,000f00000h
1034 check_2_down: test ebx,0fffc0000h
1041 sar dword [esi+4],cl
1042 sar dword [esi+8],cl
1046 not_too_big: test ebx,0ffff8000h
1048 check_4_up: test ebx,0fffff000h
1053 check_2_up: test ebx,0ffff8000h
1060 sal dword [esi+4],cl
1061 sal dword [esi+8],cl
1066 ; debug_brk commented out by mk on 05/04/94
1067 ;** debug_brk "null vector in check_vec"
1071 ;computes surface normal from three points. takes ebx=dest, eax,esi,edi=vecs
1072 ;returns eax=dest. Result vector is NOT normalized, but this routine does
1073 ;make an effort that cross product does not overflow or underflow
1079 mov ebx,[esp+20] ;src0
1080 mov ecx,[esp+24] ;src1
1081 mov edx,[esp+28] ;src2
1085 %assign i 0 ;tempv1=src2-src1
1092 %assign i 0 ;tempv0=src1-src0
1099 ; esi=tempv0, edi=tempv1
1100 call check_vec ;make sure reasonable value
1102 call check_vec ;make sure reasonable value
1103 ; edi=tempv0, esi=tempv1
1106 push dword [esp+16+8] ;dest
1107 call _vm_vec_crossprod
1116 ;compute a rotation matrix from three angles. takes edi=dest matrix,
1117 ;esi=angles vector. returns dest matrix.
1118 _vm_angles_2_matrix:
1122 mov edi,[esp+16];dest
1123 mov esi,[esp+20];angles
1125 ;get sines & cosines
1141 ;alternate entry point with sines & cosines already computed.
1142 ;Note all the registers already pushed.
1145 ;now calculate the 9 elements
1149 mov ecx,eax ;save sbsh
1154 mov esi,eax ;save cbch
1156 mov [edi+m1],eax ;m1=cbch+sbspsh
1158 mov eax,esi ;get cbch
1160 add eax,ecx ;add sbsh
1161 mov [edi+m8],eax ;m8=sbsh+cbchsp
1166 mov ecx,eax ;save cbsh
1171 mov esi,eax ;save sbch
1173 mov [edi+m2],ebx ;m2=cbshsp-sbch
1175 mov eax,esi ;get sbch
1177 sub eax,ecx ;sub from cbsh
1178 mov [edi+m7],eax ;m7=sbchsp-cbsh
1183 mov [edi+m3],eax ;m3=shcp
1187 mov [edi+m4],eax ;m4=sbcp
1191 mov [edi+m5],eax ;m5=cbcp
1195 mov [edi+m6],eax ;m6=-sp
1199 mov [edi+m9],eax ;m9=chcp
1218 ;create a rotation matrix from one or two vectors.
1219 ;requires forward vec, and assumes zero bank if up & right vecs==NULL
1220 ;up/right vector need not be exactly perpendicular to forward vec
1221 ;takes edi=matrix, esi=forward vec, eax=up vec, ebx=right vec.
1222 ;returns edi=matrix. trashes eax,ebx,esi
1223 ;Note: this routine loses precision as the forward vector approaches
1224 ;straigt up or down (I think)
1225 _vm_vector_2_matrix:
1230 mov esi,[esp+20];fvec
1231 mov eax,[esp+24];uvec
1232 mov ebx,[esp+28];rvec
1236 break_if z,"vm_vector_2_matrix: forward vec cannot be NULL!"
1239 or eax,eax ;up vector present?
1240 jnz near use_up_vec ;..yep
1242 or ebx,ebx ;right vector present?
1243 jz near just_forward_vec ;..nope
1245 push edi ;save matrix
1249 call _vm_vec_copy_normalize
1256 call _vm_vec_copy_normalize
1261 push dword xvec;src1
1262 push dword zvec;src0
1263 push dword yvec;dest
1264 call _vm_vec_crossprod ;get y = z cross x
1267 ;normalize new perpendicular vector
1268 push eax ;get new vec (up) in esi
1269 call _vm_vec_normalize
1274 ;now recompute right vector, in case it wasn't entirely perpendiclar
1276 push dword zvec;src1
1277 push dword yvec;src0
1278 push dword xvec;dest
1279 call _vm_vec_crossprod ;x = y cross z
1282 pop edi ;get matrix back
1284 jmp copy_into_matrix
1287 ;one of the non-forward vectors caused a problem, so ignore them and
1288 ;use just the forward vector
1293 jmp just_forward_vec_norm
1295 ;use forward and up vectors
1297 push edi ;save matrix
1301 call _vm_vec_copy_normalize
1308 call _vm_vec_copy_normalize
1313 push dword zvec;src1
1314 push dword yvec;src0
1315 push dword xvec;dest
1316 call _vm_vec_crossprod ;get x = y cross z
1319 ;normalize new perpendicular vector
1320 push eax ;get new vec (up) in esi
1321 call _vm_vec_normalize
1326 ;now recompute right vector, in case it wasn't entirely perpendiclar
1328 push dword xvec;src1
1329 push dword zvec;src0
1330 push dword yvec;dest
1331 call _vm_vec_crossprod ;y = z cross x
1334 pop edi ;get matrix back
1337 vm_copy edi+rvec,xvec
1338 vm_copy edi+uvec,yvec
1339 vm_copy edi+fvec,zvec
1353 debug_brk '0-len vec in vec_2_mat'
1356 ;only the forward vector is present
1361 call _vm_vec_copy_normalize
1365 just_forward_vec_norm:
1368 or eax,[esi+8] ;check both x & z == 0
1371 ;forward vector is straight up (or down)
1373 mov dword [edi+m1],f1_0
1374 mov eax,[esi+4] ;get y componant
1378 sub eax,edx ;make sign correct
1390 m2m_neg [edi+8],[esi+0]
1392 call _vm_vec_normalize
1399 call _vm_vec_crossprod
1410 ;multiply (dot) two vectors. assumes dest ptr in ebp, src pointers in esi,edi.
1411 ;trashes ebx,ecx,edx
1414 ;macro dest,x0,y0,z0,x1,y1,z1
1430 shrd ebx,ecx,16 ;fixup ebx
1435 ;rotate a vector by a rotation matrix
1436 ;eax=dest vector, esi=src vector, edi=matrix. returns eax=dest vector
1440 break_if e,'vec_rotate: dest==src'
1446 mov ebp,[esp+20];dest vec
1447 mov esi,[esp+24];src vec
1448 mov edi,[esp+28];matrix
1451 vv_mul 0, 0,4,8, m1,m4,m7
1452 vv_mul 4, 0,4,8, m2,m5,m8
1453 vv_mul 8, 0,4,8, m3,m6,m9
1455 mov eax,ebp ;return eax=dest
1463 ;transpose a matrix in place. Takes matrix. returns matrix
1464 _vm_transpose_matrix:
1483 ;copy and transpose a matrix. Takes edi=dest, esi=src. returns edi=dest
1484 _vm_copy_transpose_matrix:
1485 mov edx,[esp+4];dest
1519 ;mulitply 2 matrices, fill in dest. returns ptr to dest
1520 ;takes dest, src0, scr1
1521 _vm_matrix_x_matrix:
1524 break_if e,'matrix_x_matrix: dest==src0'
1526 break_if e,'matrix_x_matrix: dest==src1'
1532 mov ebp,[esp+20] ;ebp=dest
1533 mov esi,[esp+24] ;esi=src0
1534 mov edi,[esp+28] ;edi=src1
1536 ;;This code would do the same as the nine lines below it, but I'm sure
1537 ;;Mike would disapprove
1538 ;; for s0,<<m1,m2,m3>,<m4,m5,m6>,<m7,m8,m9>>
1539 ;; for s1,<<m1,m4,m7>,<m2,m5,m8>,<m3,m6,m9>>
1540 ;; vv_mul @ArgI(1,s0)+@ArgI(1,s1), s0, s1
1544 vv_mul m1, m1,m2,m3, m1,m4,m7
1545 vv_mul m2, m1,m2,m3, m2,m5,m8
1546 vv_mul m3, m1,m2,m3, m3,m6,m9
1548 vv_mul m4, m4,m5,m6, m1,m4,m7
1549 vv_mul m5, m4,m5,m6, m2,m5,m8
1550 vv_mul m6, m4,m5,m6, m3,m6,m9
1552 vv_mul m7, m7,m8,m9, m1,m4,m7
1553 vv_mul m8, m7,m8,m9, m2,m5,m8
1554 vv_mul m9, m7,m8,m9, m3,m6,m9
1556 mov eax,ebp ;eax=ptr to dest
1563 ;computes the delta angle between two vectors
1564 ;two entry points: normalized and non-normalized vectors
1565 ;takes esi,edi=vectors, eax=optional forward vector
1566 ;returns ax=delta angle
1567 ;if the forward vector is NULL, the absolute values of the delta angle
1568 ;is returned. If it is specified, the rotation around that vector from
1569 ;esi to edi is returned.
1579 call _vm_vec_normalize
1582 call _vm_vec_normalize
1586 _vm_vec_delta_ang_norm:
1595 call _vm_vec_dotprod
1597 call fix_acos_asm ;now angle in ax
1598 mov ebx,[esp+24] ;get forward vec
1601 ;do cross product to find sign of angle
1602 push eax ;save angle
1606 push dword tempv0 ;new vec
1607 call _vm_vec_crossprod
1609 push ebx ;forward vec
1610 push eax ;new vector
1611 call _vm_vec_dotprod ;eax=dotprod
1617 sub eax,edx ;make sign correct
1625 ;compute a rotation matrix from the forward vector and a rotation around
1626 ;that vector. takes esi=vector, ax=angle, edi=matrix. returns edi=dest matrix.
1628 _vm_vec_ang_2_matrix:
1641 ;extract heading & pitch from vector
1643 mov eax,[esi+4] ;m6=-sp
1649 call long_sqrt_asm ;eax=cp
1665 ;compute the distance from a point to a plane. takes the normalized normal
1666 ;of the plane (ebx), a point on the plane (edi), and the point to check (esi).
1667 ;returns distance in eax
1668 ;distance is signed, so negative dist is on the back of the plane
1673 call vm_vec_sub ;vecs in esi,edi
1675 mov esi,eax ;vector plane -> point
1682 ;extract the angles from a matrix. takes esi=matrix, fills in edi=angvec
1683 vm_extract_angles_matrix:
1684 pushm eax,ebx,edx,ecx
1686 ;extract heading & pitch from forward vector
1688 mov eax,[esi].fvec.z ;ch
1689 mov ebx,[esi].fvec.x ;sh
1691 mov ecx,ebx ;check for z==x==0
1693 jz zero_head ;zero, use head=0
1695 zero_head: mov [edi].head,ax ;save heading
1697 call fix_sincos_asm ;get back sh
1701 mov ecx,eax ;save abs(sine)
1704 cmp eax,ecx ;which is larger?
1705 pop eax ;get sine back
1708 ;sine is larger, so use it
1709 mov ebx,eax ;ebx=sine heading
1710 mov eax,[esi].m3 ;cp = shcp / sh
1713 ;cosine is larger, so use it
1716 mov eax,[esi].fvec.z ;get chcp
1717 get_cosp: fixdiv ebx ;cp = chcp / ch
1723 mov ebx,[esi].fvec.y ;fvec.y = -sp
1724 neg ebx ;ebx = y (sin)
1725 mov ecx,ebx ;check for z==x==0
1727 jz zero_pitch ;bogus vec, set p=0
1729 zero_pitch: mov [edi].pitch,ax
1734 mov eax,[esi].m4 ;m4 = sbcp
1736 mov ebx,eax ;save sb
1738 mov eax,[esi].m5 ;get cbcp
1740 mov ecx,ebx ;check for z==x==0
1742 jz zero_bank ;bogus vec, set n=0
1744 zero_bank: mov [edi].bank,ax
1747 popm eax,ebx,edx,ecx
1751 ;the cosine of pitch is zero. we're pitched straight up. say no bank
1752 cp_zero: mov [edi].bank,0 ;no bank
1754 popm eax,ebx,edx,ecx
1758 ;extract the angles from a vector, assuming zero bank.
1759 ;takes esi=vec, edi=angvec
1760 ;note versions for normalized and not normalized vector
1761 ;unnormalized version TRASHES ESI
1762 vm_extract_angles_vector:
1765 call vm_vec_copy_normalize ;ecx=mag
1770 vm_extract_angles_vector_normalized:
1773 mov [edi].bank,0 ;always zero bank
1778 mov [edi].pitch,ax ;p = asin(-y)
1783 jz zero_head2 ;check for up vector
1785 mov ebx,[esi].x ;get x again
1787 zero_head2: mov [edi].head,ax ;h = atan2(x,z) (or zero)