Cleaned up shader code and removed now incompatible models.

This commit is contained in:
Warwick 2022-08-03 13:48:58 +01:00
parent d4f5f8eb50
commit 0cbcc1f094
29 changed files with 133 additions and 199859 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 181 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 MiB

View file

@ -1,16 +0,0 @@
# Blender MTL File: 'None'
# Material Count: 1
newmtl Scene_-_Root
Ns 225.000000
Ka 1.000000 1.000000 1.000000
Kd 0.800000 0.800000 0.800000
Ks 0.500000 0.500000 0.500000
Ke 0.0 0.0 0.0
Ni 1.450000
d 1.000000
illum 2
map_Kd diffuse.jpg
map_Bump normal.png
map_Ks specular.jpg

File diff suppressed because it is too large Load diff

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.5 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.1 MiB

View file

@ -1,3 +0,0 @@
Model by Berk Gedik, from: https://sketchfab.com/3d-models/survival-guitar-backpack-low-poly-799f8c4511f84fab8c3f12887f7e6b36
Modified material assignment (Joey de Vries) for easier load in OpenGL model loading chapter, and renamed albedo to diffuse and metallic to specular to match non-PBR lighting setup.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.6 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 389 KiB

View file

@ -1,13 +0,0 @@
# Blender MTL File: 'cube.blend'
# Material Count: 1
newmtl Material
Ns 225.000000
Ka 1.000000 1.000000 1.000000
Kd 0.800000 0.800000 0.800000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.450000
d 1.000000
illum 2
map_Kd Bake.jpg

View file

@ -1,50 +0,0 @@
# Blender v2.83.5 OBJ File: 'cube.blend'
# www.blender.org
mtllib cube.mtl
o Cube
v 1.000000 1.000000 -1.000000
v 1.000000 -1.000000 -1.000000
v 1.000000 1.000000 1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 1.000000 -1.000000
v -1.000000 -1.000000 -1.000000
v -1.000000 1.000000 1.000000
v -1.000000 -1.000000 1.000000
vt 0.331683 0.668317
vt 0.331683 0.998350
vt 0.001650 0.998350
vt 0.001650 0.668317
vt 0.334984 0.331683
vt 0.334984 0.001650
vt 0.665017 0.001650
vt 0.665017 0.331683
vt 0.665017 0.665017
vt 0.334984 0.665017
vt 0.334984 0.334984
vt 0.665017 0.334984
vt 0.998350 0.331683
vt 0.668317 0.331683
vt 0.668317 0.001650
vt 0.998350 0.001650
vt 0.331683 0.331683
vt 0.001650 0.331683
vt 0.001650 0.001650
vt 0.331683 0.001650
vt 0.331683 0.665017
vt 0.001650 0.665017
vt 0.001650 0.334984
vt 0.331683 0.334984
vn 0.0000 1.0000 0.0000
vn 0.0000 0.0000 1.0000
vn -1.0000 0.0000 0.0000
vn 0.0000 -1.0000 0.0000
vn 1.0000 0.0000 0.0000
vn 0.0000 0.0000 -1.0000
usemtl Material
s off
f 1/1/1 5/2/1 7/3/1 3/4/1
f 4/5/2 3/6/2 7/7/2 8/8/2
f 8/9/3 7/10/3 5/11/3 6/12/3
f 6/13/4 2/14/4 4/15/4 8/16/4
f 2/17/5 1/18/5 3/19/5 4/20/5
f 6/21/6 5/22/6 1/23/6 2/24/6

Binary file not shown.

Binary file not shown.

View file

@ -1,28 +0,0 @@
This asset is provided as is, for purely non-commercial, educational purposes. If this can help anyone make kickass(-er) art I couldn't be happier.
Please note that this is predominantly an example of PBR material work, thus such things as functionality, topology and mesh optimization might be
somewhat overlooked.
Now go make something pretty! :D->-<
Cheers,
Andrew Maximov
More PBR Tutorials:
http://www.ArtIsAVerb.info/PBR.html
http://www.ArtIsAVerb.info/PBT.html
Cerberus Videos:
https://www.youtube.com/watch?v=xIHiXnMltDg
https://www.youtube.com/watch?v=cCYVLkG3hjI
Other art, info and tutorials here:
https://www.facebook.com/ArtIsAVerb
http://www.youtube.com/user/d1v3rsion
http://d1ver.cghub.com/
http://d1ver.cgsociety.org/
http://www.ArtIsAVerb.info/
Make sure to subscribe for all the upcoming stuff! :)
2014, All Rights Reserved.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 48 MiB

View file

@ -1,34 +1,126 @@
#version 330 core
uniform mat4 MVP;
out vec4 FragColor;
in vec2 ourTexCoord;
in vec3 ourNormCoord;
in vec3 FragPos;
in vec2 texCoord;
in GS_OUT {
vec3 tangentLightPos;
vec3 tangentViewPos;
vec3 tangentFragPos;
} from_gs;
// TODO: make temporary hard coded world/camera pos dynamic
uniform vec3 CameraPos;
// Handle multiple textures from the Mesh Object (Might not even be used)
uniform sampler2D texture_diffuse1;
uniform sampler2D texture_diffuse2;
uniform sampler2D texture_diffuse3;
uniform sampler2D texture_specular1;
uniform sampler2D texture_specular2;
uniform sampler2D texture_rma1;
uniform sampler2D texture_normal1;
uniform sampler2D ourTexture;
// PBR functions from learnOpenGL.com
const float PI = 3.14159265359;
vec3 fresnelSchlick(float cosTheta, vec3 F0)
{
return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
}
float DistributionGGX(vec3 N, vec3 H, float roughness)
{
float a = roughness*roughness;
float a2 = a*a;
float NdotH = max(dot(N, H), 0.0);
float NdotH2 = NdotH*NdotH;
float num = a2;
float denom = (NdotH2 * (a2 - 1.0) + 1.0);
denom = PI * denom * denom;
return num / denom;
}
float GeometrySchlickGGX(float NdotV, float roughness)
{
float r = (roughness + 1.0);
float k = (r*r) / 8.0;
float num = NdotV;
float denom = NdotV * (1.0 - k) + k;
return num / denom;
}
float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
{
float NdotV = max(dot(N, V), 0.0);
float NdotL = max(dot(N, L), 0.0);
float ggx2 = GeometrySchlickGGX(NdotV, roughness);
float ggx1 = GeometrySchlickGGX(NdotL, roughness);
return ggx1 * ggx2;
}
vec3 PBR(vec3 albedo, float roughness, float metallic, float ao)
{
vec3 lightPosition = from_gs.tangentLightPos;
vec3 lightColor = vec3(13.47, 11.31, 10.79);
vec3 camPos = from_gs.tangentViewPos;
vec3 fragPos = from_gs.tangentFragPos;
// Thanks to us calculating the tangent space to the triangle and using
// these values instead of normal space we can just use the plain corrected
// UV values here.
vec3 N = normalize((texture(texture_normal1, texCoord).xyz * 2.0 - 1.0)/2);
vec3 V = normalize(camPos - fragPos);
vec3 F0 = vec3(0.04);
F0 = mix(F0, albedo, metallic);
// reflectance equation
vec3 Lo = vec3(0.0);
// calculate per-light radiance
vec3 L = normalize(lightPosition - fragPos);
vec3 H = normalize(V + L);
float distance = length(lightPosition - fragPos);
float attenuation = 1.0 / (distance * distance);
vec3 radiance = lightColor * attenuation;
// cook-torrance brdf
float NDF = DistributionGGX(N, H, roughness);
float G = GeometrySmith(N, V, L, roughness);
vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0);
vec3 kS = F;
vec3 kD = vec3(1.0) - kS;
kD *= 1.0 - metallic;
vec3 numerator = NDF * G * F;
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.0001;
vec3 specular = numerator / denominator;
// add to outgoing radiance Lo
float NdotL = max(dot(N, L), 0.0);
Lo += (kD * albedo / PI + specular) * radiance * NdotL;
vec3 ambient = vec3(0.03) * albedo * ao;
vec3 color = ambient + Lo;
color = color / (color + vec3(1.0));
return pow(color, vec3(1.0/2.2));
}
void main()
{
// Establish ambient lighting
float ambientStrength = 0.1;
vec3 albedo;
albedo.r = pow(texture(texture_diffuse1, texCoord).r, 2.2);
albedo.g = pow(texture(texture_diffuse1, texCoord).g, 2.2);
albedo.b = pow(texture(texture_diffuse1, texCoord).b, 2.2);
float roughness = texture(texture_rma1, texCoord).r;
float metallic = texture(texture_rma1, texCoord).g;
float ao = texture(texture_rma1, texCoord).b;
// Establish a temporary hard coded light position
vec3 lightPosition = vec3(1.0, 1.0, 1.0);
// Normal light maths
vec3 norm = normalize(ourNormCoord);
vec3 lightDir = normalize(lightPosition - FragPos);
// Calculate diffuse
float diff = max(dot(norm, lightDir), 0.0);
FragColor = texture(texture_diffuse1, ourTexCoord) * (ambientStrength + diff);
//FragColor = texture(ourTexture, ourTexCoord);
//FragColor = vec4(ourTexCoord.y,ourTexCoord.x,0,0);
FragColor = vec4(PBR(albedo, roughness, metallic, ao), 1.0);
}

View file

@ -2,27 +2,16 @@
layout (triangles) in;
layout (triangle_strip, max_vertices = 3) out;
uniform mat4 Model;
in vec2 gtexCoord[];
in vec3 gnormCoord[];
in vec3 gWorldPos[];
uniform mat4 MVP;
out vec2 texCoord;
out vec3 normCoord;
out vec3 WorldPos;
vec3 lightPosition = vec3(1, 1, 2);
uniform vec3 CameraPos;
out GS_OUT {
mat3 TBN;
vec3 tangentLightPos;
vec3 tangentViewPos;
vec3 tangentFragPos;
vec3 tangentNormPos;
} gs_out;
in DATA {
@ -57,11 +46,9 @@ void main(void)
mat3 TBN = mat3(T, B, N);
// TBN is an orthogonal matrix and so its inverse is equal to its transpose
TBN = transpose(TBN);
gs_out.TBN = TBN;
// send data to Fragment Shader
gl_Position = data_in[0].camProj * gl_in[0].gl_Position;
gs_out.tangentNormPos = data_in[0].normal;
texCoord = data_in[0].texCoord;
gs_out.tangentFragPos = TBN * gl_in[0].gl_Position.xyz;
gs_out.tangentViewPos = TBN * data_in[0].camPos;
@ -69,7 +56,6 @@ void main(void)
EmitVertex();
gl_Position = data_in[1].camProj * gl_in[1].gl_Position;
gs_out.tangentNormPos = data_in[1].normal;
texCoord = data_in[1].texCoord;
gs_out.tangentFragPos = TBN * gl_in[1].gl_Position.xyz;
gs_out.tangentViewPos = TBN * data_in[1].camPos;
@ -77,7 +63,6 @@ void main(void)
EmitVertex();
gl_Position = data_in[2].camProj * gl_in[2].gl_Position;
gs_out.tangentNormPos = data_in[2].normal;
texCoord = data_in[2].texCoord;
gs_out.tangentFragPos = TBN * gl_in[2].gl_Position.xyz;
gs_out.tangentViewPos = TBN * data_in[2].camPos;

View file

@ -1,175 +0,0 @@
#version 330 core
uniform mat4 MVP;
out vec4 FragColor;
in vec2 texCoord;
in vec3 normCoord;
in vec3 WorldPos;
in GS_OUT {
mat3 TBN;
vec3 tangentLightPos;
vec3 tangentViewPos;
vec3 tangentFragPos;
vec3 tangentNormPos;
} from_gs;
in mat3 TBN;
// TODO: make temporary hard coded world/camera pos dynamic
//uniform vec3 WorldPos ;
uniform vec3 CameraPos;
//uniform int tick;
//vec3 WorldPos = vec3(0.0f, 0.0f, 0.0f);
//vec3 CameraPos = vec3(0.0f, 0.0f, -1.0f);
//TODO: make these values rely on associated textures.
//vec3 albedo = vec3(0.8f, 0.8f, 0.8f);
//float metallic = sin(tick / 60 * 0.3f);
//float roughness = sin(tick / 60 * 0.3f);
//float ao = sin(tick / 60 * 0.8f);
//float metallic = 0.3f;
//float roughness = 0.3f;
//float ao = 0.8f;
// Handle multiple textures from the Mesh Object (Might not even be used)
uniform sampler2D texture_diffuse1;
uniform sampler2D texture_diffuse2;
uniform sampler2D texture_rma1;
uniform sampler2D texture_normal1;
// PBR functions from learnOpenGL.com
const float PI = 3.14159265359;
vec3 fresnelSchlick(float cosTheta, vec3 F0)
{
return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
}
float DistributionGGX(vec3 N, vec3 H, float roughness)
{
float a = roughness*roughness;
float a2 = a*a;
float NdotH = max(dot(N, H), 0.0);
float NdotH2 = NdotH*NdotH;
float num = a2;
float denom = (NdotH2 * (a2 - 1.0) + 1.0);
denom = PI * denom * denom;
return num / denom;
}
float GeometrySchlickGGX(float NdotV, float roughness)
{
float r = (roughness + 1.0);
float k = (r*r) / 8.0;
float num = NdotV;
float denom = NdotV * (1.0 - k) + k;
return num / denom;
}
float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
{
float NdotV = max(dot(N, V), 0.0);
float NdotL = max(dot(N, L), 0.0);
float ggx2 = GeometrySchlickGGX(NdotV, roughness);
float ggx1 = GeometrySchlickGGX(NdotL, roughness);
return ggx1 * ggx2;
}
vec3 normalMapNormal(){
// load and invert normal
vec3 normal = normalize(texture(texture_normal1, texCoord).rgb * 2.0 - 1.0);
vec3 lightDir = normalize(from_gs.tangentLightPos - from_gs.tangentFragPos);
vec3 viewDir = normalize(from_gs.tangentViewPos - from_gs.tangentFragPos);
vec3 reflectDir = reflect(-lightDir, normal);
vec3 halfwayDir = normalize(lightDir + viewDir);
float spec = pow(max(dot(normal, halfwayDir), 0.0),32.0);
vec3 normMapSpecular = vec3(1.0) * spec;
return normMapSpecular;
}
vec3 PBR(vec3 albedo, float roughness, float metallic, float ao)
{
// Establish a temporary hard coded light position
//vec3 lightPosition = vec3(1, 1, 2);
//vec3 lightPosition = vec3(1, 1, 2);
vec3 lightPosition = from_gs.tangentLightPos;
//vec3 lightPosition = vec3( (sin(tick / 1000.0)*2), 1 + sin(tick / 600.0)*2, 2.0);
//vec3 lightColor = vec3(1.0, 1.0, 1.0) - sin(tick / 90);
vec3 lightColor = vec3(13.47, 11.31, 10.79);
//vec3 camPos = CameraPos;
vec3 camPos = from_gs.tangentViewPos;
//vec3 fragPos = WorldPos;
vec3 fragPos = from_gs.tangentFragPos;
//vec3 N = normalize(normCoord);
//vec3 N = normalize(from_gs.tangentNormPos);
vec3 N = normalize(texture(texture_normal1, texCoord).xyz * 2.0 - 1.0);
vec3 V = normalize(camPos - fragPos);
//N = (N + normalize(texture(texture_normal1, texCoord).xyz * 2.0 - 1.0))/2;
//N = (N + normalize(texture(texture_normal1, texCoord).zyx * 2.0 - 1.0))/2;
//N = (N + normalMapNormal()) / 2;
//N = normalMapNormal(); //For seeing if normal map tracks with light.
vec3 F0 = vec3(0.04);
F0 = mix(F0, albedo, metallic);
// reflectance equation
vec3 Lo = vec3(0.0);
// calculate per-light radiance
vec3 L = normalize(lightPosition - fragPos);
vec3 H = normalize(V + L);
float distance = length(lightPosition - fragPos);
float attenuation = 1.0 / (distance * distance);
vec3 radiance = lightColor * attenuation;
// cook-torrance brdf
float NDF = DistributionGGX(N, H, roughness);
float G = GeometrySmith(N, V, L, roughness);
vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0);
vec3 kS = F;
vec3 kD = vec3(1.0) - kS;
kD *= 1.0 - metallic;
vec3 numerator = NDF * G * F;
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.0001;
vec3 specular = numerator / denominator;
// add to outgoing radiance Lo
float NdotL = max(dot(N, L), 0.0);
Lo += (kD * albedo / PI + specular) * radiance * NdotL;
vec3 ambient = vec3(0.03) * albedo * ao;
vec3 color = ambient + Lo;
color = color / (color + vec3(1.0));
return pow(color, vec3(1.0/2.2));
}
void main()
{
vec3 albedo;
albedo.r = pow(texture(texture_diffuse1, texCoord).r, 2.2);
albedo.g = pow(texture(texture_diffuse1, texCoord).g, 2.2);
albedo.b = pow(texture(texture_diffuse1, texCoord).b, 2.2);
float roughness = texture(texture_rma1, texCoord).r;
float metallic = texture(texture_rma1, texCoord).g;
float ao = texture(texture_rma1, texCoord).b;
FragColor = vec4(PBR(albedo, roughness, metallic, ao), 1.0);
//FragColor = vec4(PBR(albedo, roughness, metallic, ao) + normalMapNormal(), 1.0);
//FragColor = vec4(normalMapNormal(), 1.0);
//FragColor = vec4(vec3(0.1) + normalMapNormal()*5, 1.0);
//FragColor = vec4(vec3(0.5), 1.0);
}

View file

@ -1,44 +0,0 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoord;
uniform mat4 MVP;
uniform mat4 Model;
uniform vec3 CameraPos;
uniform int tick;
out vec2 gtexCoord;
out vec3 gnormCoord;
//Pbr
out vec3 gWorldPos;
out DATA {
vec3 normal;
vec2 texCoord;
mat4 camProj;
mat4 modelProj;
vec3 lightPos;
vec3 camPos;
} data_out;
void main()
{
// Saving camera projection until geometry shader.
gl_Position = Model * vec4(aPos, 1.0);
gnormCoord = aNormal;
gtexCoord = aTexCoord;
data_out.normal = aNormal;
data_out.texCoord = aTexCoord;
data_out.camProj = MVP;
data_out.modelProj = Model;
data_out.lightPos = vec3( (sin(tick / 1000.0)*2), 1 + sin(tick / 600.0)*2, 2.0);
data_out.camPos = CameraPos;
// Calculate position of fragment
gWorldPos = vec3(Model * vec4(aPos, 1.0));
};

View file

@ -5,17 +5,27 @@ layout (location = 2) in vec2 aTexCoord;
uniform mat4 MVP;
uniform mat4 Model;
uniform vec3 CameraPos;
uniform int tick;
out vec2 ourTexCoord;
out vec3 ourNormCoord;
out vec3 FragPos;
out DATA {
vec3 normal;
vec2 texCoord;
mat4 camProj;
mat4 modelProj;
vec3 lightPos;
vec3 camPos;
} data_out;
void main()
{
gl_Position = MVP * Model * vec4(aPos, 1.0);
ourNormCoord = aNormal;
ourTexCoord = aTexCoord;
// Saving camera projection until geometry shader.
gl_Position = Model * vec4(aPos, 1.0);
// Calculate position of fragment
FragPos = vec3(Model * vec4(aPos, 1.0));
data_out.normal = aNormal;
data_out.texCoord = aTexCoord;
data_out.camProj = MVP;
data_out.modelProj = Model;
data_out.lightPos = vec3( (sin(tick / 1000.0)*2), 1 + sin(tick / 600.0)*2, 2.0);
data_out.camPos = CameraPos;
};

View file

@ -70,8 +70,8 @@ int main(int argc, char **argv) {
// Create event handling struct
SDL_Event input;
ShaderLoader shader(ROOT_DIR "data/shaders/pbrVertex.glsl",
ROOT_DIR "data/shaders/pbrFragment.glsl",
ShaderLoader shader(ROOT_DIR "data/shaders/vertex.glsl",
ROOT_DIR "data/shaders/fragment.glsl",
ROOT_DIR "data/shaders/geometry.glsl");
// Model backpack(std::string(ROOT_DIR) +
@ -125,9 +125,6 @@ int main(int argc, char **argv) {
// Send our glsl shader our camera information
shader.setMat4("MVP", camera.getMVP());
// shader.setMat4("Model", camera.getModel());
// shader.setMat4("View", camera.getView());
// shader.setMat4("Projection", camera.getProjection());
shader.setVec3("CameraPos", camera.getCameraPosition());
shader.setInt("tick", SDL_GetTicks());