Skip to main content
gamedev.net
🔒 Locked

D3DMATERIAL9

Started by VitaliBR 5 replies 2.2k views
Original Post
VitaliBR
VitaliBR
I make my class (very simple) static loads Meshs:

XMesh.h
#ifndef _X_MESH_HEADER_____#define _X_MESH_HEADER_____#include <d3dx9.h>#include <d3d9.h>class XMesh{public:	XMesh();	~XMesh();	void Release();	bool LoadMesh(IDirect3DDevice9 *pDevice, char *szFolder, char *szFileName, const bool bComputeNormals);	void RenderMesh(IDirect3DDevice9 *pDevice);		void changePosition(D3DXVECTOR3 positionVector);		void setPosition(D3DXVECTOR3 positionVector);	void setRotation(D3DXVECTOR3 rotationVector);	void setScale(D3DXVECTOR3 scaleVector);		LPD3DXMESH              getMesh();		 //Return the Mesh	DWORD					getnMaterials(); //Return the nMaterials	D3DXVECTOR3				m_Min, m_Max;    // Bounding boxprivate:	ID3DXMesh		   *m_pMesh;	IDirect3DTexture9 **m_ppTextures;	DWORD				m_nMaterials;	D3DXMATRIX			transMatrix;		// the translation matrix	D3DXMATRIX			rotationMatrix;		// the rotation matrix	D3DXMATRIX			scaleMatrix;		// the scale matrix	D3DXVECTOR3		    scale;			//the models scale	D3DXVECTOR3			position;		//the models position	D3DXVECTOR3			rotation;		//the models rotation};#endif


XMesh.cpp
#include "XMesh.h"//// Constructor of XMesh//XMesh::XMesh(){	m_ppTextures = NULL;	m_pMesh = NULL;	m_nMaterials = 0x0;	m_Min.x = m_Min.y = m_Min.z = m_Max.x = m_Max.y = m_Max.z = 0.0f;}//// Destructor of XMesh//XMesh::~XMesh(){	Release();}//// Release of XMesh//void XMesh::Release(){	//Release the textures	if (m_ppTextures)	{		for (int t=0; t<(int)m_nMaterials; t++)		{			if (m_ppTextures[t])				m_ppTextures[t]->Release();		}		delete [] m_ppTextures;		m_ppTextures = NULL;	}		//Release the Mesh	if (m_pMesh != NULL)	{		m_pMesh->Release();		m_pMesh = NULL;	}	//Release the Materials	m_nMaterials = 0;}//// Load the Mesh//bool XMesh::LoadMesh(IDirect3DDevice9 *pDevice, char *szFolder, char *szFileName, const bool bComputeNormals){	Release();	scale = D3DXVECTOR3(1.0f, 1.0f , 1.0f); //set our scale variable to 1	position = D3DXVECTOR3(0.0f, 0.0f , 0.0f); //set the position to 0,0,0	rotation = D3DXVECTOR3(0.0f, 0.0f , 0.0f); //set the rotation, to nothing.	ID3DXBuffer *pBuffer;	char szLocalization[1024];	if (szFolder)		strcpy(szLocalization, szFolder);	else		szLocalization[0] = '\0';	strcat(szLocalization, szFileName);		// Load the mesh	if(FAILED(D3DXLoadMeshFromX(szLocalization,		// Localization of the Mesh								D3DXMESH_SYSTEMMEM,	// where the data will be allocated in memory								pDevice,			// pointer to the device								NULL,				// buffer for adjacency (do not need it)								&pBuffer,			// buffer where to store the texture information, ...								NULL,				// buffer for efects (do not need it)								&m_nMaterials,		// total of different materials								&m_pMesh)))			// pointer to an object of mesh where our mesh is stored	{		MessageBox(NULL, "Mesh not found!", "Erro", MB_OK);		return false;	}	// takes a pointer array of materials	D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pBuffer->GetBufferPointer();	// creates a pointer to array of textures. Each array element is a texture	m_ppTextures = new IDirect3DTexture9 * [m_nMaterials];	for(int t=0; t<(int)m_nMaterials; t++)	{			// First of all, each texture zeroes in the array		m_ppTextures[t] = NULL;			// if the current material has a texture ...		if(d3dxMaterials[t].pTextureFilename != NULL && strlen(d3dxMaterials[t].pTextureFilename) > 0)		{			if (szFolder)				strcpy(szLocalization, szFolder);			else				szLocalization[0] = '\0';			strcat(szLocalization, d3dxMaterials[t].pTextureFilename);				// load the texture...			if(FAILED(D3DXCreateTextureFromFile(pDevice, 												szLocalization, 												&m_ppTextures[t])))			{				MessageBox(NULL, "Textura not found!", d3dxMaterials[t].pTextureFilename, MB_OK);			}		}	}	// Clears the buffer material	pBuffer->Release();	// If you want recomputes the normal ...	if (bComputeNormals)		D3DXComputeNormals(m_pMesh, NULL);		return true;	BYTE* pVertices=NULL;	m_pMesh->LockVertexBuffer(D3DLOCK_READONLY, (LPVOID*)&pVertices);		D3DXComputeBoundingBox((D3DXVECTOR3*)pVertices, m_pMesh->GetNumVertices(), D3DXGetFVFVertexSize(m_pMesh->GetFVF()), &m_Min, &m_Max);	m_pMesh->UnlockVertexBuffer();}void XMesh::RenderMesh(IDirect3DDevice9 *pDevice){	//set the scale matrix	D3DXMatrixScaling(&scaleMatrix, scale.x,scale.y,scale.z);	// store the position information into the translation matrix	D3DXMatrixTranslation(&transMatrix, position.x, position.y, position.z);	// set the rotation matrix	D3DXMatrixRotationYawPitchRoll(&rotationMatrix,rotation.x,rotation.y,rotation.z);	// Multiply the translation matrix by the rotation matrix, store the result in the translation matrix	D3DXMatrixMultiply(&transMatrix, &rotationMatrix, &transMatrix);	// Multiply the translation matrix by the scale matrix,  store the result in the translation matrix	D3DXMatrixMultiply(&transMatrix, &scaleMatrix, &transMatrix);		// Position the object for rendering based on the position, rotation and scaleing.	pDevice->SetTransform(D3DTS_WORLD, &transMatrix);	for(int t=0; t<(int)m_nMaterials; t++)	{		// arrow texture of that part of meshairplane		if (m_ppTextures)			pDevice->SetTexture(0, m_ppTextures[t]);			// designing this part of the mesh			m_pMesh->DrawSubset(t);	}	// Now that we draw the mesh, we can reset the texture	// So that the texture of the mesh is not applied to other things	// Accidentally	pDevice->SetTexture(0, NULL);}////   Note: sets the position of the model//void XMesh::setPosition(D3DXVECTOR3 positionVector){	position = positionVector;}////   Note: adds the changeVector to the position, adjusting the position relitive to its current position//void XMesh::changePosition(D3DXVECTOR3 changeVector){	position = position + changeVector;}////   Note: sets the rotation of the model//void XMesh::setRotation(D3DXVECTOR3 rotationVector){	rotation = rotationVector;}////   Note: sets the scale of the model//void XMesh::setScale(D3DXVECTOR3 scaleVector){	scale = scaleVector;}////   Note: Return the current Mesh.//LPD3DXMESH XMesh::getMesh(){ 	return m_pMesh; }////   Note: Return the Materials//DWORD XMesh::getnMaterials(){	return m_nMaterials;}


But I'm not using the Materials,
I tried to implement the materials, but I couldn't :(


I'm looking this source for help me:
http://read.pudn.com/downloads87/sourcecode/graph/333737/XFile.cpp__.htm

Render of Source:
       // render the mesh           for(DWORD i = 0; i < mesh->m_num_materials; i++)           {               // set the materials properties for the device               g_d3d_device->SetMaterial(&mesh->m_materials);                  // assigns a texture to a stage for a device               g_d3d_device->SetTexture(0, mesh->m_textures);                  // draw a subset of a mesh               mesh_to_draw->DrawSubset(i);           }   


My render:
	for(int t=0; t<(int)m_nMaterials; t++)	{		// arrow texture of that part of meshairplane		if (m_ppTextures)			pDevice->SetTexture(0, m_ppTextures[t]);			// designing this part of the mesh			m_pMesh->DrawSubset(t);	}


Thanks guys :)
http://mateusvitali.wordpress.com/
Gage64
Gage64
Add this variable to the class:

D3DXMATERIAL* m_ppMaterials;

In, XMesh::LoadMesh(), change this:

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

to this:

m_ppMaterials = (D3DXMATERIAL*)pBuffer->GetBufferPointer();

and replace all references to d3dxMaterials with m_ppMaterials.

Then modify the drawing code to use the material:

    for(int t=0; t<(int)m_nMaterials; t++)    {        pDevice->SetMaterial(&m_ppMaterials[t].MatD3D);        // arrow texture of that part of meshairplane	if (m_ppTextures)	    pDevice->SetTexture(0, m_ppTextures[t]);		// designing this part of the mesh	m_pMesh->DrawSubset(t);    }
VitaliBR
VitaliBR
Only that Gage64? :D

Just an observation, would:

D3DXMATERIAL* m_ppMaterials;
or
D3DMATERIAL9* m_ppMaterials;

?


Thanks :)
http://mateusvitali.wordpress.com/
Gage64
Gage64
Just realized that my code won't work, since the buffer material is released here:

// Clears the buffer materialpBuffer->Release();


so you can't keep a pointer to it. You'll need to keep the buffer around and only release it in the destructor.

This isn't the best way to do this though, look at tutorial 6 in the SDK for a better solution.
VitaliBR
VitaliBR
pDevice->SetMaterial(&m_ppMaterials[t].MatD3D)


What is MatD3D?

Thanks

EDIT:
I ended up using D3DMATERIAL9 and it worked, thanks for the help friend

I have:
	IDirect3DTexture9 **m_ppTextures;		// array of textures  	D3DMATERIAL9*       m_ppMaterials;		// array of materials  


And I need to return them in a function

I try this:
	D3DMATERIAL9			getMaterials();	LPDIRECT3DTEXTURE		getTextures();//...//...D3DMATERIAL9 XMesh::getMaterials(){	return m_ppMaterials;}LPDIRECT3DTEXTURE XMesh::getTextures(){	return m_ppTextures;}


But it is not working :/


[Edited by - VitaliBR on September 13, 2010 9:46:34 AM]
http://mateusvitali.wordpress.com/

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.