Transforming the World of DirectX with C++

Welcome back to the thrilling realm of Hardware 3D, where we’ll embark on a journey of experimentation and exploration. In this tutorial, we’ll take our quick and dirty draw test triangle function and push it to its limits. Get ready to break some stuff and see what sticks to the wall!

Transforming the World of DirectX with C++
Transforming the World of DirectX with C++

Visualizing the Power of Triangles

Let’s start by drawing not just one triangle, but two! All we need to do is add three more vertices to our array. Can you guess what kind of triangle this will create just by looking at the coordinates? Take a moment to visualize it in your mind before we reveal the answer.

Triangles and Winding

If your prediction was correct, you might have realized that triangles have a concept called “winding.” By default, the pipeline will perform back face culling, meaning it will discard triangles with incorrect winding. In other words, it will only render triangles with a specific winding order. So, if your second triangle appears to be missing, fear not! All we have to do is adjust the winding by interposing two of the vertices. Now, we should see two separate triangles.

Lines, Lines, Lines

Let’s break away from the monotony of triangles and experiment with lines. To achieve this, we’ll make a simple change to the primitive topology. Instead of using a triangle list, we’ll switch to a line list. But what do you expect to happen? Will we get a single line or something more? Take a guess before we unveil the results.

Further reading:  3D Printed PlayStation Watch

Demystifying Line Lists

Surprise! We get just one line. But why? The answer lies in the fact that a line list requires pairs of vertices to create lines. Since we only have three vertices, we can only create one line. To draw a triangle using lines, we need to connect each vertex sequentially. Remember, there’s no winding for lines; their order doesn’t matter.

Stripping Away Redundancies

While we’ve been duplicating our vertices for each triangle, there’s a more efficient way to handle this. Enter the line strip, a primitive configuration perfect for rendering lines or triangles. By using a strip, we don’t need to repeat vertices but instead connect them in an ordered sequence. However, remember to double up on the last vertex if you want to close the shape.

Colors that Blend and Shine

Now, let’s add some pizzazz to our plain white triangle by assigning unique colors to each vertex. To achieve this, we’ll need to venture into the world of shaders. First, we modify the input layout to include a color element alongside the position. Then, we update the C++ structure to include RGB values for each vertex. Finally, we tweak the vertex and pixel shaders to handle the newly added color attribute.

The Power of Index Drawing

Up until now, we’ve been redundantly specifying our vertices for each triangle. But there’s a more efficient solution: index drawing. Instead of repeating ourselves, we can define the vertices once and then reference them using indices. By creating an index buffer and modifying our draw function, we can achieve the same result with fewer vertices, saving both memory and processing power.

Further reading:  The Wonders of the Doom Fire Algorithm

Viewport: Scaling and Clipping

Last but not least, let’s explore the viewport and its impact on our rendering. By defining a viewport, we can control the size and position of our scene. This is useful not only for scaling down or splitting the screen but also for clipping unwanted portions of our image. Adjust the viewport parameters, and witness the transformation firsthand.

In conclusion, this tutorial has revealed only a fraction of the possibilities that DirectX and C++ can offer. By diving deeper into the intricacies of this powerful tool, you’ll uncover endless opportunities to create stunning visuals and push the boundaries of what’s possible in the world of 3D graphics. Happy coding!

Techal

YouTube video
Transforming the World of DirectX with C++