Tags
A bit late to post but better late than ever, this week has been all about 3D-graphics, we have gotten a small introduction on how to initialize DirectX in a program. But we have also gained knowledge on how the computer sees graphical structures. When we see a box the computer actually sees it as triangles, well…if you go even deeper the computer sees the triangles as vertices (dots) in a 3D-space, between these dots it connects with lines in order to form the triangle.
I’ll show some code to explain a bit further:
vertices[] =
{
// triangle 1
{ -1.0f, 1.0f, 0.0f, 0x99FF99 },
{ 1.0f, 1.0f, 0.0f, 0x000000ff },
{ 1.0f, -1.0f, 0.0f, 0x99FF99 },
}
If you imagine a coordinate system with X,Y and Z-axis and let’s for the simplicity of it make 1 as its maximum value. At the center of the screen you will find origo (0,0,0). From there we will try to create a triangle. Now before we start we must also remember in what order we want the vertices to be calculated, this is important because it will determine on how the surface/texture is displayed. DirectX counts the vertices clockwise so in order to have the surface being displayed from the right angle we must also count clockwise (unless we define it to count counter-clockwise). So back to the triangle, starting from origo we place our first vertice at the top left corner with no depth (-1,1,0) and then we want it to connect to the next vertice at the top right corner with zero depth (1,1,0) and the last one to be connected is at the bottom right corner with no depth (1,-1,0). When the third vertice is calculated the third and the first will be connected as well in order to form a triangle; like this:
How it actually looks though is like this, do notice that I’ve enabled colors in my surface (the hexadecimal values in the code). The colors are enabled by writing your own shader. Bu any way, here’s how the triangle really looks like:
And by using triangle you can create more complex structures like the lectures final shapes; a cube (sorry for the weird angle):
And that concludes my coding blog post of the week 🙂