Jump to content
  • Advertisement
Sign in to follow this  
Tiege

Material and Texture not rendering?

This topic is 2544 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can anyone see something in this code that I can't as to why my materials and textures wont load on my animated models? I'm sure the pathing is correct because I can get it to work for a static model but when I load mesh from hierarchy the model loads, renders, and animates but it's just black (no material or texture).

Any thoughts? I am using code from tutorials I've read



HRESULT CMeshHierarchy::CreateMeshContainer(
LPCSTR Name,
CONST D3DXMESHDATA *meshData,
CONST D3DXMATERIAL *materials,
CONST D3DXEFFECTINSTANCE *effectInstances,
DWORD numMaterials,
CONST DWORD *adjacency,
LPD3DXSKININFO pSkinInfo,
LPD3DXMESHCONTAINER* retNewMeshContainer)
{
// Create a mesh container structure to fill and initilaise to zero values
// Note: I use my extended version of the structure (D3DXMESHCONTAINER_EXTENDED) defined in MeshStructures.h
D3DXMESHCONTAINER_EXTENDED *newMeshContainer=new D3DXMESHCONTAINER_EXTENDED;
ZeroMemory(newMeshContainer, sizeof(D3DXMESHCONTAINER_EXTENDED));

// Always a good idea to initialise return pointer before proceeding
*retNewMeshContainer = 0;

// The mesh name (may be 0) needs copying over
if (Name != NULL)
{
newMeshContainer->Name = new char[strlen(Name) + 1];
strcpy(newMeshContainer->Name, Name);
}

// The mesh type (D3DXMESHTYPE_MESH, D3DXMESHTYPE_PMESH or D3DXMESHTYPE_PATCHMESH)
if (meshData->Type!=D3DXMESHTYPE_MESH)
{
// This demo does not handle mesh types other than the standard
// Other types are D3DXMESHTYPE_PMESH (progressive mesh) and D3DXMESHTYPE_PATCHMESH (patch mesh)
DestroyMeshContainer(newMeshContainer);
return E_FAIL;
}

newMeshContainer->MeshData.Type = D3DXMESHTYPE_MESH;

// Adjacency data - holds information about triangle adjacency, required by the ID3DMESH object
DWORD dwFaces = meshData->pMesh->GetNumFaces();
newMeshContainer->pAdjacency = new DWORD[dwFaces*3];
if (adjacency)
{
memcpy(newMeshContainer->pAdjacency, adjacency, sizeof(DWORD) * dwFaces*3);
}
else
{
// Added 24/08/10: previously did not detect null adjacency
if(FAILED( newMeshContainer->MeshData.pMesh->GenerateAdjacency( 1e-6f, (DWORD*)(void*)newMeshContainer->pAdjacency ) ) )
FillMemory((void*)newMeshContainer->pAdjacency, dwFaces*3, 0xFF );
}

// Get the Direct3D device, luckily this is held in the mesh itself (Note: must release it when done with it)
LPDIRECT3DDEVICE9 pd3dDevice = 0;
meshData->pMesh->GetDevice(&pd3dDevice);

// Changed 24/09/07 - can just assign pointer and add a ref rather than need to clone
newMeshContainer->MeshData.pMesh=meshData->pMesh;
newMeshContainer->MeshData.pMesh->AddRef();

// Create material and texture arrays. Note that I always want to have at least one
newMeshContainer->NumMaterials = max(numMaterials,1);
newMeshContainer->exMaterials = new D3DMATERIAL9[newMeshContainer->NumMaterials];
newMeshContainer->exTextures = new LPDIRECT3DTEXTURE9[newMeshContainer->NumMaterials];

ZeroMemory(newMeshContainer->exTextures, sizeof(LPDIRECT3DTEXTURE9) * newMeshContainer->NumMaterials);

if (numMaterials>0)
{
// Load all the textures and copy the materials over
for(DWORD i = 0; i < numMaterials; ++i)
{
newMeshContainer->exTextures = 0;
newMeshContainer->exMaterials=materials.MatD3D;

if(materials.pTextureFilename)
{

D3DXCreateTextureFromFile(pd3dDevice, materials.pTextureFilename,
&newMeshContainer->exTextures);



}
}
}
else
// make a default material in the case where the mesh did not provide one
{
ZeroMemory(&newMeshContainer->exMaterials[0], sizeof( D3DMATERIAL9 ) );
newMeshContainer->exMaterials[0].Diffuse.r = 0.5f;
newMeshContainer->exMaterials[0].Diffuse.g = 0.5f;
newMeshContainer->exMaterials[0].Diffuse.b = 0.5f;
newMeshContainer->exMaterials[0].Specular = newMeshContainer->exMaterials[0].Diffuse;
newMeshContainer->exTextures[0]=0;
}

// If there is skin data associated with the mesh copy it over
if (pSkinInfo)
{
// save off the SkinInfo
newMeshContainer->pSkinInfo = pSkinInfo;
pSkinInfo->AddRef();

// Need an array of offset matrices to move the vertices from the figure space to the bone's space
UINT numBones = pSkinInfo->GetNumBones();
newMeshContainer->exBoneOffsets = new D3DXMATRIX[numBones];

// Create the arrays for the bones and the frame matrices
newMeshContainer->exFrameCombinedMatrixPointer = new D3DXMATRIX*[numBones];

// get each of the bone offset matrices so that we don't need to get them later
for (UINT i = 0; i < numBones; i++)
newMeshContainer->exBoneOffsets = *(newMeshContainer->pSkinInfo->GetBoneOffsetMatrix(i));


// Note: in the Microsoft samples a GenerateSkinnedMesh function is called here in order to prepare
// the skinned mesh data for optimial hardware acceleration. As mentioned in the notes this sample
// does not do hardware skinning but instead uses software skinning.
}
else
{
// No skin info so 0 all the pointers
newMeshContainer->pSkinInfo = 0;
newMeshContainer->exBoneOffsets = 0;
newMeshContainer->exSkinMesh = 0;
newMeshContainer->exFrameCombinedMatrixPointer = 0;
}

// When we got the device we caused an internal reference count to be incremented
// So we now need to release it
pd3dDevice->Release();

// The mesh may contain a reference to an effect file
if (effectInstances)
{
if (effectInstances->pEffectFilename)
MessageBox(NULL, "Effect Instance", "Model Load Error", MB_OK);
}

// Set the output mesh container pointer to our newly created one
*retNewMeshContainer = newMeshContainer;

return S_OK;
}




void Model::DrawMeshContainer(LPD3DXMESHCONTAINER meshContainerBase, LPD3DXFRAME frameBase)
{
// Cast to our extended frame type
D3DXFRAME_EXTENDED *frame = (D3DXFRAME_EXTENDED*)frameBase;

// Cast to our extended mesh container
D3DXMESHCONTAINER_EXTENDED *meshContainer = (D3DXMESHCONTAINER_EXTENDED*)meshContainerBase;

// Loop through all the materials in the mesh rendering each subset
for (unsigned int iMaterial = 0; iMaterial < meshContainer->NumMaterials; iMaterial++)
{
// use the material in our extended data rather than the one in meshContainer->pMaterials[iMaterial].MatD3D
pd3dDevice->SetMaterial( &meshContainer->exMaterials[iMaterial] );
pd3dDevice->SetTexture( 0, meshContainer->exTextures[iMaterial] );


// Select the mesh to draw, if there is skin then use the skinned mesh else the normal one

LPD3DXMESH pDrawMesh = (meshContainer->pSkinInfo) ? meshContainer->exSkinMesh: meshContainer->MeshData.pMesh;
// Finally Call the mesh draw function
pDrawMesh->DrawSubset(iMaterial);
}
}

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!