📄 oxnewtonutils.pas
字号:
TOXNewtonBoneCube = class( TGLCube )
public
end;
{******************************************************************************}
// [15-9-2007]: TOXNewtonBoneCylinder Utils last change by Dave Gravel. //
{******************************************************************************}
type
TOXBaseObject = class( TGLBaseSceneObject )
public
Body: PNewtonBody;
end;
{******************************************************************************}
// [15-9-2007]: TGLVisibilityDeterminationEvent Utils last change by Dave Gravel.
{******************************************************************************}
type
TGLVisibilityDeterminationEvent = function ( Sender: TObject;
var rci: TRenderContextInfo ): Boolean of object;
{******************************************************************************}
// [15-9-2007]: TGLOXNewtonDummy Utils last change by Dave Gravel. //
{******************************************************************************}
type
TOXNewtonDummy = class ( TGLCameraInvariantObject )
private
{ Private Declarations }
FOXVisual: Boolean;
FLineSize: Float;
FStippleView: boolean;
FCubeSizable: TAffineVector;
FEdgeColor: TGLColor;
FVisibleAtRunTime, FAmalgamate: Boolean;
FGroupList: TGLListHandle;
FOnVisibilityDetermination: TGLVisibilityDeterminationEvent;
procedure SetCubeWidth( const aValue: single );
procedure SetCubeHeight( const aValue: single );
procedure SetCubeDepth( const aValue: single );
protected
{ Protected Declarations }
procedure SetEdgeColor( const val: TGLColor );
procedure SetVisibleAtRunTime( const val: Boolean );
procedure SetAmalgamate( const val: Boolean );
public
{ Public Declarations }
constructor Create( AOwner: TComponent ); override;
destructor Destroy; override;
procedure Assign( Source: TPersistent ); override;
function AxisAlignedDimensionsUnscaled: TVector; override;
function RayCastIntersect( const rayStart, rayVector: TVector;
intersectPoint: PVector = nil;
intersectNormal: PVector = nil ): Boolean; override;
procedure BuildList( var rci: TRenderContextInfo ); override;
procedure DoRender( var rci: TRenderContextInfo; renderSelf,
renderChildren: Boolean ); override;
procedure StructureChanged; override;
function BarycenterAbsolutePosition: TVector; override;
published
{ Published Declarations }
property OXVisual: boolean read FOXVisual write FOXVisual;
property CubeWidth: TGLFloat read FCubeSizable[0] write
SetCubeWidth stored False;
property CubeHeight: TGLFloat read FCubeSizable[1] write
SetCubeHeight stored False;
property CubeDepth: TGLFloat read FCubeSizable[2] write
SetCubeDepth stored False;
property EdgeColor: TGLColor read FEdgeColor write SetEdgeColor;
property VisibleAtRunTime: Boolean read FVisibleAtRunTime write
SetVisibleAtRunTime default False;
property Amalgamate: Boolean read FAmalgamate write
SetAmalgamate default False;
property CamInvarianceMode default cimNone;
property OnVisibilityDetermination: TGLVisibilityDeterminationEvent read
FOnVisibilityDetermination write FOnVisibilityDetermination;
property LineSize: Float read FLineSize write FLineSize;
property StippleView: boolean read FStippleView write FStippleView;
end;
type
TOXNewtonBone = class( TOXNewtonDummy )
private
public
published
end;
{******************************************************************************}
// [15-9-2007]: TOXNewtonJoint Utils last change by Dave Gravel. //
{******************************************************************************}
type
TOXNewtonJoint = class( TOXNewtonDummy )
private
FJointObject1: TGLSceneObject;
FJointObject2: TGLSceneObject;
FJoint: PNewtonJoint;
FJointChildPin: TGLCoordinates;
FJointParentPin: TGLCoordinates;
procedure SetChildPin( const val: TGLCoordinates );
procedure SetParentPin( const val: TGLCoordinates );
public
constructor Create( AOwner: TComponent ); override;
destructor Destroy; override;
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
property Joint: PNewtonJoint read FJoint write FJoint;
property Object1: TGLSceneObject read FJointObject1 write FJointObject1;
property Object2: TGLSceneObject read FJointObject2 write FJointObject2;
published
property JointChildPin: TGLCoordinates read FJointChildPin write SetChildPin;
property JointParentPin: TGLCoordinates read FJointParentPin write SetParentPin;
end;
//Special physic update Step
type
TOXState = record
x: float;
v: float;
end;
type
TOXDerivative = record
dx: float;
dv: float;
end;
{******************************************************************************}
// [15-9-2007]: SizableDummyWireframeBuildList Utils last change by Dave Gravel.
{******************************************************************************}
procedure SizableDummyWireframeBuildList( var rci: TRenderContextInfo;
LineSize: single; size: TAffineVector; stipple: Boolean;
const color: TColorVector );
{******************************************************************************}
// [15-9-2007]: oxVehicleLoadQ3Skin Utils last change by Dave Gravel. //
{******************************************************************************}
function NormalRefraction( var v: TVector3f ): single;
procedure oxVehicleLoadQ3Skin( FileName: string; FreeForm: TGLFreeForm );
procedure DatCompressStream( const inpStream, outStream: TStream );
procedure DatDecompressStream( const inpStream, outStream: TStream );
procedure VehicleStreamCom( const FreeForm: TGLFreeForm; const Name: ShortString; const matlib: TGLMaterialLibrary );
procedure VehicleStreamDec( const FreeForm: TGLFreeForm; const Name: ShortString; const matlib: TGLMaterialLibrary );
function StepInterpolate( const previous: TOXState; const current: TOXState; alpha: float ): TOXState;
function StepAcceleration( const state: TOXState; t: float ): float;
function StepEvaluate( const initial: TOXState; t: float ): TOXDerivative; overload;
function StepEvaluate( const initial: TOXState; t: float; dt: float; const d: TOXDerivative ): TOXDerivative; overload;
procedure StepIntegrate( state: TOXState; t: float; dt: float );
implementation
uses oxNewtondll, oxNewtonDynamicObjects;
//
function StepInterpolate( const previous: TOXState; const current: TOXState; alpha: float ): TOXState;
var state: TOXState;
begin
state.x:= current.x * alpha + previous.x * ( 1 - alpha );
state.v:= current.v * alpha + previous.v * ( 1 - alpha );
result:= state;
end;
//
function StepAcceleration( const state: TOXState; t: float ): float;
const
k: float = 10;
b: float = 1;
begin
result:= - k * state.x - b * state.v;
end;
//
function StepEvaluate( const initial: TOXState; t: float ): TOXDerivative;
var output: TOXDerivative;
begin
output.dx:= initial.v;
output.dv:= StepAcceleration( initial, t );
result:= output;
end;
//
function StepEvaluate( const initial: TOXState; t: float; dt: float; const d: TOXDerivative ): TOXDerivative;
var
state: TOXState;
output: TOXDerivative;
begin
state.x:= initial.x + d.dx * dt;
state.v:= initial.v + d.dv * dt;
output.dx:= state.v;
output.dv:= StepAcceleration( state, t + dt );
result:= output;
end;
//
procedure StepIntegrate( state: TOXState; t: float; dt: float );
var
a: TOXDerivative;
b: TOXDerivative;
c: TOXDerivative;
d: TOXDerivative;
dxdt: float;
dvdt: float;
begin
a:= StepEvaluate( state, t );
b:= StepEvaluate( state, t, dt * 0.5, a );
c:= StepEvaluate( state, t, dt * 0.5, b );
d:= StepEvaluate( state, t, dt, c );
dxdt:= 1.0 / 6.0 * ( a.dx + 2.0 *( b.dx + c.dx ) + d.dx );
dvdt:= 1.0 / 6.0 * ( a.dv + 2.0 *( b.dv + c.dv ) + d.dv );
state.x:= state.x + dxdt * dt;
state.v:= state.v + dvdt * dt;
end;
//
procedure VehicleStreamCom( const FreeForm: TGLFreeForm; const Name: ShortString; const matlib: TGLMaterialLibrary );
var
TmpStr1, TmpStr2: TMemoryStream;
begin
TmpStr1:= TMemoryStream.Create;
TmpStr2:= TMemoryStream.Create;
try
FreeForm.MeshObjects.SaveToStream( TmpStr1 );
DatCompressStream( TmpStr1, TmpStr2 );
TmpStr2.SaveToFile( Name );
TmpStr1.Clear;
TmpStr2.Clear;
//
matlib.SaveToStream( TmpStr1 );
DatCompressStream( TmpStr1, TmpStr2 );
TmpStr2.SaveToFile( Name+'matlib' );
TmpStr1.Clear;
TmpStr2.Clear;
finally
TmpStr1.Free;
TmpStr2.Free;
end;
end;
//
procedure VehicleStreamDec( const FreeForm: TGLFreeForm; const Name: ShortString; const matlib: TGLMaterialLibrary );
var
TmpStr1, TmpStr2: TMemoryStream;
begin
TmpStr1:= TMemoryStream.Create;
TmpStr2:= TMemoryStream.Create;
try
TmpStr1.LoadFromFile( Name );
DatDecompressStream( TmpStr1, TmpStr2 );
FreeForm.MeshObjects.LoadFromStream( TmpStr2 );
TmpStr1.Clear;
TmpStr2.Clear;
TmpStr1.LoadFromFile( Name+'matlib' );
DatDecompressStream( TmpStr1, TmpStr2 );
matlib.LoadFromStream( TmpStr2 );
TmpStr1.Clear;
TmpStr2.Clear;
finally
TmpStr1.Free;
TmpStr2.Free;
end;
end;
procedure DatCompressStream( const inpStream, outStream: TStream );
var
InpBuf, OutBuf: Pointer;
InpBytes, OutBytes: Integer;
begin
InpBuf:= nil;
OutBuf:= nil;
OutBytes:= 0;
try
GetMem( InpBuf, inpStream.Size );
inpStream.Position:= 0;
InpBytes:= inpStream.Read( InpBuf^, inpStream.Size );
// Experimental...
//rmZLIBCompressBuf( InpBuf, InpBytes, OutBuf, OutBytes );
//CompressBuf( InpBuf, InpBytes, OutBuf, OutBytes );
//oxNewtonDataCompression( InpBuf, InpBytes, OutBuf, OutBytes );
ZCompress( InpBuf, InpBytes, OutBuf, OutBytes );
outStream.Write( OutBuf^, OutBytes );
finally
if InpBuf <> nil then FreeMem( InpBuf, inpStream.Size*4 );
if OutBuf <> nil then FreeMem( OutBuf, inpStream.Size*4 );
end;
end;
//
procedure DatDecompressStream( const inpStream, outStream: TStream );
var
InpBuf, OutBuf: Pointer;
OutBytes, sz: Integer;
begin
InpBuf:= nil;
OutBuf:= nil;
OutBytes:= 0;
sz:= inpStream.Size - inpStream.Position;
if sz > 0 then
try
GetMem( InpBuf, sz );
inpStream.Read( InpBuf^, sz );
// Experimental...
//rmZLIBDecompressBuf( InpBuf, sz, 0, OutBuf, OutBytes );
//oxNewtonDataDecompression( InpBuf, sz, 0, OutBuf, OutBytes );
//DecompressBuf( InpBuf, sz, 0, OutBuf, OutBytes );
ZDecompress( InpBuf, sz, OutBuf, OutBytes );
outStream.Write( OutBuf^, OutBytes );
finally
if InpBuf <> nil then FreeMem( InpBuf, inpStream.Size*4 );
if OutBuf <> nil then FreeMem( OutBuf, inpStream.Size*4 );
end;
outStream.Position:= 0;
end;
{******************************************************************************}
// [15-9-2007]: Utils last change by Dave Gravel. //
{******************************************************************************}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -