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
FilmicGrain.cpp
Go to the documentation of this file.
1
7
8#include <glad/glad.h>
9#include <GLFW/glfw3.h>
10
12#include "galaxy/core/ServiceLocator.hpp"
13
14#include "FilmicGrain.hpp"
15
19constexpr const char* const filmicgrain_vert = R"(
20 #version 460 core
21 layout (location = 0) in vec2 l_pos;
22 layout (location = 1) in vec2 l_texels;
23
24 void main()
25 {
26 gl_Position = vec4(l_pos, 0.0, 1.0);
27 }
28)";
29
63constexpr const char* const filmicgrain_frag = R"(
64 #version 460 core
65 #define PI 3.1415926535897932384626433832795
66
67 out vec4 io_frag_colour;
68
69 uniform float u_amount;
70 uniform float u_time;
71 uniform sampler2D u_texture;
72
73 void main()
74 {
75 vec2 tex_size = textureSize(u_texture, 0).xy;
76 vec2 tex_coord = gl_FragCoord.xy / tex_size;
77
78 vec4 colour = texture(u_texture, tex_coord);
79
80 float intensity = fract(10000 * sin((gl_FragCoord.x + gl_FragCoord.y * u_time) * PI));
81
82 colour.rgb += (u_amount * intensity);
83
84 io_frag_colour = colour;
85 }
86)";
87
88namespace galaxy
89{
90 namespace graphics
91 {
92 FilmicGrain::FilmicGrain(const int width, const int height)
93 : m_amount {0.01}
94 {
95 m_fb.create(width, height);
96
98 {
100 }
101
102 m_shader.set_uniform("u_texture", 0);
103 m_shader.set_uniform("u_amount", m_amount);
104 m_shader.set_uniform("u_time", static_cast<float>(glfwGetTime()));
105 }
106
107 void FilmicGrain::resize(const int width, const int height)
108 {
109 m_fb.recreate(width, height);
110 }
111
112 unsigned int FilmicGrain::render(const unsigned int input)
113 {
114 m_fb.bind(true);
115 m_shader.bind();
116
117 m_shader.set_uniform("u_time", static_cast<float>(glfwGetTime()));
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 FilmicGrain::set_amount(const float amount)
127 {
128 m_amount = amount;
129 m_shader.set_uniform("u_amount", m_amount);
130 }
131
133 {
134 return core::ServiceLocator<core::Config>::ref().get<bool>("film_grain", "graphics.effects");
135 }
136 } // namespace graphics
137} // namespace galaxy
constexpr const char *const filmicgrain_vert
FilmicGrain.cpp galaxy.
constexpr const char *const filmicgrain_frag
Filmic Grain fragment shader.
RenderTexture m_fb
Framebuffer to render aberration.
Shader m_shader
Shader for post processing effect.
void resize(const int width, const int height) override
Resize framebuffers.
void set_amount(const float amount)
Set intensity of film grain effect.
bool is_enabled() override
Is this effect enabled?
unsigned int render(const unsigned int input) override
Render effect to input texture.
float m_amount
Film grain intensity.
FilmicGrain()=delete
Constructor.
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