Daniel Keast

Stuff about Programming

Pico 8 3D Rendering: Near Plane Clipping

Now we have a working camera, and triangles are drawn from the most distant to the nearest. We’re getting very close to a complete renderer. The main issue remaining is when we walk into one of the cubes, everything goes haywire. This is down to the division by z in the projection function. If z is 0, then we get infinity as a result and odd results. If z is negative, then we get warped triangles and inverted rendering. What we need to do is detect when triangles are too close to the screen, or are behind the camera and skip rendering them.

Pico 8 3D Rendering: Painter's Algorithm

So now we have a working camera, we can move around and look at objects that are moving independently from us. There are two obvious issues remaining though, firstly, no matter which direction we look at the cubes the same one is always drawn in front. The other is corruption when we walk into them. I’m going to fix the first one now.

Pico 8 3D Rendering: Camera

As of the last post in this series we had a textured floating cube in space. You could rotate it, move it and zoom in and out. This falls down as soon as you have multiple objects however. That rotation and movement was performed directly on the cube, with the camera effectively staying in place looking down the z axis. We need to pick apart two concepts which are currently tied together. First we need to be able to move and rotate objects in the world, and then we need to be able to move the camera and look at things without them being related to each other.

Pico 8 3D Rendering: Texture Mapping

Now it’s time to add a texture to the cube. I’ve done this with the use of the tline function in Pico 8.

Pico 8 3D Rendering: Scanline Rasterising

After the last post we have solid triangles. The cube is no longer a wireframe. Unfortunately it’s possible to count the frame rate by eye. In this post I replace the implementation with another approach, called scanline rasterisation, which is significantly faster when implemented with a CPU.

Pico 8 3D Rendering: Inside Test

For the latest update to my Pico 8 software renderer I have filled in the triangles with colour so that the cube is no longer a wireframe. To do this I reused the front function in the previous culling post. The function takes three points, a, b, and c. Previously I used the three points of the triangle I was drawing. The function returns true if the points go in a clockwise direction, and false if not. The triangle is made of three lines (a->b), (b->c), and (c->a). For any arbitrary screen pixel, if we use the points of one of the lines and then the coordinates of the pixel the function will return whether the clockwise direction is unchanged. If that direction is unchanged when using the pixel coordinates with all three lines, then that point must be inside the triangle. We can then walk through the pixels of the screen and set a colour when this condition is true.

Pico 8 3D Rendering: Culling

Part 4 of my ongoing series creating a 3D software renderer for Pico 8. Click here to give it a try, and here to download it. Here are the previous posts:

Pico 8 3D Rendering: Rotation

This is part 3 of me walking through the process of software rendering 3D objects on Pico 8. Click here to give it a try, and here to download it. Here are the previous posts:

Pico 8 3D Rendering: Wireframe

This is part 2 of me walking through the process of software rendering 3D objects on Pico 8, click here for part 1. In this post I’m projecting a wireframe cube made up of triangles. Click here to give it a try, and here to download it.

Pico 8 3D Rendering: Projection

This Pico 8 cart projects the points of a square in 3D space onto the screen. Click here to give it a try, and here to download it.

Pico 8: Mode 7

I made a Pico 8 cart which produces a SNES Mode 7 style floor. The trick is in the function tline, which draws a line between two points, sampling from the map data along the way. You give it the map position to start from, as well as dx and dy values it should use as an increment after each pixel. The other main thing is setting a depth value per scanline, the closer to the bottom of the screen the nearer to the player’s view. The rest is just trigonometry, applying angles to vectors using sin and cos.

Spectrum Machine Language for the Absolute Beginner - William Tang

A lovely book, easy to read and clear. There are some typos in the code examples but that was literally always the case back then. It was a part of how you learnt I think, forced you into figuring it out.

Introducing Erlang

Pretty short but clear book on Erlang. The first half consists of things I’m familiar with already (immutability, pattern matching, recursion, higher order functions). It was the last half that I was more interested in, process oriented programming and OTP. I’ll have to read a more in depth book to understand it thoroughly, but it’s a very interesting approach.

Python HTTP Server

Python has an http server in it’s standard library, and if you invoke the module directly from the command line it conveniently serves the local directory:

Delete a remote git branch

I can never remember the command to delete remote git branches, ending up on the page in the git book. Maybe writing it down here will help: