-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticleHelper.cpp
More file actions
145 lines (117 loc) · 4.14 KB
/
ParticleHelper.cpp
File metadata and controls
145 lines (117 loc) · 4.14 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
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
#include "Defines.h"
#include "ParticleHelper.h"
// Debug/ParticleVS.cso"
bool loadComputeShader(std::string name, ID3D11Device* device, ID3D11ComputeShader*& computeShader)
{
std::string shaderData;
std::ifstream reader;
reader.open("x64/Debug/" + name, std::ios::binary | std::ios::ate);
if (!reader.is_open())
{
std::cout << "ERROR! COULD NOT OPEN CS FILE!" << std::endl;
return false;
}
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->CreateComputeShader(shaderData.c_str(), shaderData.length(), nullptr, &computeShader)))
{
std::cerr << "ERROR! COULD NOT CREATE COMPUTE SHADER!" << std::endl;
return false;
}
return true;
}
float Randomizer(float min, float max)
{
if (min == max && min == 0) {
return 0;
}
return ((float)(rand() % (int)((max - min) * 100) + (int)((min) * 100))) / 100;
}
bool CreateVertexConstantBuffer(ID3D11Device* device, ID3D11Buffer*& buffer, Camera& camera)
{
D3D11_BUFFER_DESC CbDesc;
CbDesc.ByteWidth = sizeof(VertexCb);
CbDesc.Usage = D3D11_USAGE_DYNAMIC;
CbDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
CbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
CbDesc.MiscFlags = 0;
CbDesc.StructureByteStride = 0;
D3D11_SUBRESOURCE_DATA InitData;
VertexCb vcb = {};
vcb.projection.element = camera.GetPerspectiveMatrix();
vcb.view.element = camera.GetViewMatrix();
InitData.SysMemPitch = 0;
InitData.SysMemSlicePitch = 0;
InitData.pSysMem = &vcb;
HRESULT hr = device->CreateBuffer(&CbDesc, &InitData, &buffer);
if (FAILED(hr))
{
std::cout << "ERROR! COULD NOT CREATE CB PARTICLE!" << std::endl;
return false;
}
return !FAILED(hr);
}
bool CreateGeometryConstantBuffer(ID3D11Device* device, ID3D11Buffer*& buffer)
{
D3D11_BUFFER_DESC ConstantBufferDesc;
ConstantBufferDesc.ByteWidth = sizeof(GeoCb);
ConstantBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
ConstantBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
ConstantBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
ConstantBufferDesc.MiscFlags = 0;
ConstantBufferDesc.StructureByteStride = 0;
PixelCb pcb = {};
pcb.ka = { 0.5f,0.5f,0.5f,1 };
pcb.kd = { 1.f,1.f,1.f,0 };
pcb.ks = { 1.f,1.f,1.f,0 };
D3D11_SUBRESOURCE_DATA InitData;
InitData.pSysMem = &pcb;
InitData.SysMemPitch = 0;
InitData.SysMemSlicePitch = 0;
HRESULT hr = device->CreateBuffer(&ConstantBufferDesc, &InitData, &buffer);
return FAILED(hr);
}
bool CreatePixelConstantBuffer(ID3D11Device* device, ID3D11Buffer*& buffer)
{
D3D11_BUFFER_DESC ConstantBufferDesc;
ConstantBufferDesc.ByteWidth = sizeof(PixelCb);
ConstantBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
ConstantBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
ConstantBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
ConstantBufferDesc.MiscFlags = 0;
ConstantBufferDesc.StructureByteStride = 0;
PixelCb pcb = {};
pcb.ka = { 0.5f,0.5f,0.5f,1 };
pcb.kd = { 1.f,1.f,1.f,0 };
pcb.ks = { 1.f,1.f,1.f,0 };
D3D11_SUBRESOURCE_DATA InitData;
InitData.pSysMem = &pcb;
InitData.SysMemPitch = 0;
InitData.SysMemSlicePitch = 0;
HRESULT hr = device->CreateBuffer(&ConstantBufferDesc, &InitData, &buffer);
return FAILED(hr);
}
bool CreateConstBuffer(ID3D11Device* device, ID3D11Buffer*& buffer, UINT size, constB* initData)
{
D3D11_BUFFER_DESC ConstantBufferDesc;
ConstantBufferDesc.ByteWidth = size;
ConstantBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
ConstantBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
ConstantBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
ConstantBufferDesc.MiscFlags = 0;
ConstantBufferDesc.StructureByteStride = 0;
D3D11_SUBRESOURCE_DATA InitData;
InitData.SysMemPitch = 0;
InitData.SysMemSlicePitch = 0;
InitData.pSysMem = &initData;
HRESULT hr = device->CreateBuffer(&ConstantBufferDesc, &InitData, &buffer);
if (FAILED(hr))
{
std::cout << "ERROR! COULD NOT CREATE CONST BUFFER FOR PARTICLES!" << std::endl;
return false;
}
return !FAILED(hr);
}