A New World

The world of C and C style C++, low level programming, writing everything by hand. Some would argue why even bother, there are already solutions out there for me to use. Don’t re-invent the wheel (even though we’ve been continuously reinventing the wheel since the day it was invented). Use existing solutions. Why write a software renderer, its an obsolete way to render anything. Why use DirectX when you can use Unreal or Unity. Why, why, why…

The short, it makes me happy and makes me feel the kind of fulfillment that I haven’t felt before when programming. The long answer, I don’t want to be the guy that only knows how to use other peoples stuff. I don’t want to #include <bobs_best_parset.h> call a bob_init() and the rest is magic. I want to be able to write the parser myself, I want to have the skill set to solve problems myself. The more I used things like Unity and Unreal, the more I relied on existing solutions, the worse I felt as a programmer. Could I even call myself a programmer anymore? I felt like I couldn’t. And finally, I always like to think, is this going to be the solution in 100-200 years? Is Unity or what ever other tool you suggest I use going to be the solution in the future? The answer is always no. And if its no, then I want to be part of that new solution. I bet you in 200 years, we are not going to have Unity version 1135.30. The software will be dead, and there will be other software to replace it. I want to be part of the group of people who write new and better software, who explore new and better solution, not the group that satisfied with the status quo.

Thus begins my journey as a low level Systems and Engine programmer. This isn’t the first time I’ve programmed in C/C++. I did a ton if it in university, and a little in my free time. But this marks the moment in time where I spent hours every day, practicing and improving and indulging myself in a style of programming that I didn’t even know existed before “Handmade Hero” (I will likely be referencing this series and its community multiple times through this blog series). While following the series, I found that I was just copying exactly what Casey was doing, almost line by line. I didn’t feel like I was learning, but more listening to a lecture where you forget most of it later anyway. So after following the first 50ish episodes of the series, writing everything verbatim, I decided to re-write the entire program myself in my own style, while using the series as a guide.

The first project I decided to work on was to rasterize triangles and I wanted to do it with my own software renderer. No aliasing, anti-aliasing or multi-sampling. Nothing special. The rules I wanted to follow were that of MSDN’s DirectX rasterization rules and I wanted to mimic the results of wikipedia’s rasterization reference to resolve how pixels are determined to be in or out of a triangle.
MSDN: D3D Rasterization Rules
Wiki: Rasterization

So… the first step was to figure out how to draw things using the windows API. The concept itself is pretty simple, although as usual Windows made it hard to figure out and complicate with the number of steps and settings you need to setup right off the bat. Once you are able to create and register a window class, you need to setup a few more things to be able to draw to a back buffer, such as a device context, bitmap info, and some basic information about how large this buffer is going to be. For this, I created a RenderBuffer struct that contained all this information for me, and once I populate the back buffer with data, I can display it using StretchDIBits().

With just this code, we can easily rasterize any rectangle we want with any color, by just iterating over some starting point in the buffer, and advancing by the stride to get to the next row of pixels we want to fill in.

From here, we can start understanding how to draw all kinds of simple shapes such as a line, circle, ray, triangle, quad (2 triangles), …
So naturally we now want to be able to draw a triangle. And this turns out to be also pretty simple. But first we have to understand some terminology such as flat bottom triangle and flat top triangles. All this means is that every triangle we want to draw, either is flat at the bottom and meets at the top, or flat at the top and meets at the bottom.

From here, we just have to calculate the rise over run of both sides, in case it is not uniform and every time we rasterize a row, we either go up or down by the rise/run for each side to completely draw the entire triangle. The code for this looks something like this (where draw_flatbottom_triangle() would be the same except in the opposite direction):

Finally, we need to be able to rasterize all kinds of triangles, specifically ones that have not flat bottom or flat top. The way to do this is to realize that you can take any single triangle, split it in half and turn it into both a flat bottom and flat top triangle. To do this, you just need to figure out which point is in the middle with respect to height, and use that point as the splitting point to create one flat bottom and one flat top triangle. Once you have that information, you can just call the two functions to render both flat top and flat bottom triangles!

Now that I’m able to rasterize any triangle I want, I can start working towards replicating the example shown in Wikipedia while using the DirectX rasterization rules on how to resolve a) if a pixel is in our out of a triangle, and b) which triangle draws the line if two triangles overlap. After banging my head around this for a long time, it turned out that it all revolved around how you round your values, and a couple of ceils that you might have noticed in the above code to draw a flat top triangle.
So with a little bit of work, I’m able to draw the individual pixels that are identical to the wiki, enlarge them by 50 pixels or so, and pass them in as individual triangles to be rendered. The result is this:

At this point the project is pretty much done. But I did spend some more time playing around with shapes, colors and more and ended up drawing a whole bunch of stuff including some simple linear alpha blending. (line, ray, segment, circle (filled/outlined), rect (filled/outlined), quad (filled/outlined), triangle)