glClear函数:关于参数的问题

发布于 2024-12-04 16:37:24 字数 697 浏览 5 评论 0原文

我想深入了解 glClear 函数。我明白它的一般解释 ->清除颜色、深度、模板和累积的缓冲区,但我还有其他问题。 我的朋友假设您清除了代表颜色、深度、模板和内存中累积(堆栈?)的位。通过指定和应用参数:(例如,仅颜色和深度)“掩码”,您仅清除内存中的那些位(因此是“按位运算”)。

举个例子:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

www.khronos.org 对“mask”参数的解释。 mask:掩码的按位或,指示要清除的缓冲区。

我的问题是:

  • 参数 GL_COLOR_BUFFER_BIT 和 GL_DEPTH_BUFFER_BIT 分别代表 1 位内存吗?或者它们由多少位表示(我想了解位掩码的实际应用方式)?
  • 我不明白“OR”掩码如何取消位?如果对标记为“设置”(而不是“未设置”)的位使用“OR”,它仍然会返回零(1 OR 0 仍然返回 1)?我是否认为这是完全错误的/我在这里错过了什么吗?
  • “或”符号(管道)是怎么回事?为什么这些参数没有用通常的逗号分隔,并且按位运算“OR”不是在实际函数中声明的吗?

也许我把事情搞混了,因为我是这个领域的新手。 您能给我一个详尽的解释吗?当我继续学习 OpenGL 时,我不想跳过这些问题;我想知道我在做什么,这种理解可能会帮助我一路走来。谢谢!

I would like to understand the glClear function to its profound level. I understand its general explanation -> clear the buffers for color, depth, stencil and accumulation, but I have additional questions.
My friend assumed that you clear the bits that represent the color, depth, stencil and accumulation in the memory (stack?). By specifying and applying the parameters: (eg. only color and depth) 'masks', you clear only those bits in the memory (hence 'bitwise operation').

Take the example:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

Explanation of the parameters by www.khronos.org of "mask".
mask: Bitwise OR of masks that indicate the buffers to be cleared.

Here are my questions:

  • Are the parameters GL_COLOR_BUFFER_BIT and GL_DEPTH_BUFFER_BIT representing 1 bit of memory each? Or how many bits are they represented by (I'd like to understand how the bit mask is actually applied)?
  • I don't see how the "OR" mask can cancel bits? If you use "OR" on a bit that is flagged "set" (instead of "unset"), it will still return zero (1 OR 0 still returns 1)? Do I see this entirely wrong / am I missing something here?
  • What is up with the "or" sign (pipe); why are these parameters not separated by a usual comma and isn't the bitwise operation "OR" just declared in the actual function?

Perhaps I am confusing things, as I am new in this field.
Could you please provide me an exhaustive explanation? I prefer not to skip these questions as I move on in OpenGL; I want to know what I am doing and having this understanding may help me along the way. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

作业与我同在 2024-12-11 16:37:24

当您指定要清除的内容时,编写方式可以提供更大的灵活性。这里是如何定义标志的:

#define GL_COLOR_BUFFER_BIT 1 // 0000 0001
#define GL_DEPTH_BUFFER_BIT 2 // 0000 0010

如您所见,这些是 2 的幂。这样,在内存中,每个标志只有一位设置为 1(显然在不同的位置)。
当您对这些标志计算按位“或”时,您会得到 0000 0011。要知道结果值中是否设置了标志,您只需与检查的标志计算按位“与”即可。

int foo = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT; // foo = 0000 0011

if (foo & GL_COLOR_BUFFER_BIT) { // 0000 0011 & 0000 0001 => 0000 0001 (!= 0)
    // clear color buffer(which is located at a position OpenGL knows)
}
if (foo & GL_DEPTH_BUFFER_BIT) { // 0000 0011 & 0000 0010 => 0000 0010 (!= 0)
    // clear depth buffer
}

The way of writing things permits more flexibility when you're specifying what you want to clear. Here how the flags could be defined:

#define GL_COLOR_BUFFER_BIT 1 // 0000 0001
#define GL_DEPTH_BUFFER_BIT 2 // 0000 0010

As you can see, those are power of 2. This way, in memory only one bit is set to 1 for each flag (at different positions, obviously).
When you're computing a bitwise OR on those flags, you get 0000 0011. To know if a flag is set in the resulting value, you just need to compute a bitwise AND with the checked flag.

int foo = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT; // foo = 0000 0011

if (foo & GL_COLOR_BUFFER_BIT) { // 0000 0011 & 0000 0001 => 0000 0001 (!= 0)
    // clear color buffer(which is located at a position OpenGL knows)
}
if (foo & GL_DEPTH_BUFFER_BIT) { // 0000 0011 & 0000 0010 => 0000 0010 (!= 0)
    // clear depth buffer
}
零時差 2024-12-11 16:37:24

您提供给 glClear 的按位组合不是清除缓冲区的位。缓冲区使用其特定的透明颜色(glClearColor)或透明深度值(glClearDepth,我认为?)单独清除。 glClear 的按位标志仅告诉它要清除哪些缓冲区。这些按位或简单地指定多个要清除的缓冲区。

编辑:您可以想象它的工作方式如下:

void glClear(unsigned int bits)
{
    if(bits & GL_COLOR_BUFFER_BIT)    //color bit is set
    {
        //clear color buffer using current clear color
    }
    if(bits & GL_DEPTH_BUFFER_BIT)    //depth bit is set
    {
        //clear depth buffer using current clear depth value (usually 1)
    }
    if(bits & GL_STENCIL_BUFFER_BIT)  //stencil bit is set
    {
        //clear stencil buffer using current clear stencil value (usually 0)
    }
}

The bitwise combination you give to glClear are not the bits that the buffers are cleared with. The buffer are cleared indivually with their specific clear color (glClearColor) or clear depth value (glClearDepth, I think?). The bitwise flags for glClear only tell it what buffers to clear. These are ored bitwise to simply specify more than one buffer to clear.

EDIT: You can imagine it to work like:

void glClear(unsigned int bits)
{
    if(bits & GL_COLOR_BUFFER_BIT)    //color bit is set
    {
        //clear color buffer using current clear color
    }
    if(bits & GL_DEPTH_BUFFER_BIT)    //depth bit is set
    {
        //clear depth buffer using current clear depth value (usually 1)
    }
    if(bits & GL_STENCIL_BUFFER_BIT)  //stencil bit is set
    {
        //clear stencil buffer using current clear stencil value (usually 0)
    }
}
红墙和绿瓦 2024-12-11 16:37:24

正如其他人所说,那些 GL_COLOR_BUFFER_BIT 和相关位掩码与写入各种缓冲区的最终清除值无关。每个只是一个标志,glClear() 在内部检查(使用按位 AND,正如其他人指出的那样)来决定对哪些缓冲区进行操作。

从概念上讲,一旦检查了缓冲区标志,glClear() 就会循环遍历帧缓冲区中的每个像素,并将其设置为“清除”值,这样您就有了一个可以绘制的空白石板。这些值是用glClearColor()等设置的。你可能会这样想象:

void glClear(GLuint buffers)
{
  if (buffers & GL_COLOR_BUFFER_BIT) {
    for (int i = 0; i < height; ++i) {
      for (int j = 0; j < width; ++j) {
        colorBuffer[i][j].r = clearColor.r;
        colorBuffer[i][j].g = clearColor.g;
        colorBuffer[i][j].b = clearColor.b;
        colorBuffer[i][j].a = clearColor.a;
      }
    }
  }

  if (buffers & GL_DEPTH_BUFFER_BIT) {
    // Etc, using depthBuffer and glClearDepth value instead here
  }

  // etc. for accum & aux buffers.
}

As others have said, those GL_COLOR_BUFFER_BIT and related bit masks have nothing to do with the eventual clear values written to various buffers. Each is simply a flag that glClear() checks internally (using bitwise AND as others have pointed out) to decide which buffers to act on.

Once the buffer flags have been checked, conceptually, glClear() just loops over every pixel in the frame buffer and sets it to the "clear" value so you have a blank slate to draw on. Those values are set with glClearColor() and the like. You might imagine it like this:

void glClear(GLuint buffers)
{
  if (buffers & GL_COLOR_BUFFER_BIT) {
    for (int i = 0; i < height; ++i) {
      for (int j = 0; j < width; ++j) {
        colorBuffer[i][j].r = clearColor.r;
        colorBuffer[i][j].g = clearColor.g;
        colorBuffer[i][j].b = clearColor.b;
        colorBuffer[i][j].a = clearColor.a;
      }
    }
  }

  if (buffers & GL_DEPTH_BUFFER_BIT) {
    // Etc, using depthBuffer and glClearDepth value instead here
  }

  // etc. for accum & aux buffers.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文