Saturday, November 3, 2012

Initial Output

The problem with the rasterizer is that you can't see any output until you complete the rasteriation stage, which means finishing up all the previous stages. So I started with a simple triangle and proceeded from there checking it though each of the stages.

Stuff I implemented so far:
  • Vertex Shader: Here is converted the vertices into the Projection view, which is nothing but bring the model into eye coordinates and then transforming the truncated pyramid of projection frustrum into a cube. This step of converting it into a cube skews the object and provides a perspective view.
  • Primitive Assembly: Here, we assemble each of the individual vertices into a primitive (in my case a triangle). The colors and normals associated with the vertex are also transferred into the primitive.
  • Rasterizer: This is the meat of the process, which converts the pixels into fragments. A single pixel becomes a fragment when it is actually eligible to be displayed. Following steps need to be performed:
    • Project the triangle from the perspective space onto the screen and find where exactly on the screen the vertices lie.
    • Start a scanline algorithm to actually determine which pixels lie within that primitive.
    • Once you are sure that the pixel lies within the primitive, project it back into the projection space using barycentric coordinates. This new coordinate's z-value can be used to deterine the depth at which the actual point on the primitive lies. If there are other pixels which lie in front of it, this pixel will be obscured and will not become a fragment. So we perform this depth test for each of the points that we get from the scanline algorithm.
    • The colors can also be interpolated in a similar manner using Barycentric coordinates.
    • The problem with updating the depth buffer is that all the primitives are running in parallel and there maybe 2 or more primitives at the same pixel location trying to put their value into the depth buffer. This has to be resolved so that only one thread access an index in the depth buffer at one time. I achieved this using atomics in CUDA. I sort of lock the index in the depth buffer when it is being updated, so that no one else can modify it.
    • At the end of this stage, you get all the fragments which are ready to be displayed.
  • Fragment Shader: Currently I am just wrting out the values of the interpolated color values from the depth buffer. Haven't done any particular shading here yet, so its just flat shading.
Images so far:

 A Cube rotated 45 deg around x and y

Scaled up cow
 
Flat Shading

Here each of the vertices are given Red, Green and Blue colors.

No comments:

Post a Comment