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
Sharpen.cpp
Go to the documentation of this file.
1
7
8#include <algorithm>
9
10#include <glad/glad.h>
11
13#include "galaxy/core/ServiceLocator.hpp"
14
15#include "Sharpen.hpp"
16
20constexpr const char* const sharpen_vert = R"(
21 #version 460 core
22 layout (location = 0) in vec2 l_pos;
23 layout (location = 1) in vec2 l_texels;
24
25 void main()
26 {
27 gl_Position = vec4(l_pos, 0.0, 1.0);
28 }
29)";
30
64constexpr const char* const sharpen_frag = R"(
65 #version 460 core
66
67 out vec4 io_frag_colour;
68
69 uniform float u_amount;
70 uniform sampler2D u_texture;
71
72 void main()
73 {
74 vec2 tex_size = textureSize(u_texture, 0).xy;
75 vec2 frag_coord = gl_FragCoord.xy;
76 vec2 tex_coord = frag_coord / tex_size;
77
78 float neighbour = u_amount * -1.0;
79 float center = u_amount * 4.0 + 1.0;
80
81 vec3 sharpen =
82 texture(u_texture, (frag_coord + vec2( 0, 1)) / tex_size).rgb * neighbour
83 + texture(u_texture, (frag_coord + vec2(-1, 0)) / tex_size).rgb * neighbour
84 + texture(u_texture, (frag_coord + vec2( 0, 0)) / tex_size).rgb * center
85 + texture(u_texture, (frag_coord + vec2( 1, 0)) / tex_size).rgb * neighbour
86 + texture(u_texture, (frag_coord + vec2( 0, -1)) / tex_size).rgb * neighbour;
87
88 io_frag_colour = vec4(sharpen, texture(u_texture, tex_coord).a);
89 }
90)";
91
92namespace galaxy
93{
94 namespace graphics
95 {
96 Sharpen::Sharpen(const int width, const int height)
97 : m_amount {0.1f}
98 {
99 m_fb.create(width, height);
100
102 {
104 m_shader.set_uniform("u_texture", 0);
105 m_shader.set_uniform("u_amount", m_amount);
106 }
107 }
108
109 void Sharpen::resize(const int width, const int height)
110 {
111 m_fb.recreate(width, height);
112 }
113
114 unsigned int Sharpen::render(const unsigned int input)
115 {
116 m_fb.bind(true);
117 m_shader.bind();
118
119 glActiveTexture(GL_TEXTURE0);
120 glBindTexture(GL_TEXTURE_2D, input);
121 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
122
123 return m_fb.texture();
124 }
125
126 void Sharpen::set_amount(const float amount)
127 {
128 m_amount = std::clamp(amount, 0.0f, 10.0f);
129 m_shader.set_uniform("u_amount", m_amount);
130 }
131
133 {
134 return m_amount;
135 }
136
138 {
139 return core::ServiceLocator<core::Config>::ref().get<bool>("sharpen", "graphics.effects");
140 }
141 } // namespace graphics
142} // namespace galaxy
constexpr const char *const sharpen_frag
Sharpen fragment shader.
Definition Sharpen.cpp:64
constexpr const char *const sharpen_vert
Sharpen.cpp galaxy.
Definition Sharpen.cpp:20
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
Shader m_shader
Shader for post processing effect.
Definition Sharpen.hpp:91
void set_amount(const float amount)
Set sharpening amount.
Definition Sharpen.cpp:126
unsigned int render(const unsigned int input) override
Render effect to input texture.
Definition Sharpen.cpp:114
Sharpen()=delete
Constructor.
float m_amount
Sharpening amount.
Definition Sharpen.hpp:101
void resize(const int width, const int height) override
Resize framebuffers.
Definition Sharpen.cpp:109
RenderTexture m_fb
Framebuffer to render effect to.
Definition Sharpen.hpp:96
bool is_enabled() override
Is this effect enabled?
Definition Sharpen.cpp:137
float get_amount() const
Get sharpening amount.
Definition Sharpen.cpp:132
Animated.cpp galaxy.
Definition Animated.cpp:16