Depth Bias 以及 Ogre材质中的depth_bias
通过给多边形增加一个z方向深度偏移(depth bias,z_bias),使3D空间的共面多边形看起来好像并不共面,以便它们能够被正确渲染。这种技术是很有用的,例如,我们要渲染投射在墙上的阴影,这时候墙和阴影共面,如果没有深度偏移,先渲染墙,再渲染阴影,由于depth test,阴影可能不能正确显示。我们给墙设置一个深度偏移,使它增大,例如z增加0.01,先渲染墙,再渲染阴影,则墙和阴影可以正确的显示。
Depth-bias操作在clipping之后进行实施,所以depth-bias对几何clipping没有影响。另外需要注意的是:对一个给定体元(primitive),bias值是一个常量,在进行差值操作之前,它施加在每个顶点上。偏移操作都是32位浮点运算,还有Bias不能施加在点以及线体元上(除了线框模式的线段)。
Polygons that are coplanar in your 3-D space can be made to appear as if they are not coplanar by adding a z-bias to each one.
This is a technique commonly used to ensure that shadows in a scene are displayed properly. For instance, a shadow on a wall will likely have the
same depth value as the wall does. If you render the wall first and then the shadow, the shadow might not be visible, or depth artifacts might be
visible. You can reverse the order in which you render the coplanar objects in hopes of reversing the effect, but depth artifacts are still likely.
An application can help ensure that coplanar polygons are rendered properly by adding a bias to the z-values that the system uses when
rendering the sets of coplanar polygons.
To add a z-bias to a set of polygons, call the IDirect3DDevice9::SetRenderState method just before rendering them, setting the State parameter to
D3DRS_DEPTHBIAS, and the Value parameter to a value between 0-16 inclusive.
A higher z-bias value increases the likelihood that the polygons you render will be visible when displayed with other coplanar polygons.
Offset = m * D3DRS_SLOPESCALEDEPTHBIAS + D3DRS_DEPTHBIAS
where m is the maximum depth slope of the triangle being rendered.
m = max(abs(delta z / delta x), abs(delta z / delta y))
The units for the D3DRS_DEPTHBIAS and D3DRS_SLOPESCALEDEPTHBIAS render states depend on whether z-buffering or w-buffering is enabled. The application must provide suitable values.
The bias is not applied to any line and point primitive. However, this bias needs to be applied to triangles drawn in wireframe mode.
// RenderStates
D3DRS_SLOPESCALEDEPTHBIAS :
Used to determine how much bias can be applied to co-planar primitives to reduce z-fighting. The default value is 0.
bias = (max * D3DRS_SLOPESCALEDEPTHBIAS) + D3DRS_DEPTHBIAS.
where max is the maximum depth slope of the triangle being rendered:max = max(abs(delta z / delta x), abs(delta z / delta y))
D3DRS_DEPTHBIAS :
A floating-point value that is used for comparison of depth values. SeeDepth Bias (Direct3D 9). The default value is 0.
// Caps
D3DPRASTERCAPS_DEPTHBIAS
D3DPRASTERCAPS_SLOPESCALEDEPTHBIAS
上面对depth bias进行了说明,包括D3D中如何使用渲染状态来设置depth bias,下面对Ogre材质中设置depth bias进行说明:
depth_bias
设置此渲染通路的深度值的偏向。可用于使共面的多边形中的一个位于其它之上。例如:印制花纹图案。
格式: depth_bias <constant_bias> [<slopescale_bias>]
深度偏向值最终由偏向常数(constant_bias)*最小可观察的深度(minObservableDepth)+最大坡度(maxSlope)*坡度偏向(slopescale_bias)共同决定,即: ( constant_bias * minObservableDepth + maxSlope * slopescale_bias)。坡度偏向与多边形到镜头的角度有关,形成相应的偏向值,但是在一些时间比较早的硬件上,这被忽略了。偏向常数是形成最小深度值的一个因素,所以1的价值就在于一点一点地缓慢地推进深度。
与D3D参考文档中说的最终的depth bias值的计算不同,那就是多乘以了一个minObservableDepth(对于D3D渲染系统,该值为:-1 / 250000.f)。
而最终的深度值
depth = depth - (constant_bias * minObservableDepth + maxSlope * slopescale_bias)
depth_bias 1表示深度值向前移动一个刻度。当两个面共顶点时,depth_bias 1即可将其移到上面。如果不共顶点,数据精度会导致计算的深度存在偏差(距离相机越近深度偏差越大,同样的距离差在相机很近时对应了较大的深度差),这种情况下至少要depth_bias 2 2才能移到上层。
下面分别看下Ogre中对于D3D渲染系统和GL渲染系统,设置depth bias渲染状态实现:
D3D渲染系统如下
- void D3D9RenderSystem::_setDepthBias(float constantBias, float slopeScaleBias)
- {
- if ((mDeviceManager->getActiveDevice()->getD3D9DeviceCaps().RasterCaps & D3DPRASTERCAPS_DEPTHBIAS) != 0)
- {
- // Negate bias since D3D is backward
- // D3D also expresses the constant bias as an absolute value, rather than
- // relative to minimum depth unit, so scale to fit
- constantBias = -constantBias / 250000.0f;
- HRESULT hr = __SetRenderState(D3DRS_DEPTHBIAS, FLOAT2DWORD(constantBias));
- if (FAILED(hr))
- OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Error setting constant depth bias",
- "D3D9RenderSystem::_setDepthBias");
- }
- if ((mDeviceManager->getActiveDevice()->getD3D9DeviceCaps().RasterCaps & D3DPRASTERCAPS_SLOPESCALEDEPTHBIAS) != 0)
- {
- // Negate bias since D3D is backward
- slopeScaleBias = -slopeScaleBias;
- HRESULT hr = __SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, FLOAT2DWORD(slopeScaleBias));
- if (FAILED(hr))
- OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Error setting slope scale depth bias",
- "D3D9RenderSystem::_setDepthBias");
- }
- }
GL渲染系统
- void GLRenderSystem::_setDepthBias(float constantBias, float slopeScaleBias)
- {
- if (constantBias != 0 || slopeScaleBias != 0)
- {
- glEnable(GL_POLYGON_OFFSET_FILL);
- glEnable(GL_POLYGON_OFFSET_POINT);
- glEnable(GL_POLYGON_OFFSET_LINE);
- glPolygonOffset(-slopeScaleBias, -constantBias);
- }
- else
- {
- glDisable(GL_POLYGON_OFFSET_FILL);
- glDisable(GL_POLYGON_OFFSET_POINT);
- glDisable(GL_POLYGON_OFFSET_LINE);
- }
- }
这样就很清楚了设置depth bias的过程,所以就能理解对于Ogre中最终的深度值为需要减去这个偏移,而显示在前面。如果为负数,那么就相当于真正加上了一个便宜,深度值增加,导致靠后!
另外一个关于depth_bias属性:
iteration_depth_bias
用于通道多次迭代时,设置附加的偏移值。如果需迭代三次,第一次偏移为depth_bias,第二次为depth_bais+ iteration_depth_bias,第三次为depth_bais+ iteration_depth_bias*2
格式: iteration_depth_bias <bias_per_iteration>
Depth Bias 以及 Ogre材质中的depth_bias的更多相关文章
- 转:Ogre的MaterialSystem分析
1. Mesh .SubMesh.SubEntity和Entity 所有的Mesh对象是由SubMesh构成的,每个SubMesh代表了Mesh对象的一部分,该部分只能使用一种Meterial.如果一 ...
- 有关UnrealEngine材质编辑器中的Custom节点的一些小贴士
PS:本文写于2017.2.1日,使用版本为4.13.第二次更新时间为2017.3.15增加了四.一些材质编辑器中的奇怪的技巧: 一.前言在Unreal中材质编辑器提供了Custom节点,作为HLSL ...
- 在Ogre中加载自己的资源包
转自:http://www.cnblogs.com/minggoddess/archive/2011/02/19/1958472.html 由于数据保护的需要,一款游戏一般都会有自己独有的资源包,这样 ...
- [转]unity3d中创建双面材质
在其它三维软件中设置好的双面材质导入到unity3d中就失去了效果,不过我们可以通过自定义材质来在unity3d中实现双面材质的效果.步骤如下:1.在资源库中新建一新shader:代码如下: Shad ...
- Ogre参考手册(五)3.2 合成器
3.2 合成器Compositor 合成器框架是Ogre用于全屏后处理的API.你可以通过脚本而不是API定义合成器.你可以很容易为视口实例化合成器. 合成器基础 无论是要替换还是要与主渲染窗口混合, ...
- 转:OGRE 渲染通路(Pass)
一个渲染通路就是几何问题里的一次渲染:一个带有一整套渲染属性的渲染API的一次调用.一个技术可以包含有1到16个渲染通路,当然,渲染通路用得越多,技术在渲染的时候开销越大. 为了清楚识别使用的到底是哪 ...
- Ogre 学习记录
http://www.cppblog.com/richardhe/articles/55722.html 1: 设计初衷 它设计初衷是完全跨平台的.抽象的接口隐藏了平台相关的细节. 它设计初衷是大幅度 ...
- [比较老的文章]三维渲染引擎 OGRE 与 OSG 的比较综述
1 .引言随着计算机可视化.虚拟现实技术的飞速发展,人们对实时真实感渲染以及场景复杂度提出了更高的要求.传统的直接使用底层图形接口如OpenGL.DirectX开发图形应用的模式越来越暴露出开发复杂性 ...
- z-fighting在unity中的解决方式
如果在画面中,发现有画面闪烁的问题.那么大多数情况下是z-fighting引起的, 解决方案: 1, 在每个场景中,找到那个MainCamera,然后在Inspector上,找到MainCamera的 ...
随机推荐
- (C#) Tasks 中的异常处理(Exception Handling.)
多线程编程中要注意对线程异常的处理.首先写个例子. 一个线程用于显示信息(Show Messages).主线程用于做其他工作(Do Works). using (Task taskShowMessag ...
- Discuz有关问题解决办法汇总
1.Can not write to cache files, please check directory ./data/ and ./data/cache/ . 无法写入缓存 解决办法:在sour ...
- [家里蹲大学数学杂志]第013期2010年西安偏微分方程暑期班试题---NSE,非线性椭圆,平均曲率流,非线性守恒律,拟微分算子
Navier-Stokes equations 1 Let $\omega$ be a domain in $\bbR^3$, complement of a compact set $\mathca ...
- 利用反射及JDBC元数据编写通用查询方法
元数据:描述数据的数据,ResultSetMetaData是描述ResultSet的元数据对象,从它可以得到数据集有多少了,每一列的列名... ResultSetMetaData可以通过ResultS ...
- 【VB.NET】类绑定控件,实现文本框快捷键全选
Public Class KeyBinder Public Sub BindControl(ByRef CControl As TextBox) AddHandler CControl.KeyDown ...
- DCC Software and Graphics System
After working with DCC software for so many years, I saw the realtime solution went forward so much, ...
- Spark Streaming 事务处理彻底掌握
本期内容: 1. Exactly once容错 2. 数据输出不重复 一. 事务场景 : 以银行转帐一次为例,A用户转账给B用户,如何保证事务的一致性,即A用户能够转出且只能转出一次,B用户能够收到且 ...
- Application tried to present a nil modal view controller on target “Current View Controller”解决方案
情景再现 1,自定义一个storyboard: 打开xcode,按下cmd+N,新建一个Storyboard--->next 将新建立的storyboard命名为:TestViewControl ...
- Deep Learning(深度学习)学习笔记整理(二)
本文整理了网上几位大牛的博客,详细地讲解了CNN的基础结构与核心思想,欢迎交流. [1]Deep learning简介 [2]Deep Learning训练过程 [3]Deep Learning模型之 ...
- 通过WinForm控件创建的WPF控件无法输入的问题
今天把写的一个WPF程序发布到别的机器上执行,发现一个比较奇怪的问题:在那个机器上用英文输入法无法输入数字,非要切换到中文输入法才行:但在我的机器上却是好好的. 最开始以为是输入法的问题,弄了好一阵子 ...