Voxelscape Tech Demo
A voxelscape (voxel landscape) tech demo for Sega Genesis/Mega Drive, intended for use in a future project. Use the D-pad to move around, hold down C button to go faster.
Note that this is a very early proof of concept made in two weeks, with only a small repeating room, no collision detection, no sound, and no speed correction between NTSC and PAL systems.
Voxelscape Tech Demo
(BIN, 256 KB)
Last updated: 2020-aug-05
How does it work?
Warning: heavy use of tech jargon below, knowledge of 68000 assembly recommended.
- Uses a basic preemptive multitasking scheduler with a foreground and a background task, each with their own registers and stack. The foreground task is called every frame, the background task takes up whatever CPU time is left. This allows the demo to respond to controller input at 60 FPS while the voxel rendering can take as long as it needs.
- Exploits the same "mode 7" landscape trick used in Gasega68k's 16KB demos, where coordinates are stored in bits 6-0 and 14-8 of the address offset (i.e. excluding the MSB of each byte). This requires a 128×128 (16KB) heightmap spread over 32KB (the gaps can be reused for other purposes).
- X coordinate needs to be byte-swapped due to the above trick, so it
addxinstruction in order to carry over from the fractional part to the integer part (carry is delayed by one step but it's easy to notice).
- Abuses the 68000's attempts to keep the stack "aligned" in order to write to every other byte while painting. The 68000 will make the stack advance by two bytes even when doing a byte-sized move.
- Only half of the viewport is drawn, then mirrored vertically. This is what achieves the cave-like appareance of the room.
- Look-up tables are used to determine the direction in which the rays scan the heightmap, to project 3D heights to 2D space, and to compute the color of each voxel. The fog is achieved by changing the colormap at certain steps of each ray.
- Both plane A and plane B are "interleaved" in order to double the size of the pixels vertically for cheap (raster effects would eat up too much CPU time). This requires the use of sprites and window plane to make any sort of HUD.
Some more interesting notes not made obvious by the tech demo:
- The viewport is mirrored, but the upper and lower halves are assigned different palettes, so in theory the ceiling and floor can be given different colors by making them different in each palette. This can be used e.g. to make hazards that only appear on the floor.
- There can be up to 128 unique combinations of height and color for a voxel. The demo is only using 16 of the possible values.