galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
PostProcess.cpp
Go to the documentation of this file.
1
7
8#include "PostProcess.hpp"
9
13constexpr const auto vao_vert = R"(
14 #version 460 core
15
16 layout (location = 0) in vec2 l_pos;
17 layout (location = 1) in vec2 l_texels;
18
19 out vec2 io_texels;
20
21 void main()
22 {
23 io_texels = l_texels;
24 gl_Position = vec4(l_pos, 0.0, 1.0);
25 }
26)";
27
31constexpr const auto vao_frag = R"(
32 #version 460 core
33
34 in vec2 io_texels;
35 out vec4 io_frag_colour;
36
37 uniform sampler2D u_texture;
38
39 void main()
40 {
41 io_frag_colour = texture(u_texture, io_texels);
42 }
43)";
44
45namespace galaxy
46{
47 namespace graphics
48 {
50 : m_screen_vbo {0}
51 , m_screen_vao {0}
52 , m_output_fb {0}
53 {
54 }
55
60
61 void PostProcess::init(const int width, const int height)
62 {
64 {
66 }
67
68 // We use the old style of opengl here, rather than DSA, just to keep it simple.
69
70 glGenBuffers(1, &m_screen_vbo);
71 glGenVertexArrays(1, &m_screen_vao);
72
73 // clang-format off
74 constexpr const std::array<float, 16> verticies =
75 {
76 // First 2 are pos, last 2 are texels.
77 -1.0f, 1.0f, 0.0f, 1.0f,
78 -1.0f, -1.0f, 0.0f, 0.0f,
79 1.0f, 1.0f, 1.0f, 1.0f,
80 1.0f, -1.0f, 1.0f, 0.0f
81 };
82 // clang-format on
83
84 glBindVertexArray(m_screen_vao);
85 glBindBuffer(GL_ARRAY_BUFFER, m_screen_vbo);
86 glBufferData(GL_ARRAY_BUFFER, verticies.size() * sizeof(float), verticies.data(), GL_STATIC_DRAW);
87 glEnableVertexAttribArray(0);
88 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), nullptr);
89 glEnableVertexAttribArray(1);
90
91 constexpr const std::uint64_t size = 2 * sizeof(float);
92 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), reinterpret_cast<void*>(size));
93
94 glBindVertexArray(0);
95 glBindBuffer(GL_ARRAY_BUFFER, 0);
96
97 m_fb.create(width, height);
98 }
99
101 {
102 m_effects.clear();
103
104 if (m_screen_vao != 0)
105 {
106 glDeleteVertexArrays(1, &m_screen_vao);
107 m_screen_vao = 0;
108 }
109
110 if (m_screen_vbo != 0)
111 {
112 glDeleteBuffers(1, &m_screen_vbo);
113 m_screen_vbo = 0;
114 }
115
116 m_output_fb = 0;
117 }
118
119 void PostProcess::resize(const int width, const int height)
120 {
121 m_fb.recreate(width, height);
122
123 for (auto&& effect : m_effects)
124 {
125 effect->resize(width, height);
126 }
127 }
128
130 {
131 m_fb.bind(true);
132 }
133
135 {
136 m_fb.unbind();
137 }
138
140 {
141 glBindVertexArray(m_screen_vao);
142
144
145 // Post-processing effects pass.
146 for (auto&& effect : m_effects)
147 {
148 if (effect->is_enabled())
149 {
150 m_output_fb = effect->render(m_output_fb);
151 }
152 }
153 }
154
156 {
157 m_output.bind();
158
159 glBindVertexArray(m_screen_vao);
160 glActiveTexture(GL_TEXTURE0);
161 glBindTexture(GL_TEXTURE_2D, m_output_fb);
162 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
163 }
164 } // namespace graphics
165} // namespace galaxy
constexpr const auto vao_vert
PostProcess.cpp galaxy.
constexpr const auto vao_frag
Plain VAO fragment shader.
void bind()
Bind to draw to post processor framebuffer.
RenderTexture m_fb
For geometry and lighting.
void destroy()
Cleanup used memory.
unsigned int m_screen_vao
Simple quad to draw when applying effects (array).
meta::vector< std::unique_ptr< graphics::PostEffect > > m_effects
List of effects to apply in order.
void resize(const int width, const int height)
Resize framebuffers.
void render_effects()
Draw post effects to stored framebuffer.
Shader m_output
Simple output shader.
void render_output()
Draw finished post effects to default framebuffer (screen).
unsigned int m_output_fb
Output cache.
void unbind()
Unbind to draw to post processor framebuffer.
unsigned int m_screen_vbo
Simple quad to draw when applying effects (buffer).
void init(const int width, const int height)
Initialize post processor and GL buffers.
void unbind()
Deactivate context.
void create(const int width, const int height)
Create framebuffer and texture.
unsigned int texture() const
Gets framebuffer texture.
void recreate(const int width=-1, const int height=-1)
Destroy and re-create framebuffer.
void bind(bool clear=true)
Activate context.
bool parse(const std::string &src)
Loads a combined raw shader.
Definition Shader.cpp:100
void compile()
Compiles shader into GPU mem.
Definition Shader.cpp:130
void bind() const
Make active shader.
Definition Shader.cpp:227
Animated.cpp galaxy.
Definition Animated.cpp:16