My problem is following:
I offset the red cube 1 uni downwards and as You can see It behaves with light like its still in position 0 0 0 but it is in 0 -1 0 but the lighting didnt adjust.
Here’s code that draws my red cube ( va.Bind()
binds vertexArray, and same with shader.Bind()
) :
va.Bind();
objectShader.Bind();
glm::mat4 View = inputCompute(deltaTime, window);
glm::mat4 Projection = glm::perspective(glm::radians(45.0f), (float)CONFIG::WINDOW_WIDTH / (float)CONFIG::WINDOW_HEIGHT, 0.1f, 100.0f);
glm::mat4 Model = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, -1.0f, 0.0f));
objectShader.SetUniformMat4f("projection", Projection);
objectShader.SetUniformMat4f("view", View);
objectShader.SetUniformMat4f("model", Model);
objectShader.SetUniform3f("viewPos", camPosition);
objectShader.SetUniform3f("lightPos", currentLampPos); //objectShader.SetUniformMat4f("u_MVP", mvp);
GLCall(glDrawArrays(GL_TRIANGLES, 0, 12 * 3));
va.Unbind();
objectShader.Unbind();
Here’s it’s shader:
#shader vertex
#version 330 core
layout(location = 0) in vec3 position;
layout(location = 1) in vec3 aNormal;
out vec3 Normal;
out vec3 FragPos;
uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;
uniform vec3 viewPos;
void main()
{
FragPos = vec3(model * vec4(position, 1.0));
Normal = mat3(transpose(inverse(model))) * aNormal;
gl_Position = projection * view * model * vec4(FragPos, 1.0);
};
#shader fragment
#version 330 core
out vec4 color;
in vec3 Normal;
in vec3 FragPos;
uniform vec3 objectColor;
uniform vec3 lightColor;
uniform vec3 lightPos;
uniform vec3 viewPos;
void main()
{
// ambient
float ambientStrength = 0.05;
vec3 ambient = ambientStrength * lightColor;
// diffuse
vec3 norm = normalize(Normal);
vec3 lightDir = normalize(lightPos - FragPos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * lightColor;
// specular
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) * objectColor;
color = vec4(result, 1.0);
}
Now the code that draw’s the light:
// For the lamp
va.Bind();
lampShader.Bind();
glm::mat4 View = inputCompute(deltaTime, window);
glm::mat4 Projection = glm::perspective(glm::radians(45.0f), (float)CONFIG::WINDOW_WIDTH / (float)CONFIG::WINDOW_HEIGHT, 0.1f, 100.0f);
glm::mat4 Model = glm::mat4(1.0f) * glm::scale(glm::mat4(1.0f), glm::vec3(0.5f, 0.5f, 0.5f)) * glm::translate(glm::mat4(1.0f), lampPosition) * glm::translate(glm::mat4(1.0f), transVectorControl(deltaTime, window));
glm::mat4 mvp = Projection * View * Model;
lampShader.SetUniformMat4f("u_MVP", mvp);
lampShader.SetUniform3f("u_Color", lampLightColor);
currentLampPos = glm::vec3(Model[3][0], Model[3][1], Model[3][2]);
GLCall(glDrawArrays(GL_TRIANGLES, 0, 12 * 3));
va.Unbind();
lampShader.Unbind();
Light’s shader is nothing special, it just calculates the gl_Position
.
Sorry for so many lines of code, I wanted to give you guys a clear view what I am doing.
Thanks for all the help!