Last week I finished and released a small puzzle game called Bionic Carrots. The game is one of three little games I’ve challenged myself to make before September.
This week I’ve been taking a break from Unity and my small game challenge. Instead I’ve been looking into another voxel project.
I have implemented very basic voxel engines before, where each voxel was rendered as a cube made of triangles. This was to get an initial feeling of what voxels are and what might be interesting research further at a later time.
A World of Voxels
Building a world for a game or similar requires an enormous amount of voxels. So it came to no surprise that I find myself drawn to how this amount of data can be stored while still maintaining the possibility to change to world at run time.
In January I then made a small 3-week project on Data Structures for Storing Voxels. I implemented, tested and compared data structures such as 3D arrays, hashing and octrees.
The project was made as part of the Computer Science and Engineering masters program I’m on. 25% of the program is elective courses with the possibility to create our own courses/projects if we can find a supervisor for it.
The aim was not to find the optimal data structure storing voxels, but to quickly survey a few possibilities and get an idea on which directions in which I could proceed in later projects.
As part of the project I also started to look into techniques which might be used to make cache friendly octrees.
Below is three examples of the data sets I used for the project.
Voxel rendering using Raycasting
So I’ve tried rendering voxels using triangles. Now I want look into how voxels can be rendered using ray casting.
I’m doing this in my spare time out of curiosity. But my aim is also to find related data structure problems to research further in one or two university projects.
CPU vs. GPU
At first I’ll be exploring implementing voxel rendering on the CPU. And before someone starts talking about how slow that is, keep in mind that this is not a specific game project, but something I do to explore, learn and find problems to solve 😉
I also want to render using the GPU, but getting there should be a natural evolving process considering pros and cons at all steps on the way. I’ll also be doing a lot of tests and measurements to make it possible to compare the different solutions I’ll implement.
First steps, writing pixels to the screen
When rendering the voxels using ray casting, we cast a ray for each pixel on the screen to find to color of this pixels. The first problem to consider is therefore how to draw these pixels to the screen.
There a several ways to do this, but I’ve chosen to start with OpenGL since this is something I’m already familiar with. You can get other ideas on how to do this from this post I made in the VoxelGameDev subreddit.
I then render a single quad which fills the entire screen. The quad is colored using a texture. To draw the pixels I then just edit the image for the texture.
Before starting to work on ray casting, I’ve made a small test to make sure that drawing the pixels works. Below is gifs of two tests rendering and changing images of 128×128 pixels.
The next step is to implement a simpel raycasting solution on the CPU. I also have a bunch of articles gathered I want to read and sort.
As a first basic approach I will store the voxels in a 3D array and render at a low resolution. I hope to have enough time to implement and test this during the next weeks time.