here is the vertex shader:
float3 fvLightPosition;
float3 fvEyePosition;
float4x4 matView;
float4x4 matViewProjection;
struct VS_INPUT
{
float4 Position : POSITION0;
float2 Texcoord : TEXCOORD0;
float3 Normal : NORMAL0;
};
struct VS_OUTPUT
{
float4 Position : POSITION0;
float2 Texcoord : TEXCOORD0;
float3 ViewDirection : TEXCOORD1;
float3 LightDirection : TEXCOORD2;
float3 Normal : TEXCOORD3;
};
VS_OUTPUT vs_main( VS_INPUT Input )
{
VS_OUTPUT Output;
Output.Position = mul( Input.Position, matViewProjection );
Output.Texcoord = Input.Texcoord;
float3 fvObjectPosition = mul( Input.Position, matView );
Output.ViewDirection = fvEyePosition - fvObjectPosition;
Output.LightDirection = fvLightPosition - fvObjectPosition;
Output.Normal = mul( Input.Normal, matView );
return( Output );
}
// Registers:
//
// Name Reg Size
// ----------------- ----- ----
// matViewProjection c0 4
// matView c4 3
// fvLightPosition c7 1
// fvEyePosition c8 1
//
vs_2_0
dcl_position v0
dcl_texcoord v1
dcl_normal v2
dp4 oPos.x, v0, c0
dp4 oPos.y, v0, c1
dp4 oPos.z, v0, c2
dp4 oPos.w, v0, c3
dp4 r0.x, v0, c4
dp4 r0.y, v0, c5
dp4 r0.z, v0, c6
add oT1.xyz, -r0, c8
add oT2.xyz, -r0, c7
dp3 oT3.x, v2, c4
dp3 oT3.y, v2, c5
dp3 oT3.z, v2, c6
mov oT0.xy, v1
the first thing I dont understand is:
float3 fvObjectPosition = mul( Input.Position, matView ).
If the matView is the view matrix then this means it is transforming the vertex into view space wheras the light and eye vectors are both in world space, I've tried converting these vectors also into view space and also leaving the vertex in world space, but both of these don't work.
here's the pixel shader:
float4 fvAmbient;
float4 fvSpecular;
float4 fvDiffuse;
float fSpecularPower;
sampler2D baseMap;
struct PS_INPUT
{
float2 Texcoord : TEXCOORD0;
float3 ViewDirection : TEXCOORD1;
float3 LightDirection: TEXCOORD2;
float3 Normal : TEXCOORD3;
};
float4 ps_main( PS_INPUT Input ) : COLOR0
{
float3 fvLightDirection = normalize( Input.LightDirection );
float3 fvNormal = normalize( Input.Normal );
float fNDotL = dot( fvNormal, fvLightDirection );
float3 fvReflection = normalize( ( ( 2.0f * fvNormal ) * ( fNDotL ) ) - fvLightDirection );
float3 fvViewDirection = normalize( Input.ViewDirection );
float fRDotV = max( 0.0f, dot( fvReflection, fvViewDirection ) );
float4 fvBaseColor = tex2D( baseMap, Input.Texcoord );
float4 fvTotalAmbient = fvAmbient * fvBaseColor;
float4 fvTotalDiffuse = fvDiffuse * fNDotL * fvBaseColor;
float4 fvTotalSpecular = fvSpecular * pow( fRDotV, fSpecularPower );
return( saturate( fvTotalAmbient + fvTotalDiffuse + fvTotalSpecular ) );
}
// Registers:
//
// Name Reg Size
// -------------- ----- ----
// fvAmbient c0 1
// fvSpecular c1 1
// fvDiffuse c2 1
// fSpecularPower c3 1
// baseMap s0 1
//
ps_2_0
def c4, 2, 0, 0, 0
dcl t0.xy
dcl t1.xyz
dcl t2.xyz
dcl t3.xyz
dcl_2d s0
texld r0, t0, s0
nrm r1.xyz, t3
nrm r2.xyz, t2
dp3 r1.w, r1, r2
mul r1.xyz, r1, r1.w
mul r3, r1.w, c2
mad r1.xyz, r1, c4.x, -r2
nrm r2.xyz, r1
nrm r1.xyz, t1
dp3 r1.w, r2, r1
max r2.w, r1.w, c4.y
pow r1.w, r2.w, c3.x
mul r2, r0, r3
mad r0, c0, r0, r2
mad_sat r0, c1, r1.w, r0
mov oC0, r0
I convert this into the asm code and then place this in my application, I then set one vertex shader constant every time the world matrix changes, i.e.
pDevice->SetVertexShaderConstantF(0, (float*)&(getWorldMatrix() * getViewMatrix() * getProjectionMatrix()).transpose(), 4);
and for every frame I set these constants:
// pos == light position
// eye == camera position
// (both in world space)
pDevice->SetVertexShaderConstantF(7, pos, 1);
pDevice->SetVertexShaderConstantF(8, eye, 1);
pDevice->SetVertexShaderConstantF(4, (float*)&getViewMatrix().transpose(), 3);
float test[4] = { 0.3f, 0.3f, 0.3f, 0.3f };
pDevice->SetPixelShaderConstantF(0, test, 1);
pDevice->SetPixelShaderConstantF(1, test, 1);
pDevice->SetPixelShaderConstantF(2, test, 1);
pDevice->SetPixelShaderConstantF(3, test, 1);
but the lighting seems way off.
thanks in advance.












