WIP: normal maps

This commit is contained in:
2023-11-04 21:40:01 +02:00
parent b403495da2
commit 43c9111a8c
12 changed files with 320 additions and 43 deletions
+21 -9
View File
@@ -2,6 +2,7 @@
in vec3 fragNormals;
in vec3 fragPos;
in vec2 texCoords;
in mat3 fragTBN;
out vec4 FragColor;
@@ -10,6 +11,7 @@ struct Material {
sampler2D texture_specular;
sampler2D texture_ambient;
sampler2D texture_emission;
sampler2D texture_normal;
vec3 ambient;
vec3 diffuse;
@@ -63,6 +65,7 @@ uniform PointLight pointLights[NR_POINT_LIGHTS];
uniform SpotLight spotLight;
uniform bool useTextures;
uniform bool hasEmissionTexture;
uniform bool hasNormalTexture;
vec3 CalculateDirLight(DirLight light, vec3 normal, vec3 viewDir) {
vec3 lightDir = normalize(-light.direction);
@@ -83,9 +86,9 @@ vec3 CalculateDirLight(DirLight light, vec3 normal, vec3 viewDir) {
vec3 specular = vec3(0.0, 0.0, 0.0);
if (useTextures) {
ambient = light.ambient * vec3(texture(material.texture_ambient, texCoords)) * material.ambient;
diffuse = light.diffuse * diff * vec3(texture(material.texture_diffuse, texCoords)) * material.diffuse;
specular = light.specular * spec * vec3(texture(material.texture_specular, texCoords)) * material.specular;
ambient = light.ambient * vec3(texture(material.texture_ambient, texCoords));
diffuse = light.diffuse * diff * vec3(texture(material.texture_diffuse, texCoords));
specular = light.specular * spec * vec3(texture(material.texture_specular, texCoords));
} else {
ambient = light.ambient * material.ambient;
diffuse = light.diffuse * (diff * material.diffuse);
@@ -118,9 +121,9 @@ vec3 CalculatePointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewD
vec3 specular = vec3(0.0, 0.0, 0.0);
if (useTextures) {
ambient = light.ambient * vec3(texture(material.texture_ambient, texCoords)) * material.ambient;
diffuse = light.diffuse * diff * vec3(texture(material.texture_diffuse, texCoords)) * material.diffuse;
specular = light.specular * spec * vec3(texture(material.texture_specular, texCoords)) * material.specular;
ambient = light.ambient * vec3(texture(material.texture_ambient, texCoords));
diffuse = light.diffuse * diff * vec3(texture(material.texture_diffuse, texCoords));
specular = light.specular * spec * vec3(texture(material.texture_specular, texCoords));
} else {
ambient = light.ambient * material.ambient;
diffuse = light.diffuse * (diff * material.diffuse);
@@ -162,9 +165,9 @@ vec3 CalculateSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir
vec3 specular = vec3(0.0, 0.0, 0.0);
if (useTextures) {
ambient = light.ambient * vec3(texture(material.texture_ambient, texCoords)) * material.ambient;
diffuse = light.diffuse * diff * vec3(texture(material.texture_diffuse, texCoords)) * material.diffuse;
specular = light.specular * spec * vec3(texture(material.texture_specular, texCoords)) * material.specular;
ambient = light.ambient * vec3(texture(material.texture_ambient, texCoords));
diffuse = light.diffuse * diff * vec3(texture(material.texture_diffuse, texCoords));
specular = light.specular * spec * vec3(texture(material.texture_specular, texCoords));
} else {
ambient = light.ambient * material.ambient;
diffuse = light.diffuse * (diff * material.diffuse);
@@ -190,6 +193,14 @@ void main()
}
vec3 norm = normalize(fragNormals);
// use normal map if available
if (useTextures && hasNormalTexture) {
vec3 normal = texture(material.texture_normal, texCoords).rgb;
normal = normal * 2.0 - 1.0;
norm = normalize(fragTBN * normal);
}
vec3 viewDir = normalize(viewPos - fragPos);
// direction light
@@ -202,6 +213,7 @@ void main()
result += CalculateSpotLight(spotLight, norm, fragPos, viewDir);
// emission
if (useTextures && hasEmissionTexture) {
result += texture(material.texture_emission, texCoords).rgb;
}
+10
View File
@@ -2,9 +2,12 @@
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoords;
layout (location = 3) in vec3 aTangent;
layout (location = 4) in vec3 aBitangent;
out vec3 fragNormals;
out vec3 fragPos;
out vec2 texCoords;
out mat3 fragTBN;
uniform mat4 model;
uniform mat4 view;
@@ -19,7 +22,14 @@ void main()
fragNormals = normalMatrix * aNormal;
else
fragNormals = mat3(transpose(inverse(model))) * aNormal;
vec3 T = normalize(vec3(model * vec4(aTangent, 0.0)));
vec3 B = normalize(vec3(model * vec4(aBitangent, 0.0)));
vec3 N = normalize(vec3(model * vec4(aNormal, 0.0)));
mat3 TBN = mat3(T, B, N);
fragPos = vec3(model * vec4(aPos, 1.0));
texCoords = aTexCoords;
fragTBN = TBN;
}