Redirection

Redirection

Not enough ratings
3D demo - Platonic Solids
   
Award
Favorite
Favorited
Unfavorite
File Size
Posted
240.857 KB
4 Apr, 2020 @ 10:08am
1 Change Note ( view )

Subscribe to download
3D demo - Platonic Solids

Description
This is a demo of 3D rendering in Redirection arcade, not an actual game.
You can switch between all 5 platonic solids using your X key and rotate the camera around it with your arrow keys.
The Y key toggles rendering of all vs only visible edges.

I aimed for simplicity a minimalistic code. I started a bit more complex and then I was throwing some stuff away. It's just 3 functions: the first one processes user input, the second converts 3D coordinates to 2D, and the last one handles the overall logic of the rendering process. All in under 100 lines of code.

I have never done anything with 3D before (in terms of programming) so it was also a nice mental exercise to think about how linear algebra a transformation of 3D space work. Later I might write a more complete 3D engine just because it's fun.

The only difficulty in this setting is the problem of which polygons hide which. Since we are only rendering platonic solids, the solution is easy (but took me a while to realise). If two polygons are both facing the camera then they cannot overlap each other. This is true in general for every convex shape. So all I had to do was to align my normals and draw only those faces that face towards the camera.
Before my idea about normals I implemented a more general solution. Since dealing with each pixel individualy turn out to be too slow (maybe just my laptop?) my solution was to handle each faces as a whole. That means sorting the faces so that the hidden ones are drawn first. This is not as easy as it sounds but I managed to get a solution that (almost always but not really working in general) worked. After my faces were sorted I filled the inside of each polygon with white (using triangulation and gpu.FillTriangle) and then the outline. Doing this I discovered a bug - attempt to draw a triangle with zero area raises a division by zero error instead of not drawing anything.