总之,我们在shader中经常会用到场景的深度数据,这里介绍相关的坐标计算过程和深度使用的技巧。数学计算记录在http://www.songho.ca/opengl/g...这里是投影矩阵的具体计算顶点计算过程:相机空间中的一个点[x,y,z,1]通过通过投影矩阵计算点--[xxx,xxx,Az+B,-z]--//投影空间中的点A=-(f+n)/(f-n)B=-2fn/(f-n)然后通过透视除法得到----------------------z_n=-A/z-B----------NDC中的//zspacez_n[-1,1]depthBuffer存储的深度值范围为[0,1]-------------z_b=z_n*0.5+0.5;------//zindepthBuffer/Texturez_b[0,1]计算:z_viewtoz_b:z_b=(far(z_view+near))/(z_view(far-near));//------------range:[0,1]z_btoz_view:z_view=(nearfar)/((far-near)z_b-far);//--------range:[-near,-far]参考https://stackoverflow.com/que...http://olivers.posterous.com/...存储深度数据的介绍使用DEPTHTEXTURE存储深度信息使用colorencode存储深度信息使用rgba存储深度数据vec2PackDepth16(infloatdepth){floatdepthVal=depth*(256.0*256.0-1.0)/(256.0*256.0);vec3encode=fract(depthVal*vec3(1.0,256.0,256.0*256.0));返回encode.xy-encode.yz/256.0+1.0/512.0;}floatUnpackDepth16(invec2pack){floatdepth=dot(pack,1.0/vec2(1.0,256.0));返回深度*(256.0*256.0)/(256.0*256.0-1.0);}vec4PackDepth32_orig(infloatfrag_depth){vec4bitSh=vec4(256.0*256.0*256.0,256.0*256.0,256.0,1.0);vec4bitMsk=vec4(0.0,1.0/256.0,1.0/256.0,1.0/256.0);vec4enc=fract(frag_depth*bitSh);enc-=enc.xxyz*bitMsk;returnenc;}floatUnpackDepth32_orig(constinvec4enc){constvec4bit_shift=vec4(1.0/(256.0*256.0*256.0),1.0/(256.0*256.0),1.0/256.0,1.0);floatdecoded=dot(enc,bit_shift);returndecoded;}参考https://stackoverflow.com/que...http://aras-p.info/blog/2009/...
