Skip to content

Commit

Permalink
Refactored post-processing effects
Browse files Browse the repository at this point in the history
  • Loading branch information
Isti01 committed Aug 4, 2022
1 parent eb04628 commit 3b96d26
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 115 deletions.
11 changes: 11 additions & 0 deletions assets/shaders/gamma_correction.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#version 450 core

uniform sampler2D colorTexture;
uniform float power = 2.2;

layout(location = 0) out vec4 color;

void main() {
vec4 pixel = texelFetch(colorTexture, ivec2(gl_FragCoord.xy), 0);
color = vec4(pow(pixel.xyz, vec3(1) / power), pixel.w);
}
7 changes: 7 additions & 0 deletions assets/shaders/gamma_correction.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#version 450 core

layout(location = 0) in vec3 position;

void main() {
gl_Position = vec4(position, 1);
}
37 changes: 37 additions & 0 deletions src/Scene/Effects/ChromaticAberrationEffect.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef CHROMATICABERRATIONEFFECT_H
#define CHROMATICABERRATIONEFFECT_H

#include "PostProcessEffect.h"

class ChromaticAberrationEffect : public PostProcessEffect {
float aberrationStart = 1.5f;
float aberrationROffset = 0.005;
float aberrationGOffset = 0.01;
float aberrationBOffset = -0.005;

public:
ChromaticAberrationEffect(bool enabled)
: PostProcessEffect(AssetManager::instance().loadShaderProgram("assets/shaders/chromatic_aberration_effect"),
enabled) {}

void renderGui() override {
ImGui::Checkbox("Enable chromatic aberration effect", &enabled);

if (enabled) {
ImGui::SliderFloat("Aberration start", &aberrationStart, 0.5, 3);
ImGui::SliderFloat("Aberration R Offset", &aberrationROffset, -0.01, 0.01);
ImGui::SliderFloat("Aberration G Offset", &aberrationGOffset, -0.01, 0.01);
ImGui::SliderFloat("Aberration B Offset", &aberrationBOffset, -0.01, 0.01);
}
}

void updateUniforms() override {
shader->setFloat("start", aberrationStart);
shader->setFloat("rOffset", aberrationROffset);
shader->setFloat("gOffset", aberrationGOffset);
shader->setFloat("bOffset", aberrationBOffset);
}
};


#endif
38 changes: 38 additions & 0 deletions src/Scene/Effects/CrosshairEffect.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef CROSSHAIREFFECT_H
#define CROSSHAIREFFECT_H

#include "../../Application/Window.h"
#include "./PostProcessEffect.h"

class CrosshairEffect : public PostProcessEffect {
float crosshairSize = 0.015f;
float crosshairVerticalWidth = 0.2f;
float crosshairHorizontalWidth = 0.15f;

public:
CrosshairEffect(bool enabled)
: PostProcessEffect(AssetManager::instance().loadShaderProgram("assets/shaders/crosshair"), enabled) {}
void renderGui() override {
ImGui::Checkbox("Enable crosshair", &enabled);
if (enabled) {
ImGui::SliderFloat("Crosshair size", &crosshairSize, 0.01, 1);
ImGui::SliderFloat("Crosshair vertical width", &crosshairVerticalWidth, 0.01, 1);
ImGui::SliderFloat("Crosshair horizontal width", &crosshairHorizontalWidth, 0.01, 1);
}
}

void updateUniforms() override {
auto& window = Window::instance();
auto width = window.getWindowWidth();
auto height = window.getWindowHeight();
float aspectRatio = width == 0 || height == 0 ? 0 : static_cast<float>(width) / static_cast<float>(height);

shader->setFloat("size", crosshairSize);
shader->setFloat("verticalWidth", crosshairVerticalWidth);
shader->setFloat("horizontalWidth", crosshairHorizontalWidth);
shader->setFloat("aspectRatio", aspectRatio);
}
};


#endif
19 changes: 19 additions & 0 deletions src/Scene/Effects/InvertEffect.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef INVERTEFFECT_H
#define INVERTEFFECT_H

#include "PostProcessEffect.h"

class InvertEffect : public PostProcessEffect {
public:
InvertEffect(bool enabled)
: PostProcessEffect(AssetManager::instance().loadShaderProgram("assets/shaders/invert_effect"), enabled){}

void updateUniforms() override {}

void renderGui() override {
ImGui::Checkbox("Enable invert effect", &enabled);
}
};


#endif
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
#include "PostProcessEffect.h"

#include "../Application/Window.h"
#include "../Rendering/ColorRenderPass.h"
#include "../../Application/Window.h"
#include "../../Rendering/ColorRenderPass.h"

PostProcessEffect::PostProcessEffect(const Ref<const ShaderProgram>& shader) : shader(shader) {}
PostProcessEffect::PostProcessEffect(const Ref<const ShaderProgram>& shader, bool enabled)
: shader(shader),
enabled(enabled) {}

void PostProcessEffect::render() {
if (!enabled) {
return;
}

Window& window = Window::instance();
int32_t width = window.getWindowWidth();
int32_t height = window.getWindowHeight();
Expand All @@ -16,6 +22,8 @@ void PostProcessEffect::render() {
Ref<FramebufferStack> framebufferStack = window.getFramebufferStack();
Ref<Framebuffer> colorSource = framebufferStack->peek();
framebufferStack->push(framebuffer, 1);

updateUniforms();
ColorRenderPass::renderTextureWithEffect(colorSource->getColorAttachment(0), shader);

Ref<Framebuffer> resultFbo = framebufferStack->pop();
Expand Down
27 changes: 27 additions & 0 deletions src/Scene/Effects/PostProcessEffect.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef POSTPROCESSEFFECT_H
#define POSTPROCESSEFFECT_H

#include "../../AssetManager/AssetManager.h"
#include "../../Rendering/FrameBuffer.h"
#include "../../Rendering/ShaderProgram.h"
#include "../../glCraft.h"

class PostProcessEffect {
protected:
bool enabled;
Ref<const ShaderProgram> shader;
Ref<Framebuffer> framebuffer;

public:
explicit PostProcessEffect(const Ref<const ShaderProgram>& shader, bool enabled = false);

Ref<const ShaderProgram> getShader() { return shader; };

virtual void updateUniforms() = 0;
virtual void renderGui() = 0;
void render();

virtual ~PostProcessEffect() = default;
};

#endif
34 changes: 34 additions & 0 deletions src/Scene/Effects/VignetteEffect.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef VIGNETTEEFFECT_H
#define VIGNETTEEFFECT_H

#include "PostProcessEffect.h"

class VignetteEffect : public PostProcessEffect {
float vignetteIntensity = 2.9;
float vignetteStart = 1.5f;

public:
VignetteEffect(bool enabled)
: PostProcessEffect(AssetManager::instance().loadShaderProgram("assets/shaders/vignette_effect"), enabled) {}

void updateUniforms() override {
shader->setFloat("intensity", vignetteIntensity);
shader->setFloat("start", vignetteStart);
}

void renderGui() override {
ImGui::Checkbox("Enable vignette effect", &enabled);

if (enabled) {
float invertedIntensity = 4 - vignetteIntensity;
if (ImGui::SliderFloat("Vignette intensity", &invertedIntensity, 1, 3)) {
vignetteIntensity = 4 - invertedIntensity;
}

ImGui::SliderFloat("Vignette start", &vignetteStart, 0, 3);
}
}
};


#endif
19 changes: 0 additions & 19 deletions src/Scene/PostProcessEffect.h

This file was deleted.

78 changes: 8 additions & 70 deletions src/Scene/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@
Scene::Scene(const std::string& savePath)
: persistence(std::make_shared<Persistence>(savePath)),
world(std::make_shared<World>(persistence)),
player(world, persistence),
vignetteEffect(AssetManager::instance().loadShaderProgram("assets/shaders/vignette_effect")),
invertEffect(AssetManager::instance().loadShaderProgram("assets/shaders/invert_effect")),
chromaticAberrationEffect(
AssetManager::instance().loadShaderProgram("assets/shaders/chromatic_aberration_effect")),
crosshair(AssetManager::instance().loadShaderProgram("assets/shaders/crosshair")) {
player(world, persistence) {
onResized(Application::instance().getWindowWidth(), Application::instance().getWindowHeight());
updateMouse();
}
Expand Down Expand Up @@ -54,30 +49,8 @@ void Scene::render() {
outline.render(mvp * glm::translate(ray.getHitTarget().position));
}

if (enableCrosshair) {
crosshair.getShader()->setFloat("size", crosshairSize);
crosshair.getShader()->setFloat("verticalWidth", crosshairVerticalWidth);
crosshair.getShader()->setFloat("horizontalWidth", crosshairHorizontalWidth);
crosshair.getShader()->setFloat("aspectRatio", aspectRatio);
crosshair.render();
}

if (enableChromaticAberration) {
chromaticAberrationEffect.getShader()->setFloat("start", aberrationStart);
chromaticAberrationEffect.getShader()->setFloat("rOffset", aberrationROffset);
chromaticAberrationEffect.getShader()->setFloat("gOffset", aberrationGOffset);
chromaticAberrationEffect.getShader()->setFloat("bOffset", aberrationBOffset);
chromaticAberrationEffect.render();
}

if (enableInvertEffect) {
invertEffect.render();
}

if (enableVignetteEffect) {
vignetteEffect.getShader()->setFloat("intensity", vignetteIntensity);
vignetteEffect.getShader()->setFloat("start", vignetteStart);
vignetteEffect.render();
for (auto& effect: postProcessingEffects) {
effect->render();
}
}

Expand All @@ -87,7 +60,6 @@ void Scene::renderMenu() {
ImGui::Text("Player position: x:%f, y:%f, z:%f", position.x, position.y, position.z);
glm::vec3 lookDirection = player.getCamera().getLookDirection();
ImGui::Text("Player direction: x:%f, y:%f, z:%f", lookDirection.x, lookDirection.y, lookDirection.z);
ImGui::Text("Screen aspect ratio: %f", aspectRatio);

ImGui::Spacing();
ImGui::Spacing();
Expand All @@ -107,52 +79,18 @@ void Scene::renderMenu() {
ImGui::Spacing();
ImGui::Spacing();

ImGui::Checkbox("Enable crosshair", &enableCrosshair);
if (enableCrosshair) {
ImGui::SliderFloat("Crosshair size", &crosshairSize, 0.01, 1);
ImGui::SliderFloat("Crosshair vertical width", &crosshairVerticalWidth, 0.01, 1);
ImGui::SliderFloat("Crosshair horizontal width", &crosshairHorizontalWidth, 0.01, 1);
}

ImGui::Spacing();
ImGui::Spacing();

ImGui::Checkbox("Enable XRay", &enableXRay);

ImGui::Spacing();
ImGui::Spacing();

ImGui::Checkbox("Enable chromatic aberration effect", &enableChromaticAberration);

if (enableChromaticAberration) {
ImGui::SliderFloat("Aberration start", &aberrationStart, 0.5, 3);
ImGui::SliderFloat("Aberration R Offset", &aberrationROffset, -0.01, 0.01);
ImGui::SliderFloat("Aberration G Offset", &aberrationGOffset, -0.01, 0.01);
ImGui::SliderFloat("Aberration B Offset", &aberrationBOffset, -0.01, 0.01);
}

ImGui::Spacing();
ImGui::Spacing();

ImGui::Checkbox("Enable invert effect", &enableInvertEffect);

ImGui::Spacing();
ImGui::Spacing();
for (auto& effect: postProcessingEffects) {
effect->renderGui();

ImGui::Checkbox("Enable vignette effect", &enableVignetteEffect);

if (enableVignetteEffect) {
float invertedIntensity = 4 - vignetteIntensity;
if (ImGui::SliderFloat("Vignette intensity", &invertedIntensity, 1, 3)) {
vignetteIntensity = 4 - invertedIntensity;
}

ImGui::SliderFloat("Vignette start", &vignetteStart, 0, 3);
ImGui::Spacing();
ImGui::Spacing();
}

ImGui::Spacing();
ImGui::Spacing();

BlockData::BlockType blockToPlace = player.getBlockToPlace();
ImGui::Text("Selected Block: %s", BlockName::blockTypeToName(blockToPlace));

Expand Down Expand Up @@ -266,7 +204,7 @@ void Scene::renderGui() {
}

void Scene::onResized(int32_t width, int32_t height) {
aspectRatio = width == 0 || height == 0 ? 0 : static_cast<float>(width) / static_cast<float>(height);
float aspectRatio = width == 0 || height == 0 ? 0 : static_cast<float>(width) / static_cast<float>(height);
projectionMatrix = glm::perspective<float>(glm::half_pi<float>(), aspectRatio, zNear, zFar);
}

Expand Down
Loading

0 comments on commit 3b96d26

Please sign in to comment.