merge bloom z sun-noise

This commit is contained in:
sasankasa 2024-02-07 20:27:53 +01:00
commit bb823a60c3
9 changed files with 279 additions and 41 deletions

View File

@ -0,0 +1,33 @@
#version 330 core
out vec4 FragColor;
in vec2 TexCoords;
uniform sampler2D image;
uniform int count;
uniform bool horizontal;
uniform float weight[5] = float[] (0.2270270270, 0.1945945946, 0.1216216216, 0.0540540541, 0.0162162162);
void main()
{
vec2 tex_offset = 1.0 / textureSize(image, 0); // gets size of single texel
vec3 result = texture(image, TexCoords).rgb * weight[0];
if(horizontal)
{
for(int i = 1; i < count; ++i)
{
result += texture(image, TexCoords + vec2(tex_offset.x * i, 0.0)).rgb * weight[i];
result += texture(image, TexCoords - vec2(tex_offset.x * i, 0.0)).rgb * weight[i];
}
}
else
{
for(int i = 1; i < 5; ++i)
{
result += texture(image, TexCoords + vec2(0.0, tex_offset.y * i)).rgb * weight[i];
result += texture(image, TexCoords - vec2(0.0, tex_offset.y * i)).rgb * weight[i];
}
}
FragColor = vec4(result, 1.0);
}

View File

@ -0,0 +1,11 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoords;
out vec2 TexCoords;
void main()
{
TexCoords = aTexCoords;
gl_Position = vec4(aPos, 1.0);
}

View File

@ -0,0 +1,23 @@
#version 330 core
out vec4 FragColor;
in vec2 TexCoords;
uniform sampler2D scene;
uniform sampler2D bloomBlur;
uniform bool bloom;
uniform float exposure;
void main()
{
const float gamma = 1.5;
vec3 hdrColor = texture(scene, TexCoords).rgb;
vec3 bloomColor = texture(bloomBlur, TexCoords).rgb;
if(bloom)
hdrColor += bloomColor; // additive blending
// tone mapping
vec3 result = vec3(1.0) - exp(-hdrColor * exposure);
// also gamma correct while we're at it
result = pow(result, vec3(1.0 / gamma));
FragColor = vec4(result, 1.0);
}

View File

@ -0,0 +1,11 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoords;
out vec2 TexCoords;
void main()
{
TexCoords = aTexCoords;
gl_Position = vec4(aPos, 1.0);
}

View File

@ -1,5 +1,8 @@
#version 430 core #version 430 core
layout (location = 0) out vec4 outColor;
layout (location = 1) out vec4 BrightColor;
float AMBIENT = 0.05; float AMBIENT = 0.05;
float PI = 3.14159; float PI = 3.14159;
@ -29,7 +32,7 @@ in vec3 vecNormal;
in vec3 worldPos; in vec3 worldPos;
in vec2 vtc; in vec2 vtc;
out vec4 outColor; //out vec4 outColor;
in vec3 viewDirTS; in vec3 viewDirTS;
in vec3 lightDirTS; in vec3 lightDirTS;
@ -200,6 +203,8 @@ void main()
//sun //sun
illumination = illumination + PBRLight(sunDir, sunColor, normal, viewDir, toneMappedColor); illumination = illumination + PBRLight(sunDir, sunColor, normal, viewDir, toneMappedColor);
vec3 pbrColor = vec3(1.0) - exp(-illumination * exposition); vec3 pbrColor = vec3(1.0) - exp(-illumination * exposition);
vec3 finalColor = pbrColor; vec3 finalColor = pbrColor;
@ -210,5 +215,13 @@ void main()
finalColor = mix(pbrColor, noiseColor, noiseColor.r); finalColor = mix(pbrColor, noiseColor, noiseColor.r);
} }
float brightness = dot(finalColor, vec3(0.2126, 0.7152, 0.0722));
if(brightness > 0.2)
BrightColor = vec4(finalColor, 1.0);
else
BrightColor = vec4(0.0, 0.0, 0.0, 1.0);
outColor = vec4(finalColor, 1.0); outColor = vec4(finalColor, 1.0);
} }

View File

@ -1,14 +1,26 @@
#version 430 core #version 430 core
layout (location = 0) out vec4 outColor;
layout (location = 1) out vec4 BrightColor;
uniform samplerCube skybox; uniform samplerCube skybox;
uniform vec3 lightColor; uniform vec3 lightColor;
in vec3 texCoord; in vec3 texCoord;
out vec4 out_color; //out vec4 out_color;
void main() void main()
{ {
vec4 textureColor = texture(skybox, texCoord); vec4 textureColor = texture(skybox, texCoord);
out_color = vec4(vec3(textureColor) * lightColor * 0.35f, 1.0f);
vec3 finalColor = vec3(textureColor) * lightColor * 0.35f;
float brightness = dot(finalColor, vec3(0.2126, 0.7152, 0.0722));
if(brightness > 0.2)
BrightColor = vec4(finalColor, 1.0);
else
BrightColor = vec4(0.0, 0.0, 0.0, 1.0);
outColor = vec4(finalColor, 1.0);
} }

View File

@ -1,4 +1,6 @@
#version 430 core #version 430 core
layout (location = 0) out vec4 outColor;
layout (location = 1) out vec4 BrightColor;
uniform vec3 lightPos; uniform vec3 lightPos;
uniform vec3 lightColor; uniform vec3 lightColor;
@ -13,7 +15,7 @@ in vec3 vecNormal;
in vec3 worldPos; in vec3 worldPos;
in vec2 vtc; in vec2 vtc;
out vec4 outColor; //out vec4 outColor;
uniform sampler2D colorTexture; uniform sampler2D colorTexture;
@ -118,8 +120,14 @@ void main()
vec3 toneMappedColor = toneMapping(mixedTexture * distance); vec3 toneMappedColor = toneMapping(mixedTexture * distance);
//gamma correction //gamma correction
toneMappedColor = pow(toneMappedColor, vec3(1.0/2.2)); toneMappedColor = pow(toneMappedColor, vec3(1.0/2.2));
vec3 finalColor = toneMappedColor * lightColor * 0.2f;
vec3 textureToned = toneMappedColor * lightColor * 0.2f; float brightness = dot(finalColor, vec3(0.2126, 0.7152, 0.0722));
outColor = vec4(textureToned, 1.0); if(brightness > 0.2)
BrightColor = vec4(finalColor, 1.0);
else
BrightColor = vec4(0.0, 0.0, 0.0, 1.0);
outColor = vec4(finalColor, 1.0);
} }

View File

@ -1,5 +1,8 @@
#version 430 core #version 430 core
layout (location = 0) out vec4 outColor;
layout (location = 1) out vec4 BrightColor;
float AMBIENT = 0.05; float AMBIENT = 0.05;
float PI = 3.14159; float PI = 3.14159;
@ -23,7 +26,7 @@ in vec3 vecNormal;
in vec3 worldPos; in vec3 worldPos;
in vec2 vtc; in vec2 vtc;
out vec4 outColor; //out vec4 outColor;
vec3 toneMapping(vec3 color) vec3 toneMapping(vec3 color)
{ {
@ -124,5 +127,13 @@ void main()
finalColor = mix(toneMappedColor, noiseColor, noiseColor.r); finalColor = mix(toneMappedColor, noiseColor, noiseColor.r);
} }
float brightness = dot(finalColor, vec3(0.2126, 0.7152, 0.0722));
if(brightness > 0.2)
BrightColor = vec4(finalColor, 1.0);
else
BrightColor = vec4(0.0, 0.0, 0.0, 1.0);
outColor = vec4(finalColor, 1.0); outColor = vec4(finalColor, 1.0);
} }

View File

@ -13,11 +13,15 @@
#include <assimp/scene.h> #include <assimp/scene.h>
#include <assimp/postprocess.h> #include <assimp/postprocess.h>
#include <string> #include <string>
#include <GLFW/glfw3.h>
#include <GLFW/glfw3.h>
GLuint programTex; GLuint programTex;
GLuint programPbr; GLuint programPbr;
GLuint programSun; GLuint programSun;
GLuint programSkyBox; GLuint programSkyBox;
GLuint programBloomFinal;
GLuint programBlur;
Core::Shader_Loader shaderLoader; Core::Shader_Loader shaderLoader;
@ -65,11 +69,19 @@ float aspectRatio = 1.77f;
unsigned int hdrFBO; unsigned int hdrFBO;
unsigned int colorBuffers[2]; unsigned int colorBuffers[2];
unsigned int depthMap;
unsigned int depthMapFBO;
int HDR_WIDTH = 1024; unsigned int rboDepth;
int HDR_HEIGHT = 1024;
unsigned int pingpongFBO[2];
unsigned int pingpongColorbuffers[2];
int HDR_WIDTH;
int HDR_HEIGHT;
bool bloom = true;
float bloom_exposure = 1.f;
int blur_count = 1;
float lightPower = 8.f; float lightPower = 8.f;
glm::vec3 lightColor = glm::vec3(lightPower, lightPower, lightPower); glm::vec3 lightColor = glm::vec3(lightPower, lightPower, lightPower);
@ -116,44 +128,97 @@ glm::mat4 createPerspectiveMatrix() {
return perspectiveMatrix; return perspectiveMatrix;
} }
void initHDR() { unsigned int quadVAO = 0;
unsigned int quadVBO;
void renderQuad()
{
if (quadVAO == 0)
{
float quadVertices[] = {
// positions // texture Coords
-1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
-1.0f, -1.0f, 0.0f, 0.0f, 0.0f,
1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
};
// setup plane VAO
glGenVertexArrays(1, &quadVAO);
glGenBuffers(1, &quadVBO);
glBindVertexArray(quadVAO);
glBindBuffer(GL_ARRAY_BUFFER, quadVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), &quadVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
}
glBindVertexArray(quadVAO);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindVertexArray(0);
}
void initHDR(GLFWwindow* window) {
glfwGetWindowSize(window, &HDR_WIDTH, &HDR_HEIGHT);
glGenFramebuffers(1, &hdrFBO); glGenFramebuffers(1, &hdrFBO);
glBindFramebuffer(GL_FRAMEBUFFER, hdrFBO); glBindFramebuffer(GL_FRAMEBUFFER, hdrFBO);
// create 2 floating point color buffers (1 for normal rendering, other for brightness threshold values)
glGenTextures(2, colorBuffers); glGenTextures(2, colorBuffers);
for (unsigned int i = 0; i < 2; i++)
for (unsigned int i = 0; i < 2; i++) { {
glBindTexture(GL_TEXTURE_2D, colorBuffers[i]); glBindTexture(GL_TEXTURE_2D, colorBuffers[i]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, HDR_WIDTH, HDR_HEIGHT, 0, GL_RGBA, GL_FLOAT, NULL);
HDR_WIDTH, HDR_HEIGHT, 0, GL_RGBA, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); // we clamp to the edge as the blur filter would otherwise sample repeated texture values!
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// attach texture to framebuffer
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, colorBuffers[i], 0); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, colorBuffers[i], 0);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
} }
}
void initRBO(GLFWwindow* window) {
glfwGetWindowSize(window, &HDR_WIDTH, &HDR_HEIGHT);
glGenRenderbuffers(1, &rboDepth);
glBindRenderbuffer(GL_RENDERBUFFER, rboDepth);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, HDR_WIDTH, HDR_HEIGHT);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rboDepth);
// tell OpenGL which color attachments we'll use (of this framebuffer) for rendering
unsigned int attachments[2] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 }; unsigned int attachments[2] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 };
glDrawBuffers(2, attachments); glDrawBuffers(2, attachments);
// finally check if framebuffer is complete
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
std::cout << "Framebuffer not complete!" << std::endl;
glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0);
} }
void initDepthMap() { void blurPingPong(GLFWwindow* window) {
glGenFramebuffers(1, &depthMapFBO); glfwGetWindowSize(window, &HDR_WIDTH, &HDR_HEIGHT);
glGenTextures(1, &depthMap); glGenFramebuffers(2, pingpongFBO);
glBindTexture(GL_TEXTURE_2D, depthMap); glGenTextures(2, pingpongColorbuffers);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, for (unsigned int i = 0; i < 2; i++)
HDR_WIDTH, HDR_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL); {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindFramebuffer(GL_FRAMEBUFFER, pingpongFBO[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, pingpongColorbuffers[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, HDR_WIDTH, HDR_HEIGHT, 0, GL_RGBA, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthMap, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); // we clamp to the edge as the blur filter would otherwise sample repeated texture values!
glDrawBuffer(GL_NONE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glReadBuffer(GL_NONE); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, pingpongColorbuffers[i], 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0); // also check if framebuffers are complete (no need for depth buffer)
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
std::cout << "Framebuffer not complete!" << std::endl;
}
}
void shaderBloomConfig() {
glUseProgram(programBlur);
glUniform1i(glGetUniformLocation(programBloomFinal, "image"), 0);
glUseProgram(programBloomFinal);
glUniform1i(glGetUniformLocation(programBloomFinal, "scene"), 0);
glUniform1i(glGetUniformLocation(programBloomFinal, "bloomBlur"), 1);
} }
void drawPlanetTex(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint texture) { void drawPlanetTex(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint texture) {
@ -247,11 +312,42 @@ void drawSkyBox(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint text
glUseProgram(0); glUseProgram(0);
} }
void drawPlanetBlur() {
bool horizontal = true, first_iteration = true;
unsigned int amount = 10;
glUseProgram(programBlur);
glUniform1i(glGetUniformLocation(programBlur, "count"), blur_count);
for (unsigned int i = 0; i < amount; i++)
{
glBindFramebuffer(GL_FRAMEBUFFER, pingpongFBO[horizontal]);
glUniform1i(glGetUniformLocation(programBlur, "horizontal"), horizontal);
glBindTexture(GL_TEXTURE_2D, first_iteration ? colorBuffers[1] : pingpongColorbuffers[!horizontal]); // bind texture of other framebuffer (or scene if first iteration)
renderQuad();
horizontal = !horizontal;
if (first_iteration)
first_iteration = false;
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(programBloomFinal);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, colorBuffers[0]);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, pingpongColorbuffers[!horizontal]);
glUniform1i(glGetUniformLocation(programBloomFinal, "bloom"), bloom);
glUniform1f(glGetUniformLocation(programBloomFinal, "exposure"), bloom_exposure);
renderQuad();
}
void renderScene(GLFWwindow* window) { void renderScene(GLFWwindow* window) {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
float time = glfwGetTime(); float time = glfwGetTime();
glBindFramebuffer(GL_FRAMEBUFFER, hdrFBO);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//rysowanie planety //rysowanie planety
glm::mat4 planetScale = glm::scale(glm::vec3(planetSize)); glm::mat4 planetScale = glm::scale(glm::vec3(planetSize));
glm::mat4 planetRotate = glm::rotate(time * planetRot, glm::vec3(0, 1, 0)); glm::mat4 planetRotate = glm::rotate(time * planetRot, glm::vec3(0, 1, 0));
@ -276,11 +372,19 @@ void renderScene(GLFWwindow* window) {
drawSkyBox(cubeContext, skyBoxTranslate * skyBoxScale, skyBoxTex); drawSkyBox(cubeContext, skyBoxTranslate * skyBoxScale, skyBoxTex);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
drawPlanetBlur();
glfwSwapBuffers(window); glfwSwapBuffers(window);
} }
void framebuffer_size_callback(GLFWwindow* window, int width, int height) { void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
aspectRatio = float(width) / float(height); aspectRatio = float(width) / float(height);
initHDR(window);
initRBO(window);
blurPingPong(window);
glViewport(0, 0, width, height); glViewport(0, 0, width, height);
} }
@ -317,6 +421,13 @@ void key_callback(GLFWwindow* window, int key, int scancode, int action, int mod
//typ światła //typ światła
if (key == GLFW_KEY_P && action == GLFW_PRESS) if (key == GLFW_KEY_P && action == GLFW_PRESS)
lightingCheck = !lightingCheck; lightingCheck = !lightingCheck;
if (key == GLFW_KEY_M && action == GLFW_PRESS) {
blur_count++;
if (blur_count > 5)
blur_count = 1;
}
} }
//obsluga wejscia //obsluga wejscia
@ -410,12 +521,11 @@ void processInput(GLFWwindow* window)
void init(GLFWwindow* window) { void init(GLFWwindow* window) {
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
initHDR(window);
initRBO(window);
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
//glDisable(GL_DEPTH_TEST); //glDisable(GL_DEPTH_TEST);
//initDepthMap();
//initHDR();
glfwSetKeyCallback(window, key_callback); glfwSetKeyCallback(window, key_callback);
@ -423,6 +533,8 @@ void init(GLFWwindow* window) {
programPbr = shaderLoader.CreateProgram("shaders/shader_pbr.vert", "shaders/shader_pbr.frag"); programPbr = shaderLoader.CreateProgram("shaders/shader_pbr.vert", "shaders/shader_pbr.frag");
programSun = shaderLoader.CreateProgram("shaders/shader_sun.vert", "shaders/shader_sun.frag"); programSun = shaderLoader.CreateProgram("shaders/shader_sun.vert", "shaders/shader_sun.frag");
programSkyBox = shaderLoader.CreateProgram("shaders/shader_skybox.vert", "shaders/shader_skybox.frag"); programSkyBox = shaderLoader.CreateProgram("shaders/shader_skybox.vert", "shaders/shader_skybox.frag");
programBlur = shaderLoader.CreateProgram("shaders/shaderBlur.vert", "shaders/shaderBlur.frag");
programBloomFinal = shaderLoader.CreateProgram("shaders/shader_bloom_final.vert", "shaders/shader_bloom_final.frag");
loadModelToContext("./models/sphere.obj", sphereContext); loadModelToContext("./models/sphere.obj", sphereContext);
loadModelToContext("./models/cube.obj", cubeContext); loadModelToContext("./models/cube.obj", cubeContext);
@ -431,6 +543,10 @@ void init(GLFWwindow* window) {
sunTex = Core::LoadTexture(sunTexPaths[std::abs(sunTexIndex % 5)]); sunTex = Core::LoadTexture(sunTexPaths[std::abs(sunTexIndex % 5)]);
skyBoxTex = Core::LoadSkyBox(skyBoxPaths); skyBoxTex = Core::LoadSkyBox(skyBoxPaths);
blurPingPong(window);
shaderBloomConfig();
} }
void shutdown(GLFWwindow* window) { void shutdown(GLFWwindow* window) {