View Full Version : Beginner problem: Create a light source

11-08-2015, 03:30 PM

I need to create 2 light sources (directional light and spot light), these are my shaders:


#version 330 core

in vec3 inPos; //Posicion
in vec3 inColor; //Color
in vec3 inNormal; //Normal para el sombreado
in vec2 inTexCoord; //Coordenadas de textura

uniform mat4 modelViewProj;
uniform mat4 normal;
uniform mat4 modelView; //Pasa de coordenadas del modelo a coordenadas de la camara

out vec3 color;
out vec3 outPos; //Posicion en coordenadas de la camara
out vec3 outNormal; //Normal en coordenadas de la camara
out vec2 texCoord; //Coordenadas de textura

void main()
outPos = vec3(modelView * vec4(inPos,1.0));
outNormal = vec3 (normal* vec4(inNormal,0.0));
color = inColor;
texCoord = inTexCoord;

gl_Position = modelViewProj * vec4 (inPos,1.0);


#version 330 core

out vec4 outColor;

in vec3 color;
in vec3 outNormal;
in vec3 outPos;
in vec2 texCoord;

uniform sampler2D colorTex; //Textura donde almacenamos el color
uniform sampler2D emiTex; //Textura donde almacenamos la luz emisiva
uniform sampler2D specularTex;
uniform mat4 view;

vec3 ka; //Coeficiente de reflexion ambiental
vec3 kd; //Coeficiente de reflexion difusa
vec3 ks; //Coeficiente de reflexion especular
vec3 ke;
float n = 30.0;
vec3 N; //Normal
vec3 pos; //Posicion en coordenadas de la camara

//fuente de luz
vec3 Ia = vec3(0.3); //Intensidad de la luz
vec3 Il = vec3(1.0);
vec3 Pl = vec3 (0.0,0.0,6.0); //Posicion de la luz

vec3 shade();

void main()
ka = texture (colorTex,texCoord).rgb;
kd = ka;
//ks = vec3(1.0);
ks = texture (specularTex,texCoord).rgb;
ke = texture (emiTex,texCoord).rgb;

N = normalize(outNormal);
pos = outPos;

outColor = vec4(shade(), 1.0);

vec3 shade() //Funcion para sombrear
vec3 color = vec3(0.0);
color = Ia*ka;

vec3 L = normalize(vec3(view*vec4(Pl,1)) - pos);
color += Il*kd*dot(N,L);

vec3 V = normalize(-pos);
vec3 R = normalize(reflect(-L,N));
float factor = clamp (dot(V,R),0.0001,1.0);
factor = pow (factor,n);
color += Il * ks * factor;

color += ke;

return color;

Anyone could help me? Thanks!