Compare commits

...

6 Commits

Author SHA1 Message Date
61ffd28002 da się odpalić 2024-01-27 20:39:27 +01:00
cfc84de744 połączenie "master" z "hdr_kacper" 2024-01-27 20:24:27 +01:00
5f15bd3c9d init 2024-01-27 20:16:19 +01:00
sasankasa
5bbff74f8c zmiany na cw 22-01-2024 2024-01-22 16:54:33 +01:00
Natalia Nowakowska
adec05f6f8 wip bloom 2024-01-22 12:38:19 +01:00
Natalia Nowakowska
5050374c1f update hdr 2024-01-17 22:25:02 +01:00
11 changed files with 185 additions and 61 deletions

View File

@ -36,12 +36,14 @@
<ClInclude Include="src\Texture.h" /> <ClInclude Include="src\Texture.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="shaders\shader_bloom.frag" />
<None Include="shaders\shader_bloom.vert" />
<None Include="shaders\shader_FBO.frag" />
<None Include="shaders\shader_FBO.vert" />
<None Include="shaders\shader_pbr.frag" /> <None Include="shaders\shader_pbr.frag" />
<None Include="shaders\shader_pbr.vert" /> <None Include="shaders\shader_pbr.vert" />
<None Include="shaders\shader_skybox.frag" /> <None Include="shaders\shader_skybox.frag" />
<None Include="shaders\shader_skybox.vert" /> <None Include="shaders\shader_skybox.vert" />
<None Include="shaders\shader_smap.frag" />
<None Include="shaders\shader_smap.vert" />
<None Include="shaders\shader_tex.frag" /> <None Include="shaders\shader_tex.frag" />
<None Include="shaders\shader_tex.vert" /> <None Include="shaders\shader_tex.vert" />
<None Include="shaders\shader_sun.frag" /> <None Include="shaders\shader_sun.frag" />

View File

@ -0,0 +1,22 @@
#version 330 core
out vec4 FragColor;
in vec2 texCoords;
uniform sampler2D screenTexture;
uniform sampler2D bloomTexture;
uniform float gamma;
void main()
{
vec3 fragment = texture(screenTexture, texCoords).rgb;
vec3 bloom = texture(bloomTexture, texCoords).rgb;
vec3 color = fragment + bloom;
float exposure = 0.8f;
vec3 toneMapped = vec3(1.0f) - exp(-color * exposure);
toneMapped = pow(toneMapped, vec3(1.0f / gamma));
FragColor = vec4(toneMapped, 1.0);
}

View File

@ -0,0 +1,12 @@
#version 430 core
layout (location = 0) in vec2 inPos;
layout (location = 1) in vec2 inTexCoords;
out vec2 texCoords;
void main()
{
gl_Position = vec4(inPos.x, inPos.y, 0.0, 1.0);
texCoords = inTexCoords;
}

View File

@ -0,0 +1,58 @@
#version 430 core
out vec4 FragColor;
in vec2 texCoords;
uniform sampler2D screenTexture;
uniform bool horizontal;
// How far from the center to take samples from the fragment you are currently on
const int radius = 6;
// Keep it between 1.0f and 2.0f (the higher this is the further the blur reaches)
float spreadBlur = 2.0f;
float weights[radius];
void main()
{
// Calculate the weights using the Gaussian equation
float x = 0.0f;
for (int i = 0; i < radius; i++)
{
// Decides the distance between each sample on the Gaussian function
if (spreadBlur <= 2.0f)
x += 3.0f / radius;
else
x += 6.0f / radius;
weights[i] = exp(-0.5f * pow(x / spreadBlur, 2.0f)) / (spreadBlur * sqrt(2 * 3.14159265f));
}
vec2 tex_offset = 1.0f / textureSize(screenTexture, 0);
vec3 result = texture(screenTexture, texCoords).rgb * weights[0];
// Calculate horizontal blur
if(horizontal)
{
for(int i = 1; i < radius; i++)
{
// Take into account pixels to the right
result += texture(screenTexture, texCoords + vec2(tex_offset.x * i, 0.0)).rgb * weights[i];
// Take into account pixels on the left
result += texture(screenTexture, texCoords - vec2(tex_offset.x * i, 0.0)).rgb * weights[i];
}
}
// Calculate vertical blur
else
{
for(int i = 1; i < radius; i++)
{
// Take into account pixels above
result += texture(screenTexture, texCoords + vec2(0.0, tex_offset.y * i)).rgb * weights[i];
// Take into account pixels below
result += texture(screenTexture, texCoords - vec2(0.0, tex_offset.y * i)).rgb * weights[i];
}
}
result = vec3(0, 1, 0);
FragColor = vec4(result, 1.0f);
}

View File

@ -1,6 +0,0 @@
#version 430 core
void main()
{
}

View File

@ -10,7 +10,8 @@ in vec3 vecNormal;
in vec3 worldPos; in vec3 worldPos;
in vec2 vtc; in vec2 vtc;
vec4 textureColor; vec4 textureColor;
out vec4 outColor; layout (location = 0) out vec4 outColor;
layout (location = 1) out vec4 bloomColor;
vec3 outputColor; vec3 outputColor;
uniform sampler2D colorTexture; uniform sampler2D colorTexture;
@ -37,4 +38,10 @@ void main()
outputColor = toneMapping(outputColor); outputColor = toneMapping(outputColor);
outColor = vec4(outputColor , 1.0); outColor = vec4(outputColor , 1.0);
float brightness = dot(outColor.rgb, vec3(0.2126f, 0.7152f, 0.0722f));
if(brightness > 0.15f)
bloomColor = vec4(outColor.rgb, 1.0f);
else
bloomColor = vec4(0.0f, 0.0f, 0.0f, 1.0f);
} }

View File

@ -1,12 +0,0 @@
#version 330 core
out vec4 FragColor;
in vec2 tc;
uniform sampler2D depthMap;
void main()
{
float depthValue = texture(depthMap, tc).r;
FragColor = vec4(vec3(depthValue+0.5), 1.0);
}

View File

@ -1,14 +0,0 @@
#version 430 core
layout(location = 0) in vec3 vertexPosition;
layout(location = 1) in vec3 vertexNormal;
layout(location = 2) in vec2 vertexTexCoord;
out vec2 tc;
void main()
{
tc = vertexTexCoord;
gl_Position = vec4(vertexPosition*0.9, 1.0);
}

View File

@ -57,11 +57,12 @@ namespace textureMaterials {
GLuint clouds; GLuint clouds;
} }
*/ */
GLuint programDepth;
GLuint programTex; GLuint programTex;
GLuint programPbr; GLuint programPbr;
GLuint programSun; GLuint programSun;
GLuint programSkyBox; GLuint programSkyBox;
GLuint programBlur;
GLuint programFramebuffer;
Core::Shader_Loader shaderLoader; Core::Shader_Loader shaderLoader;
@ -104,11 +105,12 @@ GLuint VAO, VBO;
float aspectRatio = 1.77f; float aspectRatio = 1.77f;
unsigned int hdrFBO; unsigned int hdrFBO, depthMap, depthMapFBO;
unsigned int colorBuffers[2]; unsigned int colorBuffers[2];
unsigned int depthMap; unsigned int blurFBO[2];
unsigned int depthMapFBO; unsigned int blurBuffer[2];
float gamma = 2.2f;
int HDR_WIDTH = 1024; int HDR_WIDTH = 1024;
int HDR_HEIGHT = 1024; int HDR_HEIGHT = 1024;
@ -176,20 +178,24 @@ void initHDR() {
glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0);
} }
void initDepthMap() { void initBlurFBO() {
glGenFramebuffers(1, &depthMapFBO);
glGenTextures(1, &depthMap); glGenFramebuffers(2, blurFBO);
glBindTexture(GL_TEXTURE_2D, depthMap); glGenTextures(2, blurBuffer);
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, blurFBO[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, blurBuffer[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexImage2D(
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); GL_TEXTURE_2D, 0, GL_RGBA16F, HDR_WIDTH, HDR_HEIGHT, 0, GL_RGBA, GL_FLOAT, NULL
glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO); );
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthMap, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glDrawBuffer(GL_NONE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glReadBuffer(GL_NONE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glFramebufferTexture2D(
GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, blurBuffer[i], 0);
}
glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0);
} }
@ -219,6 +225,47 @@ void drawSun(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint texture
glUseProgram(0); glUseProgram(0);
} }
void drawObjectBloom(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint texture, int amount, bool horizontal, bool first_iteration) {
glUseProgram(programBlur);
glUniform1i(glGetUniformLocation(programBlur, "screenTexture"), 0);
glm::mat4 viewProjectionMatrix = createPerspectiveMatrix() * createCameraMatrix();
glm::mat4 transformation = viewProjectionMatrix * modelMatrix;
glUniformMatrix4fv(glGetUniformLocation(programBlur, "transformation"), 1, GL_FALSE, (float*)&transformation);
glUniformMatrix4fv(glGetUniformLocation(programBlur, "modelMatrix"), 1, GL_FALSE, (float*)&modelMatrix);
for (unsigned int i = 0; i < amount; i++)
{
glBindFramebuffer(GL_FRAMEBUFFER, blurFBO[horizontal]);
glUniform1i(glGetUniformLocation(programBlur, "horizontal"), horizontal);
// In the first bounc we want to get the data from the bloomTexture
if (first_iteration)
{
glBindTexture(GL_TEXTURE_2D, colorBuffers[1]);
first_iteration = false;
}
// Move the data between the pingPong textures
else
{
glBindTexture(GL_TEXTURE_2D, blurBuffer[!horizontal]);
}
// Render the image
Core::DrawContext(context);
// Switch between vertical and horizontal blurring
horizontal = !horizontal;
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glUseProgram(programFramebuffer);
glUniform1i(glGetUniformLocation(programFramebuffer, "screenTexture"), 0);
glUniform1i(glGetUniformLocation(programFramebuffer, "bloomTexture"), 1);
glUniform1f(glGetUniformLocation(programFramebuffer, "gamma"), gamma);
glUseProgram(0);
}
void drawSkyBox(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint texture) { void drawSkyBox(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint texture) {
glUseProgram(programSkyBox); glUseProgram(programSkyBox);
Core::SetActiveSkyBox(texture, "skybox", programSkyBox, 0); Core::SetActiveSkyBox(texture, "skybox", programSkyBox, 0);
@ -256,9 +303,12 @@ void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint t
glUseProgram(0); glUseProgram(0);
} }
void renderScene(GLFWwindow* window) { void renderScene(GLFWwindow* window)
glClearColor(0.5f, 0.0f, 0.25f, 1.0f); {
//glBindFramebuffer(GL_FRAMEBUFFER, hdrFBO);
glClearColor(0.0f, 0.0f, 0.15f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
float time = glfwGetTime(); float time = glfwGetTime();
//rysowanie planety //rysowanie planety
@ -267,7 +317,7 @@ void renderScene(GLFWwindow* window) {
glm::mat4 planetTranslate = glm::translate(planetPos); glm::mat4 planetTranslate = glm::translate(planetPos);
//drawPlanet(sphereContext, planetTranslate * planetRotate * planetScale, planetTex); //drawPlanet(sphereContext, planetTranslate * planetRotate * planetScale, planetTex);
drawObjectPBR(sphereContext, planetTranslate * planetRotate * planetScale, planetTex, planetRough, planetMetal); //drawObjectPBR(sphereContext, planetTranslate * planetRotate * planetScale, planetTex, planetRough, planetMetal);
//rysowanie słońca //rysowanie słońca
glm::mat4 sunScale = glm::scale(glm::vec3(sunSize)); glm::mat4 sunScale = glm::scale(glm::vec3(sunSize));
@ -282,6 +332,9 @@ void renderScene(GLFWwindow* window) {
drawSkyBox(cubeContext, skyBoxTranslate * skyBoxScale, skyBoxTex); drawSkyBox(cubeContext, skyBoxTranslate * skyBoxScale, skyBoxTex);
glBindFramebuffer(hdrFBO, 0);
drawObjectBloom(sphereContext, planetTranslate * planetRotate * planetScale, planetTex, 10, true, true);
glfwSwapBuffers(window); glfwSwapBuffers(window);
} }
@ -308,10 +361,11 @@ void init(GLFWwindow* window) {
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
//glDisable(GL_DEPTH_TEST); //glDisable(GL_DEPTH_TEST);
//initDepthMap();
initHDR(); initHDR();
initBlurFBO();
programDepth = shaderLoader.CreateProgram("shaders/shader_smap.vert", "shaders/shader_smap.frag"); programBlur = shaderLoader.CreateProgram("shaders/shader_bloom.vert", "shaders/shader_bloom.frag");
programFramebuffer = shaderLoader.CreateProgram("shaders/shader_FBO.vert", "shaders/shader_FBO.frag");
programTex = shaderLoader.CreateProgram("shaders/shader_tex.vert", "shaders/shader_tex.frag"); programTex = shaderLoader.CreateProgram("shaders/shader_tex.vert", "shaders/shader_tex.frag");
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");
@ -327,7 +381,8 @@ void init(GLFWwindow* window) {
} }
void shutdown(GLFWwindow* window) { void shutdown(GLFWwindow* window) {
shaderLoader.DeleteProgram(programDepth); shaderLoader.DeleteProgram(programBlur);
shaderLoader.DeleteProgram(programFramebuffer);
shaderLoader.DeleteProgram(programTex); shaderLoader.DeleteProgram(programTex);
shaderLoader.DeleteProgram(programPbr); shaderLoader.DeleteProgram(programPbr);
shaderLoader.DeleteProgram(programSun); shaderLoader.DeleteProgram(programSun);

View File

@ -1,7 +1,7 @@
 
Microsoft Visual Studio Solution File, Format Version 12.00 Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17 # Visual Studio Version 16
VisualStudioVersion = 17.4.33213.308 VisualStudioVersion = 16.0.34407.143
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Planeta", "cw 6\grk-cw6.vcxproj", "{3952C396-B1C6-44CD-96DD-C1AC15D32978}" Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Planeta", "cw 6\grk-cw6.vcxproj", "{3952C396-B1C6-44CD-96DD-C1AC15D32978}"
EndProject EndProject