Mesh rendering prob

Started by
9 comments, last by shakazed 21 years, 1 month ago
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
Advertisement
*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]
Try to change:
D3DXMATRIX matWorld; //World matrixm_iD3DDev9->SetTransform(D3DTS_WORLD, &matWorld);  

TO:
D3DXMATRIX matWorld; //World matrixD3DXMatrixIdentity(&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]
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
No matter where you go... &this
Changed it but it didn´t make any difference. Will have to compare my code with the SDK tutorial. Thanks anyways!



Bad Monkey Productions
Man I still can´t figure it out. I´m so tired right now, but if anyone would have a look I´ve uploaded the entire source to my page. It´s under the files tab. This is really botherine me



Bad Monkey Productions
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.
Hmm. Thank you for checking it. Will scan the code some more then



Bad Monkey Productions
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
Knowledge is power,information is a tool.UltraviolenZ
Might be it then! Will check it out when I get home from school! Thanks!

EDIT
Nope wasn´t that either. I´m using dx 9


Bad Monkey Productions

[edited by - shakazed on March 20, 2003 1:39:33 PM]

This topic is closed to new replies.

Advertisement