unit typeMatrix; interface uses dglOpenGL; type T4DMatrix = record _11, _12, _13, _14: extended; _21, _22, _23, _24: extended; _31, _32, _33, _34: extended; _41, _42, _43, _44: extended; constructor CreateIdentity; constructor CreateTransformation(const dx, dy, dz: extended); constructor CreateScale(const sx, sy, sz: extended); constructor CreateRotateZ(const degAngle: extended); constructor CreateRoll(const degAngle: extended); constructor CreateRotateY(const degAngle: extended); constructor CreateYaw(const degAngle: extended); constructor CreateRotateX(const degAngle: extended); constructor CreatePitch(const degAngle: extended); function Transpose: T4DMatrix; //compare class operator Equal(const Left, Right: T4DMatrix): boolean; inline; class operator NotEqual(const Left, Right: T4DMatrix): boolean; inline; //operators class operator Add(const Left, Right: T4DMatrix): T4DMatrix; inline; class operator Subtract(const Left, Right: T4DMatrix): T4DMatrix; inline; class operator Multiply(const A4DMatrix: T4DMatrix; const Scalar: extended): T4DMatrix; inline; class operator Multiply(const Left, Right: T4DMatrix): T4DMatrix; inline; //cast class operator Implicit(const A4DMatrix: T4DMatrix): TGLMatrixd4; inline; class operator Implicit(const AMatrixd4: TGLMatrixd4): T4DMatrix; inline; class operator Implicit(const AScalar: extended): T4DMatrix; inline; end; implementation uses math; { T4DMatrix } class operator T4DMatrix.Implicit(const A4DMatrix: T4DMatrix): TGLMatrixd4; begin result[0, 0] := A4DMatrix._11; result[1, 0] := A4DMatrix._21; result[2, 0] := A4DMatrix._31; result[3, 0] := A4DMatrix._41; result[0, 1] := A4DMatrix._12; result[1, 1] := A4DMatrix._22; result[2, 1] := A4DMatrix._32; result[3, 1] := A4DMatrix._42; result[0, 2] := A4DMatrix._13; result[1, 2] := A4DMatrix._23; result[2, 2] := A4DMatrix._33; result[3, 2] := A4DMatrix._43; result[0, 3] := A4DMatrix._14; result[1, 3] := A4DMatrix._24; result[2, 3] := A4DMatrix._34; result[3, 3] := A4DMatrix._44; end; class operator T4DMatrix.Add(const Left, Right: T4DMatrix): T4DMatrix; begin result._11 := Left._11 + Right._11; result._12 := Left._12 + Right._12; result._13 := Left._13 + Right._13; result._14 := Left._14 + Right._14; result._21 := Left._21 + Right._21; result._22 := Left._22 + Right._22; result._23 := Left._23 + Right._23; result._24 := Left._24 + Right._24; result._31 := Left._31 + Right._31; result._32 := Left._32 + Right._32; result._33 := Left._33 + Right._33; result._34 := Left._34 + Right._34; result._41 := Left._41 + Right._41; result._42 := Left._42 + Right._42; result._43 := Left._43 + Right._43; result._44 := Left._44 + Right._44; end; constructor T4DMatrix.CreateIdentity; begin self._11 := 1; self._21 := 0; self._31 := 0; self._41 := 0; self._12 := 0; self._22 := 1; self._32 := 0; self._42 := 0; self._13 := 0; self._23 := 0; self._33 := 1; self._43 := 0; self._14 := 0; self._24 := 0; self._34 := 0; self._44 := 1; end; constructor T4DMatrix.CreatePitch(const degAngle: extended); begin CreateRotateX(degAngle); end; constructor T4DMatrix.CreateRoll(const degAngle: extended); begin CreateRotateZ(degAngle); end; constructor T4DMatrix.CreateRotateX(const degAngle: extended); begin CreateIdentity; self._22 := Cos(DegToRad(degAngle)); self._23 := -Sin(DegToRad(degAngle)); self._32 := Sin(DegToRad(degAngle)); self._33 := Cos(DegToRad(degAngle)); end; constructor T4DMatrix.CreateRotateY(const degAngle: extended); begin CreateIdentity; self._11 := Cos(DegToRad(degAngle)); self._13 := Sin(DegToRad(degAngle)); self._31 := -Sin(DegToRad(degAngle)); self._33 := Cos(DegToRad(degAngle)); end; constructor T4DMatrix.CreateRotateZ(const degAngle: extended); begin CreateIdentity; self._11 := Cos(DegToRad(degAngle)); self._12 := -Sin(DegToRad(degAngle)); self._21 := Sin(DegToRad(degAngle)); self._22 := Cos(DegToRad(degAngle)); end; constructor T4DMatrix.CreateScale(const sx, sy, sz: extended); begin CreateIdentity; self._11 := sx; self._22 := sy; self._33 := sz; end; constructor T4DMatrix.CreateTransformation(const dx, dy, dz: extended); begin CreateIdentity; self._14 := dx; self._24 := dy; self._34 := dz; end; constructor T4DMatrix.CreateYaw(const degAngle: extended); begin CreateRotateY(degAngle); end; class operator T4DMatrix.Equal(const Left, Right: T4DMatrix): boolean; begin result := (Left._11 = Right._11) and (Left._12 = Right._12) and (Left._13 = Right._13) and (Left._14 = Right._14) and (Left._21 = Right._21) and (Left._22 = Right._22) and (Left._23 = Right._23) and (Left._24 = Right._24) and (Left._31 = Right._31) and (Left._32 = Right._32) and (Left._33 = Right._33) and (Left._34 = Right._34) and (Left._41 = Right._41) and (Left._42 = Right._42) and (Left._43 = Right._43) and (Left._44 = Right._44) ; end; class operator T4DMatrix.Implicit(const AScalar: extended): T4DMatrix; begin result._11 := AScalar; result._21 := AScalar; result._31 := AScalar; result._41 := AScalar; result._12 := AScalar; result._22 := AScalar; result._32 := AScalar; result._42 := AScalar; result._13 := AScalar; result._23 := AScalar; result._33 := AScalar; result._43 := AScalar; result._14 := AScalar; result._24 := AScalar; result._34 := AScalar; result._44 := AScalar; end; class operator T4DMatrix.Implicit(const AMatrixd4: TGLMatrixd4): T4DMatrix; begin result._11 := AMatrixd4[0, 0]; result._21 := AMatrixd4[1, 0]; result._31 := AMatrixd4[2, 0]; result._41 := AMatrixd4[3, 0]; result._12 := AMatrixd4[0, 1]; result._22 := AMatrixd4[1, 1]; result._32 := AMatrixd4[2, 1]; result._42 := AMatrixd4[3, 1]; result._13 := AMatrixd4[0, 2]; result._23 := AMatrixd4[1, 2]; result._33 := AMatrixd4[2, 2]; result._43 := AMatrixd4[3, 2]; result._14 := AMatrixd4[0, 3]; result._24 := AMatrixd4[1, 3]; result._34 := AMatrixd4[2, 3]; result._44 := AMatrixd4[3, 3]; end; class operator T4DMatrix.Multiply(const Left, Right: T4DMatrix): T4DMatrix; begin result._11 := (left._11 * right._11) + (left._12 * right._21) + (left._13 * right._31) + (left._14 * right._41); result._12 := (left._11 * right._12) + (left._12 * right._22) + (left._13 * right._32) + (left._14 * right._42); result._13 := (left._11 * right._13) + (left._12 * right._23) + (left._13 * right._33) + (left._14 * right._43); result._14 := (left._11 * right._14) + (left._12 * right._24) + (left._13 * right._34) + (left._14 * right._44); result._21 := (left._21 * right._11) + (left._22 * right._21) + (left._23 * right._31) + (left._24 * right._41); result._22 := (left._21 * right._12) + (left._22 * right._22) + (left._23 * right._32) + (left._24 * right._42); result._23 := (left._21 * right._13) + (left._22 * right._23) + (left._23 * right._33) + (left._24 * right._43); result._24 := (left._21 * right._14) + (left._22 * right._24) + (left._23 * right._34) + (left._24 * right._44); result._31 := (left._31 * right._11) + (left._32 * right._21) + (left._33 * right._31) + (left._34 * right._41); result._32 := (left._31 * right._12) + (left._32 * right._22) + (left._33 * right._32) + (left._34 * right._42); result._33 := (left._31 * right._13) + (left._32 * right._23) + (left._33 * right._33) + (left._34 * right._43); result._34 := (left._31 * right._14) + (left._32 * right._24) + (left._33 * right._34) + (left._34 * right._44); result._41 := (left._41 * right._11) + (left._42 * right._21) + (left._43 * right._31) + (left._44 * right._41); result._42 := (left._41 * right._12) + (left._42 * right._22) + (left._43 * right._32) + (left._44 * right._42); result._43 := (left._41 * right._13) + (left._42 * right._23) + (left._43 * right._33) + (left._44 * right._43); result._44 := (left._41 * right._14) + (left._42 * right._24) + (left._43 * right._34) + (left._44 * right._44); end; class operator T4DMatrix.Multiply(const A4DMatrix: T4DMatrix; const Scalar: extended): T4DMatrix; begin result._11 := A4DMatrix._11 * Scalar; result._12 := A4DMatrix._12 * Scalar; result._13 := A4DMatrix._13 * Scalar; result._14 := A4DMatrix._14 * Scalar; result._21 := A4DMatrix._21 * Scalar; result._22 := A4DMatrix._22 * Scalar; result._23 := A4DMatrix._23 * Scalar; result._24 := A4DMatrix._24 * Scalar; result._31 := A4DMatrix._31 * Scalar; result._32 := A4DMatrix._32 * Scalar; result._33 := A4DMatrix._33 * Scalar; result._34 := A4DMatrix._34 * Scalar; result._41 := A4DMatrix._41 * Scalar; result._42 := A4DMatrix._42 * Scalar; result._43 := A4DMatrix._43 * Scalar; result._44 := A4DMatrix._44 * Scalar; end; class operator T4DMatrix.NotEqual(const Left, Right: T4DMatrix): boolean; begin result := not (left = right); end; class operator T4DMatrix.Subtract(const Left, Right: T4DMatrix): T4DMatrix; begin result._11 := Left._11 - Right._11; result._12 := Left._12 - Right._12; result._13 := Left._13 - Right._13; result._14 := Left._14 - Right._14; result._21 := Left._21 - Right._21; result._22 := Left._22 - Right._22; result._23 := Left._23 - Right._23; result._24 := Left._24 - Right._24; result._31 := Left._31 - Right._31; result._32 := Left._32 - Right._32; result._33 := Left._33 - Right._33; result._34 := Left._34 - Right._34; result._41 := Left._41 - Right._41; result._42 := Left._42 - Right._42; result._43 := Left._43 - Right._43; result._44 := Left._44 - Right._44; end; function T4DMatrix.Transpose: T4DMatrix; begin result._11 := _11; result._21 := _12; result._31 := _13; result._41 := _14; result._12 := _21; result._22 := _22; result._32 := _23; result._42 := _24; result._13 := _31; result._23 := _32; result._33 := _33; result._43 := _34; result._14 := _41; result._24 := _42; result._34 := _43; result._44 := _44; end; end.