diff --git a/libs/gfx/include/psemek/gfx/effect/overlay.hpp b/libs/gfx/include/psemek/gfx/effect/overlay.hpp new file mode 100644 index 00000000..a6b68a12 --- /dev/null +++ b/libs/gfx/include/psemek/gfx/effect/overlay.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include +#include + +#include + +namespace psemek::gfx +{ + + struct overlay + { + overlay(); + ~overlay(); + + void invoke(texture_2d const & src, render_target const & dst); + + private: + psemek_declare_pimpl + }; + +} diff --git a/libs/gfx/source/effect/overlay.cpp b/libs/gfx/source/effect/overlay.cpp new file mode 100644 index 00000000..01463f76 --- /dev/null +++ b/libs/gfx/source/effect/overlay.cpp @@ -0,0 +1,75 @@ +#include +#include +#include + +#include + +namespace psemek::gfx +{ + + static char const overlay_vs[] = +R"(#version 330 + +const vec4 vertices[6] = vec4[6]( + vec4(-1.0, -1.0, 0.0, 1.0), + vec4( 1.0, -1.0, 0.0, 1.0), + vec4( 1.0, 1.0, 0.0, 1.0), + + vec4(-1.0, -1.0, 0.0, 1.0), + vec4( 1.0, 1.0, 0.0, 1.0), + vec4(-1.0, 1.0, 0.0, 1.0) +); + +out vec2 texcoord; + +void main() +{ + gl_Position = vertices[gl_VertexID]; + texcoord = vertices[gl_VertexID].xy * 0.5 + vec2(0.5); +} +)"; + + static char const overlay_fs[] = +R"(#version 330 + +uniform sampler2D u_texture; + +in vec2 texcoord; + +out vec4 out_color; + +void main() +{ + out_color = texture(u_texture, texcoord); +} +)"; + + struct overlay::impl + { + gfx::program program{overlay_vs, overlay_fs}; + gfx::array array; + }; + + overlay::overlay() + : pimpl_{std::make_unique()} + { + impl().program.bind(); + impl().program["u_texture"] = 0; + } + + overlay::~overlay() = default; + + void overlay::invoke(texture_2d const & src, render_target const & dst) + { + gl::Disable(gl::DEPTH_TEST); + gl::Disable(gl::CULL_FACE); + + dst.bind(); + gl::ActiveTexture(gl::TEXTURE0); + src.bind(); + impl().array.bind(); + impl().program.bind(); + gl::DrawArrays(gl::TRIANGLES, 0, 6); + } + +}