depth and distance

本文探讨了全向光源阴影映射中两种线性深度存储方法的区别:一种使用视图空间深度,另一种使用到光源的距离。文章讨论了这两种方法如何影响阴影测试,并涉及软件PCF过滤、方差阴影映射及非线性深度存储。

 

I'm implementing ominidirectional shadow mapping for point lights. I want to use a linear depth which will be stored in the color textures (cube map). A program will contain two filtering techniques: software pcf (because hardware pcf works only with depth textures) and variance shadow mapping. I found two ways of storing linear depth:

constfloat linearDepthConstant =1.0/(zFar - zNear);//firstfloat moment1 =-viewSpace.z * linearDepthConstant;float moment2 = moment1 * moment1;
outColor = vec2(moment1, moment2);//secondfloat moment1 = length(viewSpace)* linearDepthConstant;float moment2 = moment1 * moment1;
outColor = vec2(moment1, moment2);

What are differences between them ? Are both ways correct ?

For the standard shadow mapping with software pcf a shadow test will depend on the linear depth format. What about variance shadow mapping ?

I implemented omnidirectional shadow mapping for points light using a non-linear depth and hardware pcf. In that case a shadow test looks like this:

vec3 lightToPixel = worldSpacePos - worldSpaceLightPos;
vec3 aPos = abs(lightToPixel);float fZ =-max(aPos.x, max(aPos.y, aPos.z));
vec4 clip = pLightProjection * vec4(0.0,0.0, fZ,1.0);float depth =(clip.z / clip.w)*0.5+0.5;float shadow = texture(ShadowMapCube, vec4(normalize(lightToPixel), depth));

I also implemented standard shadow mapping without pcf which using second format of linear depth: (Edit 1: i.e. distance to the light + some offset to fix shadow acne)

vec3 lightToPixel = worldSpacePos - worldSpaceLightPos;constfloat linearDepthConstant =1.0/(zFar - zNear);float fZ = length(lightToPixel)* linearDepthConstant;float depth = texture(ShadowMapCube, normalize(lightToPixel)).x;if(depth <= fZ){
     shadow =0.0;}else{
     shadow =1.0;}

but I have no idea how to do that for the first format of linear depth. Is it possible ?

Edit 2: For non-linear depth I used glPolygonOffset to fix shadow acne. For linear depth and distance to the light some offset should be add in the shader. I'm trying to implement standard shadow mapping without pcf using a linear depth (-viewSpace.z * linearDepthConstant + offset) but following shadow test doesn't produce correct results:

vec3 lightToPixel = worldSpacePos - worldSpaceLightPos;
vec3 aPos = abs(lightToPixel);float fZ =-max(aPos.x, max(aPos.y, aPos.z));
vec4 clip = pLightProjection * vec4(0.0,0.0, fZ,1.0);float fDepth =(clip.z / clip.w)*0.5+0.5;float depth = texture(ShadowMapCube, normalize(lightToPixel)).x;if(depth <= fDepth){
     shadow =0.0;}else{
     shadow =1.0;}

How to fix that ?

share improve this question
 add comment

1 Answer

up vote 2 down vote accepted

The method with viewSpace.z is storing depth, while the method with length(viewSpace) is storingdistance to the light, not depth (which is always measured parallel to a view direction).

You could store either one, but you have to be consistent about it. If you store distance instead of depth when you render your shadow map, you have to compare against distance instead of depth when you apply the shadow map.

Depth is the natural choice as that's what will be generated by the GPU rasterizer when you draw the shadow map. It takes extra work to convert it to distance (you have to set up the viewSpace vector). But I can imagine that distance might give better results in some situations with VSM. You'd have to experiment to see.

转载于:https://www.cnblogs.com/oiramario/p/3567774.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值