GL_COLOR_BUFFER_BIT regenerating which memory?

时间:2015-12-08 19:25:14

标签: opengl libgdx

This is the code that probably all libgdx apps have:

<!-- <!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>Default</title>
    <meta name="description" content="">
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <link href="./app.css" rel="stylesheet">
    <link href="//fonts.googleapis.com/css?family=Open+Sans:400italic,400,300,700" rel="stylesheet">
  </head>
  <body>
    <div class="container">
      <div id="registration-form"></div>
    </div>

    <script src="./app.js"></script>
  </body>
</html> -->

or

Gdx.gl.glClearColor( 1, 0, 0, 1 );
Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT );

This sets the color with witch the screen will be flushed(first line) and than flush it(second line). But what is the meaning of the Gdx.gl.glClear(GL20.GL_DEPTH_BUFFER_BIT); ? From the docs I got that GL is an interface wrapping all the methods of OpenGL ES 2.0 and it is there so I can call methods. The meaning of GL20.GL_COLOR_BUFFER_BIT is puzzling to me. It should regenerate memory currently enabled for color writing... Does it mean that it would erase all images? Will it erase ShapeRenderer objects? Is anything on the screen that isn't part of color writing and will not be erased when this constant is used? Does GL_DEPTH_BUFFER_BIT erases the Z-position of textures?

1 个答案:

答案 0 :(得分:5)

当您在屏幕上绘制内容时,不要直接绘制它们。相反,它们首先被吸引到所谓的“后缓冲区”。这是一块内存(缓冲区),每个像素包含四个字节,每个像素的每个颜色分量(红色,绿色,蓝色和alpha)一个字节。当您准备好绘图时(当渲染方法完成时),此缓冲区将立即显示在屏幕上。

缓冲区的现有值很重要。例如,当您在屏幕上绘制图像然后在其上绘制半透明图像时,结果是两个图像的混合。第一个图像被绘制到后缓冲区,导致后缓冲区的内存包含该图像的像素数据。接下来绘制第二个图像,并将其混合在后缓冲区的现有数据之上。

存储器块的每个字节总是具有一个值,例如0表示黑色,255表示白色等。即使您没有向缓冲区绘制任何内容,它也必须具有一些值。调用glClear(GL20.GL_COLOR_BUFFER_BIT)指示GPU使用某个指定值(颜色)填充整个后台缓冲区。可以使用对glClearColor的调用来设置此值。请注意,每次调用glClearColor时都不必调用glClear,驱动程序将记住之前的值。

除了屏幕颜色值的后缓冲区(颜色缓冲区)之外,GPU还可以有其他类型的缓冲区,其中一个是深度缓冲区。这又是每个像素几个字节的内存块,但这次它包含每个像素的深度值。这使得它成为可能,例如, 3D渲染时,确保没有绘制其他对象后面的对象。需要使用GL20.GL_DEPTH_BUFFER_BIT清除此缓冲区。

请注意,您可以使用按位或操作清除它们:Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

实际上,调用glClear应该是你在渲染方法中做的第一件事(或者在绑定FBO时)。这是因为它告诉驱动程序您不关心缓冲区的现有值。这允许驱动程序进行优化,因为它不必重建(复制)原始内存块。