Archived

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

shakazed

Annoying problem

Recommended Posts

Hi! I have a class that loads and .x model and renders it. The load function requires an IDirect3DDevice9 object to be sent to it, this wokrs fine but when I try to use the id3d object again outside the class it fails on any commands. Is this a lost device problem or what could it be?

Share this post


Link to post
Share on other sites
Here´s the class.


class CXModel
{
public:

HRESULT LoadXModel(IDirect3DDevice9 *p_iD3DDevice9);
HRESULT RenderXModel(void);

private:

ID3DXMesh *p_Mesh;
LPD3DXBUFFER lpd3dxMtrlBuffer;
DWORD dwNumMtrls;
D3DMATERIAL9 *p_d3dMtrls9;
LPDIRECT3DTEXTURE9 *p_iD3DMeshTextures9;


};

HRESULT CXModel::LoadXModel(IDirect3DDevice9 *p_iD3DDevice9)
{

DX_ERROR_CHK(D3DXLoadMeshFromX("Meshes\\tiger.x", D3DXMESH_SYSTEMMEM,
p_iD3DDevice9, NULL, &lpd3dxMtrlBuffer, NULL, &dwNumMtrls, &p_Mesh), "Failed to load mesh");

D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)lpd3dxMtrlBuffer->GetBufferPointer();

p_d3dMtrls9 = new D3DMATERIAL9[dwNumMtrls];
p_iD3DMeshTextures9 = new LPDIRECT3DTEXTURE9[dwNumMtrls];

for(unsigned int i = 0; i < dwNumMtrls; i++)
{
p_d3dMtrls9[i] = d3dxMaterials[i].MatD3D;
p_d3dMtrls9[i].Ambient = p_d3dMtrls9[i].Diffuse;

DX_ERROR_CHK(D3DXCreateTextureFromFile(p_iD3DDevice9,
"Meshes\\Tiger.bmp", &p_iD3DMeshTextures9[i]), "Failed to load texture subset");

}



return S_OK;
}



I call this sending my initialized device object

CXModel cModel;
cModel.LoadXModel(p_iD3DDevice9);

Share this post


Link to post
Share on other sites
You mean this?


//-----------------------------INCLUDES-------------------------------


#include<d3d9.h>
#include<d3dx9.h>
#include<windows.h>
#include"Graphics.h"
#include"CXModel.h"

//-----------------------------DEFINITIONS-------------------------------


#define DX_ERROR_CHK(function, error) if(FAILED(function)){MessageBox(NULL,error, "Something.exe",MB_OK); return E_FAIL;}
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_TEX1)

//-----------------------------GLOBALS-------------------------------



IDirect3DDevice9 *p_iD3DDevice9;
IDirect3D9 *p_iD3D9;
IDirect3DVertexBuffer9 *p_iD3DTerrainVertexBuffer9;
IDirect3DTexture9 *p_iD3DTerrainTexture9;

extern HWND hWnd; //Defined in WinMain.cpp

float fAngle; //Camera angle


CXModel cModel;

//---------------------------------STRUCTS/CLASSES-------------------------------


struct SCustomVertex
{
float x,y,z; //Position

float u,v; //Texture coordinates

};

//-----------------------------FUNCTIONS-------------------------------


HRESULT InitializeD3D(void)
{
p_iD3D9 = Direct3DCreate9(D3D_SDK_VERSION);

D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;


DX_ERROR_CHK(p_iD3D9->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,
hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &p_iD3DDevice9), "Failed to create device");

//-----------------------------LOAD TERRAIN OBJECTS---------------------------------------


LoadTerrain();
cModel.LoadXModel(p_iD3DDevice9);

//-----------------------------RENDER STATES----------------------------------------------


p_iD3DDevice9->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
p_iD3DDevice9->SetRenderState(D3DRS_LIGHTING, FALSE);

return S_OK;

}

HRESULT RenderScene(void)
{
p_iD3DDevice9->Clear(0,NULL,D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f,0);

//-----------------------START RENDERING SCENE---------------------------------


p_iD3DDevice9->BeginScene();

SetCamera(); //Set camera position and lookat point

RenderTerrain(); //Render the base terrain


p_iD3DDevice9->EndScene();

//-----------------------STOP RENDERING SCENE----------------------------------


p_iD3DDevice9->Present(NULL,NULL,NULL,NULL);

return S_OK;

}

HRESULT LoadTerrain(void)
{
SCustomVertex sTerrainVertices[] =
{
{0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
{0.0f, 0.0f, 10.0f, 0.0f, 0.0f},
{10.0f, 0.0f, 0.0f, 1.0f, 1.0f},
{10.0f, 0.0f, 10.0f, 1.0f, 0.0f},
};

DX_ERROR_CHK(p_iD3DDevice9->CreateVertexBuffer(4*sizeof(SCustomVertex), 0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &p_iD3DTerrainVertexBuffer9,NULL), "Failed to create terrain vertex buffer");

VOID *p_Vertices;

DX_ERROR_CHK(p_iD3DTerrainVertexBuffer9->Lock(0,sizeof(SCustomVertex), (VOID**)&p_Vertices, 0),
"Failed to lock terrain vertex buffer");

memcpy(p_Vertices, sTerrainVertices, sizeof(sTerrainVertices));

DX_ERROR_CHK(p_iD3DTerrainVertexBuffer9->Unlock(), "Failed to unlock terrain vertex buffer");


//----------------------------------LOAD TERRAIN TEXTURE----------------------------------


D3DXCreateTextureFromFile(p_iD3DDevice9, "Textures\\Grass.bmp", &p_iD3DTerrainTexture9);

return S_OK;
}

HRESULT RenderTerrain(void)
{
p_iD3DDevice9->SetStreamSource(0,p_iD3DTerrainVertexBuffer9, 0, sizeof(SCustomVertex));
p_iD3DDevice9->SetFVF(D3DFVF_CUSTOMVERTEX);
p_iD3DDevice9->SetTexture(0, p_iD3DTerrainTexture9);
p_iD3DDevice9->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);

return S_OK;
}

HRESULT SetCamera(void)
{
D3DXMATRIX matView;
D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(0.0f,2.0f,-10.0f),
&D3DXVECTOR3(0.0f,0.0f,0.0f),
&D3DXVECTOR3(0.0f,1.0f,0.0f));
p_iD3DDevice9->SetTransform(D3DTS_VIEW, &matView);

D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f);
p_iD3DDevice9->SetTransform(D3DTS_PROJECTION, &matProj);

return S_OK;
}


void ReleaseGraphics(void)
{
if(p_iD3DTerrainTexture9 != NULL)
{
p_iD3DTerrainTexture9->Release();
p_iD3DTerrainTexture9 = NULL;
}
if(p_iD3DTerrainVertexBuffer9 != NULL)
{
p_iD3DTerrainVertexBuffer9->Release();
p_iD3DTerrainVertexBuffer9 = NULL;
}
if(p_iD3DDevice9 != NULL)
{
p_iD3DDevice9->Release();
p_iD3DDevice9 = NULL;
}
if(p_iD3D9 != NULL)
{
p_iD3D9->Release();
p_iD3D9 = NULL;
}
}
void GetInput(void)
{

}

void SetupSkyBox(void)
{
}

Share this post


Link to post
Share on other sites