| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- {========================================================================}
- {= (c) 1995-98 SwiftSoft Ronald Dittrich =}
- {========================================================================}
- {= All Rights Reserved =}
- {========================================================================}
- {= D 01099 Dresden = Tel.: +0351-8012255 =}
- {= Loewenstr.7a = info@swiftsoft.de =}
- {========================================================================}
- {= Actual versions on http://www.swiftsoft.de/mmtools.html =}
- {========================================================================}
- {= This code is for reference purposes only and may not be copied or =}
- {= distributed in any format electronic or otherwise except one copy =}
- {= for backup purposes. =}
- {= =}
- {= No Delphi Component Kit or Component individually or in a collection=}
- {= subclassed or otherwise from the code in this unit, or associated =}
- {= .pas, .dfm, .dcu, .asm or .obj files may be sold or distributed =}
- {= without express permission from SwiftSoft. =}
- {= =}
- {= For more licence informations please refer to the associated =}
- {= HelpFile. =}
- {========================================================================}
- {= $Date: 20.01.1998 - 18:00:00 $ =}
- {========================================================================}
- unit MM3D;
- {$I COMPILER.INC}
- {.$DEFINE _DEBUG}
- interface
- uses
- Classes,
- SysUtils,
- Windows,
- Graphics,
- MMObj;
- type
- FLOAT3D = Extended;
- LONG = LongInt;
- PVOID = Pointer;
- PMM3DColor = ^TMM3DColor;
- TMM3DColor = DWORD;
- PMM3DValue = ^TMM3DValue;
- TMM3DValue = FLOAT3D;
- PMM3DVector = ^TMM3DVector;
- TMM3DVector = record
- case Byte of
- 0: (x, y, z: TMM3DValue);
- 1: (dvX, dvY, dvZ: TMM3DValue);
- end;
- PMM3DVector4D = ^TMM3DVector4D;
- TMM3DVector4D = record
- x, y, z, w : TMM3DValue;
- end;
- PMM3DQuaternion = ^TMM3DQuaternion;
- TMM3DQuaternion = record
- s : TMM3DValue;
- v : TMM3DVector;
- end;
- PMM3DMatrix4D = ^TMM3DMatrix4D;
- TMM3DMatrix4D = array[0..3,0..3] of TMM3DValue;
- TMM3DProjectionType = (ptPerspective,ptOrthographic);
- type
- {-- TMM3DMatrix --------------------------------------------------------------}
- TMMMatrix3D = class(TMMObject)
- public
- function VectorTransform(const V: TMM3DVector): TMM3DVector4D;
- function InverseVectorTransform(const V: TMM3DVector): TMM3DVector4D;
- procedure Assign(Source: TPersistent); override;
- property AsMatrix : TMM3DMatrix4D read FMatrix write SetMatrix;
- property Cell[Row,Col: Integer] : TMM3DValue read GetCell write SetCell; default;
- end;
- {-- TMM3DVector --------------------------------------------------------------}
- TMMVector3D = class(TMMObject)
- public
- procedure Assign(Source: TPersistent); override;
- property AsVector : TMM3DVector read FVector write SetAsVector;
- published
- property X: TMM3DValue index 0 read GetComp write SetComp;
- property Y: TMM3DValue index 1 read GetComp write SetComp;
- property Z: TMM3DValue index 2 read GetComp write SetComp;
- end;
- function MM3DCreateColorRGB(red, green, blue: TMM3DValue): TMM3DColor;
- function MM3DCreateColorRGBA(red, green, blue, alpha: TMM3DValue): TMM3DColor;
- function MM3DColorGetRed(color: TMM3DColor): TMM3DValue;
- function MM3DColorGetGreen(color: TMM3DColor): TMM3DValue;
- function MM3DColorGetBlue(color: TMM3DColor): TMM3DValue;
- function MM3DColorGetAlpha(color: TMM3DColor): TMM3DValue;
- function MM3DBuildRGBColor(color: TMM3DColor): TColor;
- function MM3DVectorAdd(const s1, s2: TMM3DVector): TMM3DVector;
- function MM3DVectorSubtract(const s1, s2: TMM3DVector): TMM3DVector;
- function MM3DVectorReflect(const ray, normal: TMM3DVector): TMM3DVector;
- function MM3DVectorCrossProduct(const s1, s2: TMM3DVector): TMM3DVector;
- function MM3DVectorDotProduct(const s1, s2: TMM3DVector): TMM3DValue;
- function MM3DVectorNormalize(const v: TMM3DVector): TMM3DVector;
- function MM3DVectorNormalise(const v: TMM3DVector): TMM3DVector;
- function MM3DVectorModulus(const v: TMM3DVector): TMM3DValue;
- function MM3DVectorRotate(const v, axis: TMM3DVector; theta: TMM3DValue): TMM3DVector;
- function MM3DVectorScale(const s: TMM3DVector; factor: TMM3DValue): TMM3DVector;
- function MM3DVectorRandom: TMM3DVector;
- function MM3DQuaternionFromRotation(const v: TMM3DVector; theta: TMM3DValue): TMM3DQuaternion;
- //function MM3DQuaternionMultiply(const a, b: TMM3DQuaternion): TMM3DQuaternion;
- //function MM3DQuaternionSlerp(const a, b: TMM3DQuaternion; alpha: TMM3DValue): TMM3DQuaternion;
- function MM3DMatrixFromQuaternion(const q: TMM3DQuaternion): TMM3DMatrix4D;
- { NOTE: Not all functions listed below work properly }
- function MM3DVector(x, y, z: TMM3DValue): TMM3DVector;
- function MM3DVectorRotateQ(const v: TMM3DVector; const q: TMM3DQuaternion): TMM3DVector;
- function MM3DVectorTransform(const v: TMM3DVector; const Mat: TMM3DMatrix4D): TMM3DVector4D;
- function MM3DVector3D(const v: TMM3DVector4D): TMM3DVector;
- function MM3DCreateViewportMatrix(Front: TMM3DValue; Proj: TMM3DProjectionType): TMM3DMatrix4D;
- function MM3DMatrixMul(const M1, M2: TMM3DMatrix4D): TMM3DMatrix4D;
- function MM3DMatrixFromTranslation(const T: TMM3DVector): TMM3DMatrix4D;
- function MM3DMatrixFromScaling(const T: TMM3DVector): TMM3DMatrix4D;
- function MM3DMatrixFromRotation(const Axis: TMM3DVector; Angle: TMM3DValue): TMM3DMatrix4D;
- function MM3DMatrixFromRotationTo(const Dest: TMM3DVector): TMM3DMatrix4D;
- function MM3DCreateCameraMatrix(const Origin, Front, Top: TMM3DVector): TMM3DMatrix4D;
- function MM3DVectorInvert(const V: TMM3DVector): TMM3DVector;
- function MM3DMatrixInvert(const M: TMM3DMatrix4D): TMM3DMatrix4D;
- function MM3DMatrixDeterminant(const M: TMM3DMatrix4D): TMM3DValue;
- function MM3DMatrixCofactor(const M: TMM3DMatrix4D; i, j: Integer): TMM3DValue;
- function MM3DVectorDegToRad(const R: TMM3DVector): TMM3DVector;
- function MM3DVectorRadToDeg(const R: TMM3DVector): TMM3DVector;
- function MM3DDegToRad(D: TMM3DValue): TMM3DValue;
- function MM3DRadToDeg(R: TMM3DValue): TMM3DValue;
- function MM3DMatrixFromCameraOrientation(const Front, Top: TMM3DVector): TMM3DMatrix4D;
- function MM3DVectorEqual(const V1, V2: TMM3DVector): Boolean;
- type
- {-- EMM3DError ---------------------------------------------------------------}
- EMM3DError = class(Exception)
- end;
- const
- XUnit : TMM3DVector = (x:1;y:0;z:0);
- YUnit : TMM3DVector = (x:0;y:1;z:0);
- ZUnit : TMM3DVector = (x:0;y:0;z:1);
- ZeroVector : TMM3DVector = (x:0;y:0;z:0);
- Identity : TMM3DMatrix4D = (
- (1, 0, 0, 0),
- (0, 1, 0, 0),
- (0, 0, 1, 0),
- (0, 0, 0, 1)
- );
- var
- White : TMM3DColor;
- Red : TMM3DColor;
- Blue : TMM3DColor;
- Green : TMM3DColor;
- LightRed : TMM3DColor;
- LightBlue : TMM3DColor;
- LightGreen : TMM3DColor;
- Yellow : TMM3DColor;
- Black : TMM3DColor;
- implementation
|