Skip to content

Commit

Permalink
Added lightning support
Browse files Browse the repository at this point in the history
  • Loading branch information
ValeriiKoniushenko committed Jul 13, 2023
1 parent 6589966 commit f061d91
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 10 deletions.
52 changes: 51 additions & 1 deletion game/assets/shaders/main-fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions game/source/VaKon2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions lib/core/core-wrappers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ target_link_libraries(
Logger
Delegate
Misc
Window
)
8 changes: 8 additions & 0 deletions lib/core/core-wrappers/include/CustomShaderProgram.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
8 changes: 8 additions & 0 deletions lib/core/core-wrappers/include/Gl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 3 additions & 0 deletions lib/core/core-wrappers/include/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
};
5 changes: 4 additions & 1 deletion lib/core/core-wrappers/source/CustomShaderProgram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -38,4 +41,4 @@ CustomShaderProgram::CustomShaderProgram(bool shouldCreate) : ShaderProgram(shou

CustomShaderProgram::CustomShaderProgram(Shader& frag, Shader& vert) : ShaderProgram(frag, vert)
{
}
}
12 changes: 11 additions & 1 deletion lib/core/core-wrappers/source/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<GLenum>(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_;
}
4 changes: 2 additions & 2 deletions lib/core/shapes/include/DrawAble.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@

#include <cstdlib>

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);
Expand Down
2 changes: 1 addition & 1 deletion lib/core/shapes/include/Rectangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion lib/core/shapes/source/DrawAble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#include "Gl.h"

void DrawAble::draw(ShaderProgram& shaderProgram)
void DrawAble::draw(CustomShaderProgram& shaderProgram)
{
Gl::drawArrays(GL_TRIANGLE_STRIP, 0, getVerticesCount());
}
Expand Down
10 changes: 7 additions & 3 deletions lib/core/shapes/source/Rectangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@

#include "Rectangle.h"

#include "ShaderProgram.h"
#include "CustomShaderProgram.h"
#include "Texture.h"
#include "Window.h"

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>

void Rectangle::draw(ShaderProgram& shaderProgram)
void Rectangle::draw(CustomShaderProgram& shaderProgram)
{
if (texture_)
{
Expand All @@ -46,7 +46,11 @@ void Rectangle::draw(ShaderProgram& shaderProgram)
shaderProgram.uniform("uTransform", false, trans);
shaderProgram.uniform(
"uResolution", static_cast<float>(GetWindow().getSize().width), static_cast<float>(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);
}

Expand Down

0 comments on commit f061d91

Please sign in to comment.