Graphics Reference
In-Depth Information
Rather than creating duplicate vertices for the two points where the triangles align along their
hypotenuse, we will use an index buffer. This allows us to reduce the number of vertices sent
to the IA by reusing the same vertices by index—if we used an index buffer for the axis lines,
we would have used 12 vertices instead of 18. Although this isn't entirely necessary for our
examples, larger meshes will quickly see a reduction in the memory bandwidth used.
// v0 v1
// |-----|
// | \ A |
// | B \ |
// |-----|
// v3 v2
quadIndices = ToDispose(Buffer.Create(device, BindFlags.IndexBuffer,
new ushort[] {
0, 1, 2, // A
2, 3, 0 // B
}));
Here, we have four vertices in the vertex buffer. To build triangle A , we will use the indexes 0 ,
1 , and 2 ; and to build triangle B , we will use the indexes 2 , 3 , and 0 .
By default, a clockwise vertex direction will define the front face of a primitive (for example,
from our camera position) drawing a triangle from top-left to right to bottom-right and
then back to top-left will mean that the back face is away from the camera. Rotating the
scene by 180 degrees using the arrow keys will show that the shape no longer renders
because of back-face culling. Try reversing the vertex direction to see what happens.
We can override the default direction by assigning a RasterizerState object to the
context.Rasterizer.State property, where the RasterizerStateDescription.
IsFrontCounterClockwise property can be set to true or false , as appropriate.
When we draw the triangle list for the quad, we must set an index buffer and a vertex buffer.
Indices can be defined using 16 or 32 bits. In this example, we have used an unsigned short
(16-bit). So we must use a format of Format.R16_Uint when setting the index buffer.
To let the pipeline know that we are using the index buffer, we must call the DrawIndexed
method rather than Draw . Notice that although we only defined four vertices, we have asked it
to draw six (the number of indices).
context.DrawIndexed(6, 0, 0);
 
Search WWH ::




Custom Search