Hi,
I’ve been busting away at this for a few days now, as anyone with a gamedev.net account can tell you. I am working on a deferred render, and have the first past done. Now I am working on the lighting pass but have came up with some strange errors. Not only does the light move and rotate with me (its suposted to be a fixed location point light) but there is a black spot in the center of the screen. Can someone please help me?
in main
void init(){
…
GLfloat pos = { 0.0, 1.0, 0.0, 1.0 };
GLfloat dif = { 0.0, 1.0, 1.0, 1.0 };
glLightfv( GL_LIGHT0, GL_POSITION, pos );
glLightfv( GL_LIGHT0, GL_DIFFUSE, dif );
}
…
//start the second pass
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity();
glLightfv( GL_LIGHT0, GL_POSITION, pos );
and the light shaders
vert
varying vec2 TexCoord;
void main(){
gl_Position = ftransform();
TexCoord = gl_MultiTexCoord0.st;
}
and frag, its kinda rought, need to clean it up after
uniform sampler2D colorMap;
uniform sampler2D normalMap;
varying vec2 TexCoord;
const vec2 camerarange = vec2( 1.0, 1000.0 );
const vec3 screensize = vec3( 640.0, 480.0, 640.0 / 480.0);
float DepthToZPosition(in float depth) {
return camerarange.x / (camerarange.y - depth * (camerarange.y - camerarange.x)) * camerarange.y;
}
void main(){
vec3 n = texture2D( normalMap, TexCoord).xyz ;
vec3 color = texture2D( colorMap, TexCoord ).rgb;
float depth = texture2D( normalMap, TexCoord).w;
vec3 FragPosition = vec3( (((gl_FragCoord.x / screensize.x) - 0.5 ) * 2.0), (((-gl_FragCoord.y / screensize.y) + 0.5 ) * (2.0 / screensize.z)), DepthToZPosition( depth) ); FragPosition.xy *= FragPosition.z;
vec3 lightDir = normalize( gl_LightSource[0].position.xyz - FragPosition ); vec3 dir = lightDir; float dist = length( gl_LightSource[0].position.xyz - FragPosition ); vec3 pos = FragPosition;//vec3( 0.0, 1.0, 0.0 );
float nDotL = dot(n,dir); vec3 hv = normalize( dir + normalize( FragPosition ) );
if( nDotL > 0.0 ){ float att = 1.0 / ( gl_LightSource[0].constantAttenuation + gl_LightSource[0].linearAttenuation * dist + gl_LightSource[0].quadraticAttenuation * dist * dist); color.xyz += att * ( gl_LightSource[0].diffuse.rgb * nDotL + gl_LightSource[0].ambient.rgb ); float nDotHV = max(dot(n, hv), 0.0 ); color += att * 0.4 * gl_LightSource[0].specular.rgb * pow( nDotHV, 1.0 ); //0.4 = spec power, 1.0 = shineyness }
//color = FragPosition; gl_FragColor = vec4( color, 1.0 );
}
and here is the first pass if anyone needs it
vert
varying vec3 Normal;
varying vec2 TexCoord; //gl_TexCoord is deprecated in 1.3 =/
varying vec2 Position;
void main(){
TexCoord = gl_MultiTexCoord0.st; //but so is MultiTexCoord… find a replacement
gl_Position = ftransform();
Normal = gl_NormalMatrix * gl_Normal; Position = (gl_ModelViewMatrix * gl_Vertex).xy;
}
and frag
uniform sampler2D TextureMap;
uniform float farReciprocal; //the far plain Reciprocal, IE 1/far ( multi is faster then division )
//NOTE: varying is deprecated in 1.3
//change to in for frag and out for vert
varying vec3 Normal;
varying vec2 TexCoord; //Becouse gl_TexCoord is depercated…
varying vec2 Position;
void main(){
//normal is not normalized, so we make the normal normal
vec3 n = normalize( Normal );
float z = ( gl_FragCoord.z / gl_FragCoord.w ) * farReciprocal;
gl_FragData[0] = texture2D( TextureMap, TexCoord ); gl_FragData[1] = vec4( n, z );
}