Master the fundamental concepts of modern graphics apis (low level) through this focused micro-challenge.
Vertex shaders output attributes (color, UVs, normals) per vertex. The rasterizer interpolates these across the triangle so the fragment shader receives smoothly varying values at each pixel. In GLSL these are varyings; in HLSL they are interpolants.
Straight lines in 3D project to straight lines in 2D, but attribute values are not linear in screen space under perspective. Equal screen-space steps correspond to unequal world-space steps due to foreshortening.
The GPU interpolates A/w and 1/w, then divides:
cLoading…
For example, three vertices with colors red, green, blue and w values 1, 2, 1 produce different results at screen position (5,3) depending on whether you interpolate linearly or with perspective correction. Texture coordinates show swimming artifacts without correction; normals produce wrong lighting on angled surfaces.
noperspective qualifier: rare GLSL escape hatch for screen-linear interpolationYou will simulate color interpolation at a fragment position, computing both naive linear and perspective-correct results. This task asks you to print barycentric coordinates, both color values, and explain the difference. Getting this wrong is invisible on flat polygons but immediately visible as texture warping on steep angles.
Write a C program that simulates fragment shader attribute interpolation.
Requirements:
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
All starter code and reference implementations are available for your local setup.
View on Github