-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderSystem.cpp
93 lines (79 loc) · 3.22 KB
/
renderSystem.cpp
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
#include "renderSystem.hpp"
constexpr auto const vertCode = R"END(
#version 430
struct Particle {
vec3 color;
int onFire;
vec2 pos;
};
layout (location = 0) in vec3 vertex;
layout (location = 0) uniform mat4 pMatrix;
layout (location = 4) uniform mat4 vMatrix;
layout (std430, binding = 1) readonly buffer Particle_Buffer {
Particle particles[];
};
layout (location = 0) flat out vec4 color;
void main() {
const vec3 offset = vec3(particles[gl_InstanceID].pos, 0.0);
gl_Position = pMatrix * vMatrix * vec4((vertex * 0.5) + offset, 1.0);
color = vec4(mix(particles[gl_InstanceID].color, vec3(1, 0.2F, 0), particles[gl_InstanceID].onFire * 0.75), 1.0F);
}
)END";
constexpr auto const fragCode = R"END(
#version 430
layout (location = 0) flat in vec4 color;
layout (location = 0) out vec4 fragColor;
void main() {
fragColor = color;
}
)END";
//////////////////////////////////////////////////////////////////////
/// Custom Constructor
//////////////////////////////////////////////////////////////////////
RenderSystem::RenderSystem()
: m_shader(vertCode, fragCode), m_model({ vec3(-1, -1, 0), vec3(1, -1, 0),
vec3(1, 1, 0), vec3(-1, 1, 0) }),
m_draw(4, 0, 0, GL_DYNAMIC_STORAGE_BIT) {
addComponentType(ParticleComponent::Runtime_ID, RequirementsFlag::REQUIRED);
addComponentType(OnFireComponent::Runtime_ID, RequirementsFlag::OPTIONAL);
// Calculate viewing perspective and matrices
const auto pMatrix = mat4::perspective(1.5708F, 1.0F, 0.01F, 10.0F);
const auto vMatrix = mat4::lookAt(
vec3{ 256, 256, 256 }, vec3{ 256, 256, 0 }, vec3{ 0, 1, 0 });
m_shader.uniformLocation(0, pMatrix);
m_shader.uniformLocation(4, vMatrix);
}
//////////////////////////////////////////////////////////////////////
/// updateComponents
//////////////////////////////////////////////////////////////////////
void RenderSystem::updateComponents(
const double& /*deltaTime*/,
const std::vector<std::vector<ecsBaseComponent*>>& entityComponents) {
// Update buffered data
m_draw.setPrimitiveCount(static_cast<GLuint>(entityComponents.size()));
m_dataBuffer.beginWriting();
size_t offset(0ULL);
for (const auto& components : entityComponents) {
// Convert game particles into GPU renderable particles
const auto& particle =
*static_cast<ParticleComponent*>(components.front());
const GPU_Particle data{
particle.m_color, components.back() != nullptr ? 1 : 0,
vec2(
static_cast<float>(static_cast<int>(particle.m_pos.x())),
static_cast<float>(static_cast<int>(particle.m_pos.y())))
};
m_dataBuffer.write(offset, sizeof(GPU_Particle), &data);
offset += sizeof(GPU_Particle);
}
m_dataBuffer.endWriting();
// Flush buffers and set starting parameters
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDepthFunc(GL_LEQUAL);
glBlendFunc(GL_ONE, GL_ZERO);
m_shader.bind();
m_model.bind();
m_dataBuffer.bindBufferBase(GL_SHADER_STORAGE_BUFFER, 1);
m_draw.drawCall(GL_QUADS);
m_dataBuffer.endReading();
}