您的位置:首页 > 其它

透明物体的渲染

2016-06-26 10:33 573 查看
前段时间涉及到一个功能的开发,游戏中当模型首次出现在场景中加载的时候,需要有渐变出现的效果。当时是对模型材质进行替换为透明材质,然后对透明材质alpha做曲线变换。这个过程看似比较顺利,但是对于怎么渲染透明物体确是没有深入的研究。以后开发中需要多深入的了解过程中的一些隐藏的部分,这样能力才会有更多的提高。

---------------------------------------------------------分割线--------------------------------------------------------------

透明物体渲染会有什么问题?

如下图的对比所示,右边的透明物体渲染可以看出有穿插的部分。因为对于复杂的模型,本身即有层次性,另外GPU中可能对顶点或片段有某些操作引起透明片段的渲染顺序不正确。



解决问题的方法

首先看看shadow mapping的渲染,从中得到启发

shadow mapping:

Pass one: 从光的方向对场景渲染,保存此时的深度buffer

Pass two: 使用投影纹理把阴影图片投射到场景中,此时用到刚才的深度信息和距离r 比较,确定是否在阴影当中

depth peeling:

上述shadow mapping 方法得到的是靠近光源一层的深度,继续拓展下去,depth peeling 使用更多级的深度信息,n个pass可以得到n层的深度信息,如下所示,4个pass 之后得到4层的深度信息



更清晰的表示如下,先从最左边看,得到一个深度buffer,之后使用深度检测方法(小于等于)剥离该部分 ; 重复上述步骤



for(i=0; i< num_passes; i++)
{
clear color buffer
A = i%2
B = (i+1)%2
depth unit 0:
if(i == 0)
disable depth test
else
enable depth test
bind buffer A
disable depth writes
set depth func to GREATER
depth unit 1:
bind buffer B
clear depth buffer
enable depth writes
enable depth test
set depth func to LESS
render scene
save color buffer RGBA as layeri
}


补充下OpenGL 中glsl 代码:

/*

* check if the point p(in clip-space )is behind the lst depth value renderd

* if so, the color is unchaged, if not, the color is set to the background color

* and gl_FragDepth is adjusted

*/

void depthPeel(inout vec4 color, in vec4 p)

{

vec4 clipCoord = (p/p.w + 1.0)/2.0;

vec2 uv = clipCoord.xy;

float currDepth = gl_FragCoord.z - 1e-4;

flaot lastDepth = texexFetch(depthPeelMap, ivec2(uv * textureSize(depthPeelMap,0)),0).r;

// step is a campare function, return 0 or 1(bigger)

float discardFrag = step(currDepth, lastDepth);

// mix = color* (1- discardFrag)+ Back * discardFrag

color = mix(color, BACKGROUND_COLOR, discardFrag);

gl_FragDepth = mix(gl_FragCoord.z, 1.0, discardFrag);

}

Refference:
eng.utah.edu/~cs5610/handouts/order_independent_transparency.pdf
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: