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
ChromaticAberration.cpp
Go to the documentation of this file.
1
7
8#include <glad/glad.h>
9
11#include "galaxy/core/ServiceLocator.hpp"
12
14
18constexpr const char* const chromaticaberration_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
62constexpr const char* const chromaticaberration_frag = R"(
63 #version 460 core
64
65 out vec4 io_frag_colour;
66
67 uniform vec2 u_direction;
68 uniform vec3 u_rgb_offset;
69 uniform sampler2D u_texture;
70
71 void main()
72 {
73 vec2 tex_size = textureSize(u_texture, 0).xy;
74 vec2 tex_coord = gl_FragCoord.xy / tex_size;
75
76 vec2 direction = tex_coord - u_direction;
77
78 io_frag_colour = texture(u_texture, tex_coord);
79 io_frag_colour.r = texture(u_texture, tex_coord + (direction * vec2(u_rgb_offset.x))).r;
80 io_frag_colour.g = texture(u_texture, tex_coord + (direction * vec2(u_rgb_offset.y))).g;
81 io_frag_colour.b = texture(u_texture, tex_coord + (direction * vec2(u_rgb_offset.z))).b;
82 }
83)";
84
85namespace galaxy
86{
87 namespace graphics
88 {
89 ChromaticAberration::ChromaticAberration(const int width, const int height)
90 : m_r_offset {0.009f}
91 , m_g_offset {0.006f}
92 , m_b_offset {-0.006f}
93 {
94 m_fb.create(width, height);
95
97 {
99 m_shader.set_uniform("u_texture", 0);
101 m_shader.set_uniform("u_direction", 0.509167f, 0.598f); // may need tweaking, in general 0.5, 0.5 is default, adjust to change direction.
102 }
103 }
104
105 void ChromaticAberration::resize(const int width, const int height)
106 {
107 m_fb.recreate(width, height);
108 }
109
110 unsigned int ChromaticAberration::render(const unsigned int input)
111 {
112 m_fb.bind(true);
113 m_shader.bind();
114
115 glActiveTexture(GL_TEXTURE0);
116 glBindTexture(GL_TEXTURE_2D, input);
117 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
118
119 return m_fb.texture();
120 }
121
123 {
124 m_r_offset = std::clamp(r, -1.0f, 1.0f);
126 }
127
129 {
130 m_g_offset = std::clamp(g, -1.0f, 1.0f);
132 }
133
135 {
136 m_b_offset = std::clamp(b, -1.0f, 1.0f);
138 }
139
140 void ChromaticAberration::set_rgb_offset(const float r, const float g, const float b)
141 {
142 m_r_offset = std::clamp(r, -1.0f, 1.0f);
143 m_g_offset = std::clamp(g, -1.0f, 1.0f);
144 m_b_offset = std::clamp(b, -1.0f, 1.0f);
145
147 }
148
150 {
151 return core::ServiceLocator<core::Config>::ref().get<bool>("chromatic_abberation", "graphics.effects");
152 }
153 } // namespace graphics
154} // namespace galaxy
constexpr const char *const chromaticaberration_frag
Chromatic Aberration fragment shader.
constexpr const char *const chromaticaberration_vert
ChromaticAberration.cpp galaxy.
ChromaticAberration()=delete
Constructor.
void resize(const int width, const int height) override
Resize framebuffers.
void set_g_offset(const float g)
Set chromatic r offset.
void set_r_offset(const float r)
Set chromatic r offset.
void set_rgb_offset(const float r, const float g, const float b)
Set chromatic rgb offset.
unsigned int render(const unsigned int input) override
Render effect to input texture.
RenderTexture m_fb
Framebuffer to render aberration.
bool is_enabled() override
Is this effect enabled?
Shader m_shader
Shader for post processing effect.
void set_b_offset(const float b)
Set chromatic r offset.
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