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
GammaCorrection.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 "GammaCorrection.hpp"
14
18constexpr const char* const gammacorrection_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 gammacorrection_frag = R"(
63 #version 460 core
64
65 out vec4 io_frag_colour;
66
67 uniform float u_gamma;
68 uniform sampler2D u_texture;
69
70 void main()
71 {
72 vec2 uv = gl_FragCoord.xy / textureSize(u_texture, 0).xy;
73
74 io_frag_colour = texture(u_texture, uv);
75 io_frag_colour.rgb = pow(io_frag_colour.rgb, vec3(1.0 / u_gamma));
76 }
77)";
78
79namespace galaxy
80{
81 namespace graphics
82 {
83 GammaCorrection::GammaCorrection(const int width, const int height)
84 : m_gamma {2.2f}
85 {
86 m_fb.create(width, height);
87
89 {
91 m_shader.set_uniform("u_texture", 0);
92 m_shader.set_uniform("u_gamma", m_gamma);
93 }
94 }
95
96 void GammaCorrection::resize(const int width, const int height)
97 {
98 m_fb.recreate(width, height);
99 }
100
101 unsigned int GammaCorrection::render(const unsigned int input)
102 {
103 m_fb.bind(true);
104 m_shader.bind();
105
106 glActiveTexture(GL_TEXTURE0);
107 glBindTexture(GL_TEXTURE_2D, input);
108 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
109
110 return m_fb.texture();
111 }
112
113 void GammaCorrection::set_gamma(const float gamma_mod)
114 {
115 m_gamma = gamma_mod;
116
117 if (static_cast<int>(m_gamma) == 0)
118 {
119 m_gamma = 1.0f;
120 }
121
122 m_shader.set_uniform("u_gamma", m_gamma);
123 }
124
126 {
127 return m_gamma;
128 }
129
131 {
132 return core::ServiceLocator<core::Config>::ref().get<bool>("gamma_correction", "graphics.effects");
133 }
134 } // namespace graphics
135} // namespace galaxy
constexpr const char *const gammacorrection_vert
GammaCorrection.cpp galaxy.
constexpr const char *const gammacorrection_frag
Gamma Correction fragment shader.
bool is_enabled() override
Is this effect enabled?
void set_gamma(const float gamma_mod)
Set gamma modifier.
unsigned int render(const unsigned int input) override
Render effect to input texture.
float m_gamma
Amount to adjust gamma by.
RenderTexture m_fb
Framebuffer to render effect to.
float get_gamma() const
Get gamma modifier.
GammaCorrection()=delete
Constructor.
void resize(const int width, const int height) override
Resize framebuffers.
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