-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShadowSceneRenderer.h
224 lines (177 loc) · 8.84 KB
/
ShadowSceneRenderer.h
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
//// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
//// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//// PARTICULAR PURPOSE.
////
//// Copyright (c) Microsoft Corporation. All rights reserved
#pragma once
#include "DeviceResources.h"
#include "ShaderStructures.h"
#include "StepTimer.h"
#include "BasicShapes.h"
struct VertexPositionColor
{
DirectX::XMFLOAT3 pos;
DirectX::XMFLOAT3 color;
};
struct VertexPos
{
DirectX::XMFLOAT3 pos;
};
struct Triangle
{
DirectX::XMVECTOR tri1;
DirectX::XMVECTOR tri2;
DirectX::XMVECTOR tri3;
float center[3];
float lower_corner[3];
float upper_corner[3];
bool intersected;
DirectX::XMVECTOR intersectionPoint = DirectX::XMVectorSet(0.0f, 0.0f, 0.0f, 0.0f);
float distance;
};
namespace ShadowMapping
{
// This sample renderer instantiates a basic rendering pipeline.
class ShadowSceneRenderer
{
public:
ShadowSceneRenderer(const std::shared_ptr<DX::DeviceResources>& deviceResources);
void CreateDeviceDependentResources();
void CreateWindowSizeDependentResources();
void ReleaseDeviceDependentResources();
void Update(DX::StepTimer const& timer);
void Render();
// CameraDrive Function
void CameraDrive();
// Round Int Value to next 10
int RoundNum(int num)
{
int rem = num % 10;
return rem >= 5 ? (num - rem + 10) : (num - rem);
}
void UpdateCamera();
void UpdateLight();
void KeyDownCheck(Windows::System::VirtualKey key);
void KeyUpCheck(Windows::System::VirtualKey key);
void DetectInput(DX::StepTimer const & timer);
void DirectInput(DX::StepTimer const& timer);
void InitShadowMap();
void SetEarthNormalMap(bool normal);
void SetMoonNormalMap(bool normal);
void SetKDTree(bool draw);
float GetDistance();
void TestIntersection(float pointX, float pointY, float HEIGHT, float WIDTH);
void DrawKDTree(std::vector<BasicVertex> vertices, DirectX::FXMVECTOR color);
void SetFilterMode(Platform::String ^test);
__forceinline bool GetFiltering() { return m_useLinear; };
__forceinline void SetFiltering(bool useLinear) { m_useLinear = useLinear; };
__forceinline float GetShadowDimension() { return m_shadowMapDimension; };
__forceinline void SetShadowDimension(float dimension) { m_shadowMapDimension = dimension; };
__forceinline bool GetD3D9ShadowsSupported() { return m_deviceSupportsD3D9Shadows; };
private:
void DetermineShadowFeatureSupport();
void RenderShadowMap();
void RenderSceneWithShadows();
void RenderQuad();
void UpdateAllConstantBuffers();
// Init Standard Points
void InitStdPoints();
//bool XM_CALLCONV Intersects(_In_ DirectX::FXMVECTOR Origin, _In_ DirectX::FXMVECTOR Direction, _In_ DirectX::FXMVECTOR V0, _In_ DirectX::GXMVECTOR V1, _In_ DirectX::HXMVECTOR V2, _Out_ float& Dist);
DirectX::XMVECTOR VectorToLocal(DirectX::XMVECTOR inVec);
void DrawRay(DirectX::FXMVECTOR Origin, DirectX::FXMVECTOR Destination, DirectX::FXMVECTOR color);
void DrawHitTriangle(Triangle& tri, DirectX::FXMVECTOR color);
// Cached pointer to device resources.
std::shared_ptr<DX::DeviceResources> m_deviceResources;
// Direct3D resources for cube geometry.
Microsoft::WRL::ComPtr<ID3D11InputLayout> m_inputLayout;
Microsoft::WRL::ComPtr<ID3D11Buffer> m_vertexBuffer;
Microsoft::WRL::ComPtr<ID3D11Buffer> m_indexBuffer;
Microsoft::WRL::ComPtr<ID3D11VertexShader> m_vertexShader;
Microsoft::WRL::ComPtr<ID3D11VertexShader> m_simpleVertexShader;
Microsoft::WRL::ComPtr<ID3D11VertexShader> m_lineVertexShader;
Microsoft::WRL::ComPtr<ID3D11PixelShader> m_shadowPixelShader_point;
Microsoft::WRL::ComPtr<ID3D11PixelShader> m_shadowPixelShader_linear;
Microsoft::WRL::ComPtr<ID3D11PixelShader> m_comparisonShader;
Microsoft::WRL::ComPtr<ID3D11PixelShader> m_textureShader;
Microsoft::WRL::ComPtr<ID3D11PixelShader> m_lineShader;
Microsoft::WRL::ComPtr<ID3D11Buffer> m_lineVertexBuffer;
Microsoft::WRL::ComPtr<ID3D11Buffer> m_lineIndexBuffer;
Microsoft::WRL::ComPtr<ID3D11Buffer> m_triangleVertexBuffer;
Microsoft::WRL::ComPtr<ID3D11Buffer> m_triangleIndexBuffer;
Microsoft::WRL::ComPtr<ID3D11Buffer> m_kdTreeVertexBuffer;
Microsoft::WRL::ComPtr<ID3D11Buffer> m_kdTreeIndexBuffer;
// Shadow buffer Direct3D resources.
Microsoft::WRL::ComPtr<ID3D11Texture2D> m_shadowMap;
Microsoft::WRL::ComPtr<ID3D11DepthStencilView> m_shadowDepthView;
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> m_shadowResourceView;
Microsoft::WRL::ComPtr<ID3D11SamplerState> m_comparisonSampler_point;
Microsoft::WRL::ComPtr<ID3D11SamplerState> m_comparisonSampler_linear;
Microsoft::WRL::ComPtr<ID3D11SamplerState> m_linearSampler;
// texture resources
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> m_textureResource;
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> m_textureResourceCube1;
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> m_textureworldnormals;
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> m_texturemoonnormals;
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> m_textureResourceCube2;
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> m_textureResourceCube3;
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> m_textureResourceLight;
Microsoft::WRL::ComPtr<ID3D11SamplerState> m_textureSampler;
// diff filter modi
Microsoft::WRL::ComPtr<ID3D11SamplerState> m_textureSampler_1;
Microsoft::WRL::ComPtr<ID3D11SamplerState> m_textureSampler_2;
Microsoft::WRL::ComPtr<ID3D11SamplerState> m_textureSampler_3;
Microsoft::WRL::ComPtr<ID3D11SamplerState> m_textureSampler_4;
Microsoft::WRL::ComPtr<ID3D11SamplerState> m_textureSampler_5;
Microsoft::WRL::ComPtr<ID3D11SamplerState> m_textureSampler_6;
Microsoft::WRL::ComPtr<ID3D11SamplerState> m_textureSampler_7;
Microsoft::WRL::ComPtr<ID3D11SamplerState> m_textureSampler_8;
Microsoft::WRL::ComPtr<ID3D11SamplerState> m_normalSampler;
// Model, view, projection constant buffers.
Microsoft::WRL::ComPtr<ID3D11Buffer> m_cubeViewProjectionBuffer;
Microsoft::WRL::ComPtr<ID3D11Buffer> m_lightViewProjectionBuffer;
Microsoft::WRL::ComPtr<ID3D11Buffer> m_orthoViewProjectionBuffer;
Microsoft::WRL::ComPtr<ID3D11Buffer> m_staticModelBuffer;
Microsoft::WRL::ComPtr<ID3D11Buffer> m_lightModelBuffer;
Microsoft::WRL::ComPtr<ID3D11Buffer> m_rotatedModelBuffer1;
Microsoft::WRL::ComPtr<ID3D11Buffer> m_rotatedModelBuffer2;
Microsoft::WRL::ComPtr<ID3D11Buffer> m_rotatedModelBuffer3;
Microsoft::WRL::ComPtr<ID3D11Buffer> m_orthoTransformBuffer;
// Render states for front face/back face culling.
Microsoft::WRL::ComPtr<ID3D11RasterizerState> m_shadowRenderState;
Microsoft::WRL::ComPtr<ID3D11RasterizerState> m_drawingRenderState;
// Direct3D resources for displaying the shadow map.
Microsoft::WRL::ComPtr<ID3D11Buffer> m_vertexBufferQuad;
Microsoft::WRL::ComPtr<ID3D11Buffer> m_indexBufferQuad;
Microsoft::WRL::ComPtr<ID3D11Buffer> m_vertexBufferFloor;
Microsoft::WRL::ComPtr<ID3D11Buffer> m_indexBufferFloor;
Microsoft::WRL::ComPtr<ID3D11Buffer> m_vertexBufferSphere;
Microsoft::WRL::ComPtr<ID3D11Buffer> m_indexBufferSphere;
D3D11_VIEWPORT m_shadowViewport;
// System resources.
ViewProjectionConstantBuffer m_cubeViewProjectionBufferData;
ViewProjectionConstantBuffer m_lightViewProjectionBufferData;
ViewProjectionConstantBuffer m_orthoViewProjectionBufferData;
ModelConstantBuffer m_staticModelBufferData;
ModelConstantBuffer m_lightModelBufferData;
ModelConstantBuffer m_rotatedModelBufferData1;
ModelConstantBuffer m_rotatedModelBufferData2;
ModelConstantBuffer m_rotatedModelBufferData3;
ModelConstantBuffer m_orthoTransformBufferData;
uint32 m_indexCountQuad;
uint32 m_indexCountFloor;
uint32 m_indexCountCube;
uint32 m_indexCountSphere; // mesh index count
uint32 m_indexCountLine;
uint32 m_indexCountTriangle;
uint32 m_indexCountkdTree;
// Variables used with the rendering loop.
bool m_loadingComplete;
float m_degreesPerSecond;
bool m_useLinear;
// Controls the size of the shadow map.
float m_shadowMapDimension;
// Cached copy of SupportsDepthAsTextureWithLessEqualComparisonFilter.
bool m_deviceSupportsD3D9Shadows;
};
}