From f061d9191de4a91a38241cde96954617947b1f12 Mon Sep 17 00:00:00 2001 From: ValeriiKoniushenko Date: Thu, 13 Jul 2023 08:41:56 +0300 Subject: [PATCH] Added lightning support --- game/assets/shaders/main-fragment.glsl | 52 ++++++++++++++++++- game/source/VaKon2D.cpp | 1 + lib/core/core-wrappers/CMakeLists.txt | 1 + .../include/CustomShaderProgram.h | 8 +++ lib/core/core-wrappers/include/Gl.h | 8 +++ lib/core/core-wrappers/include/Image.h | 3 ++ .../source/CustomShaderProgram.cpp | 5 +- lib/core/core-wrappers/source/Image.cpp | 12 ++++- lib/core/shapes/include/DrawAble.h | 4 +- lib/core/shapes/include/Rectangle.h | 2 +- lib/core/shapes/source/DrawAble.cpp | 2 +- lib/core/shapes/source/Rectangle.cpp | 10 ++-- 12 files changed, 98 insertions(+), 10 deletions(-) diff --git a/game/assets/shaders/main-fragment.glsl b/game/assets/shaders/main-fragment.glsl index ed36972ed..3f33b4709 100644 --- a/game/assets/shaders/main-fragment.glsl +++ b/game/assets/shaders/main-fragment.glsl @@ -5,8 +5,58 @@ out vec4 FragColor; in vec2 ioCv; uniform sampler2D uTexture; +uniform float uGamma; +uniform float uBrightness; +uniform float uContrast; +uniform float uSaturation; + +mat4 brightnessMatrix(float brightness) +{ + return mat4( + 1, 0, 0, 0, + 0, 1, 0, 0, + 0, 0, 1, 0, + brightness, brightness, brightness, 1); +} + +mat4 contrastMatrix(float contrast) +{ + float t = (1.0 - contrast) / 2.0; + + return mat4( + contrast, 0, 0, 0, + 0, contrast, 0, 0, + 0, 0, contrast, 0, + t, t, t, 1); + +} + +mat4 saturationMatrix(float saturation) +{ + vec3 luminance = vec3(0.3086, 0.6094, 0.0820); + + float oneMinusSat = 1.0 - saturation; + + vec3 red = vec3(luminance.x * oneMinusSat); + red+= vec3(saturation, 0, 0); + + vec3 green = vec3(luminance.y * oneMinusSat); + green += vec3(0, saturation, 0); + + vec3 blue = vec3(luminance.z * oneMinusSat); + blue += vec3(0, 0, saturation); + + return mat4( + red, 0, + green, 0, + blue, 0, + 0, 0, 0, 1); +} void main() { - FragColor = texture(uTexture, ioCv); + vec4 textureColor = texture(uTexture, ioCv); + vec3 diffuseColor = pow(texture(uTexture, ioCv).rgb, vec3(uGamma)); + vec4 color = vec4(diffuseColor, textureColor.a); + FragColor = brightnessMatrix(uBrightness) * contrastMatrix(uContrast) * saturationMatrix(uSaturation) * color; } \ No newline at end of file diff --git a/game/source/VaKon2D.cpp b/game/source/VaKon2D.cpp index 04e3b4ced..ba307758a 100644 --- a/game/source/VaKon2D.cpp +++ b/game/source/VaKon2D.cpp @@ -50,6 +50,7 @@ void VaKon2D::start() Texture texture(Gl::Texture::Target::Texture2D, true, true); Image image("assets/textures/apple.png"); + image.setInternalChannel(Gl::Texture::Channel::SRGBA); texture.setImage(image); texture.setMagAndMinFilter(Gl::Texture::MagFilter::Linear, Gl::Texture::MinFilter::LinearMipmapLinear); diff --git a/lib/core/core-wrappers/CMakeLists.txt b/lib/core/core-wrappers/CMakeLists.txt index 35f698261..eb5233ba7 100644 --- a/lib/core/core-wrappers/CMakeLists.txt +++ b/lib/core/core-wrappers/CMakeLists.txt @@ -22,4 +22,5 @@ target_link_libraries( Logger Delegate Misc + Window ) \ No newline at end of file diff --git a/lib/core/core-wrappers/include/CustomShaderProgram.h b/lib/core/core-wrappers/include/CustomShaderProgram.h index 66fb9b269..304f37705 100644 --- a/lib/core/core-wrappers/include/CustomShaderProgram.h +++ b/lib/core/core-wrappers/include/CustomShaderProgram.h @@ -31,4 +31,12 @@ class CustomShaderProgram : public ShaderProgram CustomShaderProgram(Shader& frag, Shader& vert); void OnAfterLink() override; + + struct Lightning + { + float gamma = 2.2f; + float brightness = 0.05f; + float contrast = 1.2f; + float saturation = 1.f; + } lightning; }; \ No newline at end of file diff --git a/lib/core/core-wrappers/include/Gl.h b/lib/core/core-wrappers/include/Gl.h index 0914f0efa..8b2194b9d 100644 --- a/lib/core/core-wrappers/include/Gl.h +++ b/lib/core/core-wrappers/include/Gl.h @@ -203,6 +203,14 @@ class Gl MirrorClamp2Edge = GL_MIRROR_CLAMP_TO_EDGE }; + enum class Channel + { + SRGB = GL_SRGB, + RGB = GL_RGB, + RGBA = GL_RGBA, + SRGBA = GL_SRGB_ALPHA + }; + _NODISCARD static MagFilter stringToMagFilter(const std::string& filter); _NODISCARD static MinFilter stringToMinFilter(const std::string& filter); diff --git a/lib/core/core-wrappers/include/Image.h b/lib/core/core-wrappers/include/Image.h index 6497e2c9f..067639587 100644 --- a/lib/core/core-wrappers/include/Image.h +++ b/lib/core/core-wrappers/include/Image.h @@ -58,6 +58,8 @@ class Image : Utils::NotCopyableButMovable _NODISCARD Channel getChannel() const; _NODISCARD unsigned char* data(); _NODISCARD const unsigned char* data() const; + void setInternalChannel(Gl::Texture::Channel channel); + _NODISCARD Gl::Texture::Channel getInternalChannel() const; void loadImage(std::filesystem::path&&, bool isFlipVertically = true); void loadToGpu(); void clear(); @@ -70,4 +72,5 @@ class Image : Utils::NotCopyableButMovable unsigned char* data_{}; int width_{}, height_{}; Channel channel_ = Channel::None; + Gl::Texture::Channel internalChannel_ = Gl::Texture::Channel::RGB; }; \ No newline at end of file diff --git a/lib/core/core-wrappers/source/CustomShaderProgram.cpp b/lib/core/core-wrappers/source/CustomShaderProgram.cpp index 1f4eb3068..bd085d152 100644 --- a/lib/core/core-wrappers/source/CustomShaderProgram.cpp +++ b/lib/core/core-wrappers/source/CustomShaderProgram.cpp @@ -22,12 +22,15 @@ #include "CustomShaderProgram.h" +#include "Window.h" + void CustomShaderProgram::OnAfterLink() { ShaderProgram::OnAfterLink(); glEnable(GL_BLEND); glEnable(GL_CULL_FACE); + glEnable(GL_FRAMEBUFFER_SRGB); glDisable(GL_DEPTH_TEST); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); } @@ -38,4 +41,4 @@ CustomShaderProgram::CustomShaderProgram(bool shouldCreate) : ShaderProgram(shou CustomShaderProgram::CustomShaderProgram(Shader& frag, Shader& vert) : ShaderProgram(frag, vert) { -} +} \ No newline at end of file diff --git a/lib/core/core-wrappers/source/Image.cpp b/lib/core/core-wrappers/source/Image.cpp index f6c00d5b6..a788fa610 100644 --- a/lib/core/core-wrappers/source/Image.cpp +++ b/lib/core/core-wrappers/source/Image.cpp @@ -170,6 +170,16 @@ GLenum Image::convertChannelToGlChannel(Image::Channel channel) void Image::loadToGpu() { - Gl::Texture::texImage2D(Gl::Texture::Target::Texture2D, 0, convertChannelToGlChannel(channel_), width_, height_, 0, + Gl::Texture::texImage2D(Gl::Texture::Target::Texture2D, 0, static_cast(internalChannel_), width_, height_, 0, convertChannelToGlChannel(channel_), GL_UNSIGNED_BYTE, data_); } + +void Image::setInternalChannel(Gl::Texture::Channel channel) +{ + internalChannel_ = channel; +} + +Gl::Texture::Channel Image::getInternalChannel() const +{ + return internalChannel_; +} diff --git a/lib/core/shapes/include/DrawAble.h b/lib/core/shapes/include/DrawAble.h index bfe79d9fd..2a3ca3b1b 100644 --- a/lib/core/shapes/include/DrawAble.h +++ b/lib/core/shapes/include/DrawAble.h @@ -24,12 +24,12 @@ #include -class ShaderProgram; +class CustomShaderProgram; class DrawAble { public: - virtual void draw(ShaderProgram& shaderProgram); + virtual void draw(CustomShaderProgram& shaderProgram); _NODISCARD virtual std::size_t getVerticesCount() const; void setPosition(const glm::vec2& newPosition); diff --git a/lib/core/shapes/include/Rectangle.h b/lib/core/shapes/include/Rectangle.h index f99103e21..6da89c4d6 100644 --- a/lib/core/shapes/include/Rectangle.h +++ b/lib/core/shapes/include/Rectangle.h @@ -32,7 +32,7 @@ class Texture; class Rectangle : public DrawAble { public: - void draw(ShaderProgram& shaderProgram) override; + void draw(CustomShaderProgram& shaderProgram) override; _NODISCARD std::size_t getVerticesCount() const override; void setTexture(Texture& texture); diff --git a/lib/core/shapes/source/DrawAble.cpp b/lib/core/shapes/source/DrawAble.cpp index e311ef9ec..ef15e040d 100644 --- a/lib/core/shapes/source/DrawAble.cpp +++ b/lib/core/shapes/source/DrawAble.cpp @@ -24,7 +24,7 @@ #include "Gl.h" -void DrawAble::draw(ShaderProgram& shaderProgram) +void DrawAble::draw(CustomShaderProgram& shaderProgram) { Gl::drawArrays(GL_TRIANGLE_STRIP, 0, getVerticesCount()); } diff --git a/lib/core/shapes/source/Rectangle.cpp b/lib/core/shapes/source/Rectangle.cpp index 098f52bea..c5c36aed7 100644 --- a/lib/core/shapes/source/Rectangle.cpp +++ b/lib/core/shapes/source/Rectangle.cpp @@ -22,7 +22,7 @@ #include "Rectangle.h" -#include "ShaderProgram.h" +#include "CustomShaderProgram.h" #include "Texture.h" #include "Window.h" @@ -30,7 +30,7 @@ #include #include -void Rectangle::draw(ShaderProgram& shaderProgram) +void Rectangle::draw(CustomShaderProgram& shaderProgram) { if (texture_) { @@ -46,7 +46,11 @@ void Rectangle::draw(ShaderProgram& shaderProgram) shaderProgram.uniform("uTransform", false, trans); shaderProgram.uniform( "uResolution", static_cast(GetWindow().getSize().width), static_cast(GetWindow().getSize().height)); - + shaderProgram.uniform("uGamma", shaderProgram.lightning.gamma); + shaderProgram.uniform("uBrightness", shaderProgram.lightning.brightness); + shaderProgram.uniform("uContrast", shaderProgram.lightning.contrast); + shaderProgram.uniform("uSaturation", shaderProgram.lightning.saturation); + DrawAble::draw(shaderProgram); }