Commit f0ac931f authored by chili's avatar chili
Browse files

need to work in camera's frame of reference for specular code to work

parent 92780ee3
......@@ -28,10 +28,10 @@ float4 main( float3 worldPos : Position,float3 n : Normal ) : SV_Target
// diffuse intensity
const float3 diffuse = diffuseColor * diffuseIntensity * att * max( 0.0f,dot( dirToL,n ) );
// reflected light vector
const float3 w = n * dot( dirToL,n );
const float3 r = w * 2.0f - dirToL;
const float3 w = n * dot( vToL,n );
const float3 r = w * 2.0f - vToL;
// calculate specular intensity based on angle between viewing vector and reflection vector, narrow with power function
const float3 specular = (diffuseColor * diffuseIntensity) * specularIntensity * pow( max( 0.0f,dot( normalize( r ),normalize( worldPos ) ) ),specularPower );
const float3 specular = (diffuseColor * diffuseIntensity) * specularIntensity * pow( max( 0.0f,dot( normalize( -r ),normalize( worldPos ) ) ),specularPower );
// final color
return float4(saturate( (diffuse + ambient + specular) * materialColor ),1.0f);
}
\ No newline at end of file
cbuffer CBuf
{
matrix model;
matrix modelView;
matrix modelViewProj;
};
......@@ -14,8 +14,8 @@ struct VSOut
VSOut main( float3 pos : Position,float3 n : Normal )
{
VSOut vso;
vso.worldPos = (float3)mul( float4(pos,1.0f),model );
vso.normal = mul( n,(float3x3)model );
vso.worldPos = (float3)mul( float4(pos,1.0f),modelView );
vso.normal = mul( n,(float3x3)modelView );
vso.pos = mul( float4(pos,1.0f),modelViewProj );
return vso;
}
\ No newline at end of file
cbuffer CBuf
{
matrix model;
matrix modelView;
matrix modelViewProj;
};
......
......@@ -12,13 +12,12 @@ TransformCbuf::TransformCbuf( Graphics& gfx,const Drawable& parent,UINT slot )
void TransformCbuf::Bind( Graphics& gfx ) noexcept
{
const auto model = parent.GetTransformXM();
const auto modelView = parent.GetTransformXM() * gfx.GetCamera();
const Transforms tf =
{
DirectX::XMMatrixTranspose( model ),
DirectX::XMMatrixTranspose( modelView ),
DirectX::XMMatrixTranspose(
model *
gfx.GetCamera() *
modelView *
gfx.GetProjection()
)
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment