Im trying to implement deffered shading in a project of mine but I have a problem.
In the first pass I store the pixel attributes in textures. Im using a Frame Buffer Object and I attach 4 textures to it. One for normal, one for diffuse, one for specular and shininess and the last for the fragment position in view space (gl_ModelviewMatrix * frag_pos_world_space). I want the fragment position in view space and the light's pos in view space also so I can do the light calculations in the second pass.
The problem is how to store the fragment pos. The values are floats and they vary from -inf to +inf. OGL clamps the values and changes everything.
Does anyone have an idea?
Thanks in advance!!!!!!!!
Texture setup:
...
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, screenw, screenh, 0, GL_RGBA, GL_FLOAT, NULL );
...
glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT3_EXT, GL_TEXTURE_2D, pos_txtr_id, 0);
...
Vert shader for first pass:
varying vec3 normal, vert_pos_view_space;
void main()
{
normal = gl_NormalMatrix * gl_Normal;
vert_pos_view_space = vec3( gl_ModelViewMatrix * gl_Vertex );
gl_FrontColor = gl_Color;
gl_Position = ftransform();
}
Fragment shader for fist pass:
varying vec3 normal, vert_pos_view_space;
vec3 Normal2Color( in vec3 _normal )
{
vec3 _color = _normal * 0.5;
_color.x += 0.5;
_color.y += 0.5;
_color.z += 0.5;
return _color;
}
void main()
{
// fill the normal texture
vec3 _ncol = Normal2Color( normalize(normal) );
gl_FragData[0] = vec4(_ncol, 0.0);
// fill the diffuse texture
gl_FragData[1] = gl_Color;
// fill the specular txtr
gl_FragData[2] = vec4(gl_FrontMaterial.specular.rgb, gl_FrontMaterial.shininess);
// fill the frag pos in view space ????!!!!????
gl_FragData[3] = vec4( vert_pos_view_space, 1.0 );
}