merge bloom
This commit is contained in:
commit
65081de1d9
33
grk/cw 6/shaders/shaderBlur.frag
Normal file
33
grk/cw 6/shaders/shaderBlur.frag
Normal 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);
|
||||
}
|
11
grk/cw 6/shaders/shaderBlur.vert
Normal file
11
grk/cw 6/shaders/shaderBlur.vert
Normal 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);
|
||||
}
|
23
grk/cw 6/shaders/shader_bloom_final.frag
Normal file
23
grk/cw 6/shaders/shader_bloom_final.frag
Normal 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);
|
||||
}
|
11
grk/cw 6/shaders/shader_bloom_final.vert
Normal file
11
grk/cw 6/shaders/shader_bloom_final.vert
Normal 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);
|
||||
}
|
@ -1,5 +1,8 @@
|
||||
#version 430 core
|
||||
|
||||
layout (location = 0) out vec4 outColor;
|
||||
layout (location = 1) out vec4 BrightColor;
|
||||
|
||||
float AMBIENT = 0.05;
|
||||
float PI = 3.14159;
|
||||
|
||||
@ -29,7 +32,7 @@ in vec3 vecNormal;
|
||||
in vec3 worldPos;
|
||||
in vec2 vtc;
|
||||
|
||||
out vec4 outColor;
|
||||
//out vec4 outColor;
|
||||
|
||||
in vec3 viewDirTS;
|
||||
in vec3 lightDirTS;
|
||||
@ -200,6 +203,8 @@ void main()
|
||||
//sun
|
||||
illumination = illumination + PBRLight(sunDir, sunColor, normal, viewDir, toneMappedColor);
|
||||
|
||||
|
||||
|
||||
vec3 pbrColor = vec3(1.0) - exp(-illumination * exposition);
|
||||
|
||||
vec3 finalColor = pbrColor;
|
||||
@ -210,5 +215,13 @@ void main()
|
||||
finalColor = mix(pbrColor, noiseColor, noiseColor.r);
|
||||
}
|
||||
|
||||
|
||||
|
||||
float brightness = dot(finalColor, vec3(0.2126, 0.7152, 0.0722));
|
||||
if(brightness > 1.5)
|
||||
BrightColor = vec4(finalColor, 1.0);
|
||||
else
|
||||
BrightColor = vec4(0.5, 0.5, 0.5, 1.0);
|
||||
|
||||
outColor = vec4(finalColor, 1.0);
|
||||
}
|
||||
|
@ -13,11 +13,15 @@
|
||||
#include <assimp/scene.h>
|
||||
#include <assimp/postprocess.h>
|
||||
#include <string>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
GLuint programTex;
|
||||
GLuint programPbr;
|
||||
GLuint programSun;
|
||||
GLuint programSkyBox;
|
||||
GLuint programBloomFinal;
|
||||
GLuint programBlur;
|
||||
|
||||
Core::Shader_Loader shaderLoader;
|
||||
|
||||
@ -65,11 +69,19 @@ float aspectRatio = 1.77f;
|
||||
|
||||
unsigned int hdrFBO;
|
||||
unsigned int colorBuffers[2];
|
||||
unsigned int depthMap;
|
||||
unsigned int depthMapFBO;
|
||||
|
||||
int HDR_WIDTH = 1024;
|
||||
int HDR_HEIGHT = 1024;
|
||||
unsigned int rboDepth;
|
||||
|
||||
|
||||
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;
|
||||
glm::vec3 lightColor = glm::vec3(lightPower, lightPower, lightPower);
|
||||
@ -116,44 +128,97 @@ glm::mat4 createPerspectiveMatrix() {
|
||||
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);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, hdrFBO);
|
||||
// create 2 floating point color buffers (1 for normal rendering, other for brightness threshold values)
|
||||
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]);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F,
|
||||
HDR_WIDTH, HDR_HEIGHT, 0, GL_RGBA, GL_FLOAT, NULL);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, 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_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);
|
||||
|
||||
// attach texture to framebuffer
|
||||
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 };
|
||||
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);
|
||||
}
|
||||
|
||||
void initDepthMap() {
|
||||
glGenFramebuffers(1, &depthMapFBO);
|
||||
glGenTextures(1, &depthMap);
|
||||
glBindTexture(GL_TEXTURE_2D, depthMap);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT,
|
||||
HDR_WIDTH, HDR_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO);
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthMap, 0);
|
||||
glDrawBuffer(GL_NONE);
|
||||
glReadBuffer(GL_NONE);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
void blurPingPong(GLFWwindow* window) {
|
||||
glfwGetWindowSize(window, &HDR_WIDTH, &HDR_HEIGHT);
|
||||
glGenFramebuffers(2, pingpongFBO);
|
||||
glGenTextures(2, pingpongColorbuffers);
|
||||
for (unsigned int i = 0; i < 2; i++)
|
||||
{
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, pingpongFBO[i]);
|
||||
glBindTexture(GL_TEXTURE_2D, pingpongColorbuffers[i]);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, 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_MAG_FILTER, GL_LINEAR);
|
||||
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);
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, pingpongColorbuffers[i], 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) {
|
||||
@ -246,11 +311,42 @@ void drawSkyBox(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint text
|
||||
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) {
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
float time = glfwGetTime();
|
||||
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, hdrFBO);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
//rysowanie planety
|
||||
glm::mat4 planetScale = glm::scale(glm::vec3(planetSize));
|
||||
glm::mat4 planetRotate = glm::rotate(time * planetRot, glm::vec3(0, 1, 0));
|
||||
@ -274,11 +370,19 @@ void renderScene(GLFWwindow* window) {
|
||||
|
||||
drawSkyBox(cubeContext, skyBoxTranslate * skyBoxScale, skyBoxTex);
|
||||
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
|
||||
drawPlanetBlur();
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
}
|
||||
|
||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
|
||||
aspectRatio = float(width) / float(height);
|
||||
|
||||
initHDR(window);
|
||||
initRBO(window);
|
||||
blurPingPong(window);
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
@ -315,6 +419,13 @@ void key_callback(GLFWwindow* window, int key, int scancode, int action, int mod
|
||||
//typ światła
|
||||
if (key == GLFW_KEY_P && action == GLFW_PRESS)
|
||||
lightingCheck = !lightingCheck;
|
||||
|
||||
if (key == GLFW_KEY_B && action == GLFW_PRESS) {
|
||||
blur_count++;
|
||||
if (blur_count > 5)
|
||||
blur_count = 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//obsluga wejscia
|
||||
@ -408,12 +519,11 @@ void processInput(GLFWwindow* window)
|
||||
|
||||
void init(GLFWwindow* window) {
|
||||
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||
|
||||
initHDR(window);
|
||||
initRBO(window);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
//glDisable(GL_DEPTH_TEST);
|
||||
|
||||
//initDepthMap();
|
||||
//initHDR();
|
||||
|
||||
glfwSetKeyCallback(window, key_callback);
|
||||
|
||||
@ -421,6 +531,8 @@ void init(GLFWwindow* window) {
|
||||
programPbr = shaderLoader.CreateProgram("shaders/shader_pbr.vert", "shaders/shader_pbr.frag");
|
||||
programSun = shaderLoader.CreateProgram("shaders/shader_sun.vert", "shaders/shader_sun.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/cube.obj", cubeContext);
|
||||
@ -429,6 +541,10 @@ void init(GLFWwindow* window) {
|
||||
sunTex = Core::LoadTexture(sunTexPaths[std::abs(sunTexIndex % 5)]);
|
||||
|
||||
skyBoxTex = Core::LoadSkyBox(skyBoxPaths);
|
||||
|
||||
blurPingPong(window);
|
||||
|
||||
shaderBloomConfig();
|
||||
}
|
||||
|
||||
void shutdown(GLFWwindow* window) {
|
||||
|
Loading…
Reference in New Issue
Block a user