DEV Community

gagecantrelle
gagecantrelle

Posted on

MINECRAFT-How does it work? (part 3)

If you haven’t seen Parts 1 and 2, I recommend you look at them before continuing to read this. If not, you may be confused about what is happening and why it is done like this. In part 2 we mentioned shaders, but never really talked about it due to it being able to be its own blog. Well, today let's talk about shaders and what they are.

Shaders are functions that help render images/models with vertexes. If you remember Part 1 talked about the graphic pipeline, four of the steps involve shaders. Let's talk about the two most common steps vertex input and fragment shading. For vertex input, it is used to transform geometry from 3d world space to normalize coordinates.
This uses a projection matrix, a view matrix, and a transformation matrix. When we model a 3d object we would model it around the center of origin, allowing us to apply a transformation. Transformation of the coordinates to world space.
Image description
It is helpful because let's say you are working with multiple game objects. You can think about the coordinates of a game object as if they existed in the world space.

Image description
For example, let's say you are developing a 2d game using Pixels. You Can define your world space to work in pixels and units. Since you are the developer of this 2d world you don’t have to stick with pixels, let's say you want to use meters. Then use meters this is the world you are creating do what you want and feel comfortable with. Moving on, let's say the world is defined by a 1080px by 1920px. The camera will then see you in a 1080xp by 1920px view. This is useful because it allows you to see where they will appear and lets you think where they will appear on the screen. Since we are referencing Minecraft the unit will be using will be cubs, which will represent the width, height, and depth
Image description

Once you decide on a unit to go by you want to then use three matrices, to transform our local coordinates to normalize coordinates

1.Transformation Matrix
-has scale, rotation, and translation
-in order from top to bottom is the order on how you set up scale,
rotation, and translation
-if done in a different order it will end up in a different position than
the one you want it to be in
Image description
2.View Matrix
-the camera
-to set up the camera you need it position(eye), it direction it
pointing to(center), and a up value
-there are multiple positions for the camera to be at, so to limit its
options we set it to be only one position.

  • up similar to rotation Image description Image description 3.Projection matrix -importing for rending the game objects -you want to use the perspective matrix for a 3d game, rendering fare game objects as a small game object -a simpler way to look at it is like looking at a big object get smaller when you walk away from it Image description

Now let's take a look at how we transform the vertices sent to the vertex shaders, let write a simple program that looks like this. It will be written in GLSL, Graphics library shading language.
Image description

he first line of code will represent the version it will be using and must be the first line of the shader. Here's a link for all the versions https://en.wikipedia.org/wiki/OpenGL_Shading_Language#Versions, also the core at the end tells us to use the core version. The next line of code tells us the location of the vertex, it should seem familiar since we talked about it in part 2. We can also not tell the location and OpenGl can automatically determine the location, by removing layout (location = 0) from that line. You can even specify the location of other variables
Image description
But wait won’t this cause a problem with two vertices at the same location? No, thanks to the uniform which is GLSL, that tell will be read by the CPU and then passed to the GPU. This will create a variable with a different set of locations it can use. So we can reuse a location in attributes and uniform without both being in the same location in actual memory. The other three lines of code that use mat4 or matrix4 variable will be the code that is coming from the CPU

Next, we get to the main function which needs to be declared in all the shaders. It will hold the function that will set the gl position to the combination of all the matrices. Gl_position is a special variable that is built into the language itself. Used by OpenGl to determine the final position of a vertex. Finally, we set the vertex position to the multiplication of the matrices.
Image description
If you look at the end where a vec4 is being created that is a custom constructor for OpenGL. With these custom constructors, you can combine other vectors and scalars all these lines are equivalent in GLSL. Here is a link for those customs constructors https://www.khronos.org/opengl/wiki/Data_Type_(GLSL)#Vector_constructors.

Ok, let's take a break Like I said in part 2 of Minecraft-how does it work this is a lot to take in and can be sometimes confusing. There is a lot more to go over, unfortunately, due to some problems I’m going to keep this blog short. If you are still interested here are one link used for this blog, Other links are in parts 1 and 2. https://www.youtube.com/watch?v=yrFo1_Izlk0

Top comments (0)