問題描述
GL_COLOR_BUFFER_BIT 再生哪個內存? (GL_COLOR_BUFFER_BIT regenerating which memory?)
這可能是所有 libgdx 應用程序都有的代碼:
Gdx.gl.glClearColor( 1, 0, 0, 1 );
Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT );
或
Gdx.gl.glClear(GL20.GL_DEPTH_BUFFER_BIT);
這將設置顏色與女巫屏幕將被刷新(第一行),而不是刷新它(第二行)。但是GL20.GL_COLOR_BUFFER_BIT
是什麼意思呢?從文檔中我了解到 GL 是一個包含 OpenGL ES 2.0 的所有方法的接口,它在那裡所以我可以調用方法。GL_COLOR_BUFFER_BIT
的含義讓我很困惑。它應該重新生成當前啟用用於彩色寫入的內存......這是否意味著它將擦除所有圖像?它會擦除 ShapeRenderer 對象嗎?屏幕上是否有任何不屬於彩色書寫的內容並且在使用此常量時不會被擦除?GL_DEPTH_BUFFER_BIT 是否會擦除紋理的 Z 位置?
參考解法
方法 1:
When you draw things on the screen you dont draw them directly. Instead they are first drawn to a so called "back‑buffer". This is a block of memory (a buffer) that contains four bytes for every pixel of the screen, one byte for each color component (red, green, blue and alpha) of each pixel. When you are ready drawing (when your render method finishes) this buffer is presented at once on the screen.
The existing value of the buffer is important. For example when you draw an image on the screen and then draw a semi transparent image on top of that, then the result is a mix of the two images. The first image is drawn to the back‑buffer causing the memory of the back‑buffer to contain the pixel data of that image. Next the second image is drawn and is blended on top of the existing data of the back‑buffer.
Each byte of the block of memory always has a value, e.g. 0 for black, 255 for white, etc. Even if you havent drawn anything to the buffer it has to have some value. Calling glClear(GL20.GL_COLOR_BUFFER_BIT)
instructs the GPU to fill the entire back buffer with some specified value (color). This value can be set using the call to glClearColor
. Note that you don't have to call glClearColor
each time glClear
is called, the driver will remember the previous value.
Besides the back‑buffer for the color values of the screen (the color buffer), the GPU can have other type of buffers, one of which is the depth buffer. This is again a block of memory of a few bytes per pixel, but this time it contains a depth value for each pixel. This makes it possible, e.g. when 3D rendering, to make sure that objects which are behind other objects are not being drawn. This buffer needs to be cleared using GL20.GL_DEPTH_BUFFER_BIT
.
Note that you can clear both of them together using a bitwise or operation: Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
In practice, calling glClear
should be the first thing you do in your render method (or when binding a FBO for example). This is because it tells the driver that you don't care about the existing values of the buffer. This allows the driver to do optimizations because it doesn't have to reconstruct (copy) the original memory block.