The transform below for the orthogonal projection is indeed linear, mapping 
[n,f] to [-1,1] in linear fashion. 

If you want to linearize depth with a perspective projection (i.e. survive the 
perspective divide between the vertex and fragment shader), here's a way to do 
that. The example uses HLSL but it gets the point across...

http://www.mvps.org/directx/articles/linear_z/linearz.htm

Another interesting discussion related to linear depth using W-buffers can be 
found at

http://www.mvps.org/directx/articles/using_w-buffers.htm

Just make sure you have support for W buffers...

-Shayne

-----Original Message-----
From: osg-users-boun...@lists.openscenegraph.org 
[mailto:osg-users-boun...@lists.openscenegraph.org] On Behalf Of "Martin Großer"
Sent: Wednesday, August 24, 2011 5:58 AM
To: OpenSceneGraph Users
Subject: Re: [osg-users] Is the Z-Buffer linear or non-linear?

Hello,

I think I got it. Correct me if I am wrong, if I use an orthogonal projection I 
get linear Z-Buffer values. If I use a perspective projection I get non-linear 
values.

The Z-Buffer value (z') of a perspective projection is:
z' = (far + near) / (far - near) + 1/z ((-2 * far * near)/(far-near))

The Z-Buffer value (z') of a orthogonal projection is:
z' = 2 * ((z-near)/(far-near))-1

So, I use a orthogonal projection therefore I get a linear Z-Buffer.

I hope that is right.

Cheers

Martin

-------- Original-Nachricht --------
> Datum: Wed, 24 Aug 2011 10:01:23 +0200
> Von: "Martin Großer" <grosser.mar...@gmx.de>
> An: osg-users@lists.openscenegraph.org
> Betreff: [osg-users] Is the Z-Buffer linear or non-linear?

> Hello,
> 
> I am completely confused about the Z-Buffer. I thought it is non-linear
> but now I read a lot in forums and now I am not sure about that. The reason
> about this question is I want render linear depth values into a texture. I
> think, I can do it with this shader:
> 
> varying float depth;
> uniform float near;
> uniform float far;
> void main(void)
> {
>   vec4 viewPos = gl_ModelViewMatrix * gl_Vertex;
>   depth = (-viewPos.z-near)/(far-near);
>   gl_Position = ftransform();
> };
> 
> 
> varying float depth;
> void main(void)
> {
>     gl_FragDepth = depth;
> }
> 
> But the poor think is all other shader will be overwritten.
> 
> Now I use this:
> 
> _rendertarget->setInternalFormat(GL_DEPTH_COMPONENT);
> _cam->setClearMask(GL_DEPTH_BUFFER_BIT);
> _cam->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
> _cam->attach(osg::Camera::DEPTH_BUFFER, _rendertarget);
> _cam->setRenderOrder(::osg::Camera::PRE_RENDER, 0);
> 
> But this is nonlinear, isn't it?
> 
> I hope someone can help me about this.
> 
> Thanke you very much.
> 
> Martin
> -- 
> NEU: FreePhone - 0ct/min Handyspartarif mit Geld-zurück-Garantie!             
> Jetzt informieren: http://www.gmx.net/de/go/freephone
> _______________________________________________
> osg-users mailing list
> osg-users@lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

-- 
Empfehlen Sie GMX DSL Ihren Freunden und Bekannten und wir
belohnen Sie mit bis zu 50,- Euro! https://freundschaftswerbung.gmx.de
_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to