I have been working on a new software rendering engine as a follow up from my previous one. As with the previous one, this means performing all calculations required to render 3D meshes to an array of 2D pixels, and dispatching that to the backend used by the engine.
The console and SDL renderers in action
The way I achieved this is by rendering all objects into a 2D buffer each frame. The objects have their points transformed by the opposite of the current camera transformation (which has the effect of moving them to be in the same position, rotation and scale as they would be if the scene was recreated with the camera at the origin). Then, the positions of the vertices of each triangle are scaled towards the centre of the screen depending on their z coordinate (distance from the camera). From there, the 2D triangle is drawn on the screen – but while drawing, an additional calculation is made using the z-coordinates of all of the transformed triangles to figure out the depth of the current pixel – which allows us to determine whether the current pixel is in front of whatever has been drawn already, and therefore draw intersecting triangles.
The main changes from the last engine are below:
- Written in C instead of C#
- Built with cross platform considerations
- Addition of a depth buffer when drawing triangles allows intersecting triangles to be drawn correctly
- Added a loader for the .obj format (not all features supported yet, hope to add more and more loader formats in the future)
- Supports rendering to console, SDL, X11 and video
As with the last one, this does not leverage the GPU like OpenGL – although I may change this in the future. The code is a bit messy in places – I didn’t properly understand how to use header/source files and build tools when starting. It can also be a bit difficult to understand in places, so I hope to make it more friendly soon 🙂