Code and content re-work: advanced OpenGL and advanced lighting.

This commit is contained in:
Joey de Vries
2017-06-01 22:04:34 +02:00
parent 49d8d895ae
commit f844f7c541
32 changed files with 184 additions and 144 deletions

View File

@@ -120,7 +120,7 @@ int main()
glBindBuffer(GL_ARRAY_BUFFER, buffer); glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, amount * sizeof(glm::mat4), &modelMatrices[0], GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, amount * sizeof(glm::mat4), &modelMatrices[0], GL_STATIC_DRAW);
// Set transformation matrices as an instance vertex attribute (with divisor 1) // set transformation matrices as an instance vertex attribute (with divisor 1)
// note: we're cheating a little by taking the, now publicly declared, VAO of the model's mesh(es) and adding new vertexAttribPointers // note: we're cheating a little by taking the, now publicly declared, VAO of the model's mesh(es) and adding new vertexAttribPointers
// normally you'd want to do this in a more organized fashion, but for learning purposes this will do. // normally you'd want to do this in a more organized fashion, but for learning purposes this will do.
// ----------------------------------------------------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------------------------------------------------
@@ -128,7 +128,7 @@ int main()
{ {
unsigned int VAO = rock.meshes[i].VAO; unsigned int VAO = rock.meshes[i].VAO;
glBindVertexArray(VAO); glBindVertexArray(VAO);
// Set attribute pointers for matrix (4 times vec4) // set attribute pointers for matrix (4 times vec4)
glEnableVertexAttribArray(3); glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (void*)0); glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (void*)0);
glEnableVertexAttribArray(4); glEnableVertexAttribArray(4);

View File

@@ -7,5 +7,5 @@ uniform mat4 projection;
void main() void main()
{ {
gl_Position = projection * view * model * vec4(aPos, 1.0f); gl_Position = projection * view * model * vec4(aPos, 1.0);
} }

View File

@@ -72,8 +72,8 @@ int main()
// build and compile shaders // build and compile shaders
// ------------------------- // -------------------------
Shader shader("11.1.anti_aliasing.vs", "11.1.anti_aliasing.fs"); Shader shader("11.anti_aliasing.vs", "11.anti_aliasing.fs");
Shader screenShader("11.2.aa_post.vs", "11.2.aa_post.fs"); Shader screenShader("11.aa_post.vs", "11.aa_post.fs");
// set up vertex data (and buffer(s)) and configure vertex attributes // set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------ // ------------------------------------------------------------------

View File

@@ -11,16 +11,16 @@ out vec3 fColor;
void build_house(vec4 position) void build_house(vec4 position)
{ {
fColor = gs_in[0].color; // gs_in[0] since there's only one input vertex fColor = gs_in[0].color; // gs_in[0] since there's only one input vertex
gl_Position = position + vec4(-0.2f, -0.2f, 0.0f, 0.0f); // 1:bottom-left gl_Position = position + vec4(-0.2, -0.2, 0.0, 0.0); // 1:bottom-left
EmitVertex(); EmitVertex();
gl_Position = position + vec4( 0.2f, -0.2f, 0.0f, 0.0f); // 2:bottom-right gl_Position = position + vec4( 0.2, -0.2, 0.0, 0.0); // 2:bottom-right
EmitVertex(); EmitVertex();
gl_Position = position + vec4(-0.2f, 0.2f, 0.0f, 0.0f); // 3:top-left gl_Position = position + vec4(-0.2, 0.2, 0.0, 0.0); // 3:top-left
EmitVertex(); EmitVertex();
gl_Position = position + vec4( 0.2f, 0.2f, 0.0f, 0.0f); // 4:top-right gl_Position = position + vec4( 0.2, 0.2, 0.0, 0.0); // 4:top-right
EmitVertex(); EmitVertex();
gl_Position = position + vec4( 0.0f, 0.4f, 0.0f, 0.0f); // 5:top gl_Position = position + vec4( 0.0, 0.4, 0.0, 0.0); // 5:top
fColor = vec3(1.0f, 1.0f, 1.0f); fColor = vec3(1.0, 1.0, 1.0);
EmitVertex(); EmitVertex();
EndPrimitive(); EndPrimitive();
} }

View File

@@ -12,9 +12,9 @@ uniform float time;
vec4 explode(vec4 position, vec3 normal) vec4 explode(vec4 position, vec3 normal)
{ {
float magnitude = 2.0f; float magnitude = 2.0;
vec3 direction = normal * ((sin(time) + 1.0f) / 2.0f) * magnitude; vec3 direction = normal * ((sin(time) + 1.0) / 2.0) * magnitude;
return position + vec4(direction, 0.0f); return position + vec4(direction, 0.0);
} }
vec3 GetNormal() vec3 GetNormal()

View File

@@ -11,5 +11,5 @@ uniform mat4 model;
void main() void main()
{ {
TexCoords = aTexCoords; TexCoords = aTexCoords;
gl_Position = projection * view * model * vec4(aPos, 1.0f); gl_Position = projection * view * model * vec4(aPos, 1.0);
} }

View File

@@ -19,7 +19,7 @@ void GenerateLine(int index)
void main() void main()
{ {
GenerateLine(0); // First vertex normal GenerateLine(0); // first vertex normal
GenerateLine(1); // Second vertex normal GenerateLine(1); // second vertex normal
GenerateLine(2); // Third vertex normal GenerateLine(2); // third vertex normal
} }

View File

@@ -17,6 +17,6 @@ float LinearizeDepth(float depth)
void main() void main()
{ {
float depthValue = texture(depthMap, TexCoords).r; float depthValue = texture(depthMap, TexCoords).r;
// color = vec4(vec3(LinearizeDepth(depthValue) / far_plane), 1.0); // perspective // FragColor = vec4(vec3(LinearizeDepth(depthValue) / far_plane), 1.0); // perspective
FragColor = vec4(vec3(depthValue), 1.0); // orthographic FragColor = vec4(vec3(depthValue), 1.0); // orthographic
} }

View File

@@ -1,78 +0,0 @@
#version 330 core
out vec4 FragColor;
in VS_OUT {
vec3 FragPos;
vec3 Normal;
vec2 TexCoords;
vec4 FragPosLightSpace;
} fs_in;
uniform sampler2D diffuseTexture;
uniform sampler2D shadowMap;
uniform vec3 lightPos;
uniform vec3 viewPos;
uniform bool shadows;
float ShadowCalculation(vec4 fragPosLightSpace)
{
// perform perspective divide
vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
// Transform to [0,1] range
projCoords = projCoords * 0.5 + 0.5;
// Get closest depth value from light's perspective (using [0,1] range fragPosLight as coords)
float closestDepth = texture(shadowMap, projCoords.xy).r;
// Get depth of current fragment from light's perspective
float currentDepth = projCoords.z;
// Calculate bias (based on depth map resolution and slope)
vec3 normal = normalize(fs_in.Normal);
vec3 lightDir = normalize(lightPos - fs_in.FragPos);
float bias = max(0.05 * (1.0 - dot(normal, lightDir)), 0.005);
// Check whether current frag pos is in shadow
// float shadow = currentDepth - bias > closestDepth ? 1.0 : 0.0;
// PCF
float shadow = 0.0;
vec2 texelSize = 1.0 / textureSize(shadowMap, 0);
for(int x = -1; x <= 1; ++x)
{
for(int y = -1; y <= 1; ++y)
{
float pcfDepth = texture(shadowMap, projCoords.xy + vec2(x, y) * texelSize).r;
shadow += currentDepth - bias > pcfDepth ? 1.0 : 0.0;
}
}
shadow /= 9.0;
// Keep the shadow at 0.0 when outside the far_plane region of the light's frustum.
if(projCoords.z > 1.0)
shadow = 0.0;
return shadow;
}
void main()
{
vec3 color = texture(diffuseTexture, fs_in.TexCoords).rgb;
vec3 normal = normalize(fs_in.Normal);
vec3 lightColor = vec3(0.3);
// Ambient
vec3 ambient = 0.3 * color;
// Diffuse
vec3 lightDir = normalize(lightPos - fs_in.FragPos);
float diff = max(dot(lightDir, normal), 0.0);
vec3 diffuse = diff * lightColor;
// Specular
vec3 viewDir = normalize(viewPos - fs_in.FragPos);
vec3 reflectDir = reflect(-lightDir, normal);
float spec = 0.0;
vec3 halfwayDir = normalize(lightDir + viewDir);
spec = pow(max(dot(normal, halfwayDir), 0.0), 64.0);
vec3 specular = spec * lightColor;
// Calculate shadow
float shadow = shadows ? ShadowCalculation(fs_in.FragPosLightSpace) : 0.0;
vec3 lighting = (ambient + (1.0 - shadow) * (diffuse + specular)) * color;
FragColor = vec4(lighting, 1.0f);
}

View File

@@ -1,27 +0,0 @@
#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec2 texCoords;
out vec2 TexCoords;
out VS_OUT {
vec3 FragPos;
vec3 Normal;
vec2 TexCoords;
vec4 FragPosLightSpace;
} vs_out;
uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;
uniform mat4 lightSpaceMatrix;
void main()
{
gl_Position = projection * view * model * vec4(position, 1.0f);
vs_out.FragPos = vec3(model * vec4(position, 1.0));
vs_out.Normal = transpose(inverse(mat3(model))) * normal;
vs_out.TexCoords = texCoords;
vs_out.FragPosLightSpace = lightSpaceMatrix * vec4(vs_out.FragPos, 1.0);
}

View File

@@ -6,5 +6,5 @@ uniform mat4 model;
void main() void main()
{ {
gl_Position = lightSpaceMatrix * model * vec4(aPos, 1.0f); gl_Position = lightSpaceMatrix * model * vec4(aPos, 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,22 @@
#version 330 core
out vec4 FragColor;
in vec2 TexCoords;
uniform sampler2D depthMap;
uniform float near_plane;
uniform float far_plane;
// required when using a perspective projection matrix
float LinearizeDepth(float depth)
{
float z = depth * 2.0 - 1.0; // Back to NDC
return (2.0 * near_plane * far_plane) / (far_plane + near_plane - z * (far_plane - near_plane));
}
void main()
{
float depthValue = texture(depthMap, TexCoords).r;
// FragColor = vec4(vec3(LinearizeDepth(depthValue) / far_plane), 1.0); // perspective
FragColor = vec4(vec3(depthValue), 1.0); // orthographic
}

View File

@@ -18,13 +18,13 @@ float ShadowCalculation(vec4 fragPosLightSpace)
{ {
// perform perspective divide // perform perspective divide
vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w; vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
// Transform to [0,1] range // transform to [0,1] range
projCoords = projCoords * 0.5 + 0.5; projCoords = projCoords * 0.5 + 0.5;
// Get closest depth value from light's perspective (using [0,1] range fragPosLight as coords) // get closest depth value from light's perspective (using [0,1] range fragPosLight as coords)
float closestDepth = texture(shadowMap, projCoords.xy).r; float closestDepth = texture(shadowMap, projCoords.xy).r;
// Get depth of current fragment from light's perspective // get depth of current fragment from light's perspective
float currentDepth = projCoords.z; float currentDepth = projCoords.z;
// Check whether current frag pos is in shadow // check whether current frag pos is in shadow
float shadow = currentDepth > closestDepth ? 1.0 : 0.0; float shadow = currentDepth > closestDepth ? 1.0 : 0.0;
return shadow; return shadow;

View File

@@ -23,5 +23,5 @@ void main()
vs_out.Normal = transpose(inverse(mat3(model))) * aNormal; vs_out.Normal = transpose(inverse(mat3(model))) * aNormal;
vs_out.TexCoords = aTexCoords; vs_out.TexCoords = aTexCoords;
vs_out.FragPosLightSpace = lightSpaceMatrix * vec4(vs_out.FragPos, 1.0); vs_out.FragPosLightSpace = lightSpaceMatrix * vec4(vs_out.FragPos, 1.0);
gl_Position = projection * view * model * vec4(aPos, 1.0f); gl_Position = projection * view * model * vec4(aPos, 1.0);
} }

View File

@@ -0,0 +1,6 @@
#version 330 core
void main()
{
// gl_FragDepth = gl_FragCoord.z;
}

View File

@@ -0,0 +1,10 @@
#version 330 core
layout (location = 0) in vec3 aPos;
uniform mat4 lightSpaceMatrix;
uniform mat4 model;
void main()
{
gl_Position = lightSpaceMatrix * model * vec4(aPos, 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,22 @@
#version 330 core
out vec4 FragColor;
in vec2 TexCoords;
uniform sampler2D depthMap;
uniform float near_plane;
uniform float far_plane;
// required when using a perspective projection matrix
float LinearizeDepth(float depth)
{
float z = depth * 2.0 - 1.0; // Back to NDC
return (2.0 * near_plane * far_plane) / (far_plane + near_plane - z * (far_plane - near_plane));
}
void main()
{
float depthValue = texture(depthMap, TexCoords).r;
// FragColor = vec4(vec3(LinearizeDepth(depthValue) / far_plane), 1.0); // perspective
FragColor = vec4(vec3(depthValue), 1.0); // orthographic
}

View File

@@ -23,5 +23,5 @@ void main()
vs_out.Normal = transpose(inverse(mat3(model))) * aNormal; vs_out.Normal = transpose(inverse(mat3(model))) * aNormal;
vs_out.TexCoords = aTexCoords; vs_out.TexCoords = aTexCoords;
vs_out.FragPosLightSpace = lightSpaceMatrix * vec4(vs_out.FragPos, 1.0); vs_out.FragPosLightSpace = lightSpaceMatrix * vec4(vs_out.FragPos, 1.0);
gl_Position = projection * view * model * vec4(aPos, 1.0f); gl_Position = projection * view * model * vec4(aPos, 1.0);
} }

View File

@@ -0,0 +1,6 @@
#version 330 core
void main()
{
// gl_FragDepth = gl_FragCoord.z;
}

View File

@@ -0,0 +1,10 @@
#version 330 core
layout (location = 0) in vec3 aPos;
uniform mat4 lightSpaceMatrix;
uniform mat4 model;
void main()
{
gl_Position = lightSpaceMatrix * model * vec4(aPos, 1.0);
}

View File

@@ -40,22 +40,22 @@ void main()
vec3 color = texture(diffuseTexture, fs_in.TexCoords).rgb; vec3 color = texture(diffuseTexture, fs_in.TexCoords).rgb;
vec3 normal = normalize(fs_in.Normal); vec3 normal = normalize(fs_in.Normal);
vec3 lightColor = vec3(0.3); vec3 lightColor = vec3(0.3);
// Ambient // ambient
vec3 ambient = 0.3 * color; vec3 ambient = 0.3 * color;
// Diffuse // diffuse
vec3 lightDir = normalize(lightPos - fs_in.FragPos); vec3 lightDir = normalize(lightPos - fs_in.FragPos);
float diff = max(dot(lightDir, normal), 0.0); float diff = max(dot(lightDir, normal), 0.0);
vec3 diffuse = diff * lightColor; vec3 diffuse = diff * lightColor;
// Specular // specular
vec3 viewDir = normalize(viewPos - fs_in.FragPos); vec3 viewDir = normalize(viewPos - fs_in.FragPos);
vec3 reflectDir = reflect(-lightDir, normal); vec3 reflectDir = reflect(-lightDir, normal);
float spec = 0.0; float spec = 0.0;
vec3 halfwayDir = normalize(lightDir + viewDir); vec3 halfwayDir = normalize(lightDir + viewDir);
spec = pow(max(dot(normal, halfwayDir), 0.0), 64.0); spec = pow(max(dot(normal, halfwayDir), 0.0), 64.0);
vec3 specular = spec * lightColor; vec3 specular = spec * lightColor;
// Calculate shadow // calculate shadow
float shadow = shadows ? ShadowCalculation(fs_in.FragPos) : 0.0; float shadow = shadows ? ShadowCalculation(fs_in.FragPos) : 0.0;
vec3 lighting = (ambient + (1.0 - shadow) * (diffuse + specular)) * color; vec3 lighting = (ambient + (1.0 - shadow) * (diffuse + specular)) * color;
FragColor = vec4(lighting, 1.0f); FragColor = vec4(lighting, 1.0);
} }

View File

@@ -101,5 +101,5 @@ void main()
float shadow = shadows ? ShadowCalculation(fs_in.FragPos) : 0.0; float shadow = shadows ? ShadowCalculation(fs_in.FragPos) : 0.0;
vec3 lighting = (ambient + (1.0 - shadow) * (diffuse + specular)) * color; vec3 lighting = (ambient + (1.0 - shadow) * (diffuse + specular)) * color;
FragColor = vec4(lighting, 1.0f); FragColor = vec4(lighting, 1.0);
} }

View File

@@ -25,5 +25,5 @@ void main()
else else
vs_out.Normal = transpose(inverse(mat3(model))) * aNormal; vs_out.Normal = transpose(inverse(mat3(model))) * aNormal;
vs_out.TexCoords = aTexCoords; vs_out.TexCoords = aTexCoords;
gl_Position = projection * view * model * vec4(aPos, 1.0f); gl_Position = projection * view * model * vec4(aPos, 1.0);
} }

View File

@@ -0,0 +1,16 @@
#version 330 core
in vec4 FragPos;
uniform vec3 lightPos;
uniform float far_plane;
void main()
{
float lightDistance = length(FragPos.xyz - lightPos);
// map to [0;1] range by dividing by far_plane
lightDistance = lightDistance / far_plane;
// write this as modified depth
gl_FragDepth = lightDistance;
}

View File

@@ -0,0 +1,22 @@
#version 330 core
layout (triangles) in;
layout (triangle_strip, max_vertices=18) out;
uniform mat4 shadowMatrices[6];
out vec4 FragPos; // FragPos from GS (output per emitvertex)
void main()
{
for(int face = 0; face < 6; ++face)
{
gl_Layer = face; // built-in variable that specifies to which face we render.
for(int i = 0; i < 3; ++i) // for each triangle's vertices
{
FragPos = gl_in[i].gl_Position;
gl_Position = shadowMatrices[face] * FragPos;
EmitVertex();
}
EndPrimitive();
}
}

View File

@@ -0,0 +1,9 @@
#version 330 core
layout (location = 0) in vec3 aPos;
uniform mat4 model;
void main()
{
gl_Position = model * vec4(aPos, 1.0);
}

View File

@@ -37,5 +37,5 @@ void main()
float spec = pow(max(dot(normal, halfwayDir), 0.0), 32.0); float spec = pow(max(dot(normal, halfwayDir), 0.0), 32.0);
vec3 specular = vec3(0.2) * spec; vec3 specular = vec3(0.2) * spec;
FragColor = vec4(ambient + diffuse + specular, 1.0f); FragColor = vec4(ambient + diffuse + specular, 1.0);
} }