What you are looking at here is my first Minecraft clone, and while it might look pretty advanced, it lacks many features like structures, nice shaders, an infinite world, and multi-player. So this is why I started working on a new Minecraft clone, where I will hopefully be Able to implement all of these nice features using my past experience so let’s see how far can I get. The first thing you have to do when making a Minecraft clone is to open a window and display a square to check if everything is working properly. Then you can add a 3D camera to move around. Then I added this cube renderer. Before I did anything else however I wanted to make sure that I could have a very big world like Minecraft. Let me explain. So whenever you want to draw something on the screen you usually send some 3D points that the GPU will convert into triangles. Those points are stored into floats and floats can’t store very big numbers and have precision at the same time. So I am going to send data to the GPU into an int + float combination and the block positions will be just ints. So for example, If I am here at 2 million blocks and I have to display this block in Front of me, I’ll first subtract my int position from the block’s position to get a small number then I’ll do the floating point calculations. Next, it was time to add some textures to the blocks and make our first chunk. All the block faces are stored into this texture atlas so what I had to do is make a list of all the block types and fill out the coordinates for all 6 faces of a cube. Now it was time to make multiple chunks and to start doing some optimizations. The first basic thing you have to do is to make sure you don’t draw faces that are inside the geometry like this. In order to do this you have to look at every block in the chunk and determine the final geometry that you want to draw. This process takes a lot of time so you do it only once saving the result and changing it only when the player modifies the blocks in the chunk. Now for each chunk visible, I call the draw function so this means that I have a draw call per visible chunk. Nowadays having many draw calls is not necessarily bad for performance anymore and we don’t even have that many actually. The only performance bottleneck here is the fact that the GPU has to draw many objects and the fact that I have to send a lot of data. I optimized the data sending very well, however. Any block face can have only one of 6 shapes so Instead of sending all the vertices of a block I just send its position it’s type and shape. Then I calculate on the GPU all the vertices that I have to fill using some lookup tables. This seems to be working well and it also allowed me to easily implement moving blocks. Another optimization that I can add in the future is to keep the computed chunk data directly on the GPU instead of keeping it locally on the CPU. Now for world generation, I just took a noise generation library and used it to generate my world. Structures will be kinda difficult to add but I think I know how I’ll do it. I’ll just generate them and for blocks that end up in not created chunks I will store Them somewhere as to be added later when that chunk gets generated. Now if you try this project yourself you will quickly realize that generating the world and the chunk geometry takes a lot of time so the first thing you have to do is limit The chunks generated + have their geometry computed per frame. also, start with the chunks that are closer to the player. However, since I wanted to add multiplayer anyway, I also started to move the world generation to another thread. This is very similar to how multi player works, let me show you: The client will request a chunk from the server. In this case, the server is another thread but It can also be a real server in a multi-player situation. Then the server will give the player back a message with the chunk information. You also have to make sure you don’t spam the server asking for a chunk and the server also has to make sure it doesn’t send the same chunk data multiple times. So after this, I started to actually switch to multi-player and since I already had a multi-threaded chunk system in place the beginning was not that difficult. A chunk doesn’t take up that much memory so I made it so that the server has a very big List of 2000 chunks and the player can ask for any of them because they are stored into an unordered map. If the chunk that the player asks for is not available it will be created. I also store a reference of all the chunks into a list in the order that they were requested So if the chunk capacity is filled I can simply evict the oldest chunk. This data structure is called a Least Recently Used Cache. The multi-player connection is not that difficult either, The process is called handshake and basically, the server and the client will communicate back and forth and basically the Player will send his name while the server will send him all the world data. The first problem with multi-player comes when we start to place blocks. Imagine that 2 players place a block in the same place at the same time. We want the server to choose who placed the block. Well, we can’t wait for the server to confirm our block places either because that takes a lot of time. So what I did is to place a block locally and if it isn’t validated the server will notify you to unplace it. Now I think that in Minecraft the game will undo your entire world state in a case like that including your position and I think I also have to do that in the future. Speaking of which I also want to add some nice shaders to this project because I gained Quite a lot of experience in computer graphics so that would be nice. It will take me some time because I have to first finish my Steam game so subscribe if you don’t want to miss out the part 2 of this video. Also check out this video about how you could make your own Minecraft clone, or this video about my Minecraft dungeons clone. See you there. Video Information
This video, titled ‘Can I make Multi-Player Minecraft from scratch in C++?’, was uploaded by Low Level Game Dev on 2023-09-11 16:53:51. It has garnered 2972 views and 207 likes. The duration of the video is 00:06:19 or 379 seconds.
In this video, I will try to recreate Minecraft using only C++ and OpenGL. It will also be multi-player! I will also share with you what I have learned from this programming journey!
#cpp #programming #minecraft #opengl
Source Code: https://github.com/meemknight/ourCraft
Join my Discord: https://discord.gg/eehehsPAka
Wishlist Midnight Arrow: https://store.steampowered.com/app/2310670/Midnight_Arrow/
Join this channel if you want to support me 😻: https://www.youtube.com/channel/UChkC4u4KdnIDFh9hPqExLDg/join
Make Minecraft yourself!: https://www.youtube.com/watch?v=ebeRZVHslO0
Check out my Minecraft Dungeons Clone: https://www.youtube.com/watch?v=KK-s0FZ-2oY
Check out my multi-player Terraria Clone: https://www.youtube.com/watch?v=zniPtCvyHfA
Music: Minecraft soundtrack: C418 – Minecraft Minecraft soundtrack: C418 – Living Mice Minecraft soundtrack: C418 – Haggstrom