opengl texture coordinates

OpenGL Texture Coordinates: The Ultimate Guide to Mapping Textures Like a Pro

By Daniel Smith
AD

OpenGL texture coordinates are the bridge between the geometry you render and the pixels stored in a texture image. Whether you are drawing a simple quad, a terrain, or a full 3D character, you must tell OpenGL which part of a texture belongs to each vertex. That instruction comes in the form of texture coordinates, often called UVs, and getting them right is essential for crisp, predictable rendering.

At first glance, texture coordinates seem trivial: assign a pair of numbers, pass them to the shader, and sample the texture. In practice, details such as normalized ranges, the origin of the coordinate system, wrapping modes, filtering, and interpolation all influence the final image. A solid understanding of OpenGL texture coordinates helps you avoid stretched textures, seams, blurry mipmaps, and unexpected edge artifacts.

Understanding the OpenGL Texture Coordinate System

OpenGL defines texture coordinates in a normalized space, typically ranging from 0.0 to 1.0 along each axis. For a 2D texture, the horizontal coordinate is usually called s or u, and the vertical coordinate is called t or v. The coordinate (0,0) refers to one corner of the texture, while (1,1) refers to the opposite corner. This normalized approach means the same texture coordinates work regardless of the actual pixel dimensions of the image.

AD
How to apply textures to a game character using OpenGL ES — Harold Serrano - Game Engine Developer
How to apply textures to a game character using OpenGL ES — Harold Serrano - Game Engine Developer
An intro to modern OpenGL. Chapter 1: The Graphics Pipeline
An intro to modern OpenGL. Chapter 1: The Graphics Pipeline
OpenGL - A small walk inside my procedurally generated terrain.
OpenGL - A small walk inside my procedurally generated terrain.

Unlike many image editors and windowing systems that place the origin at the top-left, OpenGL's conventional texture coordinate origin is the bottom-left. This difference is a common source of vertically flipped textures. When you load an image with a top-left origin, you may need to flip it during loading or adjust the V coordinate in your shader. Modern engines often handle this automatically, but knowing the underlying convention prevents confusion when debugging.

Normalized Coordinates and the s,t Axes

In a fragment shader, a sampler2D uses texture coordinates to fetch a texel. The call texture(myTexture, vec2(s, t)) expects normalized coordinates, not pixel indices. If you pass pixel coordinates such as (256, 128) for a 512x256 texture, you will sample far outside the valid range unless wrapping is configured. The GPU then applies wrapping rules, which may repeat or clamp the result depending on the sampler state.

OpenGL Insights
OpenGL Insights
OpenGL Transformation
OpenGL Transformation
This is why you pay attention in math class if you want to make games.
This is why you pay attention in math class if you want to make games.

Texture coordinates can also have more than two components. A 1D texture uses a single s coordinate, a 2D texture uses s and t, and a 3D texture uses s, t, and r. Some samplers also accept a q component for projective texturing, similar to homogeneous coordinates. The q component divides the other coordinates, which is useful for effects like projected lights, decals, and shadow mapping.

Wrapping Modes and Edge Behavior

Wrapping modes determine what happens when texture coordinates fall outside the 0.0 to 1.0 range. GL_REPEAT tiles the texture infinitely, so a coordinate of 1.25 samples the same texel as 0.25. GL_CLAMP_TO_EDGE clamps coordinates to the nearest edge texel, which prevents wrapping but can stretch edge pixels. GL_MIRRORED_REPEAT flips the texture at each integer boundary, creating a seamless mirrored pattern. GL_CLAMP_TO_BORDER returns a specified border color outside the range, which is often used for shadow maps.

AD

The choice of wrapping mode affects both appearance and performance. Repeated tiling is ideal for floors, walls, and terrain where a small texture must cover a large surface. Clamping is better for UI elements, decals, and textures that should not tile. Mismanaged wrapping can produce visible seams or unwanted repetition, especially when texture coordinates are generated procedurally or when using texture atlases.

How do I make a radial UV texture coordinate map?
How do I make a radial UV texture coordinate map?
An intro to modern OpenGL. Chapter 2.2: Shaders
An intro to modern OpenGL. Chapter 2.2: Shaders
Examples
Examples
the planets are shown in different colors and sizes, including one that is blue or green
the planets are shown in different colors and sizes, including one that is blue or green
an image of a very colorful pattern that looks like it is made out of squares and rectangles
an image of a very colorful pattern that looks like it is made out of squares and rectangles
a diagram showing the intersection of two intersecting lines and an x, y, w
a diagram showing the intersection of two intersecting lines and an x, y, w

Practical Techniques and Common Pitfalls

Once you understand the coordinate space, the next challenge is mapping texture coordinates to geometry and sampling them correctly. In a typical OpenGL pipeline, you define per-vertex attributes for position and texture coordinates, then pass them to the vertex shader. The rasterizer interpolates those coordinates across each triangle, and the fragment shader uses the interpolated values to sample the texture. This interpolation is what makes textures appear to follow the surface smoothly.

However, interpolation can also introduce problems. If adjacent triangles do not share consistent UVs, you may see seams or discontinuities. If UVs are compressed or stretched, the texture will distort. If coordinates are used for procedural effects, precision issues can appear at large scales. Debugging these issues often means visualizing UVs as colors, checking vertex data, and verifying sampler parameters.

AD

Mapping UVs to Geometry and Interpolation

For a simple quad, you usually assign UVs so that (0,0) maps to one corner and (1,1) maps to the opposite corner. For more complex meshes, UVs are authored in a modeling tool and exported with the vertex data. Each vertex stores a UV pair, and the GPU interpolates between them. Because interpolation is linear in screen space after perspective correction, textures on angled surfaces remain perspective-correct by default in OpenGL.

an abstract purple and blue background with squares
an abstract purple and blue background with squares
C++ OpenGL #1 - Some basic GLUT
C++ OpenGL #1 - Some basic GLUT
an abstract black and white background with wavy lines in the shape of leaves, ornament
an abstract black and white background with wavy lines in the shape of leaves, ornament
an abstract black and white background with bubbles
an abstract black and white background with bubbles

Texture atlases pack multiple images into a single texture, and each sub-image uses a specific UV rectangle. The main pitfall is bleeding: when filtering samples across the atlas boundary, colors from neighboring images can leak into the result. Adding padding, using a texture array, or clamping UVs to the sub-rectangle can reduce bleeding. Careful atlas layout and mipmap generation are also important because lower mip levels blend across larger areas.

Filtering, Mipmaps, and Texel Centers

Filtering determines how OpenGL combines texels when a texture is magnified or minified. GL_NEAREST selects the closest texel and produces a blocky look, while GL_LINEAR blends neighboring texels for a smoother result. For minification, mipmaps provide precomputed lower-resolution versions of the texture. The sampler can choose between mip levels with GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_LINEAR, and other combinations. Without mipmaps, minified textures can shimmer and alias badly.

AD

Texel centers matter when you need exact sampling. A texture with width W has texel centers at (i + 0.5) / W for i from 0 to W - 1. Sampling exactly at 0.0 or 1.0 can fall on the boundary between texels or outside the texture, depending on wrapping and filtering. If you are implementing pixel-perfect effects, post-processing, or compute-style reads, offset your coordinates to texel centers. This small detail often separates a clean result from one with half-texel shifts and blurry edges.

Texture coordinates are a small part of the OpenGL API, but they shape nearly every textured object you render. Experiment with wrapping modes, filtering, and UV layouts on a simple quad before moving to complex models. The more comfortable you become with normalized coordinates and sampler behavior, the faster you will diagnose visual artifacts and the more control you will have over your final image.

AD