Advertisement

OpenGL Diffuse Lighting Doesn't Work!!!

Started by October 02, 2018 03:44 PM
1 comment, last by babaliaris 5 years, 11 months ago

 

 

In the gif image below you can see the effect that my code produces. When I do not rotate the cube, diffuse lighting works properly (faces that face the light source are brighter) but when i rotate the cube this is what happens:

GIF

 

Cube's Vertex Shader.


#version 330 core

layout(location = 0) in vec3 aPos;
layout(location = 1) in vec3 aNormal;

uniform mat4 model;
uniform mat4 view;
uniform mat4 proj;


out vec3 FragPos;
out vec3 Normal;

void main()
{
	gl_Position = proj * view * model * vec4(aPos, 1.0);

	FragPos = vec3(model * vec4(aPos, 1.0));

	Normal = normalize(aNormal);
}

 

Cube's Fragment Shader.


#version 330 core

out vec4 Color;

uniform vec3 lightPos;
uniform vec3 lightColor;
uniform vec3 objectColor;

in vec3 FragPos;
in vec3 Normal;

void main()
{
	float ambient = 0.3f, diffuse;
	vec3 result;

	vec3 lightDirection = normalize(lightPos - FragPos);

	diffuse = max( dot(Normal, lightDirection), 0.0f );

	result = (ambient + diffuse) * lightColor;

	Color = vec4(result * objectColor, 1.0f);
}

 

 

Cube's Bindings And Matrices Code (This code is called every frame for each cube object):


void Cube::Render()
{

	//---------------------Run The Brains---------------------//
	for (unsigned int i = 0; i < m_brains->size(); i++)
	{

		//Call the Start Method.
		if (m_brains->at(i)->m_started)
		{
			m_brains->at(i)->Start();
			m_brains->at(i)->m_started = false;
		}

		//Call the Update Method.
		else
		{
			m_brains->at(i)->Update();
		}
	}
	//---------------------Run The Brains---------------------//


	//Bind everything before the draw call.
	m_texture->Bind();
	m_program->Bind();

	//Initialize the mvp transformations.
	glm::mat4 model = glm::mat4(1.0f);
	glm::mat4 view  = glm::mat4(1.0f);
	glm::mat4 proj  = glm::mat4(1.0f);

	//Translate.
	model = glm::translate(model, m_pos);

	//Rotate the model.
	model = glm::rotate(model, glm::radians(m_rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
	model = glm::rotate(model, glm::radians(m_rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
	model = glm::rotate(model, glm::radians(m_rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));

	//Scale.
	model = glm::scale(model, m_scale);

	//View.
	view = m_core->m_cam->GetView();

	//Projection.
	proj = glm::perspective(glm::radians(m_core->m_cam->m_fov), (float)m_core->m_width / m_core->m_height, 0.1f, 100.0f);

	//Set the tranformation matrices on the shader.
	m_program->SetUniformMat4f("model", model);
	m_program->SetUniformMat4f("view", view);
	m_program->SetUniformMat4f("proj", proj);
  
    //Draw.
	GLCall(glDrawArrays(GL_TRIANGLES, 0, 36));
}

 

 

 

Vertex Data:


	float vertices[] = {
      
     //Positions             //Normals
	-0.5f, -0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
	 0.5f, -0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
	 0.5f,  0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
	 0.5f,  0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
	-0.5f,  0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
	-0.5f, -0.5f, -0.5f,  0.0f,  0.0f, -1.0f,

	-0.5f, -0.5f,  0.5f,  0.0f,  0.0f, 1.0f,
	 0.5f, -0.5f,  0.5f,  0.0f,  0.0f, 1.0f,
	 0.5f,  0.5f,  0.5f,  0.0f,  0.0f, 1.0f,
	 0.5f,  0.5f,  0.5f,  0.0f,  0.0f, 1.0f,
	-0.5f,  0.5f,  0.5f,  0.0f,  0.0f, 1.0f,
	-0.5f, -0.5f,  0.5f,  0.0f,  0.0f, 1.0f,

	-0.5f,  0.5f,  0.5f, -1.0f,  0.0f,  0.0f,
	-0.5f,  0.5f, -0.5f, -1.0f,  0.0f,  0.0f,
	-0.5f, -0.5f, -0.5f, -1.0f,  0.0f,  0.0f,
	-0.5f, -0.5f, -0.5f, -1.0f,  0.0f,  0.0f,
	-0.5f, -0.5f,  0.5f, -1.0f,  0.0f,  0.0f,
	-0.5f,  0.5f,  0.5f, -1.0f,  0.0f,  0.0f,

	 0.5f,  0.5f,  0.5f,  1.0f,  0.0f,  0.0f,
	 0.5f,  0.5f, -0.5f,  1.0f,  0.0f,  0.0f,
	 0.5f, -0.5f, -0.5f,  1.0f,  0.0f,  0.0f,
	 0.5f, -0.5f, -0.5f,  1.0f,  0.0f,  0.0f,
	 0.5f, -0.5f,  0.5f,  1.0f,  0.0f,  0.0f,
	 0.5f,  0.5f,  0.5f,  1.0f,  0.0f,  0.0f,

	-0.5f, -0.5f, -0.5f,  0.0f, -1.0f,  0.0f,
	 0.5f, -0.5f, -0.5f,  0.0f, -1.0f,  0.0f,
	 0.5f, -0.5f,  0.5f,  0.0f, -1.0f,  0.0f,
	 0.5f, -0.5f,  0.5f,  0.0f, -1.0f,  0.0f,
	-0.5f, -0.5f,  0.5f,  0.0f, -1.0f,  0.0f,
	-0.5f, -0.5f, -0.5f,  0.0f, -1.0f,  0.0f,

	-0.5f,  0.5f, -0.5f,  0.0f,  1.0f,  0.0f,
	 0.5f,  0.5f, -0.5f,  0.0f,  1.0f,  0.0f,
	 0.5f,  0.5f,  0.5f,  0.0f,  1.0f,  0.0f,
	 0.5f,  0.5f,  0.5f,  0.0f,  1.0f,  0.0f,
	-0.5f,  0.5f,  0.5f,  0.0f,  1.0f,  0.0f,
	-0.5f,  0.5f, -0.5f,  0.0f,  1.0f,  0.0f
	};

	//Create the Vertex Array.
	m_vao = new Vao();

	//Create the Vertex Buffer.
	m_vbo = new Vbo(vertices, sizeof(vertices));

	//Create the attributes.
	m_attributes = new VertexAttributes();
	m_attributes->Push(3);
	m_attributes->Push(3);
	m_attributes->Commit(m_vbo);

 


void life()
{
  while (!succeed())
    try_again();

  die_happily();
}

 

I found the solution. I had to transform the Normal vectors too with this formula


Normal = mat3(transpose(inverse(model))) * aNormal;  

 


void life()
{
  while (!succeed())
    try_again();

  die_happily();
}

 

This topic is closed to new replies.

Advertisement