OpenGL - 关于 glDepthMask 的使用问题 [英] OpenGL - Question about the usage of glDepthMask

查看:20
本文介绍了OpenGL - 关于 glDepthMask 的使用问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在场景中渲染了一个 objectA,如下所示.该场景还有许多其他对象.

I have rendered an objectA in a scene as follows. The scene has many other objects too.

void Draw()
{    
    if( glIsList( displayListID ) )
    {
        glPushAttrib( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_ENABLE_BIT );

        glEnable( GL_BLEND );
        glEnable( GL_DEPTH_TEST );
        //glDepthMask( GL_FALSE );
        glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );

        glEnable( GL_LINE_SMOOTH );
        glEnable( GL_POINT_SMOOTH );
        glEnable( GL_POLYGON_SMOOTH );

        glMatrixMode( GL_MODELVIEW ); 

        color.setAlpha(alpha); // set alpha transparent of this objectA
        glCallList( displayListID );

        //glDepthMask( GL_TRUE );
        glDisable( GL_BLEND );  

        glPopAttrib();
    }
}

问题来了,

如图,我注释掉两行//glDepthMask(GL_FALSE);//glDepthMask( GL_TRUE );

As shown, I comment out two lines //glDepthMask( GL_FALSE ); //glDepthMask( GL_TRUE );

场景在深度上正确渲染了 objectA 和其他对象.然而,objectA alpha 的修改不再起作用(即 color.setAlpha(alpha) ).

the scene renders the objectA and other objects correctly in depth. However, the modification of the objectA alpha doesn't work anymore (i.e. color.setAlpha(alpha) ).

如果我取消对以上两行的注释,则 alpha 修改将恢复工作.但是,深度渲染不正确.换句话说,有时,objectA 应该在其他对象的后面,但它看起来像 objectA 是在所有对象的前面.

If I uncomment the above two lines, then alpha modification is back to work. However, the depth rendering is NOT correct. In other words, sometimes, the objectA should be behind other objects but it looks like the objectA is in front of all objects.

我该如何解决这个问题?

How do I fix this problem?

谢谢

推荐答案

  1. 打开开启深度遮罩glDepthMask(GL_TRUE)
  2. 以任意顺序绘制所有不透明对象
  3. 关闭深度遮罩 glDepthMask( GL_FALSE )
  4. 开启BLEND_MODE
  5. 绘制从最远最近排序的半透明物体
  1. Turn on the depth mask glDepthMask( GL_TRUE )
  2. Draw all opaque objects, in any order
  3. Turn off the depth mask glDepthMask( GL_FALSE )
  4. Turn on a BLEND_MODE
  5. Draw translucent objects sorted from furthest away to nearest

你为什么要这样做?

您需要担心 2 个缓冲区:深度缓冲区颜色缓冲区.这些缓冲区实际上只是一个大的二维数组,每个数组的宽度 x 高度都是你的屏幕.

There are 2 buffers you need to worry about: the depth buffer and the color buffer. These buffers are really just big 2d arrays, each the width x height of your screen.

颜色缓冲区自然会保存每个像素的最终颜色.每个屏幕像素的颜色缓冲区中有一个条目.深度缓冲区,就像颜色缓冲区一样,每个屏幕像素有一个条目,但它用于不同的东西.深度缓冲区中的条目是衡量每个彩色像素真正接近"的程度.

The color buffer naturally is going to hold the final coloration of each pixel. There is one entry in the color buffer per screen pixel. The depth buffer, is like the color buffer in that there is one entry per screen pixel, but it is used for something different. Entries in the depth buffer are a measure of "how close" each colored pixel really is.

如果您渲染 1 个远离相机的三角形,它会为它想要"覆盖在屏幕上的每个像素生成一组颜色和深度值.假设你然后渲染另一个更接近的多边形,它也会为深度和颜色缓冲区生成一组值.现在,在像素着色时间有一种竞赛",其中较远"的片段(大深度缓冲区值)被丢弃,只保留最接近的片段.更近的片段最终会为您拥有的像素着色.(当两个多边形几乎重叠时,可能发生 Z-fighting)

If you render 1 triangle, that is far away from the camera, it generates a set of colors and depth values for each pixel it "wants" to cover on the screen. Say you then render another poly that is closer, it also will generate a set of values for the depth and color buffers. Now, there is a sort of "contest" at pixel coloration time where the "further away" fragments (large depth buffer values) are discarded, and only the closest fragments are kept. The closer fragments end up coloring the pixel you had. (When two polygons are nearly overlapping, Z-fighting can occur)

首先在启用深度蒙版的情况下渲染场景中的对象.这意味着您渲染的每个形状,当其像素着色时,深度缓冲区都会更新为竞赛的获胜者".

Start by rendering the objects in your scene with the depth mask on. This means every shape you render, when its pixels get colored, the depth buffer gets updated with the "winner" of the contest.

然后,您 3) glDepthMask( GL_FALSE ) 关闭关闭深度缓冲区以进行写入,4) 打开混合,5) 从最远到最近渲染半透明形状.看起来很奇怪吧?

Then, you 3) glDepthMask( GL_FALSE ) turns off the depth buffer for writing, 4) turn on blending, 5) render translucent shapes from furthest to nearest. Seems weird, huh?

当您关闭深度遮罩并渲染半透明形状时,OpenGL 仍会读取深度缓冲区以确定要丢弃哪些片段(即,如果您的半透明形状位于已渲染的实体后面)形状,然后您将那个半透明形状的碎片扔掉).但是它不会写入深度缓冲区,所以如果半透明形状真的很靠近眼睛(比如半透明挡风玻璃),那些挡风玻璃碎片不要阻止其他实际上距离更远的片段被绘制.这很重要,因为如果您的挡风玻璃就在您的正前方并且您将其渲染为半透明,并且您让挡风玻璃片段更新深度缓冲区,那么除了挡风玻璃之外,您将在场景中看不到其他任何东西,即使它背后有形状,因为 OpenGL 会认为嘿,由于这些深度缓冲区读数,挡风玻璃是用户唯一应该看到的东西,所以我不会费心渲染比挡风玻璃更远的任何东西."关闭深度遮罩是一种欺骗"OpenGL 到不知道"非常接近但半透明的片段的方式.

When you turn off the depth mask, and render the translucent shapes, OpenGL will still read the depth buffer to determine which fragments to throw away (i.e. if your translucent shape is behind an already rendered solid shape, then you throw that translucent shape's fragments away). But it will not write to the depth buffer, so if a translucent shape is really really close to the eye (say like a translucent windshield), those windshield fragments do not prevent other fragments that are actually further away from being drawn. This is important, because if your windshield is right in front of you and you render it translucent, and you let the windshield fragments update the depth buffer then you will see nothing else in your scene except the windshield, even though there are shapes behind it, because OpenGL will think "Hey, that windshield is the only thing the user should see, due to these depth buffer readings, so I won't bother rendering anything further away than this windshield, then." Turning off the depth mask is a way of "tricking" OpenGL into "not knowing" there are very close, but translucent, fragments.

这篇关于OpenGL - 关于 glDepthMask 的使用问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆