-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderShadows.cpp
More file actions
62 lines (49 loc) · 2.51 KB
/
RenderShadows.cpp
File metadata and controls
62 lines (49 loc) · 2.51 KB
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
#include "Defines.h"
#include "RenderShadows.h"
RenderShadows::RenderShadows(ID3D11Device* device) :matrixBuffer(nullptr), vertexShader(nullptr)
{
CreateBuffer(device, matrixBuffer, sizeof(XMFLOAT4X4));
std::string shaderData;
std::ifstream reader;
// VERTEX SHADER
reader.open(vsPath, std::ios::binary | std::ios::ate);
if (!reader.is_open())
{
std::cerr << "Could not open VS file!" << std::endl;
return;
}
reader.seekg(0, std::ios::end);
shaderData.reserve(static_cast<unsigned int>(reader.tellg()));
reader.seekg(0, std::ios::beg);
shaderData.assign((std::istreambuf_iterator<char>(reader)), std::istreambuf_iterator<char>());
if (FAILED(device->CreateVertexShader(shaderData.c_str(), shaderData.length(), nullptr, &vertexShader)))
{
std::cerr << "Failed to create pixel shader!" << std::endl;
return;
}
shaderData.clear();
reader.close();
}
void RenderShadows::Render(ID3D11DeviceContext* context, Model* model)
{
// Shadows use an positions only input layout to determine the position of each pixel
context->IASetInputLayout(Shaders::positionOnlyLayout);
context->VSSetShader(vertexShader, NULL, 0); // VS containing a position
context->PSSetShader(NULL, NULL, 0); // not using a pixelshader since the shadow does not need it
context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); // Trianglelist
context->GSSetShader(NULL, NULL, 0); // Not using a GS for the shadowpass
// ShaderData::lightMatrix has got the dirLight.GetMatrix() with the matrix = XMMatrixTranspose(viewMatrix * ortographicMatrix) from DirectionalLight
XMFLOAT4X4 WVP1;
XMMATRIX WVP = Shaders::lightingMatrix * XMMatrixTranspose(model->GetWorldMatrix()); // Worldspace -> viewspace -> clipspace (lights clipspace)
// WVP1 is the address at which to store the data from WVP, WVP is the matrix contining the data that we want to store
XMStoreFloat4x4(&WVP1, WVP);
UpdateBuffer(context, matrixBuffer, WVP1); // Update the buffer containing the models TRANSPOSED worldmatrix, To write out column-major data it requires the XMMATRIX be transposed
context->VSSetConstantBuffers(0, 1, &matrixBuffer); // Set the CB for the VS
context->IASetVertexBuffers(0, 1, model->GetPositionsBuffer(), &stride, &offset); // Set the VB with the vertex position data from the vector in model.h
context->Draw(model->GetVertexCount(), 0); // draw the model for the shadow pass
}
void RenderShadows::ShutDown()
{
matrixBuffer->Release();
vertexShader->Release();
}