april 23, 2014

Voxel engine and LibGDX

Voxel engine and LibGDX

Minecraft

There.. I said it..  it must have to be said..  The creation of Minecraft has really changed the gaming industry since it was released. Look at how many participants there are on all the indie game competitions like Ludum Dare and how much it has grown in the last years and all the libraries and game engine that is released.. LibGdx and Unity to name two. Some of the growth is probably also related to the growth of mobile games but I think Minecraft has inspired a generation of programmers to get into game programming and I’m one of them.

Late to the game

I was relative late to the Minecraft world. It was last summer, when everybody else “stopped playing”, that I started to play and now my whole family plays it. Since I’m a Java developer I started to thinking about how the world actually are built in Minecraft. How is the data stored in memory? How can the performance be so good?

LibGDX

Just at this point the LibGDX team released some nightly builds with a new 3D framework. I had tested LibGDX a bit earlier to do some basic 2D programming and with the 3D release I suddenly one evening said to myself “hey.. lets try build a Minecraft copy just for fun and just to learn.” I spent an hour reading up on the framework and created a block on the screen. Did to a simple level importer where I could create blocks from a text file. My knowledge was so on beginner level that I mixed up Y and Z. Very quickly I started to get the “Dungeon game” feeling going and I build a few levels for test and did notice quite quickly how slow everything become. Why is that?  I did the same mistake that probably everyone trying to make a Voxel engine/Minecraft copy has done. Adding boxes with a createBox method from a game engine/library will not perform very well when you have lots of them.

LWJGL

At first I blamed LibGDX for being slow (how wrong I was!) not knowing anything of what actually is required. Instead I moved to LWJGL (the library that Minecraft is using). LWJGL is basically a wrapper library around OpenGL. It has no new Camera(); or that kind of helpers. It has Vector math classes at least which helps a bit. So there I was totally alone with no real tutorials to follow. There are lots of tutorials on OpenGL.. many of them focusing on C++ and many of them using various kind of deprecated ways, the preferred way to do it (but it’s about to get deprecated too) and the new way from OpenGL4. After a hours of trial and error I had a box on the screen. After a few more hours or trial and error I had the box decorated with a minecraft grass texture. A few more hours and I had created some simple landscape generation just to discover that everything was slow again. I was using the slow and really deprecated way. I had to read up on VBOs, using threads to calculate landscapes and there suddenly I had my first working voxel engine! 

Back for LibGDX

Then I decided I was going back to LibGDX and started to port the code but using all the LibGDX helper functions available now when I had much more knowledge of 3D and OpenGL plus one of the reasons was also to make use of collision engine.. I tried implement my own which was driving me crazy in my LWJGL version. and finally I ended up with this: 

Dungeon

But what I really wanted to do was a dungeon game and therefore I used the code from above and started creating a dungeon builder. The code have become a bit messy over time but it looks like this on this demo video where I show lightning (which isn’t 100% finished in this video) 

What will I blog about?

Since I have received various questions about how I did I decided I should try to summarize them in a blog.. trying to describe the technics and theory I have used and maybe during the process I will rewrite part of it and release the source.