-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDX11 Framework.fx
135 lines (104 loc) · 3.39 KB
/
DX11 Framework.fx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//--------------------------------------------------------------------------------------
// File: DX11 Framework.fx
//--------------------------------------------------------------------------------------
Texture2D txDiffuse : register(t0);
SamplerState samLinear : register(s0);
//--------------------------------------------------------------------------------------
// Constant Buffer Variables
//--------------------------------------------------------------------------------------
struct SurfaceInfo
{
float4 AmbientMtrl;
float4 DiffuseMtrl;
float4 SpecularMtrl;
};
struct Light
{
float4 AmbientLight;
float4 DiffuseLight;
float4 SpecularLight;
float SpecularPower;
float3 LightVecW;
};
cbuffer ConstantBuffer : register( b0 )
{
matrix World;
matrix View;
matrix Projection;
SurfaceInfo surface;
Light light;
float3 EyePosW;
float HasTexture;
}
struct VS_INPUT
{
float4 PosL : POSITION;
float3 NormL : NORMAL;
float2 Tex : TEXCOORD0;
};
//--------------------------------------------------------------------------------------
struct VS_OUTPUT
{
float4 PosH : SV_POSITION;
float3 NormW : NORMAL;
float3 PosW : POSITION;
float2 Tex : TEXCOORD0;
};
//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------
VS_OUTPUT VS(VS_INPUT input)
{
VS_OUTPUT output = (VS_OUTPUT)0;
float4 posW = mul(input.PosL, World);
output.PosW = posW.xyz;
output.PosH = mul(posW, View);
output.PosH = mul(output.PosH, Projection);
output.Tex = input.Tex;
float3 normalW = mul(float4(input.NormL, 0.0f), World).xyz;
output.NormW = normalize(normalW);
return output;
}
//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------
float4 PS(VS_OUTPUT input) : SV_Target
{
float3 normalW = normalize(input.NormW);
float3 toEye = normalize(EyePosW - input.PosW);
// Get texture data from file
float4 textureColour = txDiffuse.Sample(samLinear, input.Tex);
float3 ambient = float3(0.0f, 0.0f, 0.0f);
float3 diffuse = float3(0.0f, 0.0f, 0.0f);
float3 specular = float3(0.0f, 0.0f, 0.0f);
float3 lightLecNorm = normalize(light.LightVecW);
// Compute Colour
// Compute the reflection vector.
float3 r = reflect(-lightLecNorm, normalW);
// Determine how much specular light makes it into the eye.
float specularAmount = pow(max(dot(r, toEye), 0.0f), light.SpecularPower);
// Determine the diffuse light intensity that strikes the vertex.
float diffuseAmount = max(dot(lightLecNorm, normalW), 0.0f);
// Only display specular when there is diffuse
if (diffuseAmount <= 0.0f)
{
specularAmount = 0.0f;
}
// Compute the ambient, diffuse, and specular terms separately.
specular += specularAmount * (surface.SpecularMtrl * light.SpecularLight).rgb;
diffuse += diffuseAmount * (surface.DiffuseMtrl * light.DiffuseLight).rgb;
ambient += (surface.AmbientMtrl * light.AmbientLight).rgb;
// Sum all the terms together and copy over the diffuse alpha.
float4 finalColour;
if (HasTexture == 1.0f)
{
finalColour.rgb = (textureColour.rgb * (ambient + diffuse)) + specular;
//finalColour.rgb = specular;
}
else
{
finalColour.rgb = ambient + diffuse + specular;
}
finalColour.a = surface.DiffuseMtrl.a;
return finalColour;
}