I use this function applyDefaultMaterial() to apply
the same material to all the models in my scene.
The models are stored in a modelNodePtrArray; which
is populated as and when new models are created.
Somehow the following code crashes with a backtrace
that looks like this
msvcr71d.dll!_NMSG_WRITE(int rterrnum=10) Line
195 C
msvcr71d.dll!abort() Line 44 + 0x7 C
IFXCore.dll!IFXDebugAssertBox(char *
exp=0x0779efa4, char * szFile=0x0779ef88, unsigned
int uLineNum=479, char * message=0x0779ef74, int
box=1, int log=1) Line 143 C++
IFXCore.dll!IFXModifierChainState::Build(int
in_bReqValidation=1) Line 480 C++
IFXCore.dll!
CIFXModifierChain::BuildNewModifierState
(IFXModifierChainInternal * in_pBaseChain=0x00000000,
IFXModifierDataPacketInternal *
in_pOverrideDP=0x00000000, unsigned int in_ModIdx=2,
IFXModifier * in_pMod=0x024b5798,
IFXModifierChainState * *
out_ppModChainState=0x00dbfd64, int in_bReplace=0,
int in_bReqVal=1) Line 883 + 0xc C++
> IFXCore.dll!CIFXModifierChain::AddModifier
(IFXModifier & rInModifier={...}, unsigned int
in_Idx=2, int in_ReqVal=1) Line 360 + 0x2e C++
What am I doing wrong ?
-----------
void applyDefaultMaterial()
{
IFXString shaderName("MyDefaultShader");
U32 shaderID = 0;
IFXString materialName("MyDefaultMaterial");
U32 materialID = 0;
IFXPalette* shaderPalette = 0;
IFXPalette* materialPalette = 0;
IFXShaderLitTexture* shaderResource = 0;
IFXMaterialResource* materialResource = 0;
IFXRESULT result = IFX_OK;
sceneGraph->GetPalette(IFXSceneGraph::SHADER,
&shaderPalette);
sceneGraph->GetPalette(IFXSceneGraph::MATERIAL,
&materialPalette);
// Step 1: Material Resource
// Create a material and put it into the material
palette
result = IFXCreateComponent
(CID_IFXMaterialResource,
IID_IFXMaterialResource, (void**)
&materialResource);
if(IFXFAILURE(result))
{
// conversion process was a grand failure
IFXRELEASE(shaderPalette)
IFXRELEASE(materialPalette)
return;
}
materialResource->SetSceneGraph(sceneGraph);
materialResource->SetQualityFactorX
(IFX_DEFAULT_QUALITY_FACTOR);
materialPalette->Add(materialName, &materialID);
materialPalette->SetResourcePtr(materialID,
materialResource);
// set material parameters
IFXVector4 materialAmbient(0.7f, 0.7f, 0.7f,
1.0f);
IFXVector4 materialDiffuse(0.7f, 0.7f, 0.7f,
1.0f);
IFXVector4 materialSpecular(1.0f, 1.0f, 1.0f,
1.0f);
IFXVector4 materialEmission(0.2f, 0.2f, 0.2f,
1.0f);
F32 materialOpacity = 1.0f;
F32 materialReflectivity = 0.2f;
materialResource->SetAmbient(materialAmbient);
materialResource->SetDiffuse(materialDiffuse);
materialResource->SetSpecular(materialSpecular);
materialResource->SetEmission(materialEmission);
materialResource->SetOpacity(materialOpacity);
materialResource->SetReflectivity
(materialReflectivity);
// Step 2: Shader
// Create a shader and put it into the shader
palette
result = IFXCreateComponent
(CID_IFXShaderLitTexture,
IID_IFXShaderLitTexture, (void**)
&shaderResource);
if(IFXFAILURE(result))
{
// conversion process was a grand failure
IFXRELEASE(materialResource)
IFXRELEASE(shaderPalette)
IFXRELEASE(materialPalette)
return;
}
shaderResource->SetSceneGraph(sceneGraph);
shaderResource->SetQualityFactorX
(IFX_DEFAULT_QUALITY_FACTOR);
shaderPalette->Add(shaderName, &shaderID);
shaderPalette->SetResourcePtr(shaderID,
shaderResource);
// associate material with the shader
shaderResource->SetMaterialID(materialID);
// Step 3: For each model node whose pointer is
listed in
// modelNodePtrArray, do the following
// 1. Create IFXShadingModifier and add it to
the modifier
// chain of the model node.
// 2. Create a IFXShaderList and set it as the
shading list
// for the modifier prepared in the previous
step.
// 3. Add shaderID to the shader list created
in the
// previous step.
U32 modelCount =
modelNodePtrArray.GetNumberElements();
for(U32 i=0; i<modelCount; i++)
{
IFXModel* modelNode = 0;
IFXShaderList* shaderList = 0;
IFXShadingModifier* shadingModifier = 0;
IFXModifierChain* modifierChain = 0;
modelNode = modelNodePtrArray[i];
if(IFXFAILURE(result))
continue;
// create shader list.
result = IFXCreateComponent(CID_IFXShaderList,
IID_IFXShaderList, (void**)&shaderList);
if(IFXFAILURE(result))
{
IFXRELEASE(modelNode)
continue;
}
shaderList->Allocate(1);
shaderList->SetShader(0, shaderID);
// create shading modifier
result = IFXCreateComponent
(CID_IFXShadingModifier,
IID_IFXShadingModifier, (void**)
&shadingModifier);
if(IFXFAILURE(result))
{
IFXRELEASE(modelNode)
IFXRELEASE(shaderList)
continue;
}
shadingModifier->SetSceneGraph(sceneGraph);
shadingModifier->SetElementShaderList(0,
shaderList);
// add shading modifier to the modifier chain
of
// the model node.
modelNode->GetModifierChain(&modifierChain);
modifierChain->AddModifier(*shadingModifier);
IFXRELEASE(modelNode)
IFXRELEASE(shaderList)
IFXRELEASE(shadingModifier)
IFXRELEASE(modifierChain)
}
IFXRELEASE(shaderResource)
IFXRELEASE(materialResource)
IFXRELEASE(shaderPalette)
IFXRELEASE(materialPalette)
}
-----------