Texture Mapping and Sampling Techniques in OpenGL

Texture Coordinates and Fragment Interpolation

To apply a texture to a triangle, we must define which part of the texture corresponds to each vertex. Each vertex is assigned a texture coordinate that determines where on the texture image to sample. During rasterization, these coordinates are interpolated across the triangle to provide a smooth transition across all fragments.

Texture coordinates are typically normalized in the range [0, 1] along both the x and y axes. The bottom-left corner of the texture is at (0, 0), and the top-right corner is at (1, 1). Below is an example of assigning texture coordinates to a triangle:

  • Bottom-left vertex → (0, 0)
  • Bottom-right vertex → (1, 0)
  • Top vertex → (0.5, 1)

These coordinates are passed to the vertex shader and then interpolated by the rasterizer before reaching the fragment shader.

float textureCoords[] = {
    0.0f, 0.0f, // Bottom-left
    1.0f, 0.0f, // Bottom-right
    0.5f, 1.0f  // Top
};

Texture Wrapping Modes

When texture coordinates fall outside the [0, 1] range, OpenGL uses a wrapping mode to determine how to handle sampling beyond the texture's boundaries. The available options are:

Mode Description
GL_REPEAT Repeats the texture image infinitely.
GL_MIRRORED_REPEAT Repeats the texture image, but mirrors it with each repetition.
GL_CLAMP_TO_EDGE Clamps the coordinates to the edge of the texture, stretching the edge pixels.
GL_CLAMP_TO_BORDER Returns a user-defined border color for coordinates outside the range.

To apply a wrappnig mode:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);

If using GL_CLAMP_TO_BORDER, you must also define the border color:

float borderColor[] = { 1.0f, 1.0f, 0.0f, 1.0f };
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);

Texture Filtering

Texture filtering determines how OpenGL samples texture data when mapping it to geometry. Two primary methods are used:

  • GL_NEAREST: Selects the nearest texel to the texture coordinate, resulting in a blocky appearance.
  • GL_LINEAR: Blends the four nearest texels to produce a smoother result.

You can specify different filters for minification and magnification:

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

Mipmapping for Efficient Texture Rendering

Mipmaps are precomputed, smaller versions of a texture used when the texture apears distant. This improves performance and visual quality by avoiding unnecessary sampling from high-resolution textures.

OpenGL can automatically generate mipmaps:

glGenerateMipmap(GL_TEXTURE_2D);

When using mipmaps, you can choose how OpenGL samples between mipmap levels:

  • GL_NEAREST_MIPMAP_NEAREST
  • GL_LINEAR_MIPMAP_NEAREST
  • GL_NEAREST_MIPMAP_LINEAR
  • GL_LINEAR_MIPMAP_LINEAR

Example configuration:

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

Note: Mipmap filtering only appleis to minification. Using mipmap modes for magnification will result in an error.

Tags: OpenGL Texture Mapping Texture Filtering Mipmapping GLSL

Posted on Fri, 30 Oct 2026 16:39:03 +0000 by tsabar