oxnewtonstaticmesh.pas

来自「Newton Game Dynamic 1.52 Delphi下基于GLSce」· PAS 代码 · 共 231 行

PAS
231
字号
unit oxNewtonStaticMesh;
{******************************************************************************}
// [15-9-2007]: oxNewtonStaticMesh last change by Dave Gravel.               //
{******************************************************************************}
{===============================================================================

 Version: MPL 1.1

 The contents of this file are subject to the Mozilla Public License Version
 1.1 (the "License"); you may not use this file except in compliance with
 the License. You may obtain a copy of the License at
 http://www.mozilla.org/MPL/


 Software distributed under the License is distributed on an "AS IS" basis,
 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 for the specific language governing rights and limitations under the
 License.

 The Original Code is "oxNewton opengl-Dynamics-delphi-component".

 The Initial Developer of the Original Code is
 Dave Gravel, OrionX3D Opengl & Delphi Programming, dave.gravel@cgocable.ca.
                       http://www.Dave.ServeUsers.com
                       http://www.k00m.sexidude.com

 Portions created by Dave Gravel are Copyright (C) 2004 - 2006.
 Dave Gravel. All Rights Reserved.

 Contributor(s): GLScene (http://www.glscene.org) -
 Julio Jerez and Alain Suero (http://www.newtondynamics.com) -
 Sascha Willems (www.delphigl.de)

================================================================================
 oxNewton v1.55 by Dave Gravel.

PS: I request only one thing from the users of my oxNewton Component,
it is to put on your about or your help a comment saying you using the
oxNewton Component with my name Dave Gravel and my
e-mail: dave.gravel@cgocable.ca
Don't modify or remove any comment or information on my file
if you do some modification on my code please contact me.
Read the Newton license too, it is realy important.

================================================================================}

interface
{******************************************************************************}
// [15-9-2007]: oxNewtonStaticMesh last change by Dave Gravel.               //
{******************************************************************************}
uses
     // Windows
     Forms, Windows, Classes, Messages, SysUtils, Graphics, Controls, ExtCtrls,
     Dialogs, StdCtrls,
     // GLScene
     GLScene, GLMisc, GLObjects, VectorGeometry, GLVectorFileObjects, VectorLists,
     GLTexture, VectorTypes, TGA, GLFileSMD, JPEG, GLFile3DS,
     // Newton
     oxNewtonUtils, oxNewtonManager, oxNewtonJoint, oxNewtonImport;
{******************************************************************************}
// [15-9-2007]: TOXNewtonStaMesh last change by Dave Gravel.                 //
{******************************************************************************}
type
  TOXNewtonStaMesh = class( TGLFreeForm )
  private
    FOrionX3D: byte;
    FOXVisual: boolean;
    FActived: boolean;
    FManager: TOXNewtonManager;
    FCollision: PNewtonCollision;
    FBody: PNewtonBody;
    FOptimize: integer;
    FContactID: integer;
    FMaterialCustom: boolean;
    FOnStepRender: TOXOnStepRender;
    FOnTreeCollisionCallback: TOXNewtonTreeCollisionCallback;
    FOnFacesAttribute: TOXOnFacesAttribute;
    function GetGeom: PNewtonCollision;
    function GetBody: PNewtonBody;
  public
    property Manager: TOXNewtonManager read FManager;
    property Actived: boolean read FActived;
    procedure UpdateTransformation;
    procedure InitNewton; virtual;
    procedure BuildList( var rci: TRenderContextInfo ); override;
    procedure SetFaceAttrib( const faceIndexArray: Pint; attribute: int );
    procedure DoProgress( const progressTime: TProgressTimes ); override;
    property Geom: PNewtonCollision read GetGeom;
    property Body: PNewtonBody read GetBody;
    property ContactID: integer read FContactID write FContactID;
    constructor Create( aOwner: TComponent ); override;
    destructor Destroy; override;
  published
    property OXVisual: boolean read FOXVisual write FOXVisual;
    property Optimize: integer read FOptimize write FOptimize;
    property OnStepRender: TOXOnStepRender read FOnStepRender write FOnStepRender;
    property OnTreeCollisionCallback: TOXNewtonTreeCollisionCallback read FOnTreeCollisionCallback write FOnTreeCollisionCallback;
    property OnFacesAttribute: TOXOnFacesAttribute read FOnFacesAttribute write FOnFacesAttribute;
  end;
implementation
uses oxNewtondll;
{******************************************************************************}
// [15-9-2007]: TOXNewtonStaMesh last change by Dave Gravel.                 //
{******************************************************************************}
constructor TOXNewtonStaMesh.Create( aOwner: TComponent );
begin
  inherited create( aOwner );
  FOrionX3D:= 100;
  FActived:= False;
  FMaterialCustom:= False;
  FContactID:= -1;
  FOXVisual:= True;
  FOptimize:= 0;
end;
{******************************************************************************}
// [15-9-2007]: TOXNewtonStaMesh last change by Dave Gravel.                 //
{******************************************************************************}
procedure TOXNewtonStaMesh.UpdateTransformation;
begin
  if FActived then
    oxPSetInitTransform( FBody, Self );
end;
{******************************************************************************}
// [15-9-2007]: TOXNewtonStaMesh last change by Dave Gravel.                 //
{******************************************************************************}
procedure TOXNewtonStaMesh.BuildList( var rci: TRenderContextInfo );
begin
  if FOXVisual then
    inherited else
  exit;
end;
{******************************************************************************}
// [15-9-2007]: TOXNewtonStaMesh last change by Dave Gravel.                 //
{******************************************************************************}
function TOXNewtonStaMesh.GetGeom: PNewtonCollision;
begin
  result:= FCollision;
end;
{******************************************************************************}
// [15-9-2007]: TOXNewtonStaMesh last change by Dave Gravel.                 //
{******************************************************************************}
function TOXNewtonStaMesh.GetBody: PNewtonBody;
begin
  result:= FBody;
end;
{******************************************************************************}
// [15-9-2007]: TOXNewtonStaMesh last change by Dave Gravel.                 //
{******************************************************************************}
procedure TOXNewtonStaMesh.InitNewton;
var
  mTmpFace: array[0..2] of TOXVector3;
  mTriangles: TAffineVectorList;
  mf: Integer;
  Attrib: Integer;
begin
  FManager:= TOXNewtonManager(Scene);
  if FActived or ( FManager = nil ) then Exit;
  Attrib:= 1;
  FContactID:= FManager.WorldDefaultMaterialID;
  FCollision:= NewtonCreateTreeCollision( FManager.World, @oxTreeCollisionCallback );
  NewtonTreeCollisionBeginBuild( FCollision );
  mTriangles:= MeshObjects.ExtractTriangles;
  mTriangles.Scale( Scale.AsAffineVector );
  if ( mTriangles.Count > 0 ) then begin
    mf:= 0;
  repeat
    mTmpFace[0]:= oxV3FToV3D( mTriangles[mf + 0] );
    mTmpFace[1]:= oxV3FToV3D( mTriangles[mf + 1] );
    mTmpFace[2]:= oxV3FToV3D( mTriangles[mf + 2] );
    // Here it is a simple way to set face attibute but this method have already change on my version test.
    // The new implementation coming later on the next component version.
    if Assigned( FOnFacesAttribute ) then FOnFacesAttribute( mTriangles.Count, mf, Attrib );
    NewtonTreeCollisionAddFace( FCollision, 3, @mTmpFace[0], SizeOf( TOXVector3 ), Attrib );
    inc( mf, 3 );
  until mf > mTriangles.Count -1;
  end;
  oxCreateNewtonTriMeshFromBaseMesh( self, FCollision, FOptimize );
  oxStaBuildTreeMeshData( self, FOptimize );
  NewtonTreeCollisionEndBuild( FCollision, FOptimize );
  mTriangles.Free;  
  FBody:= NewtonCreateBody( Manager.World, FCollision );
  if FManager.WorldMatrialCustom then begin
    FContactID:= FManager.WorldDefaultMaterialID;
    oxSetObjectMaterial( FBody );
  end;
  NewtonBodySetUserData( FBody, Self );
  oxPSetInitTransform( FBody, Self );
  NewtonReleaseCollision( Manager.World, FCollision );
  FManager.AddLog( 'Stactic Mesh: '+Name, 1, ':Info Object' );
  FManager.AddLog(  Name + ' InitNewton.', 1, ':Info Object' );
  FActived:= True;
end;
{******************************************************************************}
// [15-9-2007]: TOXNewtonStaMesh last change by Dave Gravel.                 //
{******************************************************************************}
procedure TOXNewtonStaMesh.SetFaceAttrib( const faceIndexArray: Pint; attribute: int );
begin
  NewtonTreeCollisionSetFaceAtribute( FCollision, faceIndexArray, attribute );
end;
{******************************************************************************}
// [15-9-2007]: TOXNewtonStaMesh last change by Dave Gravel.                 //
{******************************************************************************}
procedure TOXNewtonStaMesh.DoProgress( const progressTime: TProgressTimes );
begin
  inherited;
  if FActived then begin
    if Assigned( FOnStepRender ) then
      FOnStepRender( progressTime.deltaTime, progressTime.newTime );
  end;
end;
{******************************************************************************}
// [15-9-2007]: TOXNewtonStaMesh last change by Dave Gravel.                 //
{******************************************************************************}
destructor TOXNewtonStaMesh.Destroy;
begin
  if FActived and assigned( FBody ) then begin
    if ( not ( csDesigning in ComponentState ) ) then
      if Name = '' then FManager.AddLog( ClassName + ' destroyed.', 1, ':Info Object' )
      else FManager.AddLog( Name + ' destroyed.', 1, ':Info Object' );
    FActived:= False;
    NewtonDestroyBody( FManager.World, FBody );
  end;
  inherited destroy;
end;
{******************************************************************************}
{$D '[15-9-2007]: TOXNewtonManager v1.55 by Dave Gravel under MPL-1.1 license.'}
{******************************************************************************}
initialization
  RegisterClasses( [ TOXNewtonStaMesh ] );
end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?