mercredi 25 février 2015

GLSL Point Light Problems



When I render my 3d scene using a point light shader, I run into a few problems. The light seems to rotate with the camera, the light also behaves like a directional light instead of a point light. I was following this tutorial for glsl point lights. GLSL Core Tutorial – Point Lights


My Vertex Shader:



#version 150 core

in vec4 in_Position;
in vec4 in_Color;
in vec2 in_TextureCoord;
in vec3 in_Normal;

uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;
uniform mat3 normal;

out Data {
vec3 normal;
vec3 eye;
vec3 lightDir;
vec2 st;
} Out;

void main(void) {
vec4 pos = view * model * in_Position;
vec3 norm = normal * in_Normal;
//Light Position
vec4 l_pos = view * model * vec4(0,1,0,1);

Out.normal = norm;
Out.lightDir = vec3(l_pos - pos);
Out.st = in_TextureCoord;
Out.eye = vec3(-pos);
gl_Position = projection * view * model * in_Position;
}


My Fragment Shader:



#version 150 core

uniform sampler2D texture_diffuse;

in Data {
vec3 normal;
vec3 eye;
vec3 lightDir;
vec2 st;
} DataIn;

out vec4 out_Color;

void main(void) {
vec4 diffuse = texture(texture_diffuse, DataIn.st).rgba;
vec4 spec = vec4(0.0);
vec4 specular = vec4(0.2,0.2,0.2,1);
vec4 ambient = vec4(0.2,0.2,0.2,1);

float shininess = 100;

vec3 n = normalize(DataIn.normal);
vec3 l = normalize(DataIn.lightDir);
vec3 e = normalize(DataIn.eye);

float intensity = max(dot(n,l), 0.0);
if (intensity > 0.0) {
vec3 h = normalize(l + e);
float intSpec = max(dot(h,n), 0.0);
spec = specular * pow(intSpec, shininess);
}
out_Color = max(intensity * diffuse + spec, ambient);
}


Picture of the problem:


Shader Issue




Aucun commentaire:

Enregistrer un commentaire