Skip to content
Search

GlTexParameteri

lastmod 2024-06-14created 2021-05-083 edits

OpenGL BMP texture loading.

About

The items you can set with the glTexParameter function are GL_TEXTURE_MIN_FILTER, GL_TEXTURE_MAG_FILTER, GL_TEXTURE_WRAP_S, GL_TEXTURE_WRAP_T, and GL_GENERATE_MIPMAP. What do these items mean? You can answer that once you understand what the glTexParameter function does.

Texture mapping is the process of mapping a 2D image onto 3D geometry. Tutorial 10 explained that because 3D geometry has depth and curvature, a 2D image cannot be pixel-mapped 1:1 onto 3D geometry. So you need a method for deciding which pixels to take from the 2D image, and how, to map onto the 3D geometry. If we call this sampling, the glTexParameter function sets how the pixels needed for the 3D geometry are sampled from the 2D image. The sampling method, optimizations for sampling, and so on are set through the glTexParameter function.

GL_TEXTURE_MIN_FILTER/GL_TEXTURE_MAG_FILTER

Even when the same 2D image is texture-mapped onto the same 3D geometry, its size changes with the depth of the geometry. That is, it looks large when close to the screen and small when far from it.

A nearby quad looks large and a distant quad looks small. If the 2D image were mapped 1:1, the near quad would have many gaps and the distant quad would have clumped pixels. GL_TEXTURE_MIN_FILTER and GL_TEXTURE_MAG_FILTER choose which part of the image to take pixels from as the geometry is magnified or minified.

GL_TEXTURE_MIN_FILTER.png

The figure above shows GL_TEXTURE_MIN_FILTER, which applies when the geometry (polygon) is smaller than the texture (image data extracted from the 2D image by S,T coordinates). If GL_TEXTURE_MIN_FILTER is GL_NEAREST, the texture pixel closest to the red dot (the center of the geometry's pixel) is chosen, so pixel 17 is used; if GL_LINEAR, the average of pixels 17, 18, 12, and 13 is used.

GL_TEXTURE_MAG_FILTER.png

The figure above shows GL_TEXTURE_MAG_FILTER, which applies when the geometry is larger than the texture. If GL_TEXTURE_MAG_FILTER is GL_NEAREST, the texture pixel closest to the red dot (the center of the geometry's pixel) is chosen, so pixel 10 is used; if GL_LINEAR, the average of pixels 13, 14, 9, and 10 is used.

Changing the values:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// ...
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

When you compile and run it, you get a result with heavy aliasing, as in the left image. If you set the value to GL_LINEAR and run it, the result comes out smooth, as in the right image.

GL_NEAREST-vs-GL_LINEAR.png

GL_TEXTURE_MIN_FILTER and GL_TEXTURE_MAG_FILTER don't always have to be set to the same value. Aliasing matters little when minified, so you can use GL_NEAREST there, and use GL_LINEAR so that it looks smooth when magnified.

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

GL_TEXTURE_WRAP_S/GL_TEXTURE_WRAP_T

Chapter 10 said that the range of (u,v) or (s,t) coordinates is [0.0, 1.0]. But if they are set outside this range, which part of the image should pixels be taken from? In that case, you can set the sampling method with GL_TEXTURE_WRAP_S and GL_TEXTURE_WRAP_T. Set it to either repeating the image itself (GL_REPEAT) or repeating the image's edge values (GL_CLAMP_TO_EDGE). Let's look at the difference through an example.

GL_GENERATE_MIPMAP

See also

Favorite site

References


  1. Sungcheol-kim.gitbook.io_-ios-opengles-tutorial-_chapter11.pdf ↩

author Zer0 sitemap