MM3D.INT 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. {========================================================================}
  2. {= (c) 1995-98 SwiftSoft Ronald Dittrich =}
  3. {========================================================================}
  4. {= All Rights Reserved =}
  5. {========================================================================}
  6. {= D 01099 Dresden = Tel.: +0351-8012255 =}
  7. {= Loewenstr.7a = info@swiftsoft.de =}
  8. {========================================================================}
  9. {= Actual versions on http://www.swiftsoft.de/mmtools.html =}
  10. {========================================================================}
  11. {= This code is for reference purposes only and may not be copied or =}
  12. {= distributed in any format electronic or otherwise except one copy =}
  13. {= for backup purposes. =}
  14. {= =}
  15. {= No Delphi Component Kit or Component individually or in a collection=}
  16. {= subclassed or otherwise from the code in this unit, or associated =}
  17. {= .pas, .dfm, .dcu, .asm or .obj files may be sold or distributed =}
  18. {= without express permission from SwiftSoft. =}
  19. {= =}
  20. {= For more licence informations please refer to the associated =}
  21. {= HelpFile. =}
  22. {========================================================================}
  23. {= $Date: 20.01.1998 - 18:00:00 $ =}
  24. {========================================================================}
  25. unit MM3D;
  26. {$I COMPILER.INC}
  27. {.$DEFINE _DEBUG}
  28. interface
  29. uses
  30. Classes,
  31. SysUtils,
  32. Windows,
  33. Graphics,
  34. MMObj;
  35. type
  36. FLOAT3D = Extended;
  37. LONG = LongInt;
  38. PVOID = Pointer;
  39. PMM3DColor = ^TMM3DColor;
  40. TMM3DColor = DWORD;
  41. PMM3DValue = ^TMM3DValue;
  42. TMM3DValue = FLOAT3D;
  43. PMM3DVector = ^TMM3DVector;
  44. TMM3DVector = record
  45. case Byte of
  46. 0: (x, y, z: TMM3DValue);
  47. 1: (dvX, dvY, dvZ: TMM3DValue);
  48. end;
  49. PMM3DVector4D = ^TMM3DVector4D;
  50. TMM3DVector4D = record
  51. x, y, z, w : TMM3DValue;
  52. end;
  53. PMM3DQuaternion = ^TMM3DQuaternion;
  54. TMM3DQuaternion = record
  55. s : TMM3DValue;
  56. v : TMM3DVector;
  57. end;
  58. PMM3DMatrix4D = ^TMM3DMatrix4D;
  59. TMM3DMatrix4D = array[0..3,0..3] of TMM3DValue;
  60. TMM3DProjectionType = (ptPerspective,ptOrthographic);
  61. type
  62. {-- TMM3DMatrix --------------------------------------------------------------}
  63. TMMMatrix3D = class(TMMObject)
  64. public
  65. function VectorTransform(const V: TMM3DVector): TMM3DVector4D;
  66. function InverseVectorTransform(const V: TMM3DVector): TMM3DVector4D;
  67. procedure Assign(Source: TPersistent); override;
  68. property AsMatrix : TMM3DMatrix4D read FMatrix write SetMatrix;
  69. property Cell[Row,Col: Integer] : TMM3DValue read GetCell write SetCell; default;
  70. end;
  71. {-- TMM3DVector --------------------------------------------------------------}
  72. TMMVector3D = class(TMMObject)
  73. public
  74. procedure Assign(Source: TPersistent); override;
  75. property AsVector : TMM3DVector read FVector write SetAsVector;
  76. published
  77. property X: TMM3DValue index 0 read GetComp write SetComp;
  78. property Y: TMM3DValue index 1 read GetComp write SetComp;
  79. property Z: TMM3DValue index 2 read GetComp write SetComp;
  80. end;
  81. function MM3DCreateColorRGB(red, green, blue: TMM3DValue): TMM3DColor;
  82. function MM3DCreateColorRGBA(red, green, blue, alpha: TMM3DValue): TMM3DColor;
  83. function MM3DColorGetRed(color: TMM3DColor): TMM3DValue;
  84. function MM3DColorGetGreen(color: TMM3DColor): TMM3DValue;
  85. function MM3DColorGetBlue(color: TMM3DColor): TMM3DValue;
  86. function MM3DColorGetAlpha(color: TMM3DColor): TMM3DValue;
  87. function MM3DBuildRGBColor(color: TMM3DColor): TColor;
  88. function MM3DVectorAdd(const s1, s2: TMM3DVector): TMM3DVector;
  89. function MM3DVectorSubtract(const s1, s2: TMM3DVector): TMM3DVector;
  90. function MM3DVectorReflect(const ray, normal: TMM3DVector): TMM3DVector;
  91. function MM3DVectorCrossProduct(const s1, s2: TMM3DVector): TMM3DVector;
  92. function MM3DVectorDotProduct(const s1, s2: TMM3DVector): TMM3DValue;
  93. function MM3DVectorNormalize(const v: TMM3DVector): TMM3DVector;
  94. function MM3DVectorNormalise(const v: TMM3DVector): TMM3DVector;
  95. function MM3DVectorModulus(const v: TMM3DVector): TMM3DValue;
  96. function MM3DVectorRotate(const v, axis: TMM3DVector; theta: TMM3DValue): TMM3DVector;
  97. function MM3DVectorScale(const s: TMM3DVector; factor: TMM3DValue): TMM3DVector;
  98. function MM3DVectorRandom: TMM3DVector;
  99. function MM3DQuaternionFromRotation(const v: TMM3DVector; theta: TMM3DValue): TMM3DQuaternion;
  100. //function MM3DQuaternionMultiply(const a, b: TMM3DQuaternion): TMM3DQuaternion;
  101. //function MM3DQuaternionSlerp(const a, b: TMM3DQuaternion; alpha: TMM3DValue): TMM3DQuaternion;
  102. function MM3DMatrixFromQuaternion(const q: TMM3DQuaternion): TMM3DMatrix4D;
  103. { NOTE: Not all functions listed below work properly }
  104. function MM3DVector(x, y, z: TMM3DValue): TMM3DVector;
  105. function MM3DVectorRotateQ(const v: TMM3DVector; const q: TMM3DQuaternion): TMM3DVector;
  106. function MM3DVectorTransform(const v: TMM3DVector; const Mat: TMM3DMatrix4D): TMM3DVector4D;
  107. function MM3DVector3D(const v: TMM3DVector4D): TMM3DVector;
  108. function MM3DCreateViewportMatrix(Front: TMM3DValue; Proj: TMM3DProjectionType): TMM3DMatrix4D;
  109. function MM3DMatrixMul(const M1, M2: TMM3DMatrix4D): TMM3DMatrix4D;
  110. function MM3DMatrixFromTranslation(const T: TMM3DVector): TMM3DMatrix4D;
  111. function MM3DMatrixFromScaling(const T: TMM3DVector): TMM3DMatrix4D;
  112. function MM3DMatrixFromRotation(const Axis: TMM3DVector; Angle: TMM3DValue): TMM3DMatrix4D;
  113. function MM3DMatrixFromRotationTo(const Dest: TMM3DVector): TMM3DMatrix4D;
  114. function MM3DCreateCameraMatrix(const Origin, Front, Top: TMM3DVector): TMM3DMatrix4D;
  115. function MM3DVectorInvert(const V: TMM3DVector): TMM3DVector;
  116. function MM3DMatrixInvert(const M: TMM3DMatrix4D): TMM3DMatrix4D;
  117. function MM3DMatrixDeterminant(const M: TMM3DMatrix4D): TMM3DValue;
  118. function MM3DMatrixCofactor(const M: TMM3DMatrix4D; i, j: Integer): TMM3DValue;
  119. function MM3DVectorDegToRad(const R: TMM3DVector): TMM3DVector;
  120. function MM3DVectorRadToDeg(const R: TMM3DVector): TMM3DVector;
  121. function MM3DDegToRad(D: TMM3DValue): TMM3DValue;
  122. function MM3DRadToDeg(R: TMM3DValue): TMM3DValue;
  123. function MM3DMatrixFromCameraOrientation(const Front, Top: TMM3DVector): TMM3DMatrix4D;
  124. function MM3DVectorEqual(const V1, V2: TMM3DVector): Boolean;
  125. type
  126. {-- EMM3DError ---------------------------------------------------------------}
  127. EMM3DError = class(Exception)
  128. end;
  129. const
  130. XUnit : TMM3DVector = (x:1;y:0;z:0);
  131. YUnit : TMM3DVector = (x:0;y:1;z:0);
  132. ZUnit : TMM3DVector = (x:0;y:0;z:1);
  133. ZeroVector : TMM3DVector = (x:0;y:0;z:0);
  134. Identity : TMM3DMatrix4D = (
  135. (1, 0, 0, 0),
  136. (0, 1, 0, 0),
  137. (0, 0, 1, 0),
  138. (0, 0, 0, 1)
  139. );
  140. var
  141. White : TMM3DColor;
  142. Red : TMM3DColor;
  143. Blue : TMM3DColor;
  144. Green : TMM3DColor;
  145. LightRed : TMM3DColor;
  146. LightBlue : TMM3DColor;
  147. LightGreen : TMM3DColor;
  148. Yellow : TMM3DColor;
  149. Black : TMM3DColor;
  150. implementation