Opengl es 每对象后处理
假设我需要渲染以下场景:Opengl es 每对象后处理,opengl-es,post-processing,Opengl Es,Post Processing,假设我需要渲染以下场景: 两个立方体,一个黄色,另一个红色 红色立方体需要用红光“发光”,黄色立方体不发光 立方体围绕着共同的重心旋转 摄像机被放置在一个位置 这样当红色发光的立方体靠近摄像机时, 它会部分阻挡黄色立方体,当黄色立方体 在靠近摄像机的地方,它部分挡住了红色发光的摄像机 如果没有辉光,场景渲染起来就很简单。使用光晕,我可以看到至少两种渲染方式: 方式1 将黄色立方体渲染到屏幕上 计算红色立方体在屏幕上的最终位置(简单,我们有顶点+模型视图矩阵),因此将其渲染到屏幕外 FBO刚好足够
a) 渲染带有FBO颜色附件纹理的四边形。
b) 适当地设置ModelView矩阵( 我们需要通过一些向量移动纹理,因为我们有意 在步骤2中,将红色立方体渲染为小于屏幕FBO的形状(对于 速度原因!)
c)在“合并”片段着色器中,我们需要编写 来自FBO深度附着纹理(而不是来自 FragCoord.z)
# 'Almost-Mesh-local' algorithm for rendering N different Meshes, some of them glowing
Create FBO, attach texture the size of the screen to COLOR0, another texture 1/4 the size of the screen to COLOR1.
Enable DEPTH test, clear COLOR/DEPTH
FOREACH( glowing Mesh )
{
use MRT to render it to COLOR0 and COLOR1 in one go
}
Detach COLOR1, attach STENCIL texture
Set up STENCIL so that the test always passes and writes 1s when Depth test passes
Switch off DEPTH/COLOR writes
FOREACH( glowing Mesh )
{
enlarge it by N% (amount of GLOW needs to be modifiable!)
render to STENCIL // i.e. mark the future 'glow' regions with 1s in stencil
}
Set up STENCIL so that test always passes and writes 0 when Depth test passes
Switch on DEPTH/COLOR writes
FOREACH( not glowing Mesh )
{
render to COLOR0/STENCIL/DEPTH // now COLOR0 contains everything rendered, except for the GLOW. STENCIL marks the unobstructed glowing areas with 1s
}
Blur the COLOR1 texture with BLUR radius 'N'
Merge COLOR0 and COLOR1 to the screen in the following way:
IF ( STENCIL==0 ) take pixel from COLOR0
ELSE blend COLOR0 and COLOR1
END
这不是网格局部(我们仍然需要能够首先处理所有“发光”网格),尽管我称之为“几乎网格局部”,因为它仅根据应用于网格的效果区分网格,而不是根据哪个网格在哪里或哪个网格阻挡了哪个网格
当两个发光的网格相互阻碍时(混合不必按正确的顺序进行),它也可能会出现问题。尽管发光是半透明的,我希望最终的外观会或多或少正常
看起来它甚至可以通过做一个巨大的运算变成一个完全的“网格局部”算法
FOREACH(Mesh)
{
if( glowing )
{
}
else
{
}
}
尽管在每次循环迭代中,必须从FBO连接和分离材料,并以不同的方式设置模具为代价。下意识的建议是进行混合: