Perspective-Correct Textures - Sixth 3D
Table of Contents
1. The problem
When a textured polygon is rendered at an angle to the viewer, naive linear interpolation of texture coordinates produces visible distortion.
Consider a large textured floor extending toward the horizon. Without perspective correction, the texture appears to "swim" or distort because the texture coordinates are interpolated linearly across screen space, not accounting for depth.
The Sixth 3D engine solves this through adaptive polygon tessellation. Instead of computing true perspective-correct interpolation per pixel (which is expensive), the engine subdivides large triangles into smaller pieces. Each sub-triangle is rendered with simple affine interpolation, but because the pieces are small, the error is negligible.
2. How Tessellation Works
The TexturedPolygonTessellator class recursively splits triangles:
void tessellate(TexturedTriangle polygon) { // Find the longest edge TessellationEdge longest = findLongestEdge(polygon); if (longest.length < maxDistance) { // Small enough: add to result result.add(polygon); } else { // Split at midpoint Vertex middle = longest.getMiddlePoint(); // Recurse on two sub-triangles tessellate(subTriangle1); tessellate(subTriangle2); } }
The midpoint is computed by averaging both 3D coordinates and texture coordinates.
3. Visualizing the Tessellation
Press F12 to open Developer Tools and enable "Show polygon borders". This draws yellow outlines around all textured polygons, making the tessellation visible:
This visualization helps you:
- Verify tessellation is working correctly
- See how subdivision density varies with camera distance to the polygon
- Debug texture distortion issues