I was looking for methods to render multiple lights without redrawing the scene multiple times and i found article about "Deferred Lighting". It didnt took me much time to render my scene into 4 buffers using FBO, i store 4 textures for position, normals, color and depth, then i render a fullscreen quad in ortho mode and apply lighting shader to it but, as i've suspected my shader doesn't blend multiple lights together so if i draw light B, the light A isnt rendered, now im capable of rendering just one light on scene.
I would like to blend those lights but to be honest GLSL isn't something that i'm experienced in.
This is the light pass shader that i'm using, i found it somewhere but i'm not sure who's the author of it.
Fragment:
varying vec3 p;
varying vec3 sDir;
varying vec4 lpos;
uniform sampler2D normalBuffer;
uniform sampler2D depthBuffer;
uniform sampler2D colorBuffer;
uniform vec3 lightPos;
uniform vec3 lightColor;
uniform float lightRadius;
float color_to_float(vec3 color)
{
const vec3 byte_to_float = vec3(1.0,1.0/256.0,1.0/(256.0*256.0));
return dot(color,byte_to_float);
}
vec3 lighting(vec3 SColor, vec3 SPos, float SRadius, vec3 p, vec3 n, vec3 MDiff, vec3 MSpec, float MShi)
{
vec3 l = SPos-p;
vec3 v = normalize(p);
vec3 h = normalize(v+l);
l = normalize(l);
vec3 Idiff = max(0.0, dot(l, n)) * MDiff * SColor;
float att = max(0.0,1.0-length(l)/SRadius);
vec3 Ispec = pow(max(0.0,dot(h,n)),MShi)*MSpec*SColor;
return att*(Idiff+Ispec);
}
void main()
{
vec3 depthcolor = texture2D(depthBuffer, gl_TexCoord[0].st).rgb;
vec3 n = texture2D(normalBuffer, gl_TexCoord[0].st).rgb;
float pixelDepth = color_to_float(depthcolor);
vec3 WorldPos = pixelDepth * normalize(sDir);
gl_FragColor = vec4(lighting(lightColor, lightPos, lightRadius, WorldPos, n, vec3(1.0,1.0,1.0), vec3(1,1,1) ,1) ,128);
gl_FragColor *= texture2D(colorBuffer, gl_TexCoord[0].st);
}
Vertex:
uniform vec3 lightPos;
attribute vec3 screenDir;
varying vec3 sDir;
varying vec4 lpos;
varying vec4 Ldir;
void main()
{
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_TexCoord[1] = gl_MultiTexCoord0;
gl_TexCoord[2] = gl_MultiTexCoord0;
gl_Position = ftransform();
sDir = screenDir;
vec4 lp = vec4(lightPos,1);
lpos = lp;
}
Edited by BraXi, 18 August 2012 - 06:13 AM.






