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.
13 ; Source for vector/matrix library
22 ; Cater for ELF compilers which don't prefix underscores...
24 %define _vmd_zero_vector vmd_zero_vector
25 %define _vmd_identity_matrix vmd_identity_matrix
27 %define _vm_vec_add vm_vec_add
28 %define _vm_vec_sub vm_vec_sub
29 %define _vm_vec_add2 vm_vec_add2
30 %define _vm_vec_sub2 vm_vec_sub2
31 %define _vm_vec_avg vm_vec_avg
32 %define _vm_vec_scale vm_vec_scale
33 %define _vm_vec_copy_scale vm_vec_copy_scale
34 %define _vm_vec_scale2 vm_vec_scale2
35 %define _vm_vec_scale_add vm_vec_scale_add
36 %define _vm_vec_scale_add2 vm_vec_scale_add2
37 %define _vm_vec_mag vm_vec_mag
38 %define _vm_vec_dist vm_vec_dist
39 %define _vm_vec_mag_quick vm_vec_mag_quick
40 %define _vm_vec_dist_quick vm_vec_dist_quick
41 %define _vm_vec_normalize vm_vec_normalize
42 %define _vm_vec_normalize_quick vm_vec_normalize_quick
43 %define _vm_vec_normalized_dir vm_vec_normalized_dir
44 %define _vm_vec_normalized_dir_quick vm_vec_normalized_dir_quick
45 %define _vm_vec_copy_normalize vm_vec_copy_normalize
46 %define _vm_vec_copy_normalize_quick vm_vec_copy_normalize_quick
47 %define _vm_vec_dotprod vm_vec_dotprod
48 %define _vm_vec_crossprod vm_vec_crossprod
49 %define _vm_vec_perp vm_vec_perp
50 %define _vm_vec_normal vm_vec_normal
51 %define _vm_vec_rotate vm_vec_rotate
52 %define _vm_vec_delta_ang vm_vec_delta_ang
53 %define _vm_vec_delta_ang_norm vm_vec_delta_ang_norm
54 %define _vm_vector_2_matrix vm_vector_2_matrix
55 %define _vm_vec_ang_2_matrix vm_vec_ang_2_matrix
56 %define _vm_angles_2_matrix vm_angles_2_matrix
57 %define _vm_transpose_matrix vm_transpose_matrix
58 %define _vm_copy_transpose_matrix vm_copy_transpose_matrix
59 %define _vm_matrix_x_matrix vm_matrix_x_matrix
64 ;temporary vectors for surface normal calculation
74 ;sine & cosine values for angles_2_matrix
82 global _vmd_zero_vector,_vmd_identity_matrix
84 ;These should never be changed!
98 ; calling convention is arguments on stack from right to left,
99 ; eax,ecx,edx possibly destroyed, ebx,esi,edi,ebp preserved,
100 ; caller removes arguments.
107 global _vm_vec_copy_scale
108 global _vm_vec_scale2
109 global _vm_vec_scale_add
110 global _vm_vec_scale_add2
113 global _vm_vec_mag_quick
114 global _vm_vec_dist_quick
115 global _vm_vec_normalize
116 global _vm_vec_normalize_quick
117 global _vm_vec_normalized_dir
118 global _vm_vec_normalized_dir_quick
119 global _vm_vec_copy_normalize
120 global _vm_vec_copy_normalize_quick
121 global _vm_vec_dotprod
122 global _vm_vec_crossprod
124 global _vm_vec_normal
125 global _vm_angles_2_matrix
126 global _vm_vec_rotate
127 global _vm_vec_delta_ang
128 global _vm_vec_delta_ang_norm
129 global _vm_transpose_matrix
130 global _vm_copy_transpose_matrix
131 global _vm_matrix_x_matrix
132 global _vm_vector_2_matrix
133 global _vm_vec_ang_2_matrix
166 ; offsets in fixang struct (packed)
180 ;vector offsets in matrix
185 ; vec *vm_vec_add(vec *dest, vec *src1, vec *src2);
203 ; vec *vm_vec_sub(vec *dest, vec *src1, vec *src2);
221 ; vec *vm_vec_add2(vec *dest, vec *src);
235 ; vec *vm_vec_sub2(vec *dest, vec *src);
249 ; vec *vm_vec_avg(vec *dest, vec *src1, vec *src2);
268 ; vec *vm_vec_scale(vec *dest, fix scale);
286 ; vec *vm_vec_copy_scale(vec *dest, vec *src, fix scale);
307 ; vec *vm_vec_scale_add(vec *dest, vec *src1, vec *src2, fix scale);
309 ; dest=src1+src2*scale
332 ; vec *vm_vec_scale_add2(vec *dest, vec *src, fix scale);
353 ; vec *vm_vec_scale2(vec *dest, fix n, fix d);
378 ;compute magnitude of vector. takes esi=vector, returns eax=mag
405 ;compute the distance between two points. (does sub and mag)
427 call quad_sqrt_asm ; asm version, takes eax,edx
434 ;computes an approximation of the magnitude of a vector
435 ;uses dist = largest + next_largest*3/8 + smallest*3/16
459 mag_quick_eax_ebx_ecx:
464 no_swap_ab: cmp ebx,ecx
471 do_add: sar ebx,2 ; b*1/4
473 add ebx,ecx ; b*1/4 + c*1/8
474 add eax,ebx ;a + b*1/4 + c*1/8
475 sar ebx,1 ; b*1/8 + c*1/16
476 add eax,ebx ;a + b*3/4 + c*3/16
483 ;computes an approximation of the distance between two points.
484 ;uses dist = largest + next_largest*3/8 + smallest*3/16
511 jmp mag_quick_eax_ebx_ecx
514 ;return the normalized direction vector between two points
515 ;dest = normalized(end - start).
516 ;takes edi=dest, esi=endpoint, ebx=startpoint. Returns mag of dir vec
517 ;NOTE: the order of the parameters matches the vector subtraction
518 _vm_vec_normalized_dir:
550 mov ecx,eax ;mag in ecx
569 ;normalize a vector in place.
577 jmp vm_vec_copy_normalize_nopar
579 ;normalize a vector. takes edi=dest, esi=vector
580 ;returns ecx=mag of source vec
581 _vm_vec_copy_normalize:
587 vm_vec_copy_normalize_nopar:
606 mov ecx,eax ;mag in ecx
624 ;normalize a vector in place.
626 _vm_vec_normalize_quick:
631 jmp vm_vec_copy_normalize_quick_nopar
633 ;save as vm_vec_normalized_dir, but with quick sqrt
634 ;takes dest, endpoint, startpoint. Returns mag of dir vec
635 _vm_vec_normalized_dir_quick:
649 jmp vm_vec_copy_normalize_quick_nopar
653 _vm_vec_copy_normalize_quick:
659 vm_vec_copy_normalize_quick_nopar:
661 call _vm_vec_mag_quick
664 mov ecx,eax ;mag in ecx
682 ;compute dot product of two vectors. takes esi,edi=vectors, returns eax=dotprod
707 ;ifndef NDEBUG ;check for overflow
708 ;always do overflow check, and return saturated value
709 sar edx,16 ;get real sign from high word
711 cdq ;get sign of our result
712 cmp bx,dx ;same sign?
714 ;;debug_brk 'overflow in vm_vec_dotprod'
716 or ebx,ebx ;check desired sign
727 ;computes cross product of two vectors.
728 ;Note: this magnitude of the resultant vector is the
729 ;product of the magnitudes of the two source vectors. This means it is
730 ;quite easy for this routine to overflow and underflow. Be careful that
732 ; takes dest, src vectors
744 break_if e,'crossprod: dest==src0'
746 break_if e,'crossprod: dest==src1'
758 %ifndef NDEBUG ;check for overflow
760 cdq ;get sign of result
761 shr ebx,16 ;get high 16 of quad result
762 cmp dx,bx ;sign extension the same?
763 break_if ne,'overflow in crossprod'
776 %ifndef NDEBUG ;check for overflow
778 cdq ;get sign of result
779 shr ebx,16 ;get high 16 of quad result
780 cmp dx,bx ;sign extension the same?
781 break_if ne,'overflow in crossprod'
794 %ifndef NDEBUG ;check for overflow
796 cdq ;get sign of result
797 shr ebx,16 ;get high 16 of quad result
798 cmp dx,bx ;sign extension the same?
799 break_if ne,'overflow in crossprod'
803 mov eax,ebp ;return dest in eax
812 ;computes surface normal from three points. takes ebx=dest, eax,esi,edi=vecs
813 ;returns eax=dest. Result vector is normalized.
815 push dword [esp+16+00];src2
816 push dword [esp+12+04];src1
817 push dword [esp+08+08];src0
818 push dword [esp+04+12];dest
819 call _vm_vec_perp ;get unnormalized
822 call _vm_vec_normalize
827 ;make sure a vector is reasonably sized to go into a cross product
829 ;trashes eax,ebx,cl,edx
842 xor cl,cl ;init shift count
844 test ebx,0fffc0000h ;too big
846 check_4_down: test ebx,000f00000h
851 check_2_down: test ebx,0fffc0000h
863 not_too_big: test ebx,0ffff8000h
865 check_4_up: test ebx,0fffff000h
870 check_2_up: test ebx,0ffff8000h
883 ; debug_brk commented out by mk on 05/04/94
884 ;** debug_brk "null vector in check_vec"
888 ;computes surface normal from three points. takes ebx=dest, eax,esi,edi=vecs
889 ;returns eax=dest. Result vector is NOT normalized, but this routine does
890 ;make an effort that cross product does not overflow or underflow
896 mov ebx,[esp+20] ;src0
897 mov ecx,[esp+24] ;src1
898 mov edx,[esp+28] ;src2
902 %assign i 0 ;tempv1=src2-src1
909 %assign i 0 ;tempv0=src1-src0
916 ; esi=tempv0, edi=tempv1
917 call check_vec ;make sure reasonable value
919 call check_vec ;make sure reasonable value
920 ; edi=tempv0, esi=tempv1
923 push dword [esp+16+8] ;dest
924 call _vm_vec_crossprod
933 ;compute a rotation matrix from three angles. takes edi=dest matrix,
934 ;esi=angles vector. returns dest matrix.
939 mov edi,[esp+16];dest
940 mov esi,[esp+20];angles
958 ;alternate entry point with sines & cosines already computed.
959 ;Note all the registers already pushed.
962 ;now calculate the 9 elements
966 mov ecx,eax ;save sbsh
971 mov esi,eax ;save cbch
973 mov [edi+m1],eax ;m1=cbch+sbspsh
975 mov eax,esi ;get cbch
977 add eax,ecx ;add sbsh
978 mov [edi+m8],eax ;m8=sbsh+cbchsp
983 mov ecx,eax ;save cbsh
988 mov esi,eax ;save sbch
990 mov [edi+m2],ebx ;m2=cbshsp-sbch
992 mov eax,esi ;get sbch
994 sub eax,ecx ;sub from cbsh
995 mov [edi+m7],eax ;m7=sbchsp-cbsh
1000 mov [edi+m3],eax ;m3=shcp
1004 mov [edi+m4],eax ;m4=sbcp
1008 mov [edi+m5],eax ;m5=cbcp
1012 mov [edi+m6],eax ;m6=-sp
1016 mov [edi+m9],eax ;m9=chcp
1035 ;create a rotation matrix from one or two vectors.
1036 ;requires forward vec, and assumes zero bank if up & right vecs==NULL
1037 ;up/right vector need not be exactly perpendicular to forward vec
1038 ;takes edi=matrix, esi=forward vec, eax=up vec, ebx=right vec.
1039 ;returns edi=matrix. trashes eax,ebx,esi
1040 ;Note: this routine loses precision as the forward vector approaches
1041 ;straigt up or down (I think)
1042 _vm_vector_2_matrix:
1047 mov esi,[esp+20];fvec
1048 mov eax,[esp+24];uvec
1049 mov ebx,[esp+28];rvec
1053 break_if z,"vm_vector_2_matrix: forward vec cannot be NULL!"
1056 or eax,eax ;up vector present?
1057 jnz near use_up_vec ;..yep
1059 or ebx,ebx ;right vector present?
1060 jz near just_forward_vec ;..nope
1062 push edi ;save matrix
1066 call _vm_vec_copy_normalize
1073 call _vm_vec_copy_normalize
1078 push dword xvec;src1
1079 push dword zvec;src0
1080 push dword yvec;dest
1081 call _vm_vec_crossprod ;get y = z cross x
1084 ;normalize new perpendicular vector
1085 push eax ;get new vec (up) in esi
1086 call _vm_vec_normalize
1091 ;now recompute right vector, in case it wasn't entirely perpendiclar
1093 push dword zvec;src1
1094 push dword yvec;src0
1095 push dword xvec;dest
1096 call _vm_vec_crossprod ;x = y cross z
1099 pop edi ;get matrix back
1101 jmp copy_into_matrix
1104 ;one of the non-forward vectors caused a problem, so ignore them and
1105 ;use just the forward vector
1110 jmp just_forward_vec_norm
1112 ;use forward and up vectors
1114 push edi ;save matrix
1118 call _vm_vec_copy_normalize
1125 call _vm_vec_copy_normalize
1130 push dword zvec;src1
1131 push dword yvec;src0
1132 push dword xvec;dest
1133 call _vm_vec_crossprod ;get x = y cross z
1136 ;normalize new perpendicular vector
1137 push eax ;get new vec (up) in esi
1138 call _vm_vec_normalize
1143 ;now recompute right vector, in case it wasn't entirely perpendiclar
1145 push dword xvec;src1
1146 push dword zvec;src0
1147 push dword yvec;dest
1148 call _vm_vec_crossprod ;y = z cross x
1151 pop edi ;get matrix back
1154 vm_copy edi+rvec,xvec
1155 vm_copy edi+uvec,yvec
1156 vm_copy edi+fvec,zvec
1170 debug_brk '0-len vec in vec_2_mat'
1173 ;only the forward vector is present
1178 call _vm_vec_copy_normalize
1182 just_forward_vec_norm:
1185 or eax,[esi+8] ;check both x & z == 0
1188 ;forward vector is straight up (or down)
1190 mov dword [edi+m1],f1_0
1191 mov eax,[esi+4] ;get y componant
1195 sub eax,edx ;make sign correct
1207 m2m_neg [edi+8],[esi+0]
1209 call _vm_vec_normalize
1216 call _vm_vec_crossprod
1227 ;multiply (dot) two vectors. assumes dest ptr in ebp, src pointers in esi,edi.
1228 ;trashes ebx,ecx,edx
1231 ;macro dest,x0,y0,z0,x1,y1,z1
1247 shrd ebx,ecx,16 ;fixup ebx
1252 ;rotate a vector by a rotation matrix
1253 ;eax=dest vector, esi=src vector, edi=matrix. returns eax=dest vector
1257 break_if e,'vec_rotate: dest==src'
1263 mov ebp,[esp+20];dest vec
1264 mov esi,[esp+24];src vec
1265 mov edi,[esp+28];matrix
1268 vv_mul 0, 0,4,8, m1,m4,m7
1269 vv_mul 4, 0,4,8, m2,m5,m8
1270 vv_mul 8, 0,4,8, m3,m6,m9
1272 mov eax,ebp ;return eax=dest
1280 ;transpose a matrix in place. Takes matrix. returns matrix
1281 _vm_transpose_matrix:
1300 ;copy and transpose a matrix. Takes edi=dest, esi=src. returns edi=dest
1301 _vm_copy_transpose_matrix:
1302 mov edx,[esp+4];dest
1336 ;mulitply 2 matrices, fill in dest. returns ptr to dest
1337 ;takes dest, src0, scr1
1338 _vm_matrix_x_matrix:
1341 break_if e,'matrix_x_matrix: dest==src0'
1343 break_if e,'matrix_x_matrix: dest==src1'
1349 mov ebp,[esp+20] ;ebp=dest
1350 mov esi,[esp+24] ;esi=src0
1351 mov edi,[esp+28] ;edi=src1
1353 ;;This code would do the same as the nine lines below it, but I'm sure
1354 ;;Mike would disapprove
1355 ;; for s0,<<m1,m2,m3>,<m4,m5,m6>,<m7,m8,m9>>
1356 ;; for s1,<<m1,m4,m7>,<m2,m5,m8>,<m3,m6,m9>>
1357 ;; vv_mul @ArgI(1,s0)+@ArgI(1,s1), s0, s1
1361 vv_mul m1, m1,m2,m3, m1,m4,m7
1362 vv_mul m2, m1,m2,m3, m2,m5,m8
1363 vv_mul m3, m1,m2,m3, m3,m6,m9
1365 vv_mul m4, m4,m5,m6, m1,m4,m7
1366 vv_mul m5, m4,m5,m6, m2,m5,m8
1367 vv_mul m6, m4,m5,m6, m3,m6,m9
1369 vv_mul m7, m7,m8,m9, m1,m4,m7
1370 vv_mul m8, m7,m8,m9, m2,m5,m8
1371 vv_mul m9, m7,m8,m9, m3,m6,m9
1373 mov eax,ebp ;eax=ptr to dest
1380 ;computes the delta angle between two vectors
1381 ;two entry points: normalized and non-normalized vectors
1382 ;takes esi,edi=vectors, eax=optional forward vector
1383 ;returns ax=delta angle
1384 ;if the forward vector is NULL, the absolute values of the delta angle
1385 ;is returned. If it is specified, the rotation around that vector from
1386 ;esi to edi is returned.
1396 call _vm_vec_normalize
1399 call _vm_vec_normalize
1403 _vm_vec_delta_ang_norm:
1412 call _vm_vec_dotprod
1414 call fix_acos_asm ;now angle in ax
1415 mov ebx,[esp+24] ;get forward vec
1418 ;do cross product to find sign of angle
1419 push eax ;save angle
1423 push dword tempv0 ;new vec
1424 call _vm_vec_crossprod
1426 push ebx ;forward vec
1427 push eax ;new vector
1428 call _vm_vec_dotprod ;eax=dotprod
1434 sub eax,edx ;make sign correct
1442 ;compute a rotation matrix from the forward vector and a rotation around
1443 ;that vector. takes esi=vector, ax=angle, edi=matrix. returns edi=dest matrix.
1445 _vm_vec_ang_2_matrix:
1458 ;extract heading & pitch from vector
1460 mov eax,[esi+4] ;m6=-sp
1466 call long_sqrt_asm ;eax=cp
1482 ;compute the distance from a point to a plane. takes the normalized normal
1483 ;of the plane (ebx), a point on the plane (edi), and the point to check (esi).
1484 ;returns distance in eax
1485 ;distance is signed, so negative dist is on the back of the plane
1490 call vm_vec_sub ;vecs in esi,edi
1492 mov esi,eax ;vector plane -> point
1499 ;extract the angles from a matrix. takes esi=matrix, fills in edi=angvec
1500 vm_extract_angles_matrix:
1501 pushm eax,ebx,edx,ecx
1503 ;extract heading & pitch from forward vector
1505 mov eax,[esi].fvec.z ;ch
1506 mov ebx,[esi].fvec.x ;sh
1508 mov ecx,ebx ;check for z==x==0
1510 jz zero_head ;zero, use head=0
1512 zero_head: mov [edi].head,ax ;save heading
1514 call fix_sincos_asm ;get back sh
1518 mov ecx,eax ;save abs(sine)
1521 cmp eax,ecx ;which is larger?
1522 pop eax ;get sine back
1525 ;sine is larger, so use it
1526 mov ebx,eax ;ebx=sine heading
1527 mov eax,[esi].m3 ;cp = shcp / sh
1530 ;cosine is larger, so use it
1533 mov eax,[esi].fvec.z ;get chcp
1534 get_cosp: fixdiv ebx ;cp = chcp / ch
1540 mov ebx,[esi].fvec.y ;fvec.y = -sp
1541 neg ebx ;ebx = y (sin)
1542 mov ecx,ebx ;check for z==x==0
1544 jz zero_pitch ;bogus vec, set p=0
1546 zero_pitch: mov [edi].pitch,ax
1551 mov eax,[esi].m4 ;m4 = sbcp
1553 mov ebx,eax ;save sb
1555 mov eax,[esi].m5 ;get cbcp
1557 mov ecx,ebx ;check for z==x==0
1559 jz zero_bank ;bogus vec, set n=0
1561 zero_bank: mov [edi].bank,ax
1564 popm eax,ebx,edx,ecx
1568 ;the cosine of pitch is zero. we're pitched straight up. say no bank
1569 cp_zero: mov [edi].bank,0 ;no bank
1571 popm eax,ebx,edx,ecx
1575 ;extract the angles from a vector, assuming zero bank.
1576 ;takes esi=vec, edi=angvec
1577 ;note versions for normalized and not normalized vector
1578 ;unnormalized version TRASHES ESI
1579 vm_extract_angles_vector:
1582 call vm_vec_copy_normalize ;ecx=mag
1587 vm_extract_angles_vector_normalized:
1590 mov [edi].bank,0 ;always zero bank
1595 mov [edi].pitch,ax ;p = asin(-y)
1600 jz zero_head2 ;check for up vector
1602 mov ebx,[esi].x ;get x again
1604 zero_head2: mov [edi].head,ax ;h = atan2(x,z) (or zero)