The picture on the screen display, is finally decoded into a bitmap, and then display. The storage space of a graph in the frame cache can be calculated according to the following formula,

Image storage space = image height * image width * bytes per pixelCopy the code

Texture is a kind of graphic data, mainly used for packaging different objects on the screen, just like the new house decoration, need to paste different wallpaper, at this time the wallpaper is what we call texture.

  • In OpenGL, textures are typically TGA files
  • In actual iOS development, we generally do not use OpenGL, but OpenGL ES, so that we can directly use COMPRESSED PNG and JPG images as texture data, which will eventually be decoded into bitmaps at the bottom and processed as textures.

Here are some common apis for textures

Common API for textures

There are three main steps to using textures

  • Read the file
  • Load the texture
  • Generate a texture object and set texture parameters

Read the file

  • Read file API
// Parameter 1:x, the window coordinates in the lower left corner of the rectangle // Parameter 2:y, the window coordinates in the lower left corner of the rectangle // Parameter 3:width, the width of the rectangle in pixels // Parameter 4:height, the height of the rectangle, // Parameter 5:format,OpenGL pixel format // parameter 6:type, Explain the data pointed to by the parameter Pixels. Tell OpenGL what data type in the cache is used to store color components. Void glReadPixels(GLint x,GLint y,GLSizei width,GLSizei height, GLenum format, GLenum type,const void * pixels); glReadBuffer(mode); - > Specify the cache glWriteBuffer(mode) to read; - > Specify the cache to write intoCopy the code

Refer to the table below for pixel format and pixel data type

Load the texture

GlTexImage2D is usually used because the graphics on the screen are 2D.

//target: specifies the texture mode to be applied to, usually GL_TEXTURE_2D // 'GL_TEXTURE_1D' // 'GL_TEXTURE_2D' // 'GL_TEXTURE_3D' //Level: specifies the MIP texture Level to be loaded. We usually set this parameter to 0. // internalFormat: How many color components to store in each texture unit. // Width, height, depth: indicates the width, height, and depth of the loaded texture. = = attention! These values have to be 2 to the whole power. (This is due to a requirement left over from older versions of OpenGL. Now, of course, it's possible to support an integer power other than 2. But developers are still used to setting these parameters to the power of 2.) //border: allows you to specify a border width for the texture map. If this is not specified, you can write 0. // Format parameter :OpenGL pixel format //type parameter: explains the data pointed to by the parameter Pixels, tells OpenGL what data type in the cache is used to store color components. The pixels parameter: pointer to graphic data void glTexImage1D(GLenum target,GLint level,GLint internalFormat,GLsizei Width,GLint border,GLenum format,GLenum type,void *data); void glTexImage2D(GLenum target,GLint level,GLint internalformat,GLsizei width,GLsizei height,GLint border,GLenum format,GLenum type,void * data); void glTexImage3D(GLenum target,GLint level,GLint internalformat,GLSizei width,GLsizei height,GLsizei depth,GLint border,GLenum format,GLenum type,void *data);Copy the code

Generate texture and set texture parameters

To generate the texture

There are two main steps to texture generation

  • Assigning texture objectsglGenTextures, abbreviated as Gen
  • Bind texture stateglBindTexture, called bind for short
// Use the function to allocate texture objects // specify the number of texture objects and Pointers (Pointers to an unsigned integer array filled with texture object identifiers). void glGenTextures(GLsizei n,GLuint * textTures); // Bind texture state // parameter target:GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D // parameter texture: the texture object to bind void glBindTexture(GLenum target,GLunit texture);Copy the code
Setting texture Parameters

These parameters are set through the following methods, which can be understood as the setting of rich text parameters in IOS NSAttribute by specifying parameters and setting corresponding values

// Parameter 1:target specifies which texture mode these parameters will be applied to, such as GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D. GlTexParameterf (GLenum target,GLenum pname,GLFloat param); glTexParameterf(GLenum target,GLenum pname,GLFloat param); glTexParameteri(GLenum target,GLenum pname,GLint param); glTexParameterfv(GLenum target,GLenum pname,GLFloat *param); glTexParameteriv(GLenum target,GLenum pname,GLint *param);Copy the code
  • Filtering mode Two filtering modes are commonly used: Adjacent filtering, linear filtering

==> Proximity filter (GL_NEAREST) : literally means to select the nearest color to the current location

==> Linear filter (GL_LINEAR) : all colors are integrated, similar to color mixing

==> Suggestion: Use neighborhood filter when zooming in texture, use linear filter when zooming in texture

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); 
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);

Copy the code
  • Surround mode The surround mode is used to display the edge of the texture when the texture coordinates are outside the default range. The surround mode is mainly set for the X and Y axes, and is used in the texture description instead of x, Y, but s, T. Note:S, T, R, q in the texture correspond to x, y, z, w in the coordinate system
GL_TEXTURE_2D: GL_TEXTURE_1D: 1d // GL_TEXTURE_2D: 2d // GL_TEXTURE_3D: 3D // Parameter 2: GL_TEXTURE_2D: 2d // // GL_TEXTURE_WRAP_S: x axis in the corresponding coordinate system // GL_TEXTURE_T: y axis in the corresponding coordinate system // GL_TEXTURE_R: Z-axis in the corresponding coordinate system // parameter 3: texture surround // GL_REPEAT:OpenGL repeats the texture above the texture coordinates of 1.0; // GL_CLAMP: The required texture cells are taken from the texture boundary or TEXTURE_BORDER_COLOR. // GL_CLAMP_TO_EDGE surround mode forces out-of-range texture coordinates to be sampled along the last row or column of a valid texture cell. // GL_CLAMP_TO_BORDER: only border texture units are used when texture coordinates are outside the 0.0 to 1.0 range. Boundary texture units are loaded with the base texture image as additional rows and columns around the base image. glTextParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAR_S,GL_CLAMP_TO_EDGE); glTextParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAR_T,GL_CLAMP_TO_EDGE);Copy the code

Texture-related reference table

Texture filter mode

Texture filter – constant instructions
GL_NEAREST Nearest neighbor filtering is performed at the Mip base
GL_LINEAR Perform linear filtering at the Mip base
GL_NEAREST_MIPMAP_NEAREST At the nearest Mip layer, and perform nearest neighbor filtering
GL_NEAREST_MIPMAP_LINEAR Linear interpolation is performed between the MIP layers and nearest neighbor filtering is performed
GL_LINEAR_MIPMAP_NWAREST Select the nearest Mip layer and perform linear filtering
GL_LINEAR_MIPMAP_LINEAR Perform linear interpolation between Mip layers and perform linear filtering, also known as trilinear Mip mapping

The way the texture wraps around

Around the way instructions
GL_REPEAT Default behavior for texture, repeat texture image
GL_MIRRORED_REPEAT Same as GL_REPEAT, but each repeat image is mirrored
GL_CLAMP_TO_EDGE The texture coordinates are constrained to between 0 and 1, and the excess portion repeats the edges of the texture coordinates, creating the effect of the edges being stretched
GL_CLAMP_TO_BORDER The coordinate beyond is the user-specified edge color

The effect of the four surround modes is shown below

OpenGL pixel format commonly used GL_RGB, GL_RGBA, etc

constant instructions
GL_RGB Describes the colors in red, green, and blue order
GL_RGBA Colors in red, green, blue, Alpha order
GL_BGR Arrange colors in blue, green, and red order
GL_BGRA Arrange colors in blue, green, red, Alpha order
GL_RED Each pixel contains only one red component
GL_GREEN Each pixel contains only one green component
GL_BLUE Each pixel contains only one blue component
GL_RG Each pixel in turn contains a red and green component
GL_RED_INTEGER Each pixel contains a red component in integer form
GL_GREEN_INTEGER Each pixel contains a green component in integer form
GL_BLUE_INTEGER Each pixel contains a blue component in integer form
GL_RG_INTEGER Each pixel in turn contains an integer red and green component
GL_RGB_INTEGER Each pixel contains an integer red, blue, and green component
GL_RGBA_INTEGER Each pixel contains an integer red, blue, green, and Alpah component
GL_BGR_INTEGER Each pixel contains an integer blue, green, and red component
GL_BGRA_INTEGER Each pixel contains an integer blue, green, red, and Alpah component
GL_STENCIL_INDEX Each pixel contains only one template value
GL_DEPTH_COMPONENT Each pixel contains only one depth value
GL_DEPTH_STENCIL Each pixel contains a depth value and a template value

The common book type for OpenGL pixel data is GL_UNSIGNED_BYTE

constant instructions
GL_UNSIGNED_BYTE Each color component is an 8-bit unsigned integer
GL_BYTE An 8-bit signed integer
GL_UNSIGNED_SHORT 16 – bit unsigned integer
GL_SHORT 16 – bit signed integer
CL_UNSIGNED_INT 32 – bit unsigned integer
GL_INT 32 – bit signed integer
GL_FLOAT Single-precision floating point number
GL_HALF_FLOAT Semi-precision floating point number
GL_UNSIGNED_BYTE_3_2_3 RGB value for packaging
GL_UNSIGNED_BYTE_2_3_3_REV RGB value for packaging
GL_UNSIGNED_SHORT_5_6_5 RGB value for packaging
GL_UNSIGNED_SHORT_5_6_5_REV RGB value for packaging
GL_UNSIGNED_SHORT_4_4_4_4 RGB value for packaging
GL_UNSIGNED_SHORT_4_4_4_4_REV RGB value for packaging
GL_UNSIGNED_SHORT_5_5_5_1 RGB value for packaging
GL_UNSIGNED_SHORT_1_5_5_5_REV RGB value for packaging
GL_UNSIGNED_INT_8_8_8_8 RGB value for packaging
GL_UNSIGNED_INT_8_8_8_8_REV RGB value for packaging
GL_UNSIGNED_INT_10_10_10_2 RGB value for packaging
GL_UNSIGNED_INT_2_10_10_10_REV RGB value for packaging
GL_UNSIGNED_INT_24_8 RGB value for packaging
GL_UNSIGNED_INT_10F_11F_REV RGB value for packaging
GL_FLOAT_24_UNSIGNED_INT_24_8_REV RGB value for packaging

Other texture-related apis

Change/Restore how pixels are stored

This API is used sparingly, and generally does little to change the way pixels are stored in OpenGL, mostly using the default

API instructions
void glPixelStorei(GLenum pname,GLint param); Change the way pixels are stored
void glPixelStoref(GLenum pname,GLfloat param); Restore the pixel storage mode
  • Parameter 1: How does OpenGL unpack image data from the data cache, i.e. how pixels are stored
  • Parameter 2: Set the specific arrangement of storage modes in parameter 1, that is, the arrangement of pixels after reading

For example: glPixelStorei (GL_UNPACK_ALIGNMENT, 1);

  • Parameter 1:GL_UNPACK_ALIGNMENTRepresents a permutation request at the start of each pixel row in memory, allowing the following values to be set
    • 1: byte array
    • 2: rows of even bytes
    • 4: Word arrangement
    • 8: Lines start at a double-byte boundary
  • Parameter 2: specifiesGL_UNPACK_ALIGNMENTSet the value of the

Load texture other apis

Update the texture

For descriptions of the parameters, refer to the loading texture API parameters

void glTexSubImage1D(GLenum target,GLint level,GLint xOffset,GLsizei width,GLenum format,GLenum type,const GLvoid *data);

void glTexSubImage2D(GLenum target,GLint level,GLint xOffset,GLint yOffset,GLsizei width,GLsizei height,GLenum format,GLenum type,const GLvoid *data);

void glTexSubImage3D(GLenum target,GLint level,GLint xOffset,GLint yOffset,GLint zOffset,GLsizei width,GLsizei height,GLsizei depth,Glenum type,const GLvoid * data);

Copy the code

Insert and replace texture

For descriptions of the parameters, refer to the loading texture API parameters

void glCopyTexSubImage1D(GLenum target,GLint level,GLint xoffset,GLint x,GLint y,GLsize width);

void glCopyTexSubImage2D(GLenum target,GLint level,GLint xoffset,GLint yOffset,GLint x,GLint y,GLsizei width,GLsizei height);

void glCopyTexSubImage3D(GLenum target,GLint level,GLint xoffset,GLint yOffset,GLint zOffset,GLint x,GLint y,GLsizei width,GLsizei height);

Copy the code

Use the color buffer to create a new texture in the data

  • X and y in the function are the locations specified in the color cache to start reading texture data
  • The data in the buffer is passed through the source bufferglReadBufferSet up the
void glCopyTexImage1D(GLenum target,GLint level,GLenum internalformt,GLint x,GLint y,GLsizei width,GLint border);

void glCopyTexImage2D(GLenum target,GLint level,GLenum internalformt,GLint x,GLint y,GLsizei width,GLsizei height,GLint border);

Copy the code

Note: glCopyTexImage2D does not exist, mainly because volume data cannot be retrieved from the 3D color cache

Texture object other APIS

Delete the binding texture and test whether the binding texture is valid

// Delete the binding texture object // Texture object and texture object pointer (the pointer points to an unsigned integer array filled with texture object identifiers). void glDeleteTextures(GLsizei n,GLuint *textures); The function returns GL_TRUE if the texture is a texture object that has already allocated space, GL_FALSE otherwise. GLboolean glIsTexture(GLuint texture);Copy the code