This week you are going to put together the first pieces of a scan-line z-buffer renderer.
For this week I'm going to ask you just to do the first pieces of the scan-line conversion, using the simplest approach to shading interpolation, Gouraud Interpolation, in which your Vertex Shader computes Red,Green,Blue, and your Pixel Shader pretty much does nothing.
I would like you to make some interesting scene consisting of sphere meshes. You can make a snowman, a pile of bowling balls, a sugar molecule, the iconic logo of some large entertainment corporation, or anything else that strikes your fancy.
You're going to be making a picture of your model. You can use the same Phong shading model that you used in your ray tracing assignment. Remember that you can't set the Specular power very high without getting artifacts, since you'll be using Gouraud Interpolation, which does a really bad job with specular highlights.
The reason I want you to use spherical shapes is that this makes it really easy to compute the normal vector at each vertex. If the center point of your sphere is c, and your vertex is at surface point s, then the normal vector at s is always simply: normalize(s - c).
Your algorithm should use the following algorithm:
Compute and store the surface normal vector at all vertices Initialize the zbuffer For each polygon For each vertex: Compute (r,g,b)_{v} = shade(surfacePoint_{v}, surfaceNormal_{v}) Compute perspective coords (px,py,pz) = (fx/z,fy/z,1/z) Viewport transform (px,py) to get pixel coords of vertex Scan-convert the polygon into pixels, interpolating (r,g,b,pz) At each pixel (i,j): if (pz < zbuffer[i][j]) { framebuffer[i][j] = packPixel(r,g,b) zbuffer[i][j] = pz }I recommend that to scan-convert a polygon you first split it up into triangles, and then split each triangle into two scan-line aligned trapezoids:
Polygon to trapezoids: | One trapezoid in detail: | |
---|---|---|
Within each trapezoid you should obtain the value at the left and right edges of each scan-line by linearly interpolating (r,g,b,pz). Then within each scan-line you should obtain the value at each pixel by linearly interpolating (r,g,b,pz) between the left and right edges of that scan-line.