How to code ambient light into a fragment shader? OpenGL

Viewed 129

I'm trying to figure out how to properly add ambient light into my scene using the fragment shader but it doesn't seem to come out right. I'm not too sure how to add ambient light to my scene properly but this is what my fragment shader currently looks like.

#version 330 
 
uniform vec4 uLightPosition; 
uniform vec4 uEyePosition;
uniform vec3 uAmbientLight;

in vec4 oNormal; 
in vec4 oSurfacePosition; 
 
out vec4 FragColour; 
 
void main()  
{  
 vec4 lightDir = normalize(uLightPosition - oSurfacePosition); 
 vec4 eyeDirection = normalize(uEyePosition - oSurfacePosition);
 vec4 scatteredLight = vec4(vec3(uAmbientLight),1);
 vec4 reflectedVector = reflect(-lightDir, oNormal);

 float diffuseFactor = max(dot(oNormal, lightDir), 0); 
float specularFactor = pow(max(dot(reflectedVector, eyeDirection), 0.0), 30); 
float ambientFactor = max(dot(oNormal, scatteredLight),0);
 
 FragColour = vec4(vec3(diffuseFactor + specularFactor + ambientFactor), 1); 
} 

Any help on whether I'm doing an equation wrong or if I'm completely off would be much appreciated.

This is what my scene currently looks like, the lighting doesn't look very realistic. Current Progress of Scene

1 Answers

The ambient light is constant. Just add it to the fragment color- Multiply the light factors with the color of the object:

vec3 color = vec3(1.0);

vec3 finalColor = 
    color * uAmbientLight +
    color * diffuseFactor +
    color * specularFactor;

FragColour = vec4(finalColor, 1); 
Related