{----------------------------------------------------------------------------
  9bit fixed point vector matrix math
  Copyright (c) 1994,95 by J.E. Hoffmann
  All rights reserved
 ----------------------------------------------------------------------------}
{$A+,B-,D-,E-,F-,G+,I-,L-,N-,O-,P-,Q-,R-,S-,T-,V-,X+,Y-}
unit _matrix;



interface



uses _Math;



procedure MakeMatrix(Tx,Ty,Tz,Sx,Sy,Sz :LongInt; Rx,Ry,Rz :Integer; var M :TMatrix);



implementation



{$I FX_DOOM.PAH}



procedure Translate(tx,ty,tz :LongInt; var C :TMatrix);
var
  i : Integer;
begin
  Zero(C);
  for i := 0 to 3 do C[i,i] := $200;
  C[0,3] := -tx;
  C[1,3] := -ty;
  C[2,3] := -tz
end;



procedure Scale(sx,sy,sz :LongInt; var C :TMatrix);
begin
  Zero(C);
  C[0,0] := sx;
  C[1,1] := sy;
  C[2,2] := sz;
  C[3,3] := $200;
end;



procedure Rotate3D(m : Integer;  Theta : Integer;  Var A :TMatrix);
var
  m1, m2 : Integer;
  c, s   : LongInt;
begin
  Zero(A);
  A[m-1,m-1] := $200;
  A[3,3] := $200;
  m1 := (m  Mod 3) + 1;
  m2 := (m1 Mod 3);
  m1 := m1 - 1;
  c  := FixCos(-Theta);
  s  := FixSin(-Theta);
  A[m1,m1] :=  c;
  A[m1,m2] :=  s;
  A[m2,m2] :=  c;
  A[m2,m1] := -s
end;



procedure MakeMatrix(Tx,Ty,Tz,Sx,Sy,Sz :LongInt; Rx,Ry,Rz :Integer; var M :TMatrix);
var
  S,Mx,My,Mz,T,M1,M2,M3 :TMatrix;
begin
  Scale(Sx,Sy,Sz,S);
  Rotate3D(1,Rx,Mx);
  Rotate3D(2,Ry,My);
  Rotate3D(3,Rz,Mz);
  Translate(Tx,Ty,Tz,T);
  Cross(Mx,My,M1);
  Cross(Mz,M1,M2);
  Cross(M2,T,M3);
  Cross(S,M3,M);
end;



end.