Rendering 3d From Scratch Chapter 5 - The Screen Pt 2
Last time we covered a new mathematical tool to aid in our quest to draw 3d objects to the screen: the cross product. Here’s what it looks like in javascript:
You can see that similar to the dot product, it is just a few arithmetical operations. As a result, it is blazing fast, which is a good thing, because these operations will run hundreds or thousands of times per draw frame in a modern video game!
We know that, armed with this tool, we can determine the four corners of our camera plane. A methodology to do this was covered in the last article, so take a peek back at that if you need a refresher. We’ll create a class called “Camera” which will store the four corners of our plane (and a few other useful pieces of information). This is what it looks like in code:
And finally, while we’re on the topic of code, let’s cover a quick and dirty way to draw in a web browser. This is somewhat unrelated to our ultimate goal, but it will make demonstrating all this code in action a lot cooler. I’m going to use a class called ScreenBuffer.
This code only requires that there is a canvas somewhere in your html dom. It gives the ability to set individual pixels in your canvas. Here’s an example of the code in action :-)
I encourage you to inspect the source on that metroid, and you’ll see that it’s just using the ScreenBuffer from above.
The only thing left to do is to fill in our shapes on screen. Next time, we’ll cover a cool algorithm for doing just that. That will be the final puzzle piece and then we’re ready for the grand finale.