Hello and welcome to this little blog where I will be going through Frank D Lunas book on DirectX 12 "Introduction to
3D Game Programming
with DirectX® 12"
This is is supposed to be a blog for helping me gettig a deeper understanding for DirectX 12 however since it might be of interest to some other person out there, feel free to continue reading along this if you too want to get a better understanding of Directx12.
The first section of the book covers Math stuff you need to know, so why not dive into that. I will ofcourse be reading the entire chapters of the book and work my way through it but I will not be providing a detailed walkthrough of the the entire book as that would just take too much time ofcourse. What you will see here is a birdseye view / summary of how directx12 works. First section of the book deals with Vectors so lets start with that.
Vectors:
DirectX12 uses a left handed coordinate system, this is important to note because OpenGL used a right handed coordinate system, and thats what I am used to by now. A left handed coordinate system means that the positive Z axis is pointed away from you, not towards you. Now lets delve in to define more of what a vector is.
I think of a vector as a point in a coordinate system and we compare that point to the origin point to see what direction and length the vector has. So lets say our 2D vector is 1, 1. This would mean that the vector points up and to the right completely diagonally. Now people will tell you that "oh you shouldn't think of vectors as a point in space they can contain / mean other stuff" And to that I say --.
Magnitude
The magnitude of a vector is simply the length of a vector, or more acrrately the length of the directed line. To denote this we put ||u||, | bars around the vector "variable". And to get the magnitude we simply apply pythagoras theorom to our entire vector. So if its a 3D vector thats Sqrroot(x^2 + y^2 + z^2). This would be the magnitude of the vector.
Normalization
You might have heard about this if you have ever done any type of game programming. A common problem with 2D movement diagonally is that its faster than moving just purely left or right. This is because defining a vector as up and to the right would look like this right (1, 1, 0). Whereas moving just purely right would just look like this (1, 0, 0). As you can see were essentially moving another whole extra unit of our vector each time we move diagonally. We can explicitl see this if we calculate the magnitude of our vectors here. The vector moving diagonally would be equal to squareroot of 2 which is aproximately 1,4. We should ideally have the same speed no matter the direction in which we are moving. The way to do this is by normalizing Vectors. This would mean that the vectors magnitude alwas equals to 1. The way to do this is by taking every x , y and z component of the vector and dividing it by the the ssquareroot of the magnitude.
Dot product
A dot procut is the result of multiplying two vectors with eachother. The result is a scalar value. This scalar value we can use to determine the angle between two vectors. Essentially if the result is equal to 0 we the vectors are perpendicular to eachother. If its bigger than 0 then the angle is sharp, pointy. And if its less than 0 then its stubby or obtuse. We have to involve cos Theta into our calculations whihc means we take the scalar product and divide it by the magnitude of vector 1 times the magnitude of vector 2.
The Cross Product
Cross product is used to produce another vector perpendicular to the two vectors. Taking each element (x, y ,z) and multiplying that element with the othr two. So to calculate X you multiply x with y and z. The only exception here is the middle coordinate Y which you change the sign before the number to the opposite, so positive becomes negative and vice versa. This is all sort of abstract right now but will make more sense as we delve into it deeper.
Matrices
Matrices are what allow us to move, rotate and scale objects in 3D space. Think of them as a tool used to transform vectors.
A matrix is grid of umbers, in graphics programming typically you just use 4x4 matrices. Why is that? Because they allow us to handle all transformations in a consistent way when working with 3D vectors.
Vertices
Vertices are what store the actualy data containing our object we import. So a vertex array is an array of point which will paint up a bunch of traingles forming for example a sick dinosaur, matrices are what we apply to those vertices to translate and or transform that vertex array.
Matrix x Vector
Multiplying a matrix with a vector is one of the most important operations. It looks like this v' = M * v. What this means in practice is we take a vertex (a point in our object) and apply a transformation to it. v is the original position, M is the transformation itself and v' is the transformed position or the result.
Transformation
We will cover this more deeply in the next chapter but matrices are used to perform transformations such as moving an object, scaling an object and rotating an object. Instead of manualy changing every vertex we can just multiply them with a matrix.
You can combine multiple transformations into one, rotate then scale then move for example. We can define the matrix via for example M = T * R * S, then just apply that matrix to our vertices like v' = M * v. Efficent right we do not have to calculate each part everytime!
Order matters
Very important to note that the order in which matrix multiplications are done is very important. Rotating an object then moving it is not the same as moving an object and then rotating it! It wouldn't be the same result!
Directx12 Coordinates
You constantly work with the big three in Directx12. These are:
World matrix = Positions of the object in the world
View amtrix = represents the camera
Projection matrix = converts the 3D world into 2D screen space to be drawn on your computer screen.
Homogenous Coordinates
If you have a point (x, y, z) you can scale it, rotate it all using a matrix. But if you want to move it, you cant do that with a normal matrix.
Matrix multiplication is like
new = numbers * old
translation however is
new = old + something
Matrix math doesnt handle the + part.
So we cheat by adding a 1 at the end of our vector / point
(x, y, z, 1)
That extra 1 lets the matrix sneak in a constant value, which allows us to actually translate the location. Without the 1 there would be nothing for the matrix to multiply with to add to those values. So the 1 turns multiplication into addition so to say.
This is also essentially why we use 4x4 matrices in graphics instead of 3x3.
I have a headache now so I'm gonna end the math stuff here and cover more the directx initialization theory in the next part. See you there!
Top comments (0)