Tiger 2D-3D Game and Graphics Engine
Welcome to the site of our Game and Graphics Engine
Tiger 2D-3D Game and Graphics Engine is currently in the early stages of development. It is planned to incorporate many features of leading commercial game engines when finished.

Load vox files as voxel models

Posted on 16th May, 2009

Vox file loader
A Simple demo how to load vox files and generate voxel octree. The demo loads a .vox file, and then calculates its octree and bounding boxes in 3 depth. Use t key to enable or disable the drawing of these.
Vox loader with octree
Applying bounding boxes, the engine use Frustum Culling to cut off all the voxels, which are not visible. See the FPS counter. Vox file is a simple and uncompressed binary file. The models (duke nukem, worm) used in this voxel demo are taken from Ken Silverman's homepage.

Vox file Loader Demo (linux) - not yet
Vox file Loader Demo (windows)

Oh, and how can we render a simple voxel? This is not an easy question, there a many methods in the practice. In the above example we used a very basic rendering method: simple opengl cube represents the voxel. Of course this is very slow, but you can speed up with some trick. For example you can use frustum culling to cut off those voxels which is not visible. Or you can use Vertex Buffer Object to store cube data in video card memory and use backface culling to cut hidden surfaces.
Of course, there are alternative rendering techniques. The simplest rendering method to render a voxel is to draw squares. Remember those old games, which uses voxels. For example: Shadow Warrior for pickups, Blood for pickups and some objects, etc. This technique is very simple: you know the voxel positions in 3D. From this information you can determine the 2D screen positions. So what the hell is this??? Do not worry, it is simple. In 3D you have the 3 coordinates and the size of the voxel. The size is for example 1. If you want to determine the 2D screen position of the voxel, you will get four number of 2D points, which represents a square. Look at this:

float per_z = 1.0f/z;
float s = size/1.2f;

x_screen = (+((y-s) * per_z) + half_screen_width );
y_screen = (-((x-s) * per_z) + half_screen_height );

x_screen2 = (+((y+s) * per_z) + half_screen_width );
y_screen2 = (-((x+s) * per_z) + half_screen_height );

So you have 4 points. And what does the 1.2 magic number mean??? It is a custom value, which determines the square size. In Tiger Engine, this number is a good choice. Using these 4 points you can draw a square to the screen. And how can you do this? For example you can implement an own framebuffer and z buffer, and draw this buffer to the screen like. You need an ScreenX*ScreenY*RGBA array. Each element of the array represents a pixel on screen. This code shows how you can draw your custom framebuffer with OpenGl:

glDrawPixels(framebuffer_width,framebuffer_height,GL_RGB,GL_UNSIGNED_BYTE,pixels);

Where pixel is: GLubyte* pixels; - our custom framebuffer array. Use GL_UNSIGNED_BYTE to speed up your rendering.

Drawing squares is very fast if the amount of data are not to big. The problem is the hidden voxel removal. You always overwrite pixels. This is waste of CPU time. To fix this problem you can use a self implemented z buffer, but without hardware acceleration it will be slow in case of a medium size voxel model. A good optimization point is to draw only the surface voxels. Creating a list of these voxels drastically improves rendering speed. Another good approach can be the following: If the object is enough far in Z, we do not need to draw squares anymore. It is enough to draw voxel as pixels. Use only x_screen and y_screen coordinates. You will see that the results will be the same. :-)
Tiger Engine already has implemented this method, but it is in early stage. There are a lot of difficulties: no hardware acceleration, no OpenGL functions can be used like camera movement, etc. But is is interesting. You can mix voxels and other graphics in the Engine.

We know developers love screenshots a lot :-). Some images from the implementation:

Square based voxel renderer
A little closer:
Square based voxel renderer
More closer. You can see the square blocks:
Square based voxel renderer

The same rendering method with a high quality voxel medkit :-p
Square based voxel medkit