  /
News

# In-depth: Software rasterizer and interpolating in screen space

In this reprinted #altdevblogaday in-depth piece, game programmer Simon Yeung takes readers through the steps to linear interpolate the vertex in screen space.
[In this reprinted #altdevblogaday opinion piece, game programmer Simon Yeung takes readers through the steps to linear interpolate the vertex in screen space.] Continuing with the previous post, after filling the triangle with scan line or half-space algorithm, we also need to interpolate the vertex attributes across the triangle so that we can have texture coordinates or depth on every pixel. However we cannot directly interpolate those attributes in screen space because projection transform after perspective division is not an affine transformation (i.e. after transformation, the mid-point of the line segment is no longer the mid-point). This will result in some distortion and this artifact is even more noticeable when the triangle is large: Condition for linear interpolation When interpolating the attributes in a linear way, we are saying that given a set of vertices, vi (where i is any integer>=0) with a set of attributes ai (such as texture coordinates), we have a function mapping a vertex to the corresponding attributes, i.e.
f(vi)= ai
Say, to interpolate a vertex inside a triangle in a linear way, the function f needs to have the following properties:
f(t0 *v0 + t1 *v1 + t2 *v2 ) = t0 * f(v0) + t1 * f(v1) + t2 * f(v2)
, for any t0, t1, t2 where t0 + t1 + t2=1
which means that we can calculate the interpolated attributes using the same weight ti used for interpolating vertex position. For functions having the above properties, those functions will be an affine function with the following form:
f(x)= Ax + b
, where A is a matrix, x and b are vector
Depth interpolation When a vertex is projected from view space to normalized device coordinates(NDC), we will have the following relation (ratio of the triangles) between the view space and NDC space: substitute equation 1 and 2 into the plane equation of the triangle lies on: So, 1/zview is an affine function of xndc and yndc, which can be interpolated linearly across the screen space (the transform from NDC space to screen space is a linear transform). Attributes interpolation In the last section, we know how to interpolate the depth of a pixel linearly in screen space. The next problem is to interpolate the vertex attributes (e.g. texture coordinates). In view space, we know that those attributes can be interpolated linearly, so those attributes can be calculated by an affine function with the vertex position as parameters, e.g. Similar to interpolate depth, substitute equation 1 and 2 into the above equation: Therefore, u/zview is an another affine function of xndc and yndc, which can be interpolated linearly across the screen space. Hence we can interpolate u linearly by first interpolatin 1/zview and u/zview across screen space, and then divide them per pixel. The last problem…
Now, we know that we can interpolate the view space depth and vertex attributes linearly across screen space. But during the rasterization state, we only have vertices in homogenous coordinates (vertices are transformed by the projection matrix already). How can we get the zview to do the perspective correct interpolation? Consider the projection matrix (I use D3D one, but the same for openGL): After transforming the vertex position, the w-coordinate will be the view space depth!
i.e. w-homogenous = zview
And look at the matrix again and consider the transformed z-coordinates. It will be in the form of: After transforming to the NDC, So the depth value can be directly interpolated using z-NDC for depth test. Demo A javascript demo for rasterizing the triangles can be viewed here (although not optimized…). And the source code can be downloaded here. Screen shot of the demo
Conclusion In this post, the steps to linear interpolate the vertex in screen space is described. And for rasterizing the depth buffer only (e.g. for occlusion), the depth value can be linearly interpolated directly with the z coordinate in NDC space, which is even simpler. References  http://www.lysator.liu.se/~mikaelk/doc/perspectivetexture/  http://www.gamedev.net/topic/581732-perspective-correct-depth-interpolation/  http://chrishecker.com/Miscellaneous_Technical_Articles  http://en.wikipedia.org/wiki/Affine_transformation [This piece was reprinted from #AltDevBlogADay, a shared blog initiative started by @mike_acton devoted to giving game developers of all disciplines a place to motivate each other to write regularly about their personal game development passions.]

### Latest Jobs

#### IO Interactive

Hybrid (Malmö, Sweden)
3.02.23
Gameplay Director (Project Fantasy)

#### Arizona State University

Los Angeles, CA, USA
2.27.23
Assistant Professor of XR Technologies

#### IO Interactive

Hybrid (Copenhagen, Denmark)
3.02.23
Animation Tech Programmer

#### Purdue University

West Lafayette, IN, USA
3.02.23
Assistant Professor in Game Design and Development
More Jobs

#### Game Developer Job Board

Browse open positions across the game industry or recruit new talent for your studio