Ive now switched a few bits round and have got it working fine in NVIDIA effects composer also.
So i`m pretty sure that it must be something wrong with my vectors and matrices that am passing to the shader but cant figure out which?
my latest directx code is as follows:
gDevice->GetTransform(D3DTS_WORLD,&matWorld); gDevice->GetTransform(D3DTS_VIEW,&matView); gDevice->GetTransform(D3DTS_PROJECTION,&matProjection); D3DXVECTOR4 View; View.x = matView._31; View.y = matView._32; View.z = matView._33; View.w = 1.0; D3DXMatrixMultiply(&matWVP,&matWorld,&matView); D3DXMatrixMultiply(&matWVP,&matWVP,&matProjection); D3DXVECTOR4 Lightvec; Lightvec.x = 0.0; Lightvec.y = 1.0; Lightvec.z = 0.0; Lightvec.w = 1.0; m_pEffect->SetMatrix("matWorldViewProjection", &matWVP); m_pEffect->SetMatrix("matWorld", &matWorld); m_pEffect->SetVector("vecLightDir", &Lightvec); m_pEffect->SetVector("vecEye", &View); m_pEffect->SetTechnique( m_pEffect->GetTechniqueByName("Effect1")); gDevice->SetStreamSource( 0, gVB, 0, sizeof(Vertex_pos_norm)); gDevice->SetFVF(D3DFVF_VERTEX_POS_NORM); gDevice->SetIndices( gIB); UINT iPass, cPasses; m_pEffect->Begin(&cPasses, 0); for (iPass = 0; iPass < cPasses; iPass++) { m_pEffect->BeginPass(iPass); gDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST,0,0,num_vert,0, num_tris); m_pEffect->EndPass(); } m_pEffect->End();
and my latest shader code is:
float4x4 matWorldViewProjection : WorldViewProjection;float4x4 matWorld : World;float4 vecLightDir;float4 vecEye; struct VS_OUTPUT { float4 Pos: POSITION; float3 Light : TEXCOORD0; float3 Norm : TEXCOORD1; float3 View : TEXCOORD2;};VS_OUTPUT Effect_Group_1_Effect1_Pass_0_Vertex_Shader_VS( float4 inPos: POSITION, float3 Normal : NORMAL){ VS_OUTPUT Out = (VS_OUTPUT)0; //Out.Pos = mul(matWorldViewProjection, inPos); Out.Pos = mul(inPos, matWorldViewProjection); Out.Light = vecLightDir; float3 PosWorld = normalize(mul(inPos, matWorld)); Out.View = vecEye - PosWorld; // V Out.Norm = mul(Normal, matWorld); // N return Out;}float4 Effect_Group_1_Effect1_Pass_0_Pixel_Shader_PS(float3 Light: TEXCOORD0, float3 Norm : TEXCOORD1, float3 View : TEXCOORD2) : COLOR{ float4 diffuse = { 0.0f, 0.8f, 0.0f, 1.0f}; float4 ambient = {0.2, 0.02, 0.02, 1.0}; float3 Normal = normalize(Norm); float3 LightDir = normalize(Light); float3 ViewDir = normalize(View); float4 diff = saturate(dot(Normal, LightDir)); // diffuse component // compute self-shadowing term float shadow = saturate(4* diff); float3 Reflect = normalize(2 * diff * Normal - LightDir); // R float4 specular = pow(saturate(dot(Reflect, ViewDir)), 20); // R.V^n // I = ambient + shadow * (Dcolor * N.L + (R.V)n) return ambient + shadow * (diffuse * diff + specular); }technique Effect1{ pass Pass_0 { VertexShader = compile vs_2_0 Effect_Group_1_Effect1_Pass_0_Vertex_Shader_VS(); PixelShader = compile ps_2_0 Effect_Group_1_Effect1_Pass_0_Pixel_Shader_PS(); }}
Thanks.