Skip to content

Commit 0feb6ba

Browse files
+ shapes with light
添加shapes示例的光照效果。因为miniengine自带动态广告(HDR,LDR),所以颜色要亮一些。 可以直接把渲染内容渲染到交换连的缓冲区,就与d3d12book一致了
1 parent 9ac87af commit 0feb6ba

File tree

9 files changed

+95458
-12
lines changed

9 files changed

+95458
-12
lines changed

Chapter 8 Lighting/Chapter 8 Lighting.vcxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@
174174
<ClInclude Include="Core\SystemTime.h" />
175175
<ClInclude Include="Core\Utility.h" />
176176
<ClInclude Include="Core\VectorMath.h" />
177+
<ClInclude Include="d3dUtil.h" />
177178
<ClInclude Include="GameApp.h" />
178179
<ClInclude Include="GeometryGenerator.h" />
179180
<ClInclude Include="Waves.h" />
@@ -323,6 +324,11 @@
323324
<FxCompile Include="Core\Shaders\GenerateMipsLinearOddCS.hlsl" />
324325
<FxCompile Include="Core\Shaders\GenerateMipsLinearOddXCS.hlsl" />
325326
<FxCompile Include="Core\Shaders\GenerateMipsLinearOddYCS.hlsl" />
327+
<None Include="Core\Shaders\LightingUtil.hlsl">
328+
<EntryPointName Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
329+
</EntryPointName>
330+
<FileType>Document</FileType>
331+
</None>
326332
<FxCompile Include="Core\Shaders\LinearizeDepthCS.hlsl" />
327333
<FxCompile Include="Core\Shaders\MagnifyPixelsPS.hlsl">
328334
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Pixel</ShaderType>

Chapter 8 Lighting/Chapter 8 Lighting.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,9 @@
317317
<ClInclude Include="Waves.h">
318318
<Filter>源文件</Filter>
319319
</ClInclude>
320+
<ClInclude Include="d3dUtil.h">
321+
<Filter>源文件</Filter>
322+
</ClInclude>
320323
</ItemGroup>
321324
<ItemGroup>
322325
<Text Include="Core\readme\readme_command.txt">
@@ -411,6 +414,9 @@
411414
<None Include="Core\Shaders\TextRS.hlsli">
412415
<Filter>Shaders\Text</Filter>
413416
</None>
417+
<None Include="Core\Shaders\LightingUtil.hlsl">
418+
<Filter>Shaders\default</Filter>
419+
</None>
414420
</ItemGroup>
415421
<ItemGroup>
416422
<FxCompile Include="Core\Shaders\Bitonic32InnerSortCS.hlsl">
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
//***************************************************************************************
2+
// LightingUtil.hlsl by Frank Luna (C) 2015 All Rights Reserved.
3+
//
4+
// Contains API for shader lighting.
5+
//***************************************************************************************
6+
7+
#define MaxLights 16
8+
9+
struct Light
10+
{
11+
float3 Strength;
12+
float FalloffStart; // point/spot light only
13+
float3 Direction; // directional/spot light only
14+
float FalloffEnd; // point/spot light only
15+
float3 Position; // point light only
16+
float SpotPower; // spot light only
17+
};
18+
19+
struct Material
20+
{
21+
float4 DiffuseAlbedo;
22+
float3 FresnelR0;
23+
float Shininess;
24+
};
25+
26+
float CalcAttenuation(float d, float falloffStart, float falloffEnd)
27+
{
28+
// Linear falloff.
29+
return saturate((falloffEnd-d) / (falloffEnd - falloffStart));
30+
}
31+
32+
// Schlick gives an approximation to Fresnel reflectance (see pg. 233 "Real-Time Rendering 3rd Ed.").
33+
// R0 = ( (n-1)/(n+1) )^2, where n is the index of refraction.
34+
float3 SchlickFresnel(float3 R0, float3 normal, float3 lightVec)
35+
{
36+
float cosIncidentAngle = saturate(dot(normal, lightVec));
37+
38+
float f0 = 1.0f - cosIncidentAngle;
39+
float3 reflectPercent = R0 + (1.0f - R0)*(f0*f0*f0*f0*f0);
40+
41+
return reflectPercent;
42+
}
43+
44+
float3 BlinnPhong(float3 lightStrength, float3 lightVec, float3 normal, float3 toEye, Material mat)
45+
{
46+
const float m = mat.Shininess * 256.0f;
47+
float3 halfVec = normalize(toEye + lightVec);
48+
49+
float roughnessFactor = (m + 8.0f)*pow(max(dot(halfVec, normal), 0.0f), m) / 8.0f;
50+
float3 fresnelFactor = SchlickFresnel(mat.FresnelR0, halfVec, lightVec);
51+
52+
float3 specAlbedo = fresnelFactor*roughnessFactor;
53+
54+
// Our spec formula goes outside [0,1] range, but we are
55+
// doing LDR rendering. So scale it down a bit.
56+
specAlbedo = specAlbedo / (specAlbedo + 1.0f);
57+
58+
return (mat.DiffuseAlbedo.rgb + specAlbedo) * lightStrength;
59+
}
60+
61+
//---------------------------------------------------------------------------------------
62+
// Evaluates the lighting equation for directional lights.
63+
//---------------------------------------------------------------------------------------
64+
float3 ComputeDirectionalLight(Light L, Material mat, float3 normal, float3 toEye)
65+
{
66+
// The light vector aims opposite the direction the light rays travel.
67+
float3 lightVec = -L.Direction;
68+
69+
// Scale light down by Lambert's cosine law.
70+
float ndotl = max(dot(lightVec, normal), 0.0f);
71+
float3 lightStrength = L.Strength * ndotl;
72+
73+
return BlinnPhong(lightStrength, lightVec, normal, toEye, mat);
74+
}
75+
76+
//---------------------------------------------------------------------------------------
77+
// Evaluates the lighting equation for point lights.
78+
//---------------------------------------------------------------------------------------
79+
float3 ComputePointLight(Light L, Material mat, float3 pos, float3 normal, float3 toEye)
80+
{
81+
// The vector from the surface to the light.
82+
float3 lightVec = L.Position - pos;
83+
84+
// The distance from surface to light.
85+
float d = length(lightVec);
86+
87+
// Range test.
88+
if(d > L.FalloffEnd)
89+
return 0.0f;
90+
91+
// Normalize the light vector.
92+
lightVec /= d;
93+
94+
// Scale light down by Lambert's cosine law.
95+
float ndotl = max(dot(lightVec, normal), 0.0f);
96+
float3 lightStrength = L.Strength * ndotl;
97+
98+
// Attenuate light by distance.
99+
float att = CalcAttenuation(d, L.FalloffStart, L.FalloffEnd);
100+
lightStrength *= att;
101+
102+
return BlinnPhong(lightStrength, lightVec, normal, toEye, mat);
103+
}
104+
105+
//---------------------------------------------------------------------------------------
106+
// Evaluates the lighting equation for spot lights.
107+
//---------------------------------------------------------------------------------------
108+
float3 ComputeSpotLight(Light L, Material mat, float3 pos, float3 normal, float3 toEye)
109+
{
110+
// The vector from the surface to the light.
111+
float3 lightVec = L.Position - pos;
112+
113+
// The distance from surface to light.
114+
float d = length(lightVec);
115+
116+
// Range test.
117+
if(d > L.FalloffEnd)
118+
return 0.0f;
119+
120+
// Normalize the light vector.
121+
lightVec /= d;
122+
123+
// Scale light down by Lambert's cosine law.
124+
float ndotl = max(dot(lightVec, normal), 0.0f);
125+
float3 lightStrength = L.Strength * ndotl;
126+
127+
// Attenuate light by distance.
128+
float att = CalcAttenuation(d, L.FalloffStart, L.FalloffEnd);
129+
lightStrength *= att;
130+
131+
// Scale by spotlight
132+
float spotFactor = pow(max(dot(-lightVec, L.Direction), 0.0f), L.SpotPower);
133+
lightStrength *= spotFactor;
134+
135+
return BlinnPhong(lightStrength, lightVec, normal, toEye, mat);
136+
}
137+
138+
float4 ComputeLighting(Light gLights[MaxLights], Material mat,
139+
float3 pos, float3 normal, float3 toEye,
140+
float3 shadowFactor)
141+
{
142+
float3 result = 0.0f;
143+
144+
int i = 0;
145+
146+
#if (NUM_DIR_LIGHTS > 0)
147+
for(i = 0; i < NUM_DIR_LIGHTS; ++i)
148+
{
149+
result += shadowFactor[i] * ComputeDirectionalLight(gLights[i], mat, normal, toEye);
150+
}
151+
#endif
152+
153+
#if (NUM_POINT_LIGHTS > 0)
154+
for(i = NUM_DIR_LIGHTS; i < NUM_DIR_LIGHTS+NUM_POINT_LIGHTS; ++i)
155+
{
156+
result += ComputePointLight(gLights[i], mat, pos, normal, toEye);
157+
}
158+
#endif
159+
160+
#if (NUM_SPOT_LIGHTS > 0)
161+
for(i = NUM_DIR_LIGHTS + NUM_POINT_LIGHTS; i < NUM_DIR_LIGHTS + NUM_POINT_LIGHTS + NUM_SPOT_LIGHTS; ++i)
162+
{
163+
result += ComputeSpotLight(gLights[i], mat, pos, normal, toEye);
164+
}
165+
#endif
166+
167+
return float4(result, 0.0f);
168+
}
169+
170+
Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,78 @@
1+
//***************************************************************************************
2+
// Default.hlsl by Frank Luna (C) 2015 All Rights Reserved.
3+
//
4+
// Default shader, currently supports lighting.
5+
//***************************************************************************************
6+
7+
// Defaults for number of lights.
8+
#ifndef NUM_DIR_LIGHTS
9+
#define NUM_DIR_LIGHTS 3
10+
#endif
11+
12+
#ifndef NUM_POINT_LIGHTS
13+
#define NUM_POINT_LIGHTS 0
14+
#endif
15+
16+
#ifndef NUM_SPOT_LIGHTS
17+
#define NUM_SPOT_LIGHTS 0
18+
#endif
19+
20+
// Include structures and functions for lighting.
21+
#include "LightingUtil.hlsl"
22+
23+
// Constant data that varies per frame.
24+
25+
// Constant data that varies per material.
26+
cbuffer cbPass : register(b1)
27+
{
28+
float4x4 gViewProj;
29+
float3 gEyePosW;
30+
float cbPerObjectPad1;
31+
float4 gAmbientLight;
32+
33+
// Indices [0, NUM_DIR_LIGHTS) are directional lights;
34+
// indices [NUM_DIR_LIGHTS, NUM_DIR_LIGHTS+NUM_POINT_LIGHTS) are point lights;
35+
// indices [NUM_DIR_LIGHTS+NUM_POINT_LIGHTS, NUM_DIR_LIGHTS+NUM_POINT_LIGHT+NUM_SPOT_LIGHTS)
36+
// are spot lights for a maximum of MaxLights per object.
37+
Light gLights[MaxLights];
38+
};
39+
40+
cbuffer cbMaterial : register(b2)
41+
{
42+
float4 gDiffuseAlbedo;
43+
float3 gFresnelR0;
44+
float gRoughness;
45+
};
46+
47+
148
struct VertexOut
249
{
3-
float4 PosH : SV_POSITION;
4-
float4 Color : COLOR;
50+
float4 PosH : SV_POSITION;
51+
float3 PosW : POSITION;
52+
float3 NormalW : NORMAL;
553
};
654

755
float4 main(VertexOut pin) : SV_Target0
856
{
9-
return pin.Color;
57+
// Interpolating normal can unnormalize it, so renormalize it.
58+
pin.NormalW = normalize(pin.NormalW);
59+
60+
// Vector from point being lit to eye.
61+
float3 toEyeW = normalize(gEyePosW - pin.PosW);
62+
63+
// Indirect lighting.
64+
float4 ambient = gAmbientLight * gDiffuseAlbedo;
65+
66+
const float shininess = 1.0f - gRoughness;
67+
Material mat = { gDiffuseAlbedo, gFresnelR0, shininess };
68+
float3 shadowFactor = 1.0f;
69+
float4 directLight = ComputeLighting(gLights, mat, pin.PosW,
70+
pin.NormalW, toEyeW, shadowFactor);
71+
72+
float4 litColor = ambient + directLight;
73+
74+
// Common convention to take alpha from diffuse material.
75+
litColor.a = gDiffuseAlbedo.a;
76+
77+
return litColor;
1078
}

Chapter 8 Lighting/Core/Shaders/defaultVS.hlsl

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,29 @@ cbuffer PassConstants : register(b1)
1111
struct VertexIn
1212
{
1313
float3 PosL : POSITION;
14-
float4 Color : COLOR;
14+
float3 NormalL : NORMAL;
1515
};
1616

1717
struct VertexOut
1818
{
19-
float4 PosH : SV_POSITION;
20-
float4 Color : COLOR;
19+
float4 PosH : SV_POSITION;
20+
float3 PosW : POSITION;
21+
float3 NormalW : NORMAL;
2122
};
2223

2324
VertexOut main(VertexIn vin)
2425
{
25-
VertexOut vout;
26+
VertexOut vout = (VertexOut)0.0f;
2627

2728
// Transform to world space.
28-
float4 posW = mul(float4(vin.PosL, 1.0), modelToWorld);
29+
float4 posW = mul(float4(vin.PosL, 1.0f), modelToWorld);
30+
vout.PosW = posW.xyz;
31+
32+
// Assumes nonuniform scaling; otherwise, need to use inverse-transpose of world matrix.
33+
vout.NormalW = mul(vin.NormalL, (float3x3)modelToWorld);
2934

3035
// Transform to homogeneous clip space.
3136
vout.PosH = mul(posW, gViewProj);
3237

33-
// Just pass vertex color into the pixel shader.
34-
vout.Color = vin.Color;
35-
3638
return vout;
3739
}

Chapter 8 Lighting/GameApp.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "PipelineState.h"
77
#include "Camera.h"
88
#include "Waves.h"
9+
#include "d3dUtil.h"
910

1011
class RootSignature;
1112
class StructuredBuffer;
@@ -32,12 +33,17 @@ class GameApp : public GameCore::IGameApp
3233
int indexCount;
3334
int startIndex;
3435
int baseVertex;
36+
XMFLOAT4 diffuseAlbedo;
37+
XMFLOAT3 fresnelR0;
38+
float roughness;
3539
};
3640
void buildShapesData();
41+
void buildSkull();
3742
void renderShapes(GraphicsContext& gfxContext);
3843

3944
void buildLandAndWaves();
4045
float GetHillsHeight(float x, float z) const;
46+
XMFLOAT3 GetHillsNormal(float x, float z) const;
4147
void renderLandAndWaves(GraphicsContext& gfxContext);
4248
void UpdateWaves(float deltaT);
4349

@@ -46,7 +52,7 @@ class GameApp : public GameCore::IGameApp
4652
struct Vertex
4753
{
4854
XMFLOAT3 Pos;
49-
XMFLOAT4 Color;
55+
XMFLOAT3 Normal;
5056
};
5157

5258
RootSignature m_RootSignature;
@@ -60,6 +66,11 @@ class GameApp : public GameCore::IGameApp
6066
StructuredBuffer m_VertexBuffer;
6167
ByteAddressBuffer m_IndexBuffer;
6268
std::vector<renderItem> m_vecShapes;
69+
// skull
70+
StructuredBuffer m_VertexBufferSkull;
71+
ByteAddressBuffer m_IndexBufferSkull;
72+
renderItem m_skull;
73+
6374
// land and waves
6475
StructuredBuffer m_VertexBufferLand;
6576
ByteAddressBuffer m_IndexBufferLand;

0 commit comments

Comments
 (0)