This commit is contained in:
Natalia Nowakowska 2024-02-04 22:00:38 +01:00
parent 6423dfc220
commit 816d346cb7
6 changed files with 230 additions and 34 deletions

View File

@ -0,0 +1,32 @@
#version 330 core
out vec4 FragColor;
in vec2 TexCoords;
uniform sampler2D image;
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 < 5; ++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 = 2.6;
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;
@ -198,11 +201,20 @@ 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);
vec4 noiseColor = noiseColor() * min(1, AMBIENT + cloudLightness * diffuse) * vec4(lightColor, 0.0) / cloudLight; vec4 noiseColor = noiseColor() * min(1, AMBIENT + cloudLightness * diffuse) * vec4(lightColor, 0.0) / cloudLight;
vec3 mixedColor = mix(pbrColor.rgb, noiseColor.rgb, noiseColor.r); vec3 mixedColor = mix(pbrColor.rgb, noiseColor.rgb, noiseColor.r);
float brightness = dot(mixedColor, vec3(0.2126, 0.7152, 0.0722));
if(brightness > 1.0)
BrightColor = vec4(mixedColor, 1.0);
else
BrightColor = vec4(0.0, 0.0, 0.0, 1.0);
outColor = vec4(mixedColor, 1.0); outColor = vec4(mixedColor, 1.0);
} }

View File

@ -13,11 +13,16 @@
#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 programBloom;
GLuint programBloomFinal;
GLuint programBlur;
Core::Shader_Loader shaderLoader; Core::Shader_Loader shaderLoader;
@ -65,12 +70,18 @@ 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 exposure = 1.5f;
float lightPower = 8.f; float lightPower = 8.f;
glm::vec3 lightColor = glm::vec3(lightPower, lightPower, lightPower); glm::vec3 lightColor = glm::vec3(lightPower, lightPower, lightPower);
@ -117,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) {
glfwGetFramebufferSize(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) {
glfwGetFramebufferSize(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() {
glGenFramebuffers(1, &depthMapFBO);
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) {
@ -248,11 +312,41 @@ 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);
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"), 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,6 +370,10 @@ void renderScene(GLFWwindow* window) {
drawSkyBox(cubeContext, skyBoxTranslate * skyBoxScale, skyBoxTex); drawSkyBox(cubeContext, skyBoxTranslate * skyBoxScale, skyBoxTex);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
drawPlanetBlur();
glfwSwapBuffers(window); glfwSwapBuffers(window);
} }
@ -317,6 +415,9 @@ 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_B && action == GLFW_PRESS)
bloom = !bloom;
} }
//obsluga wejscia //obsluga wejscia
@ -390,8 +491,6 @@ void init(GLFWwindow* 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);
@ -399,6 +498,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);
@ -407,6 +508,12 @@ 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);
initHDR(window);
initRBO(window);
blurPingPong();
shaderBloomConfig();
} }
void shutdown(GLFWwindow* window) { void shutdown(GLFWwindow* window) {