Archived

This topic is now archived and is closed to further replies.

ibolcina

.x file loader

Recommended Posts

ibolcina    122
Hi. I am looking for well writter .x file loader. Currently I am using stuff from MS framework and I am not happy with it. The code seems so messy and I do not have any control over it. I also dont have access to hierarchy of frames. Can you please recommand me a loader. I assume I could write myself one, but I really dont feel like reinventing the wheel. thank you, ivan

Share this post


Link to post
Share on other sites
aker_jus    122
You want a ready for you X mesh loader, but you dont like the DX SDK Sample? Don''t you understand the code in that sample? If you do, yes, write your own.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
you do have access to the frame hierarchy via the m_pNext and m_pChild member variables. but may the article and code at http://www.gamedev.net/reference/articles/article1495.asp will give you a start in writing your own. or you could always just go buy Jim''s book. his classes will also include skinned mesh loading and animation.

Share this post


Link to post
Share on other sites
ibolcina    122
I have written my own. If anyone is having same trouble, you can use this. Use MashHandler class.





#ifndef _H_MESHHANDLER
#define _H_MESHHANDLER
#include <d3d9.h>
#include <d3dx9.h>
#include <string>
#include <vector>

using namespace std;
//-----------------------------------------------------------------------------
// Miscellaneous helper functions
//-----------------------------------------------------------------------------
#define SAFE_DELETE(p) { if(p) { delete (p); (p)=NULL; } }
#define SAFE_DELETE_ARRAY(p) { if(p) { delete[] (p); (p)=NULL; } }
#define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }

//-----------------------------------------------------------------------------
// Atomic mesh classes
//-----------------------------------------------------------------------------
struct MeshContainerBox
{
D3DXVECTOR3 a;
D3DXVECTOR3 b;
};
class MeshData
{
protected:
LPD3DXMESH SysMemMesh;
LPD3DXMESH LocalMesh;
DWORD NumMaterials;
D3DMATERIAL9* Materials;
LPDIRECT3DTEXTURE9* Textures;
bool Created;
public:
MeshData();
virtual ~MeshData();
virtual void Draw(LPDIRECT3DDEVICE9 device,bool opaque);
void Destroy();
void RestoreDeviceObjects( LPDIRECT3DDEVICE9 device );
void InvalidateDeviceObjects();
LPD3DXMESH GetSysMemMesh() { return SysMemMesh; }
LPD3DXMESH GetLocalMesh() { return LocalMesh; }
void SetFVF( LPDIRECT3DDEVICE9 device, DWORD FVF );
bool IsCreated() { return Created;}
void ComputeSphere(D3DXVECTOR3 & center,float & radius);
void Transform(D3DXVECTOR3 offset,float factor);
void ScaleNormals(float factor);
void ComputeBoundingBox(D3DXVECTOR3 &a,D3DXVECTOR3 &b);
};

class FileMeshData : public MeshData
{
public:
bool Create(string pName,string pDir,LPDIRECT3DDEVICE9 device);
};

class FrameMeshData : public MeshData
{
public:
bool Create(string pDir,LPD3DXMESHDATA data,LPD3DXMATERIAL materials,DWORD NumMaterials,DWORD *pAdjacency,LPDIRECT3DDEVICE9 device);
};


//-----------------------------------------------------------------------------
// Hierarchy classes - allocator
//-----------------------------------------------------------------------------
class MeshAllocator : public ID3DXAllocateHierarchy
{
public:
LPDIRECT3DDEVICE9 device;
string dir;
STDMETHOD( CreateFrame)(LPCSTR pMesh,LPD3DXFRAME *ppNewFrame);
STDMETHOD( CreateMeshContainer)(LPCSTR Name,LPD3DXMESHDATA pMeshData,LPD3DXMATERIAL pMaterials,LPD3DXEFFECTINSTANCE pEffectInstances,DWORD NumMaterials,DWORD *pAdjacency,LPD3DXSKININFO pSkinInfo,LPD3DXMESHCONTAINER *ppNewMeshContainer);
STDMETHOD( DestroyFrame)(LPD3DXFRAME pFrameToFree);
STDMETHOD( DestroyMeshContainer)( LPD3DXMESHCONTAINER pMeshContainerToFree);
};
//-----------------------------------------------------------------------------
// Hierarchy classes - hierarchy
//-----------------------------------------------------------------------------
class HierarchyFrame : public D3DXFRAME
{
public:
void RestoreDeviceObjects( LPDIRECT3DDEVICE9 pd3dDevice );
void InvalidateDeviceObjects();
void Draw(LPDIRECT3DDEVICE9 device,D3DXMATRIX * world,bool opaque);
void SetFVF( LPDIRECT3DDEVICE9 pd3dDevice, DWORD dwFVF );
void ScaleNormals(float factor);
void ComputeBoundingBox(D3DXMATRIX * world,vector * boxes);
D3DXMATRIX * GetMatrix() {return &TransformationMatrix;}
void SetMatrix(D3DXMATRIX * value) {TransformationMatrix=*value;}
};

class HierarchyMesh: public D3DXMESHCONTAINER
{
public:
FrameMeshData * data;
void RestoreDeviceObjects( LPDIRECT3DDEVICE9 pd3dDevice );
void InvalidateDeviceObjects();
void Draw(LPDIRECT3DDEVICE9 device,D3DXMATRIX * world,bool opaque);
void SetFVF( LPDIRECT3DDEVICE9 pd3dDevice, DWORD dwFVF );
void ScaleNormals(float factor);
void ComputeBoundingBox(D3DXMATRIX * world,vector * boxes);
};

//-----------------------------------------------------------------------------
// Top-level management class
//-----------------------------------------------------------------------------



class MeshHandler
{
protected:
HierarchyFrame * RootFrame;
MeshAllocator Allocator;
FileMeshData * Mesh;
bool Created;
float Size;
D3DXVECTOR3 Center;
public:
vector Boxes;
MeshContainerBox MainBox;
MeshHandler();
~MeshHandler();
void Draw(LPDIRECT3DDEVICE9 device,D3DXMATRIX * world);
void Create(string pFileName,string pDir,LPDIRECT3DDEVICE9 device);
void Destroy();
void RestoreDeviceObjects( LPDIRECT3DDEVICE9 pd3dDevice );
void InvalidateDeviceObjects();
void SetFVF( LPDIRECT3DDEVICE9 pd3dDevice, DWORD dwFVF );
void SetSize(float pSize);
void ComputeBoundingBox();
float GetSize() { return Size; }
FileMeshData * GetMesh() { return Mesh; }
bool IsCreated() { return Created;}
};

#endif




#include "MeshHandler.h"
//-----------------------------------------------------------------------------
// Atomic mesh classes
//-----------------------------------------------------------------------------
MeshData::MeshData()
{
SysMemMesh = NULL;
LocalMesh = NULL;
NumMaterials = 0L;
Materials = NULL;
Textures = NULL;
Created = false;
}

MeshData::~MeshData()
{
Destroy();
}

void MeshData::Destroy()
{
if (Created)
{
InvalidateDeviceObjects();
for( UINT i=0; i SAFE_RELEASE( Textures );
SAFE_DELETE_ARRAY( Textures );
SAFE_DELETE_ARRAY( Materials );
SAFE_RELEASE( SysMemMesh );
NumMaterials = 0L;
Created=false;
}
}

void MeshData::SetFVF( LPDIRECT3DDEVICE9 pd3dDevice, DWORD dwFVF )
{
if (Created)
{
try
{
LPD3DXMESH pTempSysMemMesh = NULL;
LPD3DXMESH pTempLocalMesh = NULL;
if( SysMemMesh )
{
if( FAILED( SysMemMesh->CloneMeshFVF( D3DXMESH_SYSTEMMEM, dwFVF, pd3dDevice, &pTempSysMemMesh ) ) )
throw E_FAIL;
}
if( LocalMesh )
{
if( FAILED( LocalMesh->CloneMeshFVF( 0L, dwFVF, pd3dDevice,
&pTempLocalMesh ) ) )
{
SAFE_RELEASE( pTempSysMemMesh );
throw E_FAIL;
}
}
SAFE_RELEASE( SysMemMesh );
SAFE_RELEASE( LocalMesh );
if( pTempSysMemMesh ) SysMemMesh = pTempSysMemMesh;
if( pTempLocalMesh ) LocalMesh = pTempLocalMesh;
if( SysMemMesh ) D3DXComputeNormals( SysMemMesh, NULL );
if( LocalMesh ) D3DXComputeNormals( LocalMesh, NULL );
}
catch (...)
{
Destroy();
}
}
}

void MeshData::RestoreDeviceObjects( LPDIRECT3DDEVICE9 pd3dDevice )
{
if (Created && SysMemMesh)
{
if( FAILED( SysMemMesh->CloneMeshFVF( 0L, SysMemMesh->GetFVF(), pd3dDevice, &LocalMesh ) ) )
Destroy();
}
}

void MeshData::InvalidateDeviceObjects()
{
if (Created)
SAFE_RELEASE( LocalMesh );
}

void MeshData::Draw( LPDIRECT3DDEVICE9 pd3dDevice,bool opaque)
{
if(Created && LocalMesh)
{
for( DWORD i=0; i {
bool cond=(opaque ? Materials[i].Diffuse.a==1.0f : Materials[i].Diffuse.a!=1.0f);
if( cond )
{
pd3dDevice->SetMaterial( &Materials[i] );
pd3dDevice->SetTexture( 0, Textures[i] );
LocalMesh->DrawSubset( i );
}
}
}
}

void MeshData::ComputeSphere(D3DXVECTOR3 ¢er,float &radius)
{
if (Created)
{
BYTE * init;
LPDIRECT3DVERTEXBUFFER9 pVB;
SysMemMesh->GetVertexBuffer( &pVB );
pVB->Lock( 0, 0, (void**)&init, 0 );
D3DXComputeBoundingSphere((LPD3DXVECTOR3 )init,SysMemMesh->GetNumVertices(),D3DXGetFVFVertexSize(SysMemMesh->GetFVF()),¢er,&radius);
pVB->Unlock();
pVB->Release();
}
}

void MeshData::ComputeBoundingBox(D3DXVECTOR3 &a,D3DXVECTOR3 &b)
{
if (Created)
{
BYTE * init;
LPDIRECT3DVERTEXBUFFER9 pVB;
SysMemMesh->GetVertexBuffer( &pVB );
pVB->Lock( 0, 0, (void**)&init, 0 );
D3DXComputeBoundingBox((LPD3DXVECTOR3 )init,SysMemMesh->GetNumVertices(),D3DXGetFVFVertexSize(SysMemMesh->GetFVF()),&a,&b);
pVB->Unlock();
pVB->Release();
}
}


void MeshData::Transform(D3DXVECTOR3 offset,float factor)
{
if (Created)
{
BYTE * init;
BYTE * position;
D3DXVECTOR3 min,max,mid;
LPDIRECT3DVERTEXBUFFER9 pVB;
DWORD dwNumVertices;
SysMemMesh->GetVertexBuffer( &pVB );
dwNumVertices = SysMemMesh->GetNumVertices();
DWORD flags=SysMemMesh->GetFVF();
int size=D3DXGetFVFVertexSize(flags);
pVB->Lock( 0, 0, (void**)&init, 0 );
for( DWORD i=0; i {
position=init+i*size;
D3DXVECTOR3 pos=*((D3DXVECTOR3*) position);
pos+=offset;
pos*=factor;
*((D3DXVECTOR3*) position)=pos;
}
pVB->Unlock();
pVB->Release();
}
}

void MeshData::ScaleNormals(float factor)
{
}

bool FileMeshData::Create(string pName,string pDir,LPDIRECT3DDEVICE9 device)
{
LPD3DXBUFFER pAdjacencyBuffer = NULL;
LPD3DXBUFFER pMtrlBuffer = NULL;
string finalname=pDir;
finalname+="\\";
finalname+=pName;
try
{
HRESULT hr;
if( FAILED( hr = D3DXLoadMeshFromX( finalname.c_str(), D3DXMESH_SYSTEMMEM, device,
&pAdjacencyBuffer, &pMtrlBuffer, NULL,
&NumMaterials, &SysMemMesh ) ) ) throw hr;

if( FAILED( hr = SysMemMesh->OptimizeInplace(
D3DXMESHOPT_COMPACT | D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_VERTEXCACHE,
(DWORD*)pAdjacencyBuffer->GetBufferPointer(), NULL, NULL, NULL ) ) ) throw hr;

if( pMtrlBuffer && NumMaterials > 0 )
{
// Allocate memory for the materials and textures
D3DXMATERIAL* d3dxMtrls = (D3DXMATERIAL*)pMtrlBuffer->GetBufferPointer();
Materials = new D3DMATERIAL9[NumMaterials];
if( Materials == NULL ) throw E_OUTOFMEMORY;
Textures = new LPDIRECT3DTEXTURE9[NumMaterials];
if( Textures == NULL ) throw E_OUTOFMEMORY;
// Copy each material and create its texture
for( DWORD i=0; i {
// Copy the material
Materials[i] = d3dxMtrls[i].MatD3D;
Textures[i] = NULL;
// Create a texture
if( d3dxMtrls[i].pTextureFilename )
{
string filename=pDir;
filename+="\\";
filename+=d3dxMtrls[i].pTextureFilename ;
if( FAILED( D3DXCreateTextureFromFile( device, filename.c_str(),
&Textures[i] ) ) ) Textures[i] = NULL;
}
}
}
Created=true;
}
catch (...)
{
SAFE_RELEASE( SysMemMesh );
Created=false;
}
SAFE_RELEASE( pAdjacencyBuffer );
SAFE_RELEASE( pMtrlBuffer );
return Created;
}



bool FrameMeshData::Create(string pDir,LPD3DXMESHDATA data,LPD3DXMATERIAL pMaterials,DWORD pNumMaterials,DWORD *pAdjacency,LPDIRECT3DDEVICE9 device)
{
try
{
HRESULT hr;
if( FAILED( hr=data->pMesh->CloneMeshFVF( D3DXMESH_SYSTEMMEM , data->pMesh->GetFVF(), device, &SysMemMesh ) ) )
throw hr;
NumMaterials=pNumMaterials;
if( FAILED( hr = SysMemMesh->OptimizeInplace(
D3DXMESHOPT_COMPACT | D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_VERTEXCACHE,
pAdjacency, NULL, NULL, NULL ) ) ) throw hr;

if( NumMaterials > 0 )
{
// Allocate memory for the materials and textures
D3DXMATERIAL* d3dxMtrls = pMaterials;
Materials = new D3DMATERIAL9[NumMaterials];
if( Materials == NULL ) throw E_OUTOFMEMORY;
Textures = new LPDIRECT3DTEXTURE9[NumMaterials];
if( Textures == NULL ) throw E_OUTOFMEMORY;
// Copy each material and create its texture
for( DWORD i=0; i {
// Copy the material
Materials[i] = d3dxMtrls[i].MatD3D;
Textures[i] = NULL;
// Create a texture
if( d3dxMtrls[i].pTextureFilename )
{
string filename=pDir;
filename+="\\";
filename+=d3dxMtrls[i].pTextureFilename ;
if( FAILED( D3DXCreateTextureFromFile( device, filename.c_str(),
&Textures[i] ) ) ) Textures[i] = NULL;
}
}
}
Created=true;
}
catch (...)
{
SAFE_RELEASE( SysMemMesh );
Created=false;
}
return Created;
}

//-----------------------------------------------------------------------------
// Hierarchy classes - allocator
//-----------------------------------------------------------------------------

HRESULT MeshAllocator::CreateFrame(LPCSTR pMesh,LPD3DXFRAME *ppNewFrame)
{
HierarchyFrame * out=new HierarchyFrame;
out->Name=(char *) malloc(strlen(pMesh)+1);
strcpy(out->Name,pMesh);
out->Name[strlen(pMesh)]=0;
*ppNewFrame=out;
return D3D_OK;
}

HRESULT MeshAllocator::DestroyFrame(LPD3DXFRAME pFrameToFree)
{
free(pFrameToFree->Name);
delete pFrameToFree;
return D3D_OK;
}

HRESULT MeshAllocator::CreateMeshContainer(LPCSTR Name,LPD3DXMESHDATA pMeshData,LPD3DXMATERIAL pMaterials,LPD3DXEFFECTINSTANCE pEffectInstances,DWORD NumMaterials,DWORD *pAdjacency,LPD3DXSKININFO pSkinInfo,LPD3DXMESHCONTAINER *ppNewMeshContainer)
{
HierarchyMesh * out=new HierarchyMesh();
out->Name=(char *) malloc(strlen(Name)+1);
strcpy(out->Name,Name);
out->Name[strlen(Name)]=0;
out->MeshData=*pMeshData;
out->pMaterials=pMaterials;
out->pEffects=pEffectInstances;
out->NumMaterials=NumMaterials;
out->pAdjacency=pAdjacency;
out->pSkinInfo=pSkinInfo;
FrameMeshData * fmd=new FrameMeshData();
fmd->Create(dir,&out->MeshData,pMaterials,NumMaterials,pAdjacency,device);
out->data=fmd;
*ppNewMeshContainer=out;
return D3D_OK;
}

HRESULT MeshAllocator::DestroyMeshContainer(LPD3DXMESHCONTAINER pMeshContainerToFree)
{
HierarchyMesh * in=(HierarchyMesh*) pMeshContainerToFree;
in->data->Destroy();
delete in->data;
free(in->Name);
delete in;
return D3D_OK;
}
//-----------------------------------------------------------------------------
// Hierarchy classes - hierarchy
//-----------------------------------------------------------------------------

void HierarchyMesh::RestoreDeviceObjects( LPDIRECT3DDEVICE9 device )
{
data->RestoreDeviceObjects(device);
}

void HierarchyMesh::InvalidateDeviceObjects()
{
data->InvalidateDeviceObjects();
}

void HierarchyMesh::Draw(LPDIRECT3DDEVICE9 device,D3DXMATRIX *world,bool opaque)
{
device->SetTransform(D3DTS_WORLD,world);
data->Draw(device,opaque);
}

void HierarchyMesh::SetFVF( LPDIRECT3DDEVICE9 device, DWORD FVF )
{
data->SetFVF(device,FVF);
}
void HierarchyMesh::ScaleNormals(float factor)
{
data->ScaleNormals(factor);
}

void HierarchyMesh::ComputeBoundingBox(D3DXMATRIX * world,vector * boxes)
{
MeshContainerBox box;
D3DXVECTOR3 min,max;
data->ComputeBoundingBox(min,max);
D3DXVECTOR3 v[8];
v[0]=D3DXVECTOR3(min.x,min.y,min.z);
v[1]=D3DXVECTOR3(min.x,min.y,max.z);
v[2]=D3DXVECTOR3(min.x,max.y,min.z);
v[3]=D3DXVECTOR3(min.x,max.y,max.z);
v[4]=D3DXVECTOR3(max.x,min.y,min.z);
v[5]=D3DXVECTOR3(max.x,min.y,max.z);
v[6]=D3DXVECTOR3(max.x,max.y,min.z);
v[7]=D3DXVECTOR3(max.x,max.y,max.z);
for (int i=0;i<8;i++)
D3DXVec3TransformCoord(&(v[i]),&(v[i]),world);
min=v[0];
max=v[0];
for (int i=0;i<8;i++)
for (int j=0;j<3;j++)
{
if (v[i][j] if (v[i][j]>max[j]) max[j]=v[i][j];
}
box.a=min;
box.b=max;
boxes->push_back(box);
}

void HierarchyFrame::RestoreDeviceObjects( LPDIRECT3DDEVICE9 device )
{
HierarchyMesh * mesh=(HierarchyMesh*) pMeshContainer;
HierarchyFrame * sibling=(HierarchyFrame *) pFrameSibling;
HierarchyFrame * child=(HierarchyFrame *) pFrameFirstChild;

if (mesh!=NULL) mesh->RestoreDeviceObjects(device);
if (sibling!=NULL) sibling->RestoreDeviceObjects(device);
if (child!=NULL) child->RestoreDeviceObjects(device);
}

void HierarchyFrame::InvalidateDeviceObjects()
{
HierarchyMesh * mesh=(HierarchyMesh*) pMeshContainer;
HierarchyFrame * sibling=(HierarchyFrame *) pFrameSibling;
HierarchyFrame * child=(HierarchyFrame *) pFrameFirstChild;

if (mesh!=NULL) mesh->InvalidateDeviceObjects();
if (sibling!=NULL) sibling->InvalidateDeviceObjects();
if (child!=NULL) child->InvalidateDeviceObjects();
}

void HierarchyFrame::Draw(LPDIRECT3DDEVICE9 device,D3DXMATRIX * world,bool opaque)
{
D3DXMATRIX ThisWorld=TransformationMatrix*(*world);
HierarchyMesh * mesh=(HierarchyMesh*) pMeshContainer;
HierarchyFrame * sibling=(HierarchyFrame *) pFrameSibling;
HierarchyFrame * child=(HierarchyFrame *) pFrameFirstChild;

if (mesh!=NULL) mesh->Draw(device,&ThisWorld,opaque);
if (sibling!=NULL) sibling->Draw(device,world,opaque);
if (child!=NULL) child->Draw(device,&ThisWorld,opaque);
}

void HierarchyFrame::SetFVF( LPDIRECT3DDEVICE9 device, DWORD FVF )
{
HierarchyMesh * mesh=(HierarchyMesh*) pMeshContainer;
HierarchyFrame * sibling=(HierarchyFrame *) pFrameSibling;
HierarchyFrame * child=(HierarchyFrame *) pFrameFirstChild;

if (mesh!=NULL) mesh->SetFVF(device,FVF);
if (sibling!=NULL) sibling->SetFVF(device,FVF);
if (child!=NULL) child->SetFVF(device,FVF);
}

void HierarchyFrame::ScaleNormals(float factor)
{
HierarchyMesh * mesh=(HierarchyMesh*) pMeshContainer;
HierarchyFrame * sibling=(HierarchyFrame *) pFrameSibling;
HierarchyFrame * child=(HierarchyFrame *) pFrameFirstChild;

if (mesh!=NULL) mesh->ScaleNormals(factor);
if (sibling!=NULL) sibling->ScaleNormals(factor);
if (child!=NULL) child->ScaleNormals(factor);
}

void HierarchyFrame::ComputeBoundingBox(D3DXMATRIX * world,vector * boxes)
{
D3DXMATRIX ThisWorld=TransformationMatrix*(*world);
HierarchyMesh * mesh=(HierarchyMesh*) pMeshContainer;
HierarchyFrame * sibling=(HierarchyFrame *) pFrameSibling;
HierarchyFrame * child=(HierarchyFrame *) pFrameFirstChild;

if (mesh!=NULL) mesh->ComputeBoundingBox(&ThisWorld,boxes);
if (sibling!=NULL) sibling->ComputeBoundingBox(world,boxes);
if (child!=NULL) child->ComputeBoundingBox(&ThisWorld,boxes);
}



//-----------------------------------------------------------------------------
// Top-level management class
//-----------------------------------------------------------------------------
MeshHandler::MeshHandler()
{
RootFrame=NULL;
Mesh=NULL;
Created=false;
}

MeshHandler::~MeshHandler()
{
Destroy();
}

void MeshHandler::Draw(LPDIRECT3DDEVICE9 device,D3DXMATRIX* world)
{
if (Created)
{
device->SetTransform(D3DTS_WORLD,world);
Mesh->Draw(device,true);
Mesh->Draw(device,false);

// RootFrame->Draw(device,world,true);
// RootFrame->Draw(device,world,false);
}
}

void MeshHandler::Create(string pFileName,string pDir,LPDIRECT3DDEVICE9 device)
{
try
{
Mesh=new FileMeshData();
Mesh->Create(pFileName.c_str(),pDir,device);
if (!Mesh->IsCreated())
{
delete Mesh;
throw false;
}
//Mesh->ComputeSphere(Center,Size);
D3DXVECTOR3 a;
D3DXVECTOR3 b;
Mesh->ComputeBoundingBox(a,b);
Center=(a+b)*0.5f;
Size=D3DXVec3Length(&(b-a))*0.5f;
Allocator.device=device;
Allocator.dir=pDir;
LPD3DXFRAME out;
string meshname=pDir+"\\";
meshname+=pFileName;
D3DXLoadMeshHierarchyFromX(meshname.c_str(),D3DXMESH_SYSTEMMEM,device,&Allocator,NULL,&out,NULL);
RootFrame=(HierarchyFrame*)out;
Created=true;
}
catch (...)
{
Created=false;
}
}

void MeshHandler::SetSize(float pSize)
{
if (Created && Size!=0.0f)
{
float scale=pSize/Size;
D3DXMATRIX tran;
D3DXMATRIX size;
D3DXMATRIX out;
D3DXMatrixTranslation(&tran,-Center.x,-Center.y,-Center.z);
D3DXMatrixScaling(&size,scale,scale,scale);
out=tran*size;
Mesh->Transform(-Center,scale);
RootFrame->SetMatrix(&(*RootFrame->GetMatrix()*out));
RootFrame->ScaleNormals(scale);
Size=pSize;
Center=D3DXVECTOR3(0,0,0);
}
}

void MeshHandler::Destroy()
{
if (Created)
{
InvalidateDeviceObjects();
D3DXFrameDestroy(RootFrame,&Allocator);
RootFrame=NULL;
Mesh->Destroy();
delete Mesh;
Created=false;
}
}

void MeshHandler::RestoreDeviceObjects( LPDIRECT3DDEVICE9 device )
{
if (Created)
{
//RootFrame->RestoreDeviceObjects(device);
Mesh->RestoreDeviceObjects(device);
}
}

void MeshHandler::InvalidateDeviceObjects()
{
if (Created)
{
//RootFrame->InvalidateDeviceObjects();
Mesh->InvalidateDeviceObjects();
}
}

void MeshHandler::ComputeBoundingBox()
{
if (Created)
{
D3DXMATRIX identity;
D3DXMatrixIdentity(&identity);
Mesh->ComputeBoundingBox(MainBox.a,MainBox.b);
RootFrame->ComputeBoundingBox(&identity,&Boxes);
}
}

Share this post


Link to post
Share on other sites