Coding Minecraft in One Week - C++/OpenGL Programming Challenge

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Great job! However, I think the title is a little misleading. I imagine one of the biggest challenges of minecraft is optimization, since the terrain is fully 3D (blocks beneath the surface). Procedural generation of the 3D world with different biomes and mines beneath the surface, as well as optimization of the view frustrum would be very interesting topics to cover!

Anyway, I subscribed to your channel. It'll be interesting to see what you come up with in the future :)

👍︎︎ 54 👤︎︎ u/_georgesim_ 📅︎︎ Aug 10 2017 🗫︎ replies

This is me! Thanks for posting!

👍︎︎ 48 👤︎︎ u/Mat2012H 📅︎︎ Aug 10 2017 🗫︎ replies

holy fuck that guy is a super fast / productive coder. 4k lines of 3d game code in a week!

👍︎︎ 26 👤︎︎ u/asdfkjasdhkasd 📅︎︎ Aug 10 2017 🗫︎ replies

Anyone who can recommend more timelapse/coding + explaining videos like this? Really enjoyed watching this

👍︎︎ 9 👤︎︎ u/grAND1337 📅︎︎ Aug 10 2017 🗫︎ replies

Amazing. Wish I could program....

👍︎︎ 10 👤︎︎ u/Kolesko 📅︎︎ Aug 10 2017 🗫︎ replies
👍︎︎ 3 👤︎︎ u/whozurdaddy 📅︎︎ Aug 10 2017 🗫︎ replies
Captions
Hello and welcome to my video. In this video I'm going to be attempting to create Minecraft from one week using C++ and OpenGL. Of course this video greatly inspired by the Minecraft in one week programming challenge in Unity 3D by Shane Beck And I thought it would be fun to try it myself so let's get into this So anyways the first day was mostly spent getting your C++ OpenGl boilerplate code out of the way. For example a game state system, and the main game loop, that sort of thing. So, the first step of any opengl program is to get something to Draw onto the window so what I just did is created the model class which represents a draw OpenGl object. And now I'm creating a "quad render" to render according to the window. and it's fairly easy to do you give opengl some vertex positions, and you character draw it. And then it basically just works fine like you can see here. So now the next step is to get some shaders to work where shaders are little programs that run on GPU which tell opengl how to process the vertices and fragments are seen for example giving stuff color So if we run the program now as you can see the quad is now orange So next step is to get shaders to work with textures which is very important because the Minecraft game would be very boring looking if there was no textures on the blocks And a s can be seen here the quad now has a grass looking texture So the next step is to make the scene look 3D because of course Minecraft is a 3D game This is a simplest doing some Matrix magic in the vertex shader which essentially converts the 3D vertex positions were seen Into by basically the 2D screen coordinates which makes it appear 3D. Likely to me there's a library called the opengl math library which handles all the matrix math fully which makes scene 3D very easy So that's what the boilerplate out the way, and that was time to implement some gameplay such as a first-person camera And the first person camera can be seen in action right here And well thats the end of day one I started off day two by Trying to work out the coordinates of a simple cube to test if my coordinates were actually correct I created a cube renderer to... you know, render a cube And this is obviously needed because a Minecraft world is made out of blocks. Which are cube shaped The end result of this can be seen here And then it was time to implement the Minecraft game itself starting off with the block system Unfortunately about halfway through I realized I still need to do one more frame working thing and that will implement a texture atlas class which Will be able to extract basically the opengl texture coordinates for my texture atlas image And this can be seen here on the right there is my texture atlas and on the left is where the game itself, where I have a different texture on each side of the Cube It was now time to actually get the chunks to draw there are actually multiple ways to approach this problem The most obvious of which is to just drow every single block Individually using a like a cube renderer like I showed earlier in the video However that probably the most stupid way to do it. well I did instead was build the chunk mesh generator. Which can be seen being created in the background of this video right now There's a fairly simple thing to do you basically iterate through every single block in a chunk and then check the blocks that Surround it is one of the block faces is an air block or a other transparent block then add that block face to the mesh However, if there's a solid opaque look there, then build don't bother the block face that because that just be wasting the rendering time So what you end up with is they will essentially a shell around the chunk which has the blocks and the block textures on it As per usual the first test of the mesh building didn't go very well at all so something has to be done about this Unfortunately a couple of hours had passed and I still was able to get it working say about the end of Day 2 Approximately another era of looking in the incorrupt places before I realized it wasn't a mesh building that was wrong But rather the rendering that was wrong after to changing that single line of code it works So yeah, that's kind of annoying, but now it's time to continue so now that I've got a single chunk section of an area of 16 by 16 by 16 Blocks to draw it was time to get more chunks to draw to start off with I created sort of a chunk column class which represents a single Column of chunks Final result of this can be seen here And it works just about fine however if I go into the inside of the chunk you'll see that It's also rendering basically chunks faces. I mean block faces on the inside of the chunk Which is not ideal because you wouldn't normally be able to see these there is something had to be done about that this Involves basically looking at the Surrounding chunks and seeing if there's a solid block there it's meant to be a fairly simple problem to solve Well, I made a silly logic error and ended up taking over an error in fortunately So here's my chunk, and then we go on the inside. There's nothing to see which means it works perfectly So now that I've get increased going to work inside of the chanks i gonna say have it worked with a field of grass So that's I'm gonna try and do now generating an area of 16 by 16 chunks and then drawing them That number of chunks takes a little while to generate thing from doing it on the main thread for now But we're finally generate so you can see that it works just about fine So now that I've more generation of working. I just saw played around for about ten minutes However, it was now time to get the game to actually look like Minecraft For example making the top layer grass the bottom three layers dirt and then the rest stone To finish off day 3. I wanted to get blog editing to work I did this by making a ray cast Essentially it sends out the rays in the players current position and where hit the block of the players pressing the left mouse button It breaks that block however when they block changes in the chunk that means regenerating the sections Mesh where the block was mold changed once again problems would arise when it comes to chunk edges Because as you can see here when I break these blocks I've reached the edge of the chunk than me while the other chunks block Faces don't actually get updated luckily. This is a fairly easy fix However, that is now the end of day 3 To begin day 4 I attempted to create a skybox To do this I use an opengl feature known as a cube texture map Which is a texture in the shape of the cube in other words it's perfect for sky boxes the first test exactly go great because you could clearly see the edges of the skybox and To make matters worse you could actually fly to the edge and go outside of it Which clearly isn't really meant to happen in an attempt to fix this I simply made it so that well the cube of the skybox didn't get moved by the view Matrix of the camera this solve works Except had one big issue these skybox was static now. This is because getting rid of the view Matrix multiplication also got rid of the view matrixes on like Rotation which means the sky was not rotating so it fix this i've re-added a view Matrix once again But prevent flying the Edge of the skybox I reset the view Matrix translation this means that the skybox will only be affected by the cameras rotation and Well the result of the finished Skybox can be seen here and well. It works exactly as expected The next thing I want to do was make it to the world was able to generate more declare worlds playing the game because wh- Waiting for the both to load is not very fun at all so to do this I made it so that exactly one chunk mesh was generated per frame another option would be multi-threading But that's a bit complicated for now the problem with the way that I'm doing it is that the speed at which the world generated This sort of a lot to the framerate of the game and also means that the frame rate is lowered by the world generation But well for now. It works perfectly fine, so I'm not very too bothered about that the final filling that I wanted to do today was making it So the world generation is actually somewhat interesting so to achieve this I use the noise algorithm now honestly I don't really know how the noise works, but well, I just know how to use it so anyways using noise I generate a height map And then I fill in the chunk blocks using the height map the basic algorithm for this can be seen here But for now, it's quite messy, so I'll probably clean it up tomorrow, but for now. It works. Just fine I also decided to add trees and end result this can be seen here obviously so the trees are looking a bit strange Thanks to chunk edges being very annoying, but I can fix this tomorrow and that's the end of Day 4 To start off day 5. I was basically cleaning a lot of the chunk code I also add the crosshair so I could see more easier which block the player is looking at anyways after cleaning the code for a little bit I decided to trying add an event system essentially is mean sending events to an event handler And then the event handler sort of handling the events the first event of which what I did was we player dig events and this Is the event nature's cooled when we player either places or removes a block after doing that I decided to try and make the world infinite it's very simple to do rather than checking chunks in a fixed area you check them Around the player the first test wasn't exactly very impressive But you can see how the chunks are trying to generate around the player however I felt like I didn't really need this so I simply went back to the fixed size node and remove the infinite terrain after this I decided to try to add a collision Detection system, but unfortunately we clip went missing Regardless of those unfortunate events the Collision Detection system can be seen in action right here cause the jumping a bit of a strange But well it works, so that's the important thing. I then wanted to try out something I've never actually tried before and that thing it's an item system So the time was that when you break a block it would add that block into the players inventory and when you place a block The it would remove the item so today I created multiple classes in first class was material which essentially holds data about whole tool Materials the second class was itemstack which essentially holds information About how many items are in a stack and a player sort of holds an array of these? Items stack objects and then the player dig events or handles was how the blocks are removed and added into the player's inventory Now on a stay my implementation problems and passwords but to be fair it is my first time doing something (indistinctly) ok job and this can be see in action right now For now I just displayed the inventory as plain text on the left. as you can see the clare's inventory is currently empty But when I go to break some grass blocks it get added into the players inventory furthermore when I go to break some dirt blocks They get added into a different item stack and when we so close to below break some logs or so i could place the blocks you can see the numbers going down and when it reaches zero The item gets reset back into well nothing so yeah that means it works fine And you probably notice I'm not actually using the collision Detection anymore, and that's because it's all stopped working But I can fix that and well, that's the end of Day 5 so to start off Day 6 I did some very basic optimization starting off with chunk mesh building. You see, before, I was looking through every single block when you don't exactly need to do this and about 90% of the chunk Column is completely underground and occluded by other solid blocks. In other words, those blocks would have no chance of getting their block faces added into the chunk so basically I split the chunk into layers I counted how many opaque rocks were on each layer essentially before leaping through a layer in the trunk mesh building you can check the Adjacent layers to see if there's any translucent blocks and if there are any Translucent blocks then you can look through it to build the mesh However, if it's all opaque then there's no point looking for it in the console though I'm printing out how long it takes to build a mesh here You can see it with the optimizations turned off where it's taking multiple milliseconds to build a chunk Which is not good and then with the optimizations turned on you can see it's taking a fraction of milliseconds Unless it's one of the Top chunks another optimization I did was frustum culling which can be seen in very exaggerated effect right here essentially this thing's just drawing the chunks on the camera's view which just save them a rendering time really Optimizations and player what would be appropriate to try and get rid of the non fixed size world re-add be sort of infinite worlds and while doing that I also wanted to improve how the world generates and this sort of circle around The player rather than just in lines this can be seen here when I open up the game there were loads in a circle around the player and when I fly around the world loads in and if I turn around you can see the world getting deleted and then it just continues to load in a circle around the player and just To prove actually is infinite you can see some sped up footage right here but just looking at this the world is very boring It's basically just an infinite forest And so it was time to completely redo the world generation and add some biomes To create biomes a noise generator can be used where different ranges of numbers Apply to different biomes and well this can be seen here where the biome map was created using some very basic noise This was the first tests were are spawned right into a desert with some grasslands in the background of course for now There's no trees forsake of the re-write. now This is all good and over. We're a different volume means a different surface block But it's a bit boring rudy wouldn't it be better of different biomes are different like height map generation and well That's exactly what I did next Of course the trees have no leaves But that's not the important thing here the different biomes now actually do have different height map generations But obviously the border between them is a very short and harsh sheer cliff basically and this is very expected because you can't exactly expect a very nice natural transition between different noise generation functions luckily This is a fairly easy fix this problem can be solved using something called bilinear interpolation So what I did is essentially work out the height that's saying every four blocks But you can see here and let's just say you want to find the value at the question marks there where x equals two and z equals two in the function arguments each function here does some Magic and Then it pops out a value and then you can do this for every single block and then you end up with a fairly smooth transition And this is how it looks you can see that between the biomes. There are an hour Well that some water transition between them and well that looks a lot better than sheer cliffs like before However that as the end of day six now, and there's just one more day to go I spent the final day making the game Just look a bit better to start off with I added an ocean biome and then I added directional lighting for the blocks This is basically where different block faces have different light values depending on their direction for example The top box will have the most likely slides will have slightly less light and then the bottom will have the least light the directional lighting can be seen here and hopefully as you can see it's a huge improvement even before and just for comparison sake a very short before video the next thing I want to do was improve what the trees that light because right now They're practically lollipops and they also added palm tree generation for the ocean biomes and the new Trees and the palm trees can be seen here While back you may have noticed what I did water, but right now it just looks like a solid block so I wanted to make it look translucent to achieve this I actually had to run to the water separately thing with solid blocks always What opengl gets all confused with a rendering order and it ends up looking really strange? The a downside to this is it means it requires to draw for schedules. If there's water there But you know I think it's definitely worth it because this looks a lot better than it did before The final touches I wanted to do to beautify the world was to add some small plants of course this means adding some more blocks but also means Changing how the mesh building works a little bit the usually blocks have a cube shaped mesh but plants are not cube shapes they're more of an X-shaped mesh this simply means using some different opengl coordinates when they plant is about to be added to a chunks mesh Plants have them added into the world using the terrain generation depending which biome they're in the most common plant types of the rows in the tall grass But in the desert you'll also find dead shrubs however it isn't the FPS now It's pretty embarrassing and it's because everything is still being done on a single thread the rendering and the world loading So the final thing to do today is to simply put them on different threads, and then it will improve the FPS hopefully So the first test can be seen here and as you can see the FPS has been improved by a huge amount But unfortunately the whole world is black, but luckily I know why the opengl typically only actually works in one throat so if you could opengl Functions from two separate threads then you go to funkytown what you can see here what I was doing was creating OpenGL texture objects on the threads But isn't the rendering thread and fast it was all black However, if I fix that and certainly correct thread it now works fine And then you get the textured world and the unfold the very improved FPS and well that marks the end of the final day So that is it that's the end of seven days of creating Minecraft using C++ and OpenGL while it may not be the most beautiful with the most featureful version of Minecraft in the world There of course there's only so much you can actually do in seven days so anyways I hope you enjoyed the video I'll link to the source code as well as the download link for the game itself will both be found in the Description below and please comment below what you want to see next and anyway once again to thank you for watching and good bye
Info
Channel: Hopson
Views: 5,028,236
Rating: undefined out of 5
Keywords: Unity, Unreal, Engine, MineCraft, How, To, Tutorial, C++, SFML, Programming, Game, Dev, Sky, does, minecraft, pewdiepie, Multithreaded, tut, how, to, make, games, android, ios, apple, lwjgl, java, C#
Id: Xq3isov6mZ8
Channel Id: undefined
Length: 18min 13sec (1093 seconds)
Published: Thu Aug 03 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.