Details about the Texturing texture of OpenGL ES 3.0 (2), opengltexturing

Source: Internet
Author: User

Details about the Texturing texture of OpenGL ES 3.0 (2), opengltexturing

Texture Filtering and Mipmapping Texture Filtering and multi-level Texture

We have already introduced the 2D texture of a single 2D image. This article mainly describes multi-level textures. Texture coordinates are used to generate a 2D index. When the zoom-in and zoom-out values are set to GL_NEAREST, a single texture will be matched to the texture coordinate position, which is a sampling of the closest point.

When using a multi-level texture, we can set the filtering mode to achieve a more appropriate proportion of screen pixels and texture image pixels, reducing the amount of sawtooth. Because of the successful filtering of multi-level texture textures, when we look at the image from a farther distance, we move to the texture chain to reduce the Sawtooth to achieve high-quality images.

// Load mipmap level 0glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height,0, GL_RGB, GL_UNSIGNED_BYTE, pixels);level = 1;prevImage = &pixels[0];while(width > 1 && height > 1){  int newWidth,  newHeight;  // Generate the next mipmap level  GenMipMap2D( prevImage, &newImage, width, height, &newWidth,  &newHeight);  // Load the mipmap level  glTexImage2D(GL_TEXTURE_2D, level, GL_RGB,  newWidth, newHeight, 0, GL_RGB,  GL_UNSIGNED_BYTE, newImage);  // Free the previous image  free(prevImage);  // Set the previous image for the next iteration  prevImage = newImage;  level++;  // Half the width and height  width = newWidth;  height = newHeight;}free(newlmage);

GenMipMap2D is used to implement multi-level textures. There are two types of texture filtering: zoom in and out. When the size of the polygon designed on the screen is smaller than that of the texture image, we use a reduced texture. Otherwise, use amplification. The filtering type is automatically selected by the specific hardware, but the API also provides filtering control. The amplification process is irrelevant because we always use the largest
Available level. For downgrading, there are various sampling modes available. The usage of which mode is determined based on the visual quality you need to implement and the texture filter of the desired performance. Use glTexParameter [I | f] [v] to specify the filter mode.

  

  

The amplification filter can be GL_NEAREST or GL_LINEAR. In GL_NEAREST zoom-in filtering mode, texture coordinates are generated at the most recent single point of the texture. In GL_LINEAR mode, bilinear (four-point average) serves as the texture coordinate.

The following values can be used to narrow down the filter:

• GL_NEAREST-Takes a single point sample from the texture nearest to the texture coordinate.

• GL_LINEAR-Takes a bilinear sample from the texture nearest to the texture coordinate.

• GL_NEAREST_MIPMAP_NEAREST-Takes a single point sample from the closest mip level chosen.
• GL_NEAREST_MIPMAP_LINEAR-Takes a sample from the two closest mip levels and interpolates between those samples.
• GL_LINEAR_MIPMAP_NEAREST-Takes a bilinear fetch from the closest mip level chosen.
• GL_LINEAR_MIPMAP_LINEAR-Takes a bilinear fetch from each of the two closest mip levels and then interpolates between them. this last mode, which is typically referred to as trilinear filtering, produces the best quality of all modes.

GL_NEAREST and GL_LINEAR are the only filtering modes that do not require complete multi-level textures. The rest require complete multi-level processing.

  

Filter settings for GL_NEAREST and GL_LINEAR_MIPMAP_LINEAR.

It is worth mentioning that some performance will affect the texture filtering mode you choose. For most hardware, using multi-level textures is the best choice.

Seamless Cubemap Filtering

It is a new feature of 3.0. When a linear filter core is in the border of a cube texture, this filter only takes place on the side of the cube where the line is located. You do not need to set Seamless Cubemap Filtering. linear Filtering will automatically use it.

  

Automatic Multi-Level texture generation

We have created a multi-level texture with a level of 0. This is a method. In addition, the glGenerateMipmap function for Automatic Multi-Level texture generation is provided.

  

The texture object bound by our team calls glGenerateMipmap, which generates a multi-level texture chain from the original image to level 0 for us. When you use a framebuffer object, automatic multi-level texture generation is particularly important. When rendering a texture, we do not want to read the texture back to the CPU to generate a multi-level texture. GlGenerateMipmap can solve this problem.

 

Texture Coordinate Wrapping

When the texture coordinates exceed the range [0.0, 1.0], use texture packaging. The texture packaging mode is specified using glTexParameter [I | f] [v.

  

The texture packaging mode can be independently set to s and t coordinates. The GL_TEXTURE_WRAP_S mode defines that the s coordinate is out of the range [0.0, 1.0], and the GL_TEXTURE_WRAP_T sets that the t coordinate is out of the range [0.0, 1.0. Three packaging modes are available.

  

Note that the texture packaging mode affects the filtering behavior. For example, when the texture coordinate is an edge, bilinear filtering scans the edge of the texture. In this case, the packaging mode will decide which texture is outside the texture edge and apply it to the filtering algorithm. If you do not want any form of repetition, use GL_CLAMP_TO_EDGE.

Is to use textures to draw squares in three packaging Modes

  

//void Draw ( ESContext *esContext ){   UserData *userData = esContext->userData;   GLfloat vVertices[] = { -0.3f,  0.3f, 0.0f, 1.0f,  // Position 0                           -1.0f,  -1.0f,              // TexCoord 0                           -0.3f, -0.3f, 0.0f, 1.0f, // Position 1                           -1.0f,  2.0f,              // TexCoord 1                           0.3f, -0.3f, 0.0f, 1.0f, // Position 2                           2.0f,  2.0f,              // TexCoord 2                           0.3f,  0.3f, 0.0f, 1.0f,  // Position 3                           2.0f,  -1.0f               // TexCoord 3                         };   GLushort indices[] = { 0, 1, 2, 0, 2, 3 };   // Set the viewport   glViewport ( 0, 0, esContext->width, esContext->height );   // Clear the color buffer   glClear ( GL_COLOR_BUFFER_BIT );   // Use the program object   glUseProgram ( userData->programObject );   // Load the vertex position   glVertexAttribPointer ( 0, 4, GL_FLOAT,                           GL_FALSE, 6 * sizeof ( GLfloat ), vVertices );   // Load the texture coordinate   glVertexAttribPointer ( 1, 2, GL_FLOAT,                           GL_FALSE, 6 * sizeof ( GLfloat ), &vVertices[4] );   glEnableVertexAttribArray ( 0 );   glEnableVertexAttribArray ( 1 );   // Bind the texture   glActiveTexture ( GL_TEXTURE0 );   glBindTexture ( GL_TEXTURE_2D, userData->textureId );   // Set the sampler texture unit to 0   glUniform1i ( userData->samplerLoc, 0 );   // Draw quad with repeat wrap mode   glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );   glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );   glUniform1f ( userData->offsetLoc, -0.7f );   glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices );   // Draw quad with clamp to edge wrap mode   glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );   glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );   glUniform1f ( userData->offsetLoc, 0.0f );   glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices );   // Draw quad with mirrored repeat   glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT );   glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT );   glUniform1f ( userData->offsetLoc, 0.7f );   glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices );}

 

 

  

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.