Original Post
I make my class (very simple) static loads Meshs:
XMesh.h
XMesh.cpp
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:
My render:
Thanks guys :)
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};#endifXMesh.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 :)