Deffered Shading question (how to store fragment pos)

Started by
4 comments, last by kRogue 15 years ago
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 );
}
-----------------------My Page: † AncienT RituaL †
Advertisement
Multiply the position by the modelview and then normalize... worked for me
i think normalizing it takes care of the clamping.

------------------------------

redwoodpixel.com

you mean change the:

gl_FragData[3] = vec4( vert_pos_view_space, 1.0 );

to:

gl_FragData[3] = vec4( normalize(vert_pos_view_space), 1.0 );

Im not sure I understand how this will help.

I just want to know how to create a texture with at least 16bit precision per pixel and the freedom to write anything I want on in.
-----------------------My Page: † AncienT RituaL †
you are storing the numbers in the framebuffer as GL_FLOATS... so i'm confused why you think openGL is changing everything. If you try and output that frame to the screen then sure... it's going to do what it can to set those numbers as RGB. But when I implemented my deferred renderer (based on the shader from More OpenGL programming book) he just normalized the vertex for fragment after multiplying by the modelview.

My position buffer looks like so when i output it link
and yes openGL is clamping... however my final result looks like so
">link





------------------------------

redwoodpixel.com

Imagine the following code in the fragment shader of the 2nd pass:

01: uniform sampler2D pos_texture;...02: vec3 _frag_pos_view_space = texture2D( pos_texture, gl_TexCoord[0].st ).xyz;03: if( _frag_pos_view_space.x < 0.0 ) discard;



In the second line I just get the position from the texture. In the third I discard all the fragments that they are from the middle of the screen and left.

The problem is that this code doesnt discard any fragments at all.So this lead me to believe that there is a problem with clamping values. I either have a mistake in the the glTexImage2D or in the storing process (1st pass) or in the retrieving process (2nd pass). I cant understand though.

My scene looks exactly like yours when I render the pos_texture.

PS: Thanks for the help btw.Really appreciated!
-----------------------My Page: † AncienT RituaL †
you ickiness comes form the fact that output, even with floating point rneder targets are clamped to the range [0,1], but do not dispair, read:

http://oss.sgi.com/projects/ogl-sample/registry/ARB/color_buffer_float.txt

to learn how to turn clamping off.

Close this Gamedev account, I have outgrown Gamedev.

This topic is closed to new replies.

Advertisement