poprawa geometrypass

This commit is contained in:
Generacja 2023-02-12 12:28:21 +01:00
parent 01bae3ab44
commit 87a7e2919f
7 changed files with 81 additions and 107 deletions

Binary file not shown.

Binary file not shown.

View File

@ -1,18 +1,32 @@
#version 430 core #version 330 core
layout (location = 0) out vec3 gPosition; out vec4 FragColor;
layout (location = 1) out vec3 gNormal;
layout (location = 2) out vec3 gAlbedo;
in vec2 TexCoords; in vec2 TexCoords;
in vec3 FragPos;
in vec3 Normal; uniform sampler2D image;
uniform bool horizontal;
uniform float weight[5] = float[] (0.227027, 0.1945946, 0.1216216, 0.054054, 0.016216);
void main() void main()
{ {
// store the fragment position vector in the first gbuffer texture vec2 tex_offset = 1.0 / textureSize(image, 0); // gets size of single texel
gPosition = FragPos; vec3 result = texture(image, TexCoords).rgb * weight[0]; // current fragment's contribution
// also store the per-fragment normals into the gbuffer if(horizontal)
gNormal = normalize(Normal); {
// and the diffuse per-fragment color for(int i = 1; i < 5; ++i)
gAlbedo.rgb = vec3(0.95); {
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

@ -1,27 +1,14 @@
#version 430 core #version 430 core
layout (location = 0) in vec3 aPos; layout(location = 0) in vec3 vertexPosition;
layout (location = 1) in vec3 aNormal; layout(location = 1) in vec3 vertexNormal;
layout (location = 2) in vec2 aTexCoords; layout(location = 2) in vec2 vertexTexCoord;
out vec3 FragPos;
out vec2 TexCoords; out vec2 TexCoords;
out vec3 Normal;
uniform bool invertedNormals;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main() void main()
{ {
vec4 viewPos = view * model * vec4(aPos, 1.0); gl_Position = vec4(vertexPosition, 1.0);
FragPos = viewPos.xyz; TexCoords = vertexTexCoord;
TexCoords = aTexCoords; }
mat3 normalMatrix = transpose(inverse(mat3(view * model)));
Normal = normalMatrix * (invertedNormals ? -aNormal : aNormal);
gl_Position = projection * viewPos;
}

View File

@ -1,48 +1,18 @@
#version 330 core #version 430
out vec4 FragColor; layout (location = 0) out vec3 gPosition;
layout (location = 1) out vec3 gNormal;
in vec2 tc; layout (location = 2) out vec3 gAlbedo;
uniform sampler2D color; in vec2 TexCoords;
uniform sampler2D highlight; in vec3 FragPos;
in vec3 Normal;
float rescale_z(float z){
float n = 0.05;
float f = 20.;
return (2*n*f/(z*(n-f)+n+f))/f;
}
float weight[5] = float[] (0.227027, 0.1945946, 0.1216216, 0.054054, 0.016216);
vec4 blur()
{
vec2 tex_offset = 1.0 / textureSize(color, 0); // gets size of single texel
vec3 result = texture(highlight, tc).rgb * weight[0]; // current fragment's contribution
bool horizontal = true;
if(horizontal)
{
for(int i = 1; i < 5; ++i)
{
result += texture(highlight, tc + vec2(tex_offset.x * i, 0.0)).rgb * weight[i];
result += texture(highlight, tc - vec2(tex_offset.x * i, 0.0)).rgb * weight[i];
}
}
else
{
for(int i = 1; i < 5; ++i)
{
result += texture(highlight, tc + vec2(0.0, tex_offset.y * i)).rgb * weight[i];
result += texture(highlight, tc - vec2(0.0, tex_offset.y * i)).rgb * weight[i];
}
}
return vec4(result, 1.0);
}
void main() void main()
{ {
//float depthValue = texture(depthMap, tc).r; // store the fragment position vector in the first gbuffer texture
//FragColor = vec4(vec3(rescale_z(depthValue)+0.5), 1.0); gPosition = FragPos;
//FragColor = vec4(vec3((depthValue)+0.5), 1.0); // also store the per-fragment normals into the gbuffer
gNormal = normalize(Normal);
FragColor = vec4(texture(highlight, tc).rgb+texture(color, tc).rgb,1); // and the diffuse per-fragment color
//FragColor = vec4( (texture(color, tc)-0.98)*50); gAlbedo.rgb = vec3(0.95);
} }

View File

@ -1,14 +1,28 @@
#version 430 core #version 430 core
layout (location = 0) out vec3 vertexPosition;
layout (location = 1) out vec3 vertexNormal;
layout (location = 2) in vec2 vertexTexCoords;
layout(location = 0) in vec3 vertexPosition; out vec3 FragPos;
layout(location = 1) in vec3 vertexNormal; out vec2 TexCoords;
layout(location = 2) in vec2 vertexTexCoord; out vec3 Normal;
uniform bool invertedNormals;
out vec2 tc; uniform mat4 modelMatrix;
uniform mat3 transformation;
uniform mat4 view;
uniform mat4 projection;
void main() void main()
{ {
tc = vertexTexCoord; vec4 viewPos = transformation * vec4(vertexPosition, 1.0);
gl_Position = vec4(vertexPosition, 1.0); FragPos = viewPos.xyz;
} TexCoord = vertexTexCoords;
mat3 normalMatrix = transpose(inverse(mat3( * model)));
Normal = normalMatrix * (invertedNormals ? -aNormal : aNormal);
gl_Position = viewPos;
}

View File

@ -253,6 +253,9 @@ void drawSkyBox(Core::RenderContext& context, glm::mat4 modelMatrix) {
void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, glm::vec3 color, float roughness, float metallic) { void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, glm::vec3 color, float roughness, float metallic) {
geometryPass(modelMatrix);
glUseProgram(program);
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glUniform1i(glGetUniformLocation(program, "depthMap"), 0); glUniform1i(glGetUniformLocation(program, "depthMap"), 0);
glBindTexture(GL_TEXTURE_2D, depthMap); glBindTexture(GL_TEXTURE_2D, depthMap);
@ -265,6 +268,7 @@ void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, glm::vec
glm::mat4 viewProjectionMatrix = createPerspectiveMatrix() * createCameraMatrix(); glm::mat4 viewProjectionMatrix = createPerspectiveMatrix() * createCameraMatrix();
glm::mat4 transformation = viewProjectionMatrix * modelMatrix; glm::mat4 transformation = viewProjectionMatrix * modelMatrix;
glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, (float*)&transformation); glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, (float*)&transformation);
glUniformMatrix4fv(glGetUniformLocation(program, "modelMatrix"), 1, GL_FALSE, (float*)&modelMatrix); glUniformMatrix4fv(glGetUniformLocation(program, "modelMatrix"), 1, GL_FALSE, (float*)&modelMatrix);
@ -432,31 +436,16 @@ void initgBuffer()
glDrawBuffers(3, attachments); glDrawBuffers(3, attachments);
} }
void geometryPass() void geometryPass(glm::mat4 modelMatrix)
{ {
glBindFramebuffer(GL_FRAMEBUFFER, gBuffer); glBindFramebuffer(GL_FRAMEBUFFER, gBuffer);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)WIDTH / (float)HEIGHT, 0.1f, 50.0f);
glm::mat4 view = camera.GetViewMatrix();
glm::mat4 model = glm::mat4(1.0f);
glUseProgram(programGeometryPass); glUseProgram(programGeometryPass);
shaderGeometryPass.setMat4("projection", projection); glm::mat4 viewProjectionMatrix = createPerspectiveMatrix() * createCameraMatrix();
shaderGeometryPass.setMat4("view", view); glm::mat4 transformation = viewProjectionMatrix * modelMatrix;
// room cube glUniformMatrix4fv(glGetUniformLocation(programGeometryPass, "transformation"), 1, GL_FALSE, (float*)&transformation);
model = glm::mat4(1.0f); glUniformMatrix4fv(glGetUniformLocation(programGeometryPass, "modelMatrix"), 1, GL_FALSE, (float*)&modelMatrix);
model = glm::translate(model, glm::vec3(0.0, 7.0f, 0.0f)); glUniform1i(glGetUniformLocation(programGeometryPass, "invertedNormals"), 0);
model = glm::scale(model, glm::vec3(7.5f, 7.5f, 7.5f));
shaderGeometryPass.setMat4("model", model);
shaderGeometryPass.setInt("invertedNormals", 1); // invert normals as we're inside the cube
renderCube();
shaderGeometryPass.setInt("invertedNormals", 0);
// backpack model on the floor
model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(0.0f, 0.5f, 0.0));
model = glm::rotate(model, glm::radians(-90.0f), glm::vec3(1.0, 0.0, 0.0));
model = glm::scale(model, glm::vec3(1.0f));
shaderGeometryPass.setMat4("model", model);
backpack.Draw(shaderGeometryPass);
glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0);
} }