The First Block

First Block

Lucas Camolezi
3 min readSep 4, 2019

--

My goal for the first article of the Minecraft project was to draw a textured grass block on the screen. With this objective in mind, i started my journey.

The first step was to set up an OpenGL project. This process is easy enough, but we need a few dependencies to be able to compile and run OpenGL code. I used a library called GLFW to be able to open a system
independent window. GLFW also handles basic input operations. Furthermore, OpenGL needs a loading library. This kind of library handles the loading of pointers to OpenGL functions at runtime. I used GLAD for this purpose.

With this, we can finally create an OpenGL context and start the fun stuff. The starting point was to draw a simple green square on the window. There are lot of parts involved in this process. But, after defining some buffers and writing some super basic shader code we can eventually see the green square with all its glory. At this moment I additionally made some simple shader class abstraction.

The Green Friend

The next logical step was to put a texture in our green friend. Thus I download the iconic 16x16 original Minecraft grass texture. Applying the texture to the square should be quite straight forward. But it wasn’t. My first attempts didn’t look like Minecraft at all.

After a while, I found the error. It was a difference in the number of color channels in separate parts of the code. Now the rectangle at least resembles the Minecraft texture.

Following, I just needed to disable linear filtering and flip the image. Done, the beautiful 16x16 texture is perfectly applied to our square.

Our square is now the front of a Minecraft block, we simply need to add the other faces, and we’re done, right? Yeah, though it is not that easy. We have to use some pretty neat linear algebra tricks to manage points and objects in 3D. Vectors and matrices are our best friends in this field. After creating all the necessary data and utilizing the required transforms we ultimately have the extremely waited Minecraft grass cube.

Things are finally getting exciting.
Until next time.

--

--