使用OpenGL和GLSL的基本阴影映射工件

使用OpenGL和GLSL的基本阴影映射工件

本文介绍了使用OpenGL和GLSL的基本阴影映射工件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个有关基本阴影映射技术的简单OpenGL测试应用程序.

I've written a simple OpenGL test application about basic shadow mapping technique.

除了阻塞器背面上的一个工件,我已去除了大多数工件.该背面受到伪影的影响,因为在第一次渲染过程(阴影深度图填充)期间,我启用了正面剔除.因此,我有自遮蔽的Z值伪影.

I have removed most artifacts except for the one on the occluder back face. This back face is concerned by artifacts because during the first rendering pass (shadow depth map filling) I enable the front face culling. Consequently I have self-shadowing z-fighting artifacts.

要解决这种问题,它在一些教程中说过,需要以很小的偏移量(例如0.0005f)对光空间中顶点位置的深度进行偏置.

To solve this kind of problem it said on several tutorials the depth of the vertex position in light space need to be biased with a very small offset like 0.0005f.

这是我的问题的屏幕截图(为便于查看,我增加了盒子的环境光值).这里没有深度偏移:

Here's a screenshot of my problem (for a sake of visibility I have increased the ambient light value for the box). Here without depth offset:

如您所见,它具有很强的自我遮盖力.

As you can see there is a strong self-shadowing.

这是我在片段着色器中使用的一段代码:

Here's a piece of code I use in my fragment shader:

if (ShadowCoords.w > 0.0f)
{
    vec4 tmp_shadow_coords = ShadowCoords;

    tmp_shadow_coords.z -= 0.0000f; //DEPTH OFFSET DISABLE HERE
    shadowFactor = textureProj(ShadowMap, tmp_shadow_coords);
}

现在让我们看看使用等于0.0002f的偏移量会发生什么:

Now let's see what happen when using an offset equal to 0.0002f:

tmp_shadow_coords.z -= 0.0002f;

屏幕截图:

如您所见,自我阴影减少了.现在让我们尝试使用等于0.0003f的偏移量:

As you can see the self-shadowing is decreased. Now let's try with an offset equal to 0.0003f :

tmp_shadow_coords.z -= 0.0003f;

画面快照:

您可以看到自我阴影消失了!但是,如果我朝着遮挡物和阴影之间的极限放大,我们可以看到没有阴影的区域.该工件称为Peter Panning工件.

As you can see self-shadowinh has disappeared! But if I zoom towards the limit between the occluder and the shadow we can see an unshaded area. This artifact is called Peter Panning artifact.

所以这是一个奇怪的情况:为避免自阴影伪像,我需要修改光照空间中的顶点深度值.但是,对深度值序列的这种修改会导致出现一个Peter Panning伪像!

So it's a strange situation : to avoid self-shadowing artifact I need to modify the vertex depth value in light space. But this modification of the depth value trains causes a Peter Panning artifact!

如果我增加偏移值,问题会更严重.

The problem is worse if I increase the offset value.

更新

我也尝试添加偏移量.例如:

I also tried adding an offset. For example:

tmp_shadow_coords.z += 0.0002f;

屏幕截图:

自我遮蔽较少,但没有Peter Panning伪像.但不幸的是,在立方体的正面上似乎出现了一些伪像.前面的阴影满溢.

There's less self-shadowing but no Peter Panning artifact. But unfortunatly it appears some artifacts on the front faces of the cube. There's an overflow of the shadow on the front faces.

如果我增加偏移量:

tmp_shadow_coords.z += 0.0003f;

屏幕截图:

自阴影伪影已完全消失,但正面的阴影溢出更严重.

The self-shadowing artifacts have completely disappeared but the shadow overflow in front faces is worse.

所以我想知道是否可能有一个既没有自阴影伪影又没有Peter Panning伪影的渲染器?

So I wonder if it's possible to have a render without both self-shadowing artifact and no Peter Panning artifact ?

推荐答案

这很好用.铸造表面与接收表面非常靠近的地方很小.

This works pretty well. There is a small gap where the casting surface is very close to the receiving surface.

在着色器中正确使用的主要方法是如何应用阴影.仅按阴影/遮挡值缩放添加的直射光,漫反射和镜面反射项(环境项应单独放置).一个简单的检查是,阴影中的表面应看起来与禁用了阴影的背向灯光的表面完全相同.阴影不仅应使事物变暗(即finalColour *= 1.0-shadowScalefinalColour -= shadow),而且还应避免直射光.当涉及多个灯光时,这一点变得非常重要.

The main thing to get right in the shader is how a shadow is applied. Scale just the added direct light, diffuse and specular terms, by the shadow/occlusion value (ambient terms should be left alone). An easy check is that surfaces in shadow should look exactly the same as surfaces facing away from the light with shadowing disabled. Shadows should not just make things darker (i.e. finalColour *= 1.0-shadowScale or finalColour -= shadow), instead they are the absence of direct light. This becomes very important when multiple lights are involved.

这篇关于使用OpenGL和GLSL的基本阴影映射工件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 16:50