Answer the question
In order to leave comments, you need to log in
Why doesn't diffuse lighting work correctly?
Why is the lighting not working properly?
This is what nonsense it turns out, and everything works fine in the tutorials:
Model data:
vertices:
{-0.5f, 0.5f, 0.f},
{-0.5f, -0.5f, 0.f},
{0.5f, 0.5f, 0.f},
{0.5f, -0.5f, 0.f},
uvs:
{0, 1},
{0, 0},
{1, 1},
{1, 0},
normals:
0.0f,1.0f,0.0f,
0.0f,1.0f,0.0f,
0.0f,1.0f,0.0f,
0.0f,1.0f,0.0f,
Indices:
0, 1, 2,
2, 1, 3
вершинный шейдер
#version 330
layout (location = 0) in vec3 position;
layout (location = 0) in vec2 uv;
layout (location = 2) in vec3 normals;
out vec2 pass_uv;
out vec3 FragPos;
out vec3 ToLightVector;
out vec3 normal;
uniform mat4 ModelMatrix;
uniform mat4 ViewMatrix;
uniform mat4 projectionMatrix;
uniform vec3 LightPos;
uniform vec3 ModelColor;
vec3 lightPos;
void main() {
normal = normals;
vec4 WorldPosition = ModelMatrix * vec4(position, 1.0f);
gl_Position = projectionMatrix * ViewMatrix * WorldPosition;
lightPos = LightPos;
ToLightVector= lightPos - WorldPosition.xyz;
pass_uv = uv;
}
фрагментный шейдер:
#version 330
out vec4 color;
in vec2 pass_uv;
in vec3 surfaceNormal;
in vec3 ToLightVector;
in vec3 normal;
uniform vec3 LightColor;
uniform vec3 ModelColor;
float AmbientPower = 0.05f;
vec3 result;
vec3 diffuse;
vec3 Ambient;
float brightness;
void main() {
brightness = max(dot(normalize(normal), normalize(ToLightVector)),0.0f);
diffuse = brightness * LightColor;
Ambient = AmbientPower * LightColor;
result = (Ambient + diffuse) * ModelColor;
color = vec4(result, 1.0f);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question