Not logged in, Join Here! or Log In Below:  
 
News Articles Search    
 

 Home / 3D Theory & Graphics / Converting eye-space coordinates to depthbuffer-values Account Manager
 
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
 
Cygnus

April 28, 2005, 05:44 AM

Hi!

For my thesisproject, I need to adjust my depthbuffer values, in a fragment shader. To do that, I need to convert my eye-space z-coordinate to "window-space" by applying viewport mapping to it. For some reason, it goes wrong, all my depth values are mapped to the far plane.

Right now I do this in my vertex shader :

  1.  
  2. float eyeSpaceZPos = mul(ModelView,(IN.Position)).z; // multiply with modelviewmatrix
  3. OUT.PreCalculatedValues.z = eyeSpaceZPos; // passed on in texture coordinates
  4.  


And this in my fragment shader :
  1.  
  2. struct PixelOut
  3. {
  4.         half4 Color : COLOR;
  5.         half Depth : DEPTH;
  6. };
  7.  
  8. PixelOut main(vertexOutput IN, const uniform float a, const uniform float b )
  9. {
  10. ...
  11.  
  12. float zeye_dz = IN.PreCalculatedValues.z + offset; // offset is 0 for now, so I should get the values that OpenGL normally would give me
  13. OUT.Depth = ((a * zeye_dz) + b) / zeye_dz; // viewport mapping
  14. // a is far / (far - near)
  15. // b is -2 * near * far / (far - near)
  16. }
  17.  


Can anyone tell me what I'm doing wrong?

 
grelle

April 28, 2005, 06:47 AM

There's nothing wrong with your code. It must be the values...

 
Cygnus

April 28, 2005, 10:34 AM

Maybe the problem lies in my shader-structs?

My vertex shader output is defined like this

  1. struct vertexOutput {
  2.     float4 HPosition: POSITION;
  3.     float3 PreCalculatedValues : TEXCOORD1;
  4.     float Psize : PSIZE;
  5. };


My fragment shader input is

  1. struct vertexOutput {
  2.     float4 HPosition: POSITION;
  3.     float2 TexCoord : TEXCOORD0;
  4.     float3 PreCalculatedValues : TEXCOORD1;
  5.     float Psize : PSIZE;
  6. };


I need the TEXCOORD0 to know the current pixelposition on the pointsprite.

 
grelle

April 28, 2005, 01:20 PM

Looks good.
Are you sure that your nearplane isn't 0? Because that would map all depth values to the far plan.

By the way, what are you working on? By what you've said it sounds something like depth sprites?

[edit]
ohh, wait. Why don't you use the same struct for both output and input?

 
Cygnus

April 28, 2005, 03:02 PM

My near plane is at 5.0
My far plane is at 50.0

I'm working on a point rendering program. I render the points of a model as (round/ellipse) splats, which have a gaussian alpha texture (not transparent in the middle, more transparent as you go away from the middle). The splats are blended together to get a smooth surface. To achieve good blending, I have to do 2 passes. The first to the depth buffer and the second to the framebuffer (where I use the depthbuffer).

During that first pass, the depth values need to be offset a bit, so that the splats are blended correctly. Right now, without the offsetting, my rendered model looks like this : http://img186.echo.cx/img186/4224/splats5sm.jpg (as you can see, only the front pixel/splat-layer is rendered, nothing is blended)

I'm trying to simulate the normal depth value calculation right now (without the offset), but that's where it goes wrong as you can see.

I use two different structs so that CG/OpenGL fills in my TEXCOORD0. This then contains the position on the current pointsprite. The pointsprites are rendered as squares and with these pixelpositions given, I convert them to ellipses.

I hope this explains it enough...

 
grelle

April 28, 2005, 04:02 PM

According to DX documentation the outputs from the pixelshader must be float4 and float1. You could try and change that.
This is from the Cg documentation: "To ensure interoperability between vertex programs and fragment programs,both must use the same struct for their respective outputs and inputs."

 
Cygnus

April 28, 2005, 05:52 PM

Two good suggestions grelle, but unfortunaly that didn't fix it :(
I tried changing the halfs to floats in my fragment shader and removing the TEXCOORD0-usage and I still get the same wrong depth values.

 
Cygnus

April 29, 2005, 02:49 AM

Maybe there's some OpenGL-bit you need to enable before you can write to DEPTH in a the fragment shader? I had a similar problem when I wanted to write to my PSIZE in my vertex shader, until I found out I had to glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_NV);

 
Cygnus

May 17, 2005, 04:40 AM

I'm still stuck at this, does anyone see the problem? Or maybe some suggestions? Any help is appreciated.

 
This thread contains 9 messages.
 
 
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.