Recently, I decided it would be beneficial for me to learn some graphics to let me make engines etc. As such, I wanted to start a project using OpenGL. Similarly, I learned Scala recently, and quite liked it. Seeing as working in C++ is quite tedious, I thought I could improve my knowledge substantially by creating a project that combines both of these areas.
As such, I decided to create a project that combines both. On this direction, Minecraft is an extremely convenient project for such a purpose primarily using cubes for all its models. The structure is quite complex. It involves Scala Code which calls the C++ code using the Java Native Interface. Thus, there is an interface barrier between the Scala which does the logical work and the C++ which actually does the work displaying everything.
The setup was quite complex given how it needed to work with Scala, JNI and OpenGL at the same time. Eventually however, I found the solution using CMake. I wrote it into a script which compiles the C++ and JNI, while IntelliJ does the Scala compilation.
So far, the project has been quite educational. As all the models use the same cube, it makes it much simpler only needing a single VAO, VBO, Vertex Shader and Fragment Shader. However, this will undoubtedly become more complex as I try and include things such as lighting into the model, as well as more complex models.
The JNI seems complex at first, but can be extremely simple so long as you make sure to only pass and return simple data types. You need only include the right files and copy the headers.
However, one problem that occurred was #include conflicting. A simple solution to that is #pragma once, however CMake projects that directly include files that include the same file often conflict. As such, it is often necessary that the files imported are as few as possible.
On the Scala side, it seems that native methods are only included if in a Class, rather than objects, making static methods more complex. The solution I used to that was an object A that extends A. Beyond this, most of the work became Scala-side.
I decided to add chunks of 16x16x16 which house the blocks. This worked out with a 3-dimensional plane containing chunks, with a 3-dimensional plane inside of the blocks. These are made up of 3 layers of hashmaps, as it would allow O(1) time. However, this could be a problem with Chunks going forever. In addition, it might be better to use an array rather than Hashmap for the 16x16x16 block.
The primary problem with this, is if you need to search through every block of all these chunks, it’s an extremely large number of blocks., which can cause lag. As such, it might be worth finding a better way of storing in the info, maybe not storing an Air block in every empty space.
Of what I have accomplished however, the player is able to move around, including jumping, gravity and collisions. In addition, there is now support for placing and destroying blocks. As such, it is working quite well. I’m learning lots of graphics and Scala as I hoped, however, I notice in this abstracted view, the amount of OpenGL I’m currently working on is quite small. As such, I will most likely soon look into that a bit more, aiming to learn more graphical details that can be applied to the project.
The project doesn’t necessarily look too impressive as you can always consider it’s flaws compared to Minecraft, and it will likely never reach that standard. However, it does allow me to focus somewhat on technical design over game design. I could potentially work on Minecraft mods at the same time as the other side of the coin.
Anyway, end of rant. You can find the project GitHub here