OpenGL如何处理多个清晰的掩码?

时间:2016-05-05 11:18:38

标签: opengl

我已经使用OpenGL一段时间了,虽然我了解如何使用它,但我对它如何处理和理解多个蒙版非常感兴趣。例如:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
//How does it understand that I want to clear the
//color buffer and the depth buffer?

起初我以为他们可能会使用这样的静态变量:

GL_COLOR_AND_DEPTH_BUFFER_BIT = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT;

但后来我意识到他们需要数以百计才能拥有每一种可能的组合,这看起来很愚蠢。那么他们如何解释结果并找出我希望清除的两个面具呢?

1 个答案:

答案 0 :(得分:0)

正如Colonel Thirty Two所指出的,a GL implementation can use bit tests

void GLAPIENTRY
_mesa_Clear( GLbitfield mask )
{
   ...

   /* Accumulation buffers were removed in core contexts, and they never
    * existed in OpenGL ES.
    */
   if ((mask & GL_ACCUM_BUFFER_BIT) != 0
       && (ctx->API == API_OPENGL_CORE || _mesa_is_gles(ctx))) {
      _mesa_error( ctx, GL_INVALID_VALUE, "glClear(GL_ACCUM_BUFFER_BIT)");
      return;
   }

   ...

   if (ctx->RenderMode == GL_RENDER) {
      ...

      if ((mask & GL_DEPTH_BUFFER_BIT)
          && ctx->DrawBuffer->Visual.haveDepthBuffer) {
         bufferMask |= BUFFER_BIT_DEPTH;
      }

      if ((mask & GL_STENCIL_BUFFER_BIT)
          && ctx->DrawBuffer->Visual.haveStencilBuffer) {
         bufferMask |= BUFFER_BIT_STENCIL;
      }

      if ((mask & GL_ACCUM_BUFFER_BIT)
          && ctx->DrawBuffer->Visual.haveAccumBuffer) {
         bufferMask |= BUFFER_BIT_ACCUM;
      }

      assert(ctx->Driver.Clear);
      ctx->Driver.Clear(ctx, bufferMask);
   }
}

参见mask & GL_DEPTH_BUFFER_BIT - 类型测试。

#define GL_DEPTH_BUFFER_BIT   0x00000100 // == 0b000000100000000
#define GL_ACCUM_BUFFER_BIT   0x00000200 // == 0b000001000000000
#define GL_STENCIL_BUFFER_BIT 0x00000400 // == 0b000010000000000
#define GL_COLOR_BUFFER_BIT   0x00004000 // == 0b100000000000000