I
I
IliaNeverov2021-04-17 12:02:58
C++ / C#
IliaNeverov, 2021-04-17 12:02:58

Why does the light source move when the camera rotates?

Good day! I tried to make diffuse lighting and ran into a problem that when the position of the light in z is greater or less than 0 (the farther from zero, the more cant) when the camera rotates, the position of the light magically changes,
607aa3990516d792726106.png
the camera rotation is almost zero, the position of the light in z is -2.0f
607aa3d0ee114631171756.png
here I slightly rotated the camera and the light moved

Shader code:

//  атрибуты
layout (location = 0) in vec3 position;
layout (location = 0) in vec2 uv;
layout (location = 2) in vec3 normals;

// выход на вход в фрагментный шейдер
//in vec2 uv;
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 =(vec4(LightPos , 1.0f) * ViewMatrix).xyz;
  ToLightVector= lightPos - WorldPosition.xyz;
  pass_uv = uv;
}

fragment:
#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);
}

Please tell me why this is happening and how to fix it?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question