Directional lighting doesn't seem to update

I have 2 issues:

  1. As I rotate the model using its model matrix on any axis x, y or z, the dark regions remain dark irrespective of the direction of the light and seem to switch sides between dark and light unnaturally or sometimes it just dims and brightens up.
  2. I don’t observe any change in brightness due to the topology of the model. Atleast I expect to.

Vertex shader code:

#version 450

layout(location = 0) in vec3 position;
layout(location = 1) in vec3 color;
layout(location = 2) in vec3 normal;
layout(location = 3) in vec2 uv;

layout(location = 0) out vec3 fragCol;

layout(push_constant) uniform Push {
  mat4 transform; // projection * view * model
  mat4 normalMatrix;
} push;

vec3 DIRECTION_TO_LIGHT = vec3(1.0, -3.0, -1.0);

void main() {
  gl_Position = push.transform * vec4(position, 1.0);

  vec3 normalWorldSpace = normalize(mat3(push.normalMatrix) * normal);

  float lightIntensity = max(dot(normalWorldSpace, DIRECTION_TO_LIGHT), 0);

  fragCol = lightIntensity * vec3(1.0);
}

fragment shader code:

#version 450 core

layout(location = 0) in vec3 fragCol;
layout(location = 0) out vec4 outColor;

layout(push_constant) uniform Push{
	mat4 transform;
	mat4 normalMatrix;
}push;

void main(){
	outColor = vec4(fragCol, 1.0);
}

Matrix calculations:

void SimpleRenderSystem::renderGameObjects(VkCommandBuffer commandBuffer, std::vector<NGameObject>& gameObjects, const NCamera& camera){
		nPipeline->bind(commandBuffer);

		auto projectionMatrix = camera.getProjection() * camera.getView();

		for (auto& obj : gameObjects) {
			obj.transform.rotation.y += glm::radians(45.0f) * deltaTime;
			SimplePushConstantData push{};
			auto modelMatrix = obj.transform.mat4();
			push.transform = projectionMatrix * modelMatrix;
			push.normalMatrix = obj.transform.normalMatrix();
			vkCmdPushConstants(commandBuffer, pipelineLayout,
				VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
				0, sizeof(SimplePushConstantData), &push);
			obj.model->bind(commandBuffer);
			obj.model->draw(commandBuffer);
		}
	}

normal matrix:

glm::mat4 TransformComponent::normalMatrix()
	{
		const float c3 = glm::cos(rotation.z);
		const float s3 = glm::sin(rotation.z);
		const float c2 = glm::cos(rotation.x);
		const float s2 = glm::sin(rotation.x);
		const float c1 = glm::cos(rotation.y);
		const float s1 = glm::sin(rotation.y);
		const glm::vec3 inverseScale = 1.0f / scale;

		return glm::mat3{
			{
				inverseScale.x* (c1 * c3 + s1 * s2 * s3),
				inverseScale.x* (c2 * s3),
				inverseScale.x* (c1 * s2 * s3 - c3 * s1),
			},
			{
				inverseScale.y * (c3 * s1 * s2 - c1 * s3),
				inverseScale.y * (c2 * c3),
				inverseScale.y * (c1 * c3 * s2 + s1 * s3),
			},
			{
				inverseScale.z * (c2 * s1),
				inverseScale.z * (-s2),
				inverseScale.z * (c1 * c2),
			}
		};
	}

The lighting seems as if the light has been baked into the model which isn’t the case.
I might have forgotten to provide an important piece of code that might be needed to answer my question, if so please comment about it.
I just switched from opengl to vulkan so this might be a stupid mishap.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.