Fix PBR source code inconsistencies (thanks Andrei!). Also fix winding order sphere and update CMake min. version.

This commit is contained in:
Joey de Vries
2021-09-01 12:40:43 +02:00
parent 85ee9d136f
commit 946cbc9118
19 changed files with 106 additions and 106 deletions

View File

@@ -1,5 +1,5 @@
cmake_minimum_required (VERSION 2.8) cmake_minimum_required (VERSION 3.0)
cmake_policy(VERSION 2.8) cmake_policy(VERSION 3.0)
project (LearnOpenGL) project (LearnOpenGL)

View File

@@ -29,7 +29,7 @@ float DistributionGGX(vec3 N, vec3 H, float roughness)
float denom = (NdotH2 * (a2 - 1.0) + 1.0); float denom = (NdotH2 * (a2 - 1.0) + 1.0);
denom = PI * denom * denom; denom = PI * denom * denom;
return nom / max(denom, 0.0000001); // prevent divide by zero for roughness=0.0 and NdotH=1.0 return nom / denom;
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
float GeometrySchlickGGX(float NdotV, float roughness) float GeometrySchlickGGX(float NdotV, float roughness)
@@ -55,7 +55,7 @@ float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
vec3 fresnelSchlick(float cosTheta, vec3 F0) vec3 fresnelSchlick(float cosTheta, vec3 F0)
{ {
return F0 + (1.0 - F0) * pow(max(1.0 - cosTheta, 0.0), 5.0); return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void main() void main()
@@ -85,8 +85,8 @@ void main()
vec3 F = fresnelSchlick(clamp(dot(H, V), 0.0, 1.0), F0); vec3 F = fresnelSchlick(clamp(dot(H, V), 0.0, 1.0), F0);
vec3 nominator = NDF * G * F; vec3 nominator = NDF * G * F;
float denominator = 4 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0); float denominator = 4 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.0001; // + 0.0001 to prevent divide by zero
vec3 specular = nominator / max(denominator, 0.001); // prevent divide by zero for NdotV=0.0 or NdotL=0.0 vec3 specular = nominator / denominator;
// kS is equal to Fresnel // kS is equal to Fresnel
vec3 kS = F; vec3 kS = F;

View File

@@ -1,7 +1,7 @@
#version 330 core #version 330 core
layout (location = 0) in vec3 aPos; layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoords; layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec3 aNormal; layout (location = 2) in vec2 aTexCoords;
out vec2 TexCoords; out vec2 TexCoords;
out vec3 WorldPos; out vec3 WorldPos;

View File

@@ -260,9 +260,9 @@ void renderSphere()
const unsigned int X_SEGMENTS = 64; const unsigned int X_SEGMENTS = 64;
const unsigned int Y_SEGMENTS = 64; const unsigned int Y_SEGMENTS = 64;
const float PI = 3.14159265359; const float PI = 3.14159265359;
for (unsigned int y = 0; y <= Y_SEGMENTS; ++y)
{
for (unsigned int x = 0; x <= X_SEGMENTS; ++x) for (unsigned int x = 0; x <= X_SEGMENTS; ++x)
{
for (unsigned int y = 0; y <= Y_SEGMENTS; ++y)
{ {
float xSegment = (float)x / (float)X_SEGMENTS; float xSegment = (float)x / (float)X_SEGMENTS;
float ySegment = (float)y / (float)Y_SEGMENTS; float ySegment = (float)y / (float)Y_SEGMENTS;
@@ -305,30 +305,30 @@ void renderSphere()
data.push_back(positions[i].x); data.push_back(positions[i].x);
data.push_back(positions[i].y); data.push_back(positions[i].y);
data.push_back(positions[i].z); data.push_back(positions[i].z);
if (uv.size() > 0)
{
data.push_back(uv[i].x);
data.push_back(uv[i].y);
}
if (normals.size() > 0) if (normals.size() > 0)
{ {
data.push_back(normals[i].x); data.push_back(normals[i].x);
data.push_back(normals[i].y); data.push_back(normals[i].y);
data.push_back(normals[i].z); data.push_back(normals[i].z);
} }
if (uv.size() > 0)
{
data.push_back(uv[i].x);
data.push_back(uv[i].y);
}
} }
glBindVertexArray(sphereVAO); glBindVertexArray(sphereVAO);
glBindBuffer(GL_ARRAY_BUFFER, vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(float), &data[0], GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(float), &data[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);
float stride = (3 + 2 + 3) * sizeof(float); unsigned int stride = (3 + 2 + 3) * sizeof(float);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (void*)0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (void*)0);
glEnableVertexAttribArray(1); glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, stride, (void*)(3 * sizeof(float))); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, stride, (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(2); glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, stride, (void*)(5 * sizeof(float))); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, stride, (void*)(6 * sizeof(float)));
} }
glBindVertexArray(sphereVAO); glBindVertexArray(sphereVAO);

View File

@@ -77,7 +77,7 @@ float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
vec3 fresnelSchlick(float cosTheta, vec3 F0) vec3 fresnelSchlick(float cosTheta, vec3 F0)
{ {
return F0 + (1.0 - F0) * pow(max(1.0 - cosTheta, 0.0), 5.0); return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void main() void main()
@@ -112,7 +112,7 @@ void main()
vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0); vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0);
vec3 nominator = NDF * G * F; vec3 nominator = NDF * G * F;
float denominator = 4 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.001; // 0.001 to prevent divide by zero. float denominator = 4 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.0001; // + 0.0001 to prevent divide by zero
vec3 specular = nominator / denominator; vec3 specular = nominator / denominator;
// kS is equal to Fresnel // kS is equal to Fresnel

View File

@@ -1,7 +1,7 @@
#version 330 core #version 330 core
layout (location = 0) in vec3 aPos; layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoords; layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec3 aNormal; layout (location = 2) in vec2 aTexCoords;
out vec2 TexCoords; out vec2 TexCoords;
out vec3 WorldPos; out vec3 WorldPos;

View File

@@ -271,9 +271,9 @@ void renderSphere()
const unsigned int X_SEGMENTS = 64; const unsigned int X_SEGMENTS = 64;
const unsigned int Y_SEGMENTS = 64; const unsigned int Y_SEGMENTS = 64;
const float PI = 3.14159265359; const float PI = 3.14159265359;
for (unsigned int y = 0; y <= Y_SEGMENTS; ++y)
{
for (unsigned int x = 0; x <= X_SEGMENTS; ++x) for (unsigned int x = 0; x <= X_SEGMENTS; ++x)
{
for (unsigned int y = 0; y <= Y_SEGMENTS; ++y)
{ {
float xSegment = (float)x / (float)X_SEGMENTS; float xSegment = (float)x / (float)X_SEGMENTS;
float ySegment = (float)y / (float)Y_SEGMENTS; float ySegment = (float)y / (float)Y_SEGMENTS;
@@ -311,35 +311,35 @@ void renderSphere()
indexCount = indices.size(); indexCount = indices.size();
std::vector<float> data; std::vector<float> data;
for (std::size_t i = 0; i < positions.size(); ++i) for (unsigned int i = 0; i < positions.size(); ++i)
{ {
data.push_back(positions[i].x); data.push_back(positions[i].x);
data.push_back(positions[i].y); data.push_back(positions[i].y);
data.push_back(positions[i].z); data.push_back(positions[i].z);
if (uv.size() > 0)
{
data.push_back(uv[i].x);
data.push_back(uv[i].y);
}
if (normals.size() > 0) if (normals.size() > 0)
{ {
data.push_back(normals[i].x); data.push_back(normals[i].x);
data.push_back(normals[i].y); data.push_back(normals[i].y);
data.push_back(normals[i].z); data.push_back(normals[i].z);
} }
if (uv.size() > 0)
{
data.push_back(uv[i].x);
data.push_back(uv[i].y);
}
} }
glBindVertexArray(sphereVAO); glBindVertexArray(sphereVAO);
glBindBuffer(GL_ARRAY_BUFFER, vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(float), &data[0], GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(float), &data[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);
float stride = (3 + 2 + 3) * sizeof(float); unsigned int stride = (3 + 2 + 3) * sizeof(float);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (void*)0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (void*)0);
glEnableVertexAttribArray(1); glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, stride, (void*)(3 * sizeof(float))); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, stride, (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(2); glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, stride, (void*)(5 * sizeof(float))); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, stride, (void*)(6 * sizeof(float)));
} }
glBindVertexArray(sphereVAO); glBindVertexArray(sphereVAO);

View File

@@ -55,7 +55,7 @@ float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
vec3 fresnelSchlick(float cosTheta, vec3 F0) vec3 fresnelSchlick(float cosTheta, vec3 F0)
{ {
return F0 + (1.0 - F0) * pow(max(1.0 - cosTheta, 0.0), 5.0); return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void main() void main()
@@ -86,7 +86,7 @@ void main()
vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0); vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0);
vec3 nominator = NDF * G * F; vec3 nominator = NDF * G * F;
float denominator = 4 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.001; // 0.001 to prevent divide by zero. float denominator = 4 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.0001; // + 0.0001 to prevent divide by zero
vec3 specular = nominator / denominator; vec3 specular = nominator / denominator;
// kS is equal to Fresnel // kS is equal to Fresnel

View File

@@ -1,7 +1,7 @@
#version 330 core #version 330 core
layout (location = 0) in vec3 aPos; layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoords; layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec3 aNormal; layout (location = 2) in vec2 aTexCoords;
out vec2 TexCoords; out vec2 TexCoords;
out vec3 WorldPos; out vec3 WorldPos;

View File

@@ -376,9 +376,9 @@ void renderSphere()
const unsigned int X_SEGMENTS = 64; const unsigned int X_SEGMENTS = 64;
const unsigned int Y_SEGMENTS = 64; const unsigned int Y_SEGMENTS = 64;
const float PI = 3.14159265359; const float PI = 3.14159265359;
for (unsigned int y = 0; y <= Y_SEGMENTS; ++y)
{
for (unsigned int x = 0; x <= X_SEGMENTS; ++x) for (unsigned int x = 0; x <= X_SEGMENTS; ++x)
{
for (unsigned int y = 0; y <= Y_SEGMENTS; ++y)
{ {
float xSegment = (float)x / (float)X_SEGMENTS; float xSegment = (float)x / (float)X_SEGMENTS;
float ySegment = (float)y / (float)Y_SEGMENTS; float ySegment = (float)y / (float)Y_SEGMENTS;
@@ -421,30 +421,30 @@ void renderSphere()
data.push_back(positions[i].x); data.push_back(positions[i].x);
data.push_back(positions[i].y); data.push_back(positions[i].y);
data.push_back(positions[i].z); data.push_back(positions[i].z);
if (uv.size() > 0)
{
data.push_back(uv[i].x);
data.push_back(uv[i].y);
}
if (normals.size() > 0) if (normals.size() > 0)
{ {
data.push_back(normals[i].x); data.push_back(normals[i].x);
data.push_back(normals[i].y); data.push_back(normals[i].y);
data.push_back(normals[i].z); data.push_back(normals[i].z);
} }
if (uv.size() > 0)
{
data.push_back(uv[i].x);
data.push_back(uv[i].y);
}
} }
glBindVertexArray(sphereVAO); glBindVertexArray(sphereVAO);
glBindBuffer(GL_ARRAY_BUFFER, vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(float), &data[0], GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(float), &data[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);
float stride = (3 + 2 + 3) * sizeof(float); unsigned int stride = (3 + 2 + 3) * sizeof(float);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (void*)0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (void*)0);
glEnableVertexAttribArray(1); glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, stride, (void*)(3 * sizeof(float))); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, stride, (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(2); glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, stride, (void*)(5 * sizeof(float))); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, stride, (void*)(6 * sizeof(float)));
} }
glBindVertexArray(sphereVAO); glBindVertexArray(sphereVAO);

View File

@@ -58,7 +58,7 @@ float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
vec3 fresnelSchlick(float cosTheta, vec3 F0) vec3 fresnelSchlick(float cosTheta, vec3 F0)
{ {
return F0 + (1.0 - F0) * pow(max(1.0 - cosTheta, 0.0), 5.0); return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void main() void main()
@@ -89,7 +89,7 @@ void main()
vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0); vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0);
vec3 nominator = NDF * G * F; vec3 nominator = NDF * G * F;
float denominator = 4 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.001; // 0.001 to prevent divide by zero. float denominator = 4 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.0001; // + 0.0001 to prevent divide by zero
vec3 specular = nominator / denominator; vec3 specular = nominator / denominator;
// kS is equal to Fresnel // kS is equal to Fresnel

View File

@@ -1,7 +1,7 @@
#version 330 core #version 330 core
layout (location = 0) in vec3 aPos; layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoords; layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec3 aNormal; layout (location = 2) in vec2 aTexCoords;
out vec2 TexCoords; out vec2 TexCoords;
out vec3 WorldPos; out vec3 WorldPos;

View File

@@ -416,9 +416,9 @@ void renderSphere()
const unsigned int X_SEGMENTS = 64; const unsigned int X_SEGMENTS = 64;
const unsigned int Y_SEGMENTS = 64; const unsigned int Y_SEGMENTS = 64;
const float PI = 3.14159265359; const float PI = 3.14159265359;
for (unsigned int y = 0; y <= Y_SEGMENTS; ++y)
{
for (unsigned int x = 0; x <= X_SEGMENTS; ++x) for (unsigned int x = 0; x <= X_SEGMENTS; ++x)
{
for (unsigned int y = 0; y <= Y_SEGMENTS; ++y)
{ {
float xSegment = (float)x / (float)X_SEGMENTS; float xSegment = (float)x / (float)X_SEGMENTS;
float ySegment = (float)y / (float)Y_SEGMENTS; float ySegment = (float)y / (float)Y_SEGMENTS;
@@ -456,35 +456,35 @@ void renderSphere()
indexCount = indices.size(); indexCount = indices.size();
std::vector<float> data; std::vector<float> data;
for (std::size_t i = 0; i < positions.size(); ++i) for (unsigned int i = 0; i < positions.size(); ++i)
{ {
data.push_back(positions[i].x); data.push_back(positions[i].x);
data.push_back(positions[i].y); data.push_back(positions[i].y);
data.push_back(positions[i].z); data.push_back(positions[i].z);
if (uv.size() > 0)
{
data.push_back(uv[i].x);
data.push_back(uv[i].y);
}
if (normals.size() > 0) if (normals.size() > 0)
{ {
data.push_back(normals[i].x); data.push_back(normals[i].x);
data.push_back(normals[i].y); data.push_back(normals[i].y);
data.push_back(normals[i].z); data.push_back(normals[i].z);
} }
if (uv.size() > 0)
{
data.push_back(uv[i].x);
data.push_back(uv[i].y);
}
} }
glBindVertexArray(sphereVAO); glBindVertexArray(sphereVAO);
glBindBuffer(GL_ARRAY_BUFFER, vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(float), &data[0], GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(float), &data[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);
float stride = (3 + 2 + 3) * sizeof(float); unsigned int stride = (3 + 2 + 3) * sizeof(float);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (void*)0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (void*)0);
glEnableVertexAttribArray(1); glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, stride, (void*)(3 * sizeof(float))); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, stride, (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(2); glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, stride, (void*)(5 * sizeof(float))); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, stride, (void*)(6 * sizeof(float)));
} }
glBindVertexArray(sphereVAO); glBindVertexArray(sphereVAO);

View File

@@ -60,12 +60,12 @@ float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
vec3 fresnelSchlick(float cosTheta, vec3 F0) vec3 fresnelSchlick(float cosTheta, vec3 F0)
{ {
return F0 + (1.0 - F0) * pow(max(1.0 - cosTheta, 0.0), 5.0); return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
vec3 fresnelSchlickRoughness(float cosTheta, vec3 F0, float roughness) vec3 fresnelSchlickRoughness(float cosTheta, vec3 F0, float roughness)
{ {
return F0 + (max(vec3(1.0 - roughness), F0) - F0) * pow(max(1.0 - cosTheta, 0.0), 5.0); return F0 + (max(vec3(1.0 - roughness), F0) - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void main() void main()
@@ -96,7 +96,7 @@ void main()
vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0); vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0);
vec3 nominator = NDF * G * F; vec3 nominator = NDF * G * F;
float denominator = 4 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.001; // 0.001 to prevent divide by zero. float denominator = 4 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.0001; // + 0.0001 to prevent divide by zero
vec3 specular = nominator / denominator; vec3 specular = nominator / denominator;
// kS is equal to Fresnel // kS is equal to Fresnel

View File

@@ -1,7 +1,7 @@
#version 330 core #version 330 core
layout (location = 0) in vec3 aPos; layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoords; layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec3 aNormal; layout (location = 2) in vec2 aTexCoords;
out vec2 TexCoords; out vec2 TexCoords;
out vec3 WorldPos; out vec3 WorldPos;

View File

@@ -514,9 +514,9 @@ void renderSphere()
const unsigned int X_SEGMENTS = 64; const unsigned int X_SEGMENTS = 64;
const unsigned int Y_SEGMENTS = 64; const unsigned int Y_SEGMENTS = 64;
const float PI = 3.14159265359; const float PI = 3.14159265359;
for (unsigned int y = 0; y <= Y_SEGMENTS; ++y)
{
for (unsigned int x = 0; x <= X_SEGMENTS; ++x) for (unsigned int x = 0; x <= X_SEGMENTS; ++x)
{
for (unsigned int y = 0; y <= Y_SEGMENTS; ++y)
{ {
float xSegment = (float)x / (float)X_SEGMENTS; float xSegment = (float)x / (float)X_SEGMENTS;
float ySegment = (float)y / (float)Y_SEGMENTS; float ySegment = (float)y / (float)Y_SEGMENTS;
@@ -554,35 +554,35 @@ void renderSphere()
indexCount = indices.size(); indexCount = indices.size();
std::vector<float> data; std::vector<float> data;
for (std::size_t i = 0; i < positions.size(); ++i) for (unsigned int i = 0; i < positions.size(); ++i)
{ {
data.push_back(positions[i].x); data.push_back(positions[i].x);
data.push_back(positions[i].y); data.push_back(positions[i].y);
data.push_back(positions[i].z); data.push_back(positions[i].z);
if (uv.size() > 0)
{
data.push_back(uv[i].x);
data.push_back(uv[i].y);
}
if (normals.size() > 0) if (normals.size() > 0)
{ {
data.push_back(normals[i].x); data.push_back(normals[i].x);
data.push_back(normals[i].y); data.push_back(normals[i].y);
data.push_back(normals[i].z); data.push_back(normals[i].z);
} }
if (uv.size() > 0)
{
data.push_back(uv[i].x);
data.push_back(uv[i].y);
}
} }
glBindVertexArray(sphereVAO); glBindVertexArray(sphereVAO);
glBindBuffer(GL_ARRAY_BUFFER, vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(float), &data[0], GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(float), &data[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);
float stride = (3 + 2 + 3) * sizeof(float); unsigned int stride = (3 + 2 + 3) * sizeof(float);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (void*)0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (void*)0);
glEnableVertexAttribArray(1); glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, stride, (void*)(3 * sizeof(float))); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, stride, (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(2); glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, stride, (void*)(5 * sizeof(float))); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, stride, (void*)(6 * sizeof(float)));
} }
glBindVertexArray(sphereVAO); glBindVertexArray(sphereVAO);

View File

@@ -82,12 +82,12 @@ float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
vec3 fresnelSchlick(float cosTheta, vec3 F0) vec3 fresnelSchlick(float cosTheta, vec3 F0)
{ {
return F0 + (1.0 - F0) * pow(max(1.0 - cosTheta, 0.0), 5.0); return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
vec3 fresnelSchlickRoughness(float cosTheta, vec3 F0, float roughness) vec3 fresnelSchlickRoughness(float cosTheta, vec3 F0, float roughness)
{ {
return F0 + (max(vec3(1.0 - roughness), F0) - F0) * pow(max(1.0 - cosTheta, 0.0), 5.0); return F0 + (max(vec3(1.0 - roughness), F0) - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void main() void main()
@@ -125,7 +125,7 @@ void main()
vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0); vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0);
vec3 nominator = NDF * G * F; vec3 nominator = NDF * G * F;
float denominator = 4 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.001; // 0.001 to prevent divide by zero. float denominator = 4 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.0001; // + 0.0001 to prevent divide by zero
vec3 specular = nominator / denominator; vec3 specular = nominator / denominator;
// kS is equal to Fresnel // kS is equal to Fresnel

View File

@@ -1,7 +1,7 @@
#version 330 core #version 330 core
layout (location = 0) in vec3 aPos; layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoords; layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec3 aNormal; layout (location = 2) in vec2 aTexCoords;
out vec2 TexCoords; out vec2 TexCoords;
out vec3 WorldPos; out vec3 WorldPos;

View File

@@ -614,9 +614,9 @@ void renderSphere()
const unsigned int X_SEGMENTS = 64; const unsigned int X_SEGMENTS = 64;
const unsigned int Y_SEGMENTS = 64; const unsigned int Y_SEGMENTS = 64;
const float PI = 3.14159265359; const float PI = 3.14159265359;
for (unsigned int y = 0; y <= Y_SEGMENTS; ++y)
{
for (unsigned int x = 0; x <= X_SEGMENTS; ++x) for (unsigned int x = 0; x <= X_SEGMENTS; ++x)
{
for (unsigned int y = 0; y <= Y_SEGMENTS; ++y)
{ {
float xSegment = (float)x / (float)X_SEGMENTS; float xSegment = (float)x / (float)X_SEGMENTS;
float ySegment = (float)y / (float)Y_SEGMENTS; float ySegment = (float)y / (float)Y_SEGMENTS;
@@ -659,30 +659,30 @@ void renderSphere()
data.push_back(positions[i].x); data.push_back(positions[i].x);
data.push_back(positions[i].y); data.push_back(positions[i].y);
data.push_back(positions[i].z); data.push_back(positions[i].z);
if (uv.size() > 0)
{
data.push_back(uv[i].x);
data.push_back(uv[i].y);
}
if (normals.size() > 0) if (normals.size() > 0)
{ {
data.push_back(normals[i].x); data.push_back(normals[i].x);
data.push_back(normals[i].y); data.push_back(normals[i].y);
data.push_back(normals[i].z); data.push_back(normals[i].z);
} }
if (uv.size() > 0)
{
data.push_back(uv[i].x);
data.push_back(uv[i].y);
}
} }
glBindVertexArray(sphereVAO); glBindVertexArray(sphereVAO);
glBindBuffer(GL_ARRAY_BUFFER, vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(float), &data[0], GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(float), &data[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);
float stride = (3 + 2 + 3) * sizeof(float); unsigned int stride = (3 + 2 + 3) * sizeof(float);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (void*)0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (void*)0);
glEnableVertexAttribArray(1); glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, stride, (void*)(3 * sizeof(float))); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, stride, (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(2); glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, stride, (void*)(5 * sizeof(float))); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, stride, (void*)(6 * sizeof(float)));
} }
glBindVertexArray(sphereVAO); glBindVertexArray(sphereVAO);