I
I
IliaNeverov2021-04-21 21:22:30
OpenGL
IliaNeverov, 2021-04-21 21:22:30

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:
60806d4771f72793968595.png
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);
}

What is the error? Day 4 I'm already struggling to understand anything. I
also have one more question: why does all this only work in one direction?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
maaGames, 2021-04-22
@maaGames

Why model vertices lie in the XY plane and the normals are directed along the Y axis, and not along the Z?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question