Audio and video development series (7) - Opengl commonly used Api introduction part1

GLES20.glTexParameteri

GLES20.glTexParameteri is a function used by OpenGL ES 2.0 to set texture filters and texture wrapping modes. It has three parameters:

target parameter

The target parameter specifies the texture target to set the texture parameters. According to different target values, the behavior of the glTexParameteri function will be different. The following are several common target types and their application scenarios:
GLES20.GL_TEXTURE_2D: Two-dimensional texture target. Suitable for most texture maps, such as characters in games, maps, sky, etc.

GLES20.GL_TEXTURE_CUBE_MAP: Cube texture target. Suitable for rendering cubemaps, such as skyboxes in games, reflection maps, etc.

GLES20.GL_TEXTURE_EXTERNAL_OES: Used for external texture targets, generally used in media streaming, cameras and other scenarios. Textures can be output to the screen or to other textures using SurfaceTexture.

GLES20.GL_TEXTURE_3D: 3D texture target. Suitable for volume rendering, such as medical imaging, geological exploration, etc.

GLES31.GL_TEXTURE_BUFFER: Buffer texture target. Suitable for reading data from the buffer and rendering.

GLES31.GL_TEXTURE_RECTANGLE: Rectangular texture target. It can be used in scenes that need to quickly draw 2D graphics, such as UI rendering, HUD display, etc.

GLES30.GL_TEXTURE_2D_ARRAY: Two-dimensional array texture target. Suitable for rendering a large number of overlapping or different resolution textures, such as map tiles, satellite imagery, etc.

GLES31.GL_TEXTURE_CUBE_MAP_ARRAY: Cubemap array target. Suitable for rendering multiple cubemaps at the same time, such as dynamic sky in games, multi-view rendering, etc.

In short, different texture targets are suitable for different scenes, and developers need to select the appropriate target type according to specific needs to achieve the best rendering effect and performance.

pname

The parameter specifies the name of the texture parameter to be set, which can be one of the following values:
GL_TEXTURE_MIN_FILTER: Specifies how to filter the texture pixels when the texture is reduced, which can be GL_NEAREST (nearest neighbor filter), GL_LINEAR (linear filter), GL_NEAREST_MIPMAP_NEAREST (use the nearest Neighbor Filtered Mipmap Texture), GL_NEAREST_MIPMAP_LINEAR (Mipmap Texture Using Linear Filtering), GL_LINEAR_MIPMAP_NEAREST (Mipmap Texture Using Nearest Neighbor Filtering), or GL_LINEAR_MIPMAP_LINEAR (Mipmap Texture Using Linear Filtering).

GL_TEXTURE_MAG_FILTER: Specifies how to filter texels when the texture is enlarged, which can be GL_NEAREST (nearest neighbor filtering) or GL_LINEAR (linear filtering).

GL_TEXTURE_WRAP_S and GL_TEXTURE_WRAP_T: Specifies how texture coordinates should be handled when they are out of range, which can be GL_CLAMP_TO_EDGE (texture coordinates will be truncated to the maximum/minimum value within the range), GL_REPEAT (texture coordinates will be repeated) or GL_MIRRORED_REPEAT (texture coordinates will be repeated, but mirror flips on each repetition).

param

The parameter specifies the value of the texture parameter, which can have different values ​​according to the set parameters, for example:
GL_NEAREST: use the nearest neighbor filter.

GL_LINEAR: Use linear filtering.

GL_CLAMP_TO_EDGE: Texture coordinates are truncated to the max/min values ​​in range.

GL_REPEAT: Texture coordinates will be repeated.

GL_MIRRORED_REPEAT: Texture coordinates will be repeated, but mirrored on each repetition.

In summary, the GLES20.glTexParameteri function can be used to control how textures are filtered and packed, and how texture coordinates are handled when they are out of bounds.

    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)

Matrix.setIdentityM

Matrix.setIdentityM() is a matrix operation method of Android, which is used to set a matrix as the identity matrix. The identity matrix is ​​a square matrix in which all elements on the main diagonal are 1 and the rest are 0.

The full signature of the method is as follows:

public static void setIdentityM(float[] sm, int smOffset)

Among them, the parameter sm represents a one-dimensional array of float type, which is the target matrix to be set as the identity matrix. The parameter smOffset indicates the starting position of the target matrix to be set.

This method sets the elements of the destination matrix starting at the starting position as the identity matrix. Since array subscripts in Java start at 0, the index of the first element is smOffset, not 1.

Here is an example implementation of this method:

public static void setIdentityM(float[] sm, int smOffset) {
    for (int i = 0; i < 16; i++) {
        sm[smOffset + i] = ((i % 5 == 0) ? 1.0f : 0.0f);
    }
}

This implementation goes through all the elements of the matrix with a loop. For each element, set it to 1 if it is on the main diagonal, and 0 otherwise.

Using this method can conveniently set a matrix as an identity matrix, which facilitates subsequent matrix operations.

Matrix.setIdentityM(stMatrix, 0);

Sets the matrix stMatrix to be the identity matrix. An identity matrix is ​​a special kind of matrix that does not change the position or orientation of vertices.

Why call setIdentityM to set the identity matrix?
In OpenGL, the transformation matrix is ​​used to transform the vertices in the 3D scene. Each transformation operation (such as translation, rotation, scaling, etc.) can be expressed as a matrix, and these matrices can be combined to achieve complex transformation effects.

When combining transformation matrices, it must be ensured that the values ​​of the transformation matrices are correct in the initial state, that is, when no transformation is performed. If this is not the case, unexpected results may result when combining transform operations.

Therefore, it is usually necessary to initialize the transformation matrix to an identity matrix before setting up any other transformation operations. The identity matrix is ​​a special matrix that ensures that no unexpected transformations are applied to vertices when combining transformation operations.

Therefore, first set the stMatrix matrix to the identity matrix by calling the Matrix.setIdentityM(stMatrix, 0) method to ensure that the values ​​of the matrix are correct before applying any other transformations.

Do I need to call setIdentityM before every matrix operation?
In most cases, there is no need to call the setIdentityM method before each transformation matrix operation. Normally, you only need to call the setIdentityM method once to initialize the matrix when creating the transformation matrix.

In the next transformation operation, this matrix can be directly modified. For example, if you need to translate and rotate a vertex, you can first create a matrix, then use the translateM method to translate it, and use the rotateM method to rotate it. There is no need to call the setIdentityM method before each operation.

However, if you need to start over between multiple transformation operations, such as when working with multiple objects, you may need to reset the transformation matrix to the identity matrix and start applying the transformation operation again. In this case, the setIdentityM method can be used again to reset the matrix to the identity matrix.

Matrix.scaleM

Used to apply a scaling matrix to the given matrix. Its parameters are as follows:

public static void scaleM(float[] m, int mOffset, float x, float y, float z)

m: The matrix to be scaled, expressed as a length-16 floating-point array.
mOffset: The offset of the first element in the matrix.
x: scaling factor for the x-axis.
y: scaling factor for the y-axis.
z: scaling factor for the z-axis.

The meanings of these parameters are as follows:
m: This is an array of floating point numbers with a length of 16, indicating the matrix to be scaled. A scaling matrix multiplies each element of the matrix by a scaling factor such that each vector in the matrix is ​​scaled on the corresponding axis. The elements in the matrix are arranged as follows:

m[0]  m[4]  m[8]   m[12]
m[1]  m[5]  m[9]   m[13]
m[2]  m[6]  m[10]  m[14]
m[3]  m[7]  m[11]  m[15]

mOffset: This is the offset of the first element in the matrix. Set mOffset to 0 if you want to scale the entire matrix. If you only want to scale a part of the matrix, you can set mOffset to the corresponding offset.
x: This is the scaling factor to apply to the x-axis. The factor should be a floating point number, usually a number greater than zero. If the value is 1, no scaling will be done to the matrix. If the value is greater than 1, it will make the matrix expand in the x-axis direction; if the value is less than 1, it will make the matrix shrink in the x-axis direction.
y: This is the scaling factor to be applied to the y-axis, which has the same meaning as the x-axis.
z: This is the scaling factor to be applied to the z-axis, which has the same meaning as the x-axis.
Using these parameters, the Matrix.scaleM() method will compute a scaling matrix and apply it to the given matrix.

For the understanding of the mOffset parameter,
suppose we have a 4x4 matrix matrix, which represents the transformation matrix of an object, and we only want to scale the rotation part of the matrix without affecting the translation part of the matrix. At this time, we can set mOffset as the starting position of the rotation part in the matrix, and then call the Matrix.scaleM() method to scale the rotation part.

For example, suppose the first three columns in our matrix represent the rotated part of the object, while the last column represents the translated part of the object. We can set mOffset to 0, and then set the x, y, z scaling factors to 1, 2, 1, so that only the rotation part is scaled in the y-axis direction:

Matrix.scaleM(matrix, 0, 1, 2, 1);

This method multiplies all elements in the first three columns of the matrix by 1, 2, and 1, thereby scaling the rotated part in the y-axis direction. Since we set mOffset to 0, this operation will only affect the elements of the first three columns, not the elements of the last column. This way, we can scale only one part of the matrix without affecting other parts.

glGetUniformLocation

Uniform variables are global variables that are shared between shaders, and they can persist across multiple draw calls. You can use the glGetUniformLocation function to get the location of Uniform variables in order to update their values.

The glGetUniformLocation function is defined as follows:

GLint glGetUniformLocation(GLuint program, const GLchar* name);

Among them, the program parameter is the identifier of a connected shader program object, and the name parameter is the name of the Uniform variable. This function returns the location of the Uniform variable, or -1 if the Uniform variable was not found.

For example, if we have the following vertex shader:

#version 330

uniform mat4 modelViewProjectionMatrix;

layout(location = 0) in vec4 position;

void main()
{
    gl_Position = modelViewProjectionMatrix * position;
}

We can use the following code to get the position of the Uniform variable and update it to a new 4x4 matrix:

GLuint program = // 已连接的着色器程序对象
GLint matrixLocation = glGetUniformLocation(program, "modelViewProjectionMatrix");

float matrixData[16] = // 新的4x4矩阵数据
glUniformMatrix4fv(matrixLocation, 1, GL_FALSE, matrixData);

In this example, we first get the location of the Uniform variable using the glGetUniformLocation function. We then pass the new 4x4 matrix data to this Uniform variable using the glUniformMatrix4fv function. The glUniformMatrix4fv function specifies the location of the Uniform variable, the number of matrices to be updated, whether to transpose the matrix data, and the pointer to the matrix data.

In short, glGetUniformLocation is a useful function that enables us to dynamically update Uniform variables in OpenGL programs.

Guess you like

Origin blog.csdn.net/u012124438/article/details/129426746