// if materials provided, copy them
if( NumMaterials > 0 )
{
memcpy( pMeshContainer->pMaterials, pMaterials, sizeof( D3DXMATERIAL ) * NumMaterials );
for( iMaterial = 0; iMaterial < NumMaterials; iMaterial++ )
{
if( pMeshContainer->pMaterials[iMaterial].pTextureFilename != NULL )
{
LPDIRECT3DTEXTURE9 texture = NULL;
WCHAR strTexturePath[MAX_PATH];
WCHAR wszBuf[MAX_PATH] = L"Data\\";
MultiByteToWideChar( CP_ACP, 0, pMeshContainer->pMaterials[iMaterial].pTextureFilename, -1, strTexturePath, MAX_PATH );
wcsncat(wszBuf, strTexturePath, wcslen(wszBuf)+wcslen(strTexturePath));
if( FAILED( D3DXCreateTextureFromFileW( pd3dDevice, wszBuf,
&texture) ) )
{
pMeshContainer->textures.clear();
}
else
{
pMeshContainer->textures.push_back(texture);
}
// don't remember a pointer into the dynamic memory, just forget the name after loading
pMeshContainer->pMaterials[iMaterial].pTextureFilename = NULL;
}
}
}
else // if no materials provided, use a default one
{
pMeshContainer->pMaterials[0].pTextureFilename = NULL;
memset( &pMeshContainer->pMaterials[0].MatD3D, 0, sizeof( D3DMATERIAL9 ) );
pMeshContainer->pMaterials[0].MatD3D.Diffuse.r = 0.5f;
pMeshContainer->pMaterials[0].MatD3D.Diffuse.g = 0.5f;
pMeshContainer->pMaterials[0].MatD3D.Diffuse.b = 0.5f;
pMeshContainer->pMaterials[0].MatD3D.Specular = pMeshContainer->pMaterials[0].MatD3D.Diffuse;
}
In Rendering code,
for (iAttrib = 0; iAttrib < pMeshContainer->NumAttributeGroups; iAttrib++)
{
D3DXMATRIX invWorld;
D3DXMatrixInverse(&invWorld, NULL, &pFrame->CombinedTransformationMatrix);
D3DXMatrixTranspose(&invWorld, &invWorld);
//Normal Static Mesh
g_pNormalEffect->SetMatrix("gWVP", &(pFrame->CombinedTransformationMatrix * (m_Cam.GetViewMatrix() * m_Cam.GetProjMatrix())));
g_pNormalEffect->SetMatrix("gWorld", &pFrame->CombinedTransformationMatrix);
g_pNormalEffect->SetMatrix("gWorldInvTrans", &invWorld);
D3DXHANDLE hTech = g_pNormalEffect->GetTechniqueByName("PhongDirLtTexTech");
g_pNormalEffect->SetTechnique(hTech);
// Sum of all ambient and emissive contribution
D3DXCOLOR color1( pMeshContainer->pMaterials[iAttrib].MatD3D.Ambient );
D3DXCOLOR color2( .25, .25, .25, 1.0 );
D3DXCOLOR ambEmm;
D3DXColorModulate( &ambEmm, &color1, &color2 );
ambEmm += D3DXCOLOR( pMeshContainer->pMaterials[iAttrib].MatD3D.Emissive );
//Render the mesh
// set material color properties
g_pNormalEffect->SetVector( "MaterialDiffuse",
( D3DXVECTOR4* )&(
pMeshContainer->pMaterials[iAttrib].MatD3D.Diffuse ) );
g_pNormalEffect->SetVector( "MaterialAmbient", ( D3DXVECTOR4* )&ambEmm ) ;
if (pMeshContainer->pMaterials[iAttrib].MatD3D.Diffuse.a < 1.0f)
{
//g_pEffect1->SetValue("vMaterialColor", &boneMesh->materials[i].Diffuse, sizeof(D3DCOLORVALUE));
//m_pDevice->SetMaterial(&boneMesh->materials[i]);
if (!pMeshContainer->textures.empty())
g_pNormalEffect->SetTexture("gTex", pMeshContainer->textures[iAttrib]);
g_pNormalEffect->Begin(NULL, NULL);
g_pNormalEffect->BeginPass(0);
pMeshContainer->MeshData.pMesh->DrawSubset(iAttrib);
g_pNormalEffect->EndPass();
g_pNormalEffect->End();
}
}
}
In shader,
float4 lhtDir = {0.0f, 0.0f, -1.0f, 1.0f}; //light Direction
float4 LightAmbient = {0.3f, 0.3f, 0.3f, 0.3f};
float4 LightDiffuse = {0.6f, 0.6f, 0.6f, 1.0f}; // Light Diffuse
float4 LightSpec = {0.3f, 0.3f, 0.3f, 0.3f};
float4 MaterialAmbient : MATERIALAMBIENT = {0.1f, 0.1f, 0.1f, 1.0f};
float4 MaterialDiffuse : MATERIALDIFFUSE = {0.8f, 0.8f, 0.8f, 1.0f};
float4 MaterialSpec : MATERIALSPECULAR = {0.1f, 0.1f, 0.1f, 0.1f};
extern float SpecPower = 0.5f;
uniform extern float4x4 gWorld;
uniform extern float4x4 gWorldInvTrans;
uniform extern float4x4 gWVP; // World-View-Projection
uniform extern float3 gEyePosW;
uniform extern texture gTex;
sampler TexS = sampler_state
{
Texture = <gTex>;
MinFilter = Anisotropic;
MagFilter = LINEAR;
MipFilter = LINEAR;
MaxAnisotropy = 8;
AddressU = WRAP;
AddressV = WRAP;
};
struct OutputVS
{
float4 posH : POSITION0;
float3 normalW : TEXCOORD0;
float3 toEyeW : TEXCOORD1;
float2 tex0 : TEXCOORD2;
};
OutputVS PhongDirLtTexVS(float3 posL : POSITION0, float3 normalL : NORMAL0, float2 tex0: TEXCOORD0)
{
// Zero out our output.
OutputVS outVS = (OutputVS)0;
// Transform normal to world space.
outVS.normalW = mul(float4(normalL, 0.0f), gWorldInvTrans).xyz;
// Transform vertex position to world space.
float3 posW = mul(float4(posL, 1.0f), gWorld).xyz;
// Compute the unit vector from the vertex to the eye.
outVS.toEyeW = gEyePosW - posW;
// Transform to homogeneous clip space.
outVS.posH = mul(float4(posL, 1.0f), gWVP);
// Pass on texture coordinates to be interpolated in rasterization.
outVS.tex0 = tex0;
// Done--return the output.
return outVS;
}
float4 PhongDirLtTexPS(float3 normalW : TEXCOORD0, float3 toEyeW : TEXCOORD1, float2 tex0 : TEXCOORD2) : COLOR
{
// Interpolated normals can become unnormal--so normalize.
normalW = normalize(normalW);
toEyeW = normalize(toEyeW);
// Light vector is opposite the direction of the light.
//float3 lightVecW = -gLight.dirW;
float3 lightVecW = lhtDir;
// Compute the reflection vector.
float3 r = reflect(-lightVecW, normalW);
// Determine how much (if any) specular light makes it into the eye.
float t = pow(max(dot(r, toEyeW), 0.0f), SpecPower);
// Determine the diffuse light intensity that strikes the vertex.
float s = max(dot(lightVecW, normalW), 0.0f);
// Compute the ambient, diffuse and specular terms separatly.
float3 spec = t*(MaterialSpec*LightSpec).rgb;
float3 diffuse = s*(MaterialDiffuse*LightDiffuse).rgb;
float3 ambient = MaterialAmbient*LightAmbient;
// Get the texture color.
float4 texColor = tex2D(TexS, tex0);
// Combine the color from lighting with the texture color.
float3 color = (ambient + diffuse)*texColor.rgb + spec;
//float3 color = (ambient + diffuse).rgb + spec;
// Sum all the terms together and copy over the diffuse alpha.
return float4(color, MaterialDiffuse.a*texColor.a);
//return float4(color, MaterialDiffuse.a);
}
technique PhongDirLtTexTech
{
pass P0
{
Lighting = true;
AlphaBlendEnable = TRUE;
DestBlend = INVSRCALPHA;
SrcBlend = SRCALPHA;
// Specify the vertex and pixel shader associated with this pass.
vertexShader = compile vs_2_0 PhongDirLtTexVS();
pixelShader = compile ps_2_0 PhongDirLtTexPS();
}
}
The full alpha execution path is symmetrical.
Anyone shed some lights on this?
Thanks
Jack
Edited by lucky6969b, 01 November 2012 - 04:19 AM.






