PDA

View Full Version : Using X Files.



Arius Myst
29-09-2003, 12:37 PM
I can't seem to load an x-file. using the example in the sdk or the CD3DFile class. Well I think i can load one using CD3DFile. It wont render, i think because of the matrix, i have no idea how to set that up. Heres my code. Firstly a pas file i wrote and added to the uses clause. The handles initialization e.t.c:


unit AerInit;

interface

uses Direct3d9, D3DX9, windows, sysutils, Dialogs, Graphics, Forms;

Function Start(Windowed: Boolean; Width, Height: Integer; Diagh: Boolean; Name: HWND; Form: TForm): HRESULT; // Initialises Aeris And A Basic Font(DiagFnt).
Function FPS(): Integer; // When Called Returns the Frames Per Second.( Method Needs Revision As Its Essentially Bodged ).
Procedure TextIt(LeftX, TopY: Integer; Text: String); // Draws A String Of Text To The Screen At The Given Co-Ord's.
Procedure Err(Err: String); // Displays An Error Message Then Terminates The Engine.
Procedure Flip(); // Brings The BackBuffer To The Front And Renders The Scene.
Procedure Clean(); // Cleans Up.
Procedure Diag(); // Displays Diagnostics, If Enabled When The Device Is Created.

Var
GfxDevice : IDirect3DDevice9 = nil; // The Device. Uses The Systems Defauly Display Adapter.
D3DObj : IDirect3d9 = nil; // A Direct3d Object. Will Be Used To Initialise The Device.
DiagFont : ID3DXFont ; // Direct3D Font Object.
DiagFontObj : TFont ; // Delphi Font Object - Used For Direct3D Font Objects Properties.
TxtRect : TRect ; // Rectangle In Which The The Text String From "TextIt" Will Be Displayed.
Formh : TForm ; // The Form Being Used.
DiagOn : Boolean ; // Is Diagnostics Mode Enabled??.


implementation

//-----------------------------------------------
// NAME: Diag
// DESC: Displays Diagnostics.
//-----------------------------------------------

Procedure Diag();
Begin
TextIt(0, 0, '[ '+Formh.Caption+' ]');
TextIt(0, 11, '[ FPS ] ');
End;

//-----------------------------------------------
// NAME: FPS
// DESC: Returns Frames Per Second.
//-----------------------------------------------

Function FPS(): Integer;
Begin

End;


//-----------------------------------------------
// NAME: Start
// DESC: Initialises The Device And Default Font.
//-----------------------------------------------



Function Start(Windowed: Boolean; Width, Height: Integer; Diagh: Boolean; Name: HWND; Form: TForm): HRESULT;
Var
Present: TD3DPresentParameters;
Begin
FillChar(Present, SizeOf(Present), 0);

DiagOn := False;
If Diagh = True Then
Begin
DiagOn := True;
End;

//-----------------
// Type Of Display

If Windowed = True Then
Begin
Present.Windowed := True;
Present.BackBufferFormat := D3DFMT_A8R8G8B8;
End;

If Windowed = False Then
Begin
Present.Windowed := False;
Present.BackBufferFormat := D3DFMT_A8R8G8B8;
Present.BackBufferWidth := Width;
Present.BackBufferHeight := Height;
Present.BackBufferCount := 1;
End;

//---------------------
// Main Initialisation

D3DObj := Direct3dCreate9( D3D_SDK_VERSION );
If (D3DObj = Nil) Then Err('Engine Failure: Direct3D Object Failed During Creation.');

Present.SwapEffect := D3DSwapEffect_Discard;



Result := D3DObj.CreateDevice( D3DAdapter_Default, D3DDevType_HAL, Name, D3DCreate_Software_VertexProcessing, @Present, GfxDevice);

DiagFontObj := TFont.Create;
With DiagFontObj Do
Begin
Name := 'Tahoma';
Size := 8;
End;

D3DXCreateFont(GfxDevice, DiagFontObj.Handle, DiagFont);


If Failed( Result ) then
Begin
Result := E_FAIL;
Err('Device Failed To Initialise.');
End;
Formh := Form;
GfxDevice.SetDialogBoxMode(True);
GfxDevice.BeginScene;

End;

//-----------------------------------------------
// NAME: Err
// DESC: Displays Errors.
//-----------------------------------------------

Procedure Err(Err: String);
Begin
ShowMessage(Err);
End;

//-----------------------------------------------
// NAME: TextIt
// DESC: Prints Text To The Screen.
//-----------------------------------------------

Procedure TextIt(LeftX, TopY: Integer; Text: String);
Begin
TxtRect.Left := LeftX;
TxtRect.Top := TopY;
TxtRect.Right := Formh.Canvas.TextWidth(Text) + 4;
TxtRect.Bottom := TopY+Formh.Canvas.TextHeight(Text);

DiagFont._Begin;
DiagFont.DrawTextA(PChar(Text), -1, TxtRect, DT_LEFT, D3DColor_XRGB( 255, 255, 255 ));
DiagFont._End;
End;

//-----------------------------------------------
// NAME: Flip
// DESC: Flips The Back To The Front.
//-----------------------------------------------

Procedure Flip();
Begin

If DiagOn = True Then
Begin
Diag();
End;

GfxDevice.EndScene;
GfxDevice.Present(Nil, Nil, 0, Nil);
GfxDevice.Clear(0, nil, D3DClear_Target, D3DColor_XRGB( 46, 66, 90 ), 1.0, 0);
GfxDevice.BeginScene;
End;

//-----------------------------------------------
// NAME: Clean
// DESC: Cleans Then Terminates.
//-----------------------------------------------

Procedure Clean();
Begin
GfxDevice._Release;
GfxDevice := nil;
D3DObj._Release;
D3DObj := nil;

If DiagOn = True Then
Begin
ShowMessage('Debug Message: Cleanup Completed.');
End;
End;

end.

This is my actual XFile test app:


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Direct3D9, D3DX9, AerInit, ExtCtrls, DxFile, D3DFile;

type
TForm1 = class(TForm)
Timer1: TTimer;
procedure FormShow(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1 : TForm1;
Mesh : IDirectXFile;
Enum : IDirectXFileEnumObject;
Data : IDirectXFileData;
XFile : CD3DFile;
Matri : TD3DXMatrix;

implementation

{$R *.dfm}

procedure TForm1.FormShow(Sender: TObject);
begin
Start(False, 1024, 768, True, Form1.Handle, Form1);

//DirectXFileCreate( Mesh );
//Mesh.CreateEnumObject( @Data, DXFILELOAD_FROMFILE, Enum);
//Data.GetData(
//Mesh.LoadMesh

Xfile := CD3DFile.Create;
xfile.CreateFromFile(GfxDevice, 'test.x');


Timer1.Enabled := True;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
//Matri := XFile.GetMatrix;
xfile.Render(GfxDevice, @matri);
Flip;
end;

end.

This compiles fine but doesn't display the x file. Its text only and is a simple square. Id rather do it without using the CD3DFile Class but needs must as the devil drives. Can anyone offer any advice to some1 trying to make progress(slow but still lol).

Avatar
01-10-2003, 04:19 AM
I don't use this framework ... But I can't see the definition of matrices anywhere ... Maybe you didn't showed that part, but in order to render a scene, you'll need to define 3 matrices :

-Projection matrix which will "simulate" 3D
-View Matrix representing the camera
-World Matrix representing the transformations(R, S, T) on the object you'll draw.

To set thoses matrices, use D3DX :

D3DXMatrixPerspectiveFOVLH(Projection, PI/4, 1.33, 1, 1000) // I don't remember well, BUt I think it's this :P

Next, let's define the View Matrix (we'll need 3 vectors : v1, v2, v3) :
v1 := D3DXVector3(0, 0, -3);
v2 := D3DXVector3(0, 0, 0);
v3 := D3DXVector3(0, 1, 0);
D3DXMatrixLookAtLH(View, v1, v2, v3);

And then the world matrix, since you don't want to achieve transformation for the first displays :) Let's put it at identity state :

D3DXMatrixIdentity(World);

Finally, you apply these matrices to the device :

Device.SetTransform(D3DTS_PROJECTION, Projection);
Device.SetTransform(D3DTS_VIEW, View);
Device.SetTransform(D3DTS_WORLD, World);


I really don't know if this was the missing part, but without it, you'll never get anything working !

Hope that helped :)

Bye
Avatar