opengles texture object-texture unit

Keywords: Fragment

When using textures, there is a question why these methods can create textures and use them. Here we will discuss.
Let's take a look at the general steps for using two-dimensional textures:

// Generate texture
int[] textureHandle = new int[1];
GLES20.glGenTextures(1, textureHandle, 0);

int TextureID = textureHandle[0];
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, TextureID);

GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);

GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

// In onDrawFrame
GLES20.glUniform1i(mTextureHandler, 0);

// In fragment shader
uniform sampler2D sTexture;
vec4 color = texture2D(sTexture, vTextureCoord);

Firstly, the glGenTextures method is used to create texture objects, which are cached in the memory of the GPU, and many texture objects can be cached in the GPU at the same time.
The id number of the texture object is obtained, and then the glBindTexture method is called to bind the texture object generated just now to the texture unit. In fact, before calling glBindTexture, a glActive Texture (GL_TEXTURE0) is written, which means activating the texture unit 0. If it is not written, the default texture unit 0 is used.
The following glTexParameteri is to set the bound texture object.
Then call texImage2D to store bitmap in the corresponding texture object. After configuring the above settings, fragment shader is told to use texture unit 0 through glUniform 1i, so a 0 parameter is passed to sampler2D, and the sampler will sample in texture unit 0.

If we need to do multi-texture processing, we need to do this:

// Create two texture objects
int[] textureHandle = new int[2];
GLES20.glGenTextures(2, textureHandle, 0);

// Activate Texture Unit 0
glActiveTexture(GL_TEXTURE0);
// Binding Texture Object and Texture Unit 0
glBindTexture(GL_TEXTURE2D,textureHandle[0]);
// Store bitmap in the display memory corresponding to texture object
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap1, 0);

// Activating Texture Unit 1
glActiveTexture(GL_TEXTURE1);
// Binding Texture Objects and Texture Unit 1
glBindTexture(GL_TEXTURE2D,textureHandle[1]);
// Store bitmap in the display memory corresponding to texture object
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap2, 0);

// Setting Texture Sampler to Sample in Texture Unit 0
GLES20.glUniform1i(mTextureHandler1, 0);
GLES20.glUniform1i(mTextureHandler2, 1);

...

A graph is drawn to show the relationship between them (opengl is made up of 32 texture units)

The glTexSubImage 2D can also be used to modify the display content in the texture object.

Posted by kkeim on Mon, 03 Jun 2019 22:17:17 -0700