added basic lighting

This commit is contained in:
Lukian 2025-02-08 15:36:41 +01:00
parent 32fd486cc3
commit 93333821d8
3 changed files with 36 additions and 4 deletions

View file

@ -1,11 +1,32 @@
#version 330 core
out vec4 FragColor;
in vec3 Normal;
in vec2 TexCoords;
in vec3 FragPos;
uniform sampler2D texture_diffuse1;
uniform vec3 lightColor;
uniform vec3 lightPos;
uniform vec3 viewPos;
void main()
{
FragColor = texture(texture_diffuse1, TexCoords);
{
float ambientStrength = 0.1;
vec3 ambient = ambientStrength * lightColor;
vec3 norm = normalize(Normal);
vec3 lightDir = normalize(lightPos - FragPos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * lightColor;
float specularStrength = 0.5;
vec3 viewDir = normalize(viewPos - FragPos);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
vec3 specular = specularStrength * spec * lightColor;
vec3 result = (ambient + diffuse + specular) * vec3(1.0, 0.5, 0.2);
vec4 light = vec4(result, 1.0);
FragColor = texture(texture_diffuse1, TexCoords) * light;
}

View file

@ -3,7 +3,9 @@ layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoords;
out vec3 Normal;
out vec2 TexCoords;
out vec3 FragPos;
uniform mat4 model;
uniform mat4 view;
@ -11,6 +13,8 @@ uniform mat4 projection;
void main()
{
TexCoords = aTexCoords;
gl_Position = projection * view * model * vec4(aPos, 1.0);
TexCoords = aTexCoords;
Normal = mat3(transpose(inverse(model))) * aNormal;
FragPos = vec3(model * vec4(aPos, 1.0));
gl_Position = projection * view * model * vec4(aPos, 1.0);
}