Jump to content
  • Advertisement
Sign in to follow this  
Siim Annuk

OpenGL GLSL specular light problem wrt the camera position

This topic is 2541 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


I'm currently implementing a simple Pool game in OpenGL 3+ with GLSL shaders (custom matrix stack and the works). I've got diffuse lighting working but I'm having trouble with the specular lighting for directional lights.

I think it is best if I illustrate my problem with a video capture. Observe in the video as the camera moves toward the cue ball and when crossing the world origin (0,0,0) the specular highlight on the cue ball moves strangely. I think it is something to do with how I have set up the "camera" and how I move it, since when switching to the second camera (seen in the video at the end) which is located at an opposite direction and attached to the cue ball, the specular highlights are not to be seen at all.


At each frame I set the perspective and camera as follows:

void Camera::Set() {
// calculate new perspective matrix
RenderSystemServices::GetRenderSystem()->CalculatePerspective(m_fov, m_clippingPlanes);
// load the modelveiw identity
// apply lookat
MODELVIEW_MULTIPLY(glm::gtx::transform2::lookAt(GetPosition(), m_lookAt, m_up));
// apply rotation
// store the view matrix
m_viewMatrix = MODELVIEW_TOP;

Also, I calculate the light position as follows:

void DirectionalLight::Set() {
const matrix3x3f normal_mat = glm::transpose(glm::inverse(glm::mat3(MODELVIEW_TOP)));
CurrentPosition = vector3f(normal_mat * GetPosition());

The 'CurrentPosition' is then sent to the fragment shader as 'LightPos'.

The vertex and fragment shaders are as follows.

vertex shader:

#version 130
uniform mat4 ModelViewProjectionMat;
uniform mat4 ModelViewMat;
uniform mat3 NormalMat;
in vec3 InVertex;
in vec3 InNormal;
in vec4 InColor;
in vec2 InTexCoord;
out vec3 EyeVert;
out vec3 EyeNorm;
out vec4 Color;
out vec2 TexCoord;
void main() {
TexCoord = InTexCoord;
Color = InColor;
EyeVert = vec3(ModelViewMat * vec4(InVertex, 0.0));
EyeNorm = NormalMat * InNormal;

gl_Position = ModelViewProjectionMat * vec4(InVertex, 1.0);

fragment shader:

#version 130
uniform sampler2D Texture0;
uniform vec3 LightPos;
uniform vec3 EyePos;
uniform vec4 LightAmbient;
uniform vec4 LightDiffuse;
uniform vec4 LightSpecular;
uniform vec4 MatAmbient;
uniform vec4 MatDiffuse;
uniform vec4 MatSpecular;
uniform vec4 MatEmission;
uniform float MatShininess;
in vec3 EyeVert;
in vec3 EyeNorm;
in vec4 Color;
in vec2 TexCoord;
out vec4 FragColor;
void main() {
vec3 normal = normalize(EyeNorm);

float lambertTerm = max(dot(normal, normalize(LightPos)), 0.0);
vec4 diffuse = MatDiffuse * LightDiffuse * lambertTerm;
vec4 ambient = MatAmbient * LightAmbient;

vec3 lightDir = normalize(LightPos);
vec3 eyeDir = normalize(EyePos - EyeVert);
vec3 halfVec = normalize(eyeDir + lightDir);
vec4 specular = MatSpecular * LightSpecular * pow(max(dot(normal, halfVec), 0.0), MatShininess);

FragColor = diffuse + ambient + specular;

Any ideas or questions very welcome!

Share this post

Link to post
Share on other sites
Is the "EyePos" that's being passed in to the fragment shader in eye space? If it were in object space then that could potentially be the problem since the "eyeDir" calculation has "EyeVert" in eye space.

EDIT: Actually this is very likely the issue since "EyePos" should be in eye space and thus there would be a problem if it were anything but (0, 0, 0).

Share this post

Link to post
Share on other sites
From what I saw from video it looks like your lightPos isn't correct
When you are calculating lightDir in Fragment shader is your light position in world space or camera space?

Share this post

Link to post
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!