Archived

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

shakazed

Mesh rendering prob

Recommended Posts

shakazed    138
Well, I started to work on a .x loader but there is a slight problem. I can´t get the mesh to render. I dunno is it´s just the camera or something wrong in the loading code but I would appreciate if someone could check this code out and maybe inform me on what´s wrong. First the function that loads the model
  
bool cXEngine::GetXFile(void)
{
	LPD3DXBUFFER lpD3DMatBuf; //Material and texture buffer


	if(FAILED(D3DXLoadMeshFromX("tiger.x", D3DXMESH_SYSTEMMEM, 
		m_iD3DDev9, NULL, &lpD3DMatBuf, NULL, &m_dwNumMaterials, &m_iMesh)))
	{
		return false;
	}

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


	m_matMaterials  = new D3DMATERIAL9[m_dwNumMaterials];
	m_matTextures   = new LPDIRECT3DTEXTURE9[m_dwNumMaterials];

	for(DWORD i=0;i<m_dwNumMaterials;i++)
	{
		m_matMaterials[i] = d3dxMaterials[i].MatD3D;
		m_matMaterials[i].Ambient = m_matMaterials[i].Diffuse;

		m_matTextures[i] = NULL;
		// Create the texture.

		if( FAILED( D3DXCreateTextureFromFile( m_iD3DDev9, 
			d3dxMaterials[i].pTextureFilename, &m_matTextures[i] ) ) )
		{
			return false;
		}
			m_matTextures[i] = NULL;
	}
	
	lpD3DMatBuf->Release();

	return true;
}
  
And the code that displays it
  
void cXEngine::Render(void)
{
	m_iD3DDev9->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255),1.0f,0);
	
	SetupMatrices(); //Setup world,view,projection matrices


	m_iD3DDev9->BeginScene();

	//Render the mesh

	for(DWORD i=1; i<m_dwNumMaterials;i++)
	{
		m_iD3DDev9->SetMaterial( &m_matMaterials[i] );
		m_iD3DDev9->SetTexture( 0, m_matTextures[i] );
		m_iMesh->DrawSubset( i );
	}

	m_iD3DDev9->EndScene();
	
	m_iD3DDev9->Present(NULL,NULL,NULL,NULL); //Present the scene, Src=bb, dest=frontb,dest win=hWnd, dirty=NULL


}
  
Oh and the camera if that´s necessary
  
void cXEngine::SetupMatrices(void)
{

	D3DXVECTOR3 vCamPos(0.0f, 3.0f,-5.0f);
	D3DXVECTOR3 vCamLookAt(0.0f, 0.0f, 0.0f);
	D3DXVECTOR3 vCamUpDown(0.0f, 1.0f, 0.0f);

	D3DXMATRIX matWorld; //World matrix

	m_iD3DDev9->SetTransform(D3DTS_WORLD, &matWorld);

	D3DXMATRIX matView; //View(camera) matrix

	D3DXMatrixLookAtLH(&matView, &vCamPos, &vCamLookAt, &vCamUpDown);
	m_iD3DDev9->SetTransform(D3DTS_VIEW, &matView);

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

Bad Monkey Productions

Share this post


Link to post
Share on other sites
Bilgates    128
*DISCLAIMER*
I'm an idiot so don't listen to me.

I thought the camera positions z component could be from 0 to 1. 1 being the furthest point possible before an object disapears.

---Nevermind. I was wrong.

[edited by - Bilgates on March 18, 2003 7:12:25 PM]

Share this post


Link to post
Share on other sites
LukeSkyRunner    136
Try to change:

D3DXMATRIX matWorld; //World matrix
m_iD3DDev9->SetTransform(D3DTS_WORLD, &matWorld);

TO:

D3DXMATRIX matWorld; //World matrix
D3DXMatrixIdentity(&matWorld);
m_iD3DDev9->SetTransform(D3DTS_WORLD, &matWorld);


I'm not sure about it, but I think that the matrix (D3DXMATRIX) is not automaticaly identity when you declare it. So, you will never take a correct geometry without right matrices transformations.

[edited by - lukeskyrunner on March 18, 2003 7:18:30 PM]

Share this post


Link to post
Share on other sites
DerMeister    122
I agree with LukeSkyRunner, you must set the world matrix to a the matrix identity which is:
1.0,0.0,0.0,0.0,
0.0,1.0,0.0,0.0,
0.0,0.0,1.0,0.0,
0.0,0.0,0.0,1.0,

(using D3DXMatrixIdentity())
Which means no transformation is made to the world matrix.
But if you don''t do this, you are not initializing the matWorld object, so instead of setting that matrix you are setting a 4x4 matrix with all its members uninitialized or with a value like:
-4.31602e+008. So don''t expect your code to render properly.



No matter where you go... &this

Share this post


Link to post
Share on other sites
Bilgates    128
Your mesh isn''t being loaded. I don''t know why since I don''t know how to load in a mesh myself.

bool cXEngine::GetXFile(void)
{
LPD3DXBUFFER lpD3DMatBuf; //Material and texture buffer
LPCTSTR filename("tiger.x");

if(FAILED(D3DXLoadMeshFromX(filename, D3DPOOL_MANAGED,
m_iD3DDev9, NULL, &lpD3DMatBuf, NULL, &m_dwNumMaterials, &m_iMesh)))
{
return false;
}

This is returning false.

Share this post


Link to post
Share on other sites
UltraviolenZ    122
shakazed,

You are passing 8 parameters to D3DXLoadMeshFromX. I think that is where your problem lies:

HRESULT D3DXLoadMeshFromX(
LPSTR pFilename,
DWORD Options,
LPDIRECT3DDEVICE8 pDevice,
LPD3DXBUFFER* ppAdjacency,
LPD3DXBUFFER* ppMaterials,
PDWORD pNumMaterials,
LPD3DXMESH* ppMesh
);

but maybe you''re using a different version than I am.

What I do when I run into problems where I suspect a function not doing what I expect: I let the program display a messagebox to inform me.


Knowledge is power,
information is a tool.

UltraviolenZ

Share this post


Link to post
Share on other sites
shakazed    138
Updated my site with the latest code. Haven´t found out what could be the source of the malfunction. It´seems to be loading the model allright and there is nothing wrong with the rendering otherwise. Please have a look at the code if you´ve got the time, I´m about to Harakiri meself.




Bad Monkey Productions

[edited by - shakazed on March 21, 2003 1:56:33 AM]

Share this post


Link to post
Share on other sites