• Advertisement
Sign in to follow this  

incorrect lighting (normals)

This topic is 1883 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Until now everything was going ok with my program, until I tried to implement simple directional lighting. I am not exactly sure where is the problem. I checked everything. I get this



The thing on the right supposed to be torus (???)
Now my rendering function look like this
void RenderScene()


	// set light properties
	float fSine = sin(SunAngle*3.1415/180.0);
	glm::vec3 vSunPos(cos(SunAngle*3.1415/180.0)*70, sin(SunAngle*3.1415/180.0)*70, 0.0);

	// change color of sky depending on sun's position
	glClearColor(0.0f, std::max(0.0f, 0.9f*fSine), std::max(0.0f, 0.9f*fSine), 1.0f);

	spDirLight.setUniform("sunLight.vColor", glm::vec3(1.0f, 1.0f, 1.0f));
	spDirLight.setUniform("sunLight.fAmbientIntensity", 0.25f);
	spDirLight.setUniform("sunLight.vDirection", -glm::normalize(vSunPos));
	spDirLight.setUniform("projectionMatrix", &mProjection);

	mProjection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 1000.0f);
	mModelView = CameraLook(vEye, vView, vUp);

	spDirLight.setUniform("gSampler", 0);
	spDirLight.setUniform("modelViewMatrix", &mModelView);
	spDirLight.setUniform("vColor", glm::vec4(1.0f, 1.0f, 1.0f, 1.0f));

	// render ground
	glDrawArrays(GL_TRIANGLES, 36, 6);


	glm::vec3 vPos = glm::vec3(0.0f, 0.0f, 0.0f);
	mModelToCamera = glm::translate(glm::mat4(1.0), vPos);
	mModelToCamera = glm::scale(mModelToCamera, glm::vec3(5.0f, 5.0f, 5.0f));
	// we need to trasnsform normals properly, it's done by transpose of inverse matrix of rotations and scales
	spDirLight.setUniform("normalMatrix", glm::transpose(glm::inverse(mModelToCamera)));
	spDirLight.setUniform("modelViewMatrix", mModelView*mModelToCamera);

	glDrawArrays(GL_TRIANGLES, 0, 36);

	// render torus

	glm::vec3 vPos1 = glm::vec3(20.0f, 0.0f, 0.0f);
	mModelToCamera = glm::translate(glm::mat4(1.0), vPos1);
	spDirLight.setUniform("normalMatrix", glm::transpose(glm::inverse(mModelToCamera)));
	spDirLight.setUniform("modelViewMatrix", mModelView*mModelToCamera);
	glDrawArrays(GL_TRIANGLES, 42, iTorusFaces1*3);


and my shaders look like this


vertex shader

#version 330

uniform mat4 projectionMatrix;
uniform mat4 modelViewMatrix;
uniform mat4 normalMatrix;

layout (location = 0) in vec3 inPosition;
layout (location = 1) in vec2 inCoord;
layout (location = 2) in vec3 inNormal;

out vec2 texCoord;

smooth out vec3 vNormal;

void main()
	gl_Position = projectionMatrix*modelViewMatrix*vec4(inPosition, 1.0);
	texCoord = inCoord;
	vec4 vRes = normalMatrix*vec4(inNormal, 0.0);
	vNormal = vRes.xyz;




fragment shader

#version 330

in vec2 texCoord;
smooth in vec3 vNormal;
out vec4 outputColor;

uniform sampler2D gSampler;
uniform vec4 vColor;

struct SimpleDirectionalLight
	vec3 vColor;
	vec3 vDirection;
	float fAmbientIntensity;

uniform SimpleDirectionalLight sunLight;

void main()
	vec4 vTexColor = texture2D(gSampler, texCoord);
	float fDiffuseIntensity = max(0.0, dot(normalize(vNormal), -sunLight.vDirection));
	outputColor = vTexColor*vColor*vec4(sunLight.vColor*(sunLight.fAmbientIntensity+fDiffuseIntensity), 1.0);


I am more concerned about box rendering incorrectly than the torus error. I render the box same way as I did when I was using texture shader, and there was no problems. Now I used the same approach and this happens.


What exactly causes this to happen? unsure.png

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement