I have been trying to implement diffuse lighting, but have been having trouble calculating the normal's. This is what I have so far.
const GLfloat vertices[] =
{
-0.5f, 0.5f, 0.0f, // Top Left
-0.5f, -0.5f, 0.0f, // Bottom Left
0.5f, -0.5f, 0.0f, // Bottom Right
0.5f, 0.5f, 0.0f, // Top Right
-0.5f, 0.5f, -1.0f, // Top Left (back)
-0.5f, -0.5f, -1.0f, // Bottom Left (back)
0.5f, -0.5f, -1.0f, // Bottom Right (back)
0.5f, 0.5f, -1.0f // Top Right (back)
};
const GLfloat color[] =
{
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f
};
const GLuint indices[] =
{
0, 1, 3,
1, 2, 3,
4, 5, 6,
4, 6, 7,
4, 5, 1,
1, 0, 4,
3, 6, 2,
7, 6, 3,
7, 4, 3,
4, 0, 3,
1, 2, 5,
2, 5, 6
};
glm::vec3 V1 = glm::vec3(vertices[3] - vertices[9], vertices[4] - vertices[10], vertices[5] - vertices[11]);
glm::vec3 V2 = glm::vec3(vertices[6] - vertices[9], vertices[7] - vertices[10], vertices[8] - vertices[11]);
glm::vec3 V3 = glm::vec3(V1.y * V2.z - V1.z * V2.y, V1.z * V2.x - V1.x * V2.z, V1.x * V2.y - V1.y * V2.x);
float Magnitude = glm::length(V3);
glm::vec3 nV = glm::vec3(V3.x / Magnitude, V3.y / Magnitude, V3.z / Magnitude);
const int SZ = 3;
GLfloat normalizedVec[SZ];
normalizedVec[0] = nV.x;
normalizedVec[1] = nV.y;
normalizedVec[2] = nV.z;
From my understanding in order to find a normal, one has to
* Find two vectors (edges of the face/triangle).
* Use cross product to find the perpendicular vector
* Normalize the perpendicular vector
* Then send the vector to OpenGL
Here is how its being sent
// Normal Vertex
glBindBuffer(GL_ARRAY_BUFFER, VBO[2]);
glBufferData(GL_ARRAY_BUFFER, sizeof(normalizedVec), normalizedVec, GL_STATIC_DRAW);
glVertexAttribPointer(NORMALS_POS, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
glEnableVertexAttribArray(NORMALS_POS);
Then here is the vertex shader followed be the fragment shader.
#version 400 core
layout (location = 0) in vec3 VertexPosition;
layout (location = 1) in vec4 color;
layout (location = 2) in vec3 VertexNormal
out vec4 out_color;
out vec3 LightIntensity;
uniform vec4 LightPosition;
uniform vec3 Kd;
uniform vec3 Ld;
uniform mat4 ModelViewMatrix;
uniform mat3 NormalMatrix;
uniform mat4 ProjectionMatrix;
uniform mat4 rotation_matrix;
uniform mat4 projection_matrix;
uniform mat4 PVM;
void main()
{
// Convert normal and position to eye coords
vec3 tnorm = normalize(NormalMatrix * VertexNormal);
vec4 eyeCoords = ModelViewMatrix * vec4(VertexPosition, 1.0);
vec3 s = normalize(vec3(LightPosition - eyeCoords));
// The diffuse shading equation
LightIntensity = Ld * Kd * max(dot(s, tnorm), 0.0);
out_color = color;
gl_Position = PVM * rotation_matrix * vec4(position.x, position.y, position.z, 1.0f);
}
Fragment Shader
#version 400 core
layout (location = 0) out vec4 FragColor;
in vec3 LightIntensity;
in vec4 out_color;
out vec4 OUTPUT_COLOR;
void main()
{
Output_Color = out_color;
FragColor = vec4(LightIntensity, 1.0);
}
I'm not exactly sure if I'm calculating the normal's correctly?