GL_COLOR_BUFFER_BIT 再生哪個內存? (GL_COLOR_BUFFER_BIT regenerating which memory?)


問題描述

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.

(by potatoXoppa)

參考文件

  1. GL_COLOR_BUFFER_BIT regenerating which memory? (CC BY‑SA 2.5/3.0/4.0)

#libgdx #opengl






相關問題

為什麼原生 libmpg123 在帶有 libgdx 的 android 上花費這麼長時間? (Why is native libmpg123 taking so long on android with libgdx?)

睡眠後重新加載應用程序 (Re-load the app after sleep)

獲取實際觸摸位置 LibGDX (Get actual touch position LibGDX)

LibGDX - 只有可拖動的運動 (LibGDX - only draggable movement)

無法解析符號“android” - 在使用 libgdx 在 Android 應用程序中實現 Google Analytics 時 (Cannot resolve symbol 'android' - While implementing Google Analytics in Android App with libgdx)

如何從矩形數組中刪除隨機矩形? (How to remove random rectangle from rectangle array?)

球在傾斜平面上滾動 java libgdx (Ball rolling on an incline plane java libgdx)

libgdx - Intellij 類未找到異常? (libgdx - Intellij class not found exception?)

GL_COLOR_BUFFER_BIT 再生哪個內存? (GL_COLOR_BUFFER_BIT regenerating which memory?)

libGDX - 更改屏幕後的黑色按鈕和文本 (libGDX - Black buttons and text after changing screen)

帶有 9patch 的 LibGdx 標籤背景 (LibGdx label background with 9patch)

如何從平鋪中刪除對象? (How do I remove an object from tiled?)







留言討論