Commit fc18dc8b authored by chili's avatar chili
Browse files

textured shading

parent 035f0975
......@@ -294,10 +294,9 @@ std::unique_ptr<Mesh> Model::ParseMesh( Graphics& gfx,const aiMesh& mesh,const a
struct PSMaterialConstant
{
DirectX::XMFLOAT3 color = { 0.6f,0.6f,0.8f };
float specularIntensity = 0.6f;
float specularPower = 30.0f;
float padding[3];
float padding[2];
} pmc;
bindablePtrs.push_back( std::make_unique<Bind::PixelConstantBuffer<PSMaterialConstant>>( gfx,pmc,1u ) );
......
cbuffer LightCBuf
{
float3 lightPos;
float3 ambient;
float3 diffuseColor;
float diffuseIntensity;
float attConst;
float attLin;
float attQuad;
float3 lightPos;
float3 ambient;
float3 diffuseColor;
float diffuseIntensity;
float attConst;
float attLin;
float attQuad;
};
cbuffer ObjectCBuf
{
float3 materialColor;
float specularIntensity;
float specularPower;
float specularIntensity;
float specularPower;
float padding[2];
};
Texture2D tex;
float4 main( float3 worldPos : Position,float3 n : Normal ) : SV_Target
SamplerState splr;
float4 main(float3 worldPos : Position, float3 n : Normal, float2 tc : Texcoord) : SV_Target
{
// fragment to light vector data
const float3 vToL = lightPos - worldPos;
const float distToL = length( vToL );
const float3 dirToL = vToL / distToL;
const float3 vToL = lightPos - worldPos;
const float distToL = length(vToL);
const float3 dirToL = vToL / distToL;
// attenuation
const float att = 1.0f / (attConst + attLin * distToL + attQuad * (distToL * distToL));
const float att = 1.0f / (attConst + attLin * distToL + attQuad * (distToL * distToL));
// diffuse intensity
const float3 diffuse = diffuseColor * diffuseIntensity * att * max( 0.0f,dot( dirToL,n ) );
const float3 diffuse = diffuseColor * diffuseIntensity * att * max(0.0f, dot(dirToL, n));
// reflected light vector
const float3 w = n * dot( vToL,n );
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 = att * (diffuseColor * diffuseIntensity) * specularIntensity * pow( max( 0.0f,dot( normalize( -r ),normalize( worldPos ) ) ),specularPower );
const float3 specular = att * (diffuseColor * diffuseIntensity) * specularIntensity * pow(max(0.0f, dot(normalize(-r), normalize(worldPos))), specularPower);
// final color
return float4(saturate( (diffuse + ambient + specular) * materialColor ),1.0f);
return float4(saturate(diffuse + ambient + specular), 1.0f) * tex.Sample(splr, tc);
}
\ No newline at end of file
cbuffer CBuf
{
matrix modelView;
matrix modelViewProj;
matrix modelView;
matrix modelViewProj;
};
struct VSOut
{
float3 worldPos : Position;
float3 normal : Normal;
float4 pos : SV_Position;
float3 worldPos : Position;
float3 normal : Normal;
float2 tc : Texcoord;
float4 pos : SV_Position;
};
VSOut main( float3 pos : Position,float3 n : Normal )
VSOut main(float3 pos : Position, float3 n : Normal, float2 tc : Texcoord)
{
VSOut vso;
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;
VSOut vso;
vso.worldPos = (float3) mul(float4(pos, 1.0f), modelView);
vso.normal = mul(n, (float3x3) modelView);
vso.pos = mul(float4(pos, 1.0f), modelViewProj);
vso.tc = tc;
return vso;
}
\ No newline at end of file
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