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
GaussianBlur.cpp
Go to the documentation of this file.
1
7
8#include <glad/glad.h>
9
11#include "galaxy/core/ServiceLocator.hpp"
12
13#include "GaussianBlur.hpp"
14
18constexpr const char* const gaussianblur_vert = R"(
19 #version 460 core
20 layout (location = 0) in vec2 l_pos;
21 layout (location = 1) in vec2 l_texels;
22
23 void main()
24 {
25 gl_Position = vec4(l_pos, 0.0, 1.0);
26 }
27)";
28
29/*
30The MIT License (MIT) Copyright (c) 2015 Jam3
31
32Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in
33the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
34the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
35
36The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
37
38THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR
39A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
40ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41*/
42constexpr const char* const gaussianblur_frag = R"(
43 #version 460 core
44
45 out vec4 io_frag_colour;
46
47 uniform int u_strength;
48 uniform vec2 u_direction;
49 uniform sampler2D u_texture;
50
51 vec4 blur5(sampler2D image, vec2 uv, vec2 resolution, vec2 direction) {
52 vec4 color = vec4(0.0);
53 vec2 off1 = vec2(1.3333333333333333) * direction;
54
55 color += texture2D(image, uv) * 0.29411764705882354;
56 color += texture2D(image, uv + (off1 / resolution)) * 0.35294117647058826;
57 color += texture2D(image, uv - (off1 / resolution)) * 0.35294117647058826;
58
59 return color;
60 }
61
62 vec4 blur9(sampler2D image, vec2 uv, vec2 resolution, vec2 direction) {
63 vec4 color = vec4(0.0);
64 vec2 off1 = vec2(1.3846153846) * direction;
65 vec2 off2 = vec2(3.2307692308) * direction;
66
67 color += texture2D(image, uv) * 0.2270270270;
68 color += texture2D(image, uv + (off1 / resolution)) * 0.3162162162;
69 color += texture2D(image, uv - (off1 / resolution)) * 0.3162162162;
70 color += texture2D(image, uv + (off2 / resolution)) * 0.0702702703;
71 color += texture2D(image, uv - (off2 / resolution)) * 0.0702702703;
72
73 return color;
74 }
75
76 vec4 blur13(sampler2D image, vec2 uv, vec2 resolution, vec2 direction) {
77 vec4 color = vec4(0.0);
78 vec2 off1 = vec2(1.411764705882353) * direction;
79 vec2 off2 = vec2(3.2941176470588234) * direction;
80 vec2 off3 = vec2(5.176470588235294) * direction;
81
82 color += texture2D(image, uv) * 0.1964825501511404;
83 color += texture2D(image, uv + (off1 / resolution)) * 0.2969069646728344;
84 color += texture2D(image, uv - (off1 / resolution)) * 0.2969069646728344;
85 color += texture2D(image, uv + (off2 / resolution)) * 0.09447039785044732;
86 color += texture2D(image, uv - (off2 / resolution)) * 0.09447039785044732;
87 color += texture2D(image, uv + (off3 / resolution)) * 0.010381362401148057;
88 color += texture2D(image, uv - (off3 / resolution)) * 0.010381362401148057;
89
90 return color;
91 }
92
93 void main()
94 {
95 vec2 size = textureSize(u_texture, 0).xy;
96 vec2 uv = gl_FragCoord.xy / size.xy;
97
98 if (u_strength == 0)
99 {
100 io_frag_colour = blur5(u_texture, uv, size, u_direction);
101 }
102 else if (u_strength == 1)
103 {
104 io_frag_colour = blur9(u_texture, uv, size, u_direction);
105 }
106 else if (u_strength == 2)
107 {
108 io_frag_colour = blur13(u_texture, uv, size, u_direction);
109 }
110 }
111)";
112
113namespace galaxy
114{
115 namespace graphics
116 {
117 GaussianBlur::GaussianBlur(const int width, const int height)
118 : m_strength {Strength::NORMAL}
119 {
120 m_horizontal.create(width, height);
121 m_vertical.create(width, height);
122
124 {
126 m_shader.set_uniform("u_texture", 0);
127 m_shader.set_uniform("u_strength", static_cast<int>(m_strength));
128 }
129 }
130
131 void GaussianBlur::resize(const int width, const int height)
132 {
133 m_horizontal.recreate(width, height);
134 m_vertical.recreate(width, height);
135 }
136
137 unsigned int GaussianBlur::render(const unsigned int input)
138 {
139 m_shader.bind();
140
141 m_horizontal.bind(true);
142 m_shader.set_uniform("u_direction", 1.0f, 0.0f);
143
144 glActiveTexture(GL_TEXTURE0);
145 glBindTexture(GL_TEXTURE_2D, input);
146 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
147
148 m_vertical.bind(true);
149 m_shader.set_uniform("u_direction", 0.0f, 1.0f);
150
151 glBindTexture(GL_TEXTURE_2D, m_horizontal.texture());
152 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
153
154 return m_vertical.texture();
155 }
156
158 {
159 m_strength = strength;
160 m_shader.set_uniform("u_strength", static_cast<int>(m_strength));
161 }
162
164 {
165 return core::ServiceLocator<core::Config>::ref().get<bool>("gaussian_blur", "graphics.effects");
166 }
167 } // namespace graphics
168} // namespace galaxy
constexpr const char *const gaussianblur_vert
GaussianBlur.cpp galaxy.
constexpr const char *const gaussianblur_frag
bool is_enabled() override
Is this effect enabled?
GaussianBlur()=delete
Constructor.
void resize(const int width, const int height) override
Resize framebuffers.
RenderTexture m_horizontal
Framebuffer to render horizontal blur to.
unsigned int render(const unsigned int input) override
Render effect to input texture.
RenderTexture m_vertical
Framebuffer to render vertical blur to.
Strength
Pixel sample strength.
Strength m_strength
Which pixel sample strength to use.
void set_strength(const Strength strength)
Set gaussian blur strength.
Shader m_shader
Shader for post processing effect.
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 set_uniform(const std::string &name, const Uniforms &... args)
Specialized variadic template for setting shader uniforms.
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