Jump to content
  • Advertisement

Recommended Posts

Hey guys,

I'm having a little trouble with my point light. My light is working but the light source moves when I move my camera. I've been trying all week to fix it but at my wits end. Maybe I haven't converted it to the proper space.

**VERTEX SHADER**

in vec3 positions;
in vec2 texCoords;
in vec3 normals;

out vec2 pass_texCoords;
out vec3 mvVertexNormal;
out vec3 mvVertexPosition;

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

void main()
{
pass_texCoords = texCoords;
vec4 mvPos = modelViewMatrix * vec4(positions, 1.0);
mvVertexNormal = mat3(normalMatrix) * normals;
mvVertexPosition = vec3(modelViewMatrix * vec4(positions, 1.0));
gl_Position = projectionMatrix * mvPos;

}

**FRAGMENT SHADER**

in vec2 pass_texCoords;
in vec3 mvVertexPosition;
in vec3 mvVertexNormal;

out vec4 out_colour;

struct Material
{
    int hasNormalMap;
    int hasTexture;
    vec3 diffuse;
    vec3 ambient;
    vec3 specular;
    float reflectivity;
};

struct PointLight
{
    vec3 position;
    vec3 colour;
    float intensity;
};    

uniform sampler2D our_texture;
uniform Material material;
uniform vec3 ambientLight;
uniform float specularPower;
uniform PointLight pointLight;
uniform vec3 viewer_position;

vec4 calculateLight(PointLight light, Material material, vec3 position, vec3 normal)
{
// AMBIENT
vec3 ambient = ambientLight * light.colour;

// DIFFUSE
vec3 norm = normalize(normal);
vec3 lightDir = normalize(light.position - position);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * light.colour;  

// SPECULAR
vec3 viewDir = normalize(viewer_position - position);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
vec3 specular = specularPower * spec * light.colour;

vec3 result = (ambient + diffuse + specular);
return vec4(result, 1.0);
}

void main()
{

vec4 lightResult = calculateLight(pointLight, material, mvVertexPosition, mvVertexNormal);

out_colour = texture(our_texture, pass_texCoords) * lightResult;

}

 

The light source is coming from the cube in the red circle. Even the first picture the light isnt directly under the cube. If I don't move my camera and have the cube light source move around automatically the the light will stay with it. It's only if I move the camera that I get problems. The second picture i've moved my camera to the left and the light is moving with me and moving away from the cube light source.

light.png

light2.png

Share this post


Link to post
Share on other sites
Advertisement
mvVertexPosition = vec3(modelViewMatrix * vec4(positions, 1.0));

.

.

.

vec4 lightResult = calculateLight(pointLight, material, mvVertexPosition, mvVertexNormal);

 

You are applying the view matrix to the position of the vertices, so as the view matrix changes, the light will change... I think.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • 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!