对 gl.COLOR_BUFFER_BIT 的变量感到困惑

confusion about the variable of gl.COLOR_BUFFER_BIT

我目前正在学习Webgl,在下面的例子中,我对gl.COLOR_BUFFER_BIT的用法感到困惑:

  const canvas = document.querySelector("#glcanvas");
  // Initialize the GL context
  const gl = canvas.getContext("webgl");

  // Only continue if WebGL is available and working
  if (!gl) {
    alert("Unable to initialize WebGL. Your browser or machine may not support it.");
    return;
  }
  // Set clear color to black, fully opaque
  gl.clearColor(0.0, 0.0, 0.0, 1.0);
  gl.clear(gl.COLOR_BUFFER_BIT);
  console.log('1: ', gl.COLOR_BUFFER_BIT);

  // Clear the color buffer with specified clear color
  gl.clearColor(1, 1, 1, 1.0);
  gl.clear(gl.COLOR_BUFFER_BIT);
  console.log('2: ', gl.COLOR_BUFFER_BIT);

我对gl.clear(gl.COLOR_BUFFER_BIT)的理解是将gl.COLOR_BUFFER_BIT的值设置为gl.clearColor()中设置的颜色。

所以上面的两个console.log(gl.COLOR_BUFFER_BIT)应该输出不同的值。但实际输出如下:

1:  16384
2:  16384

所以这有什么问题吗?

COLOR_BUFFER_BIT是一个常量,用来告诉clear清除什么缓冲区,还有DEPTH_BUFFER_BITSTENCIL_BUFFER_BIT,这些值是位掩码,意味着你可以提供多个"clear targets" 通过二进制 ORing 它们,例如您可以通过调用 gl.clear(gl.COLOR_BUFFER_BIT|gl.DEPTH_BUFFER_BIT) 来清除颜色和深度缓冲区。调用clearColor设置the(只有一个)global clearcolor,还有clearDepthclearStencil设置各自全局值的函数。

换句话说,clear 实际上使用先前通过 clear**** 方法定义的值清除给定的目标,一旦设置这些值,这些值将一直存在,直到您设置另一个值。