-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShaders.cpp
More file actions
116 lines (91 loc) · 3.54 KB
/
Shaders.cpp
File metadata and controls
116 lines (91 loc) · 3.54 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
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
#include "Defines.h"
#include"Shaders.h"
std::string Shaders::positionOnlyVSPath = "Debug/ParticleVS.cso";
ID3D11VertexShader* Shaders::positionOnlyVS;
XMFLOAT3 Shaders::camPosition;
XMMATRIX Shaders::viewMatrix;
XMMATRIX Shaders::lightingMatrix;
XMMATRIX Shaders::perspectiveMatrix;
ID3D11InputLayout* Shaders::positionOnlyLayout;
ID3D11InputLayout* Shaders::modelLayout;
//std::string Shaders::gsPath = "Debug/CullingGS.cso"; //culling shader
//ID3D11GeometryShader* Shaders::gShader;
//ID3D11Buffer* Shaders::camPos; buffer for culling
Shadow* Shaders::shadowmap;
void Shaders::Shutdown()
{
if(positionOnlyLayout)
positionOnlyVS->Release();
if(positionOnlyLayout)
positionOnlyLayout->Release();
if(modelLayout)
modelLayout->Release();
//camPos->Release();
/*if(gShader)
gShader->Release();*/
if(shadowmap)
shadowmap->ShutDown();
}
void Shaders::Initialize(ID3D11Device* device, std::string modelVSByteCode)
{
shadowmap = new Shadow(device, 4096);
std::string byteCode;
std::string Shaders;
std::ifstream reader;
//Clears the reader and Shaders and does the same thing for the PixelShader
reader.open(positionOnlyVSPath, 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);
Shaders.reserve(static_cast<unsigned int>(reader.tellg()));
reader.seekg(0, std::ios::beg);
Shaders.assign((std::istreambuf_iterator<char>(reader)), std::istreambuf_iterator<char>());
if (FAILED(device->CreateVertexShader(Shaders.c_str(), Shaders.length(), nullptr, &positionOnlyVS)))
{
std::cerr << "Failed to create position VERTEX shader!" << std::endl;
return;
}
byteCode = Shaders;
Shaders.clear();
reader.close();
D3D11_INPUT_ELEMENT_DESC positionDesc[1] =
{
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0}
};
HRESULT hr = device->CreateInputLayout(positionDesc, 1, byteCode.c_str(), byteCode.length(), &positionOnlyLayout);
if (FAILED(hr))
{
std::cout << "FAILED TO CREATE POSITION INPUT LAYOUT" << std::endl;
}
// Array av element med en beskrivning
D3D11_INPUT_ELEMENT_DESC inputDesc[] =
{
//name, index, format, slot, byteOffset, inputSlotClass, stepRate
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0}, // Input data is per-vertex data.
{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0}
};
//Viktigt!! Allt behöver vara i rätt ordning för om det laddas in i fel ordning så funkar det ej
hr = device->CreateInputLayout(inputDesc, ARRAYSIZE(inputDesc), modelVSByteCode.c_str(), modelVSByteCode.length(), &modelLayout);
if FAILED(hr)
{
std::cerr << "ERROR INPUTLAYOUT" << std::endl;
return;
}
Shaders.clear();
reader.close();
}
void Shaders::Update(ID3D11DeviceContext*& context, Camera camera, Lighting& light)
{
camPosition = camera.GetPosition();
viewMatrix = camera.GetViewMatrix();
perspectiveMatrix = camera.GetPerspectiveMatrix();
lightingMatrix = light.GetMatrix(); // Lights transposed orthographic viewPerspective matrix
//UpdateBuffer(context, camPos, camera.GetPosition()); back culling
//context->GSSetConstantBuffers(0, 1, &camPos);
// CAM POS
//std::cout << "X: " << cameraPosition.x << " Y: " << cameraPosition.y << " Z: " << cameraPosition.z << std::endl;
}