Graphics Reference
In-Depth Information
28. We will now render the quad using the following code placed in the DoRender
override. We will use the same topology as the triangle renderer; however, this
time we also need to set the index buffer and use them when drawing the vertices:
var context = this.DeviceManager.Direct3DContext;
// Tell the IA we are using a triangle list
context.InputAssembler.PrimitiveTopology = SharpDX.Direct3D.
PrimitiveTopology.TriangleList;
// Set the index buffer
context.InputAssembler.SetIndexBuffer(quadIndices, Format.R16_
UInt, 0);
// Pass in the quad vertices (note: only 4 vertices)
context.InputAssembler.SetVertexBuffers(0, quadBinding);
// Draw the 6 vertices that make up the two triangles in the quad
// using the vertex indices
context.DrawIndexed(6, 0, 0);
// Note: we have called DrawIndexed to use the index buffer
29. Compile and run the project and you will now see a result similar to the figure shown
at the beginning of this recipe.
To add the key down and mouse wheel handlers for rotating the objects,
copy the code from the sample code that can be downloaded for this topic
from Packt's website. With this code in place, the scene can be rotated around
the x, y, and z axes using the arrow keys and mouse wheel. The camera is
moved with the W , A , S , and D keys and Shift + mouse wheel. Pressing X
will reinitialize the device—this is useful for testing the initialization code.
How it works…
We have started by creating our HLSL shader code. This consists of one constant buffer that
stores the WVP matrix, two structures that store the input/output of the vertex shader and
also the input for the pixel shader, and our two shader methods for the vertex shader and
pixel shader.
cbuffer PerObject : register(b0) {
// WorldViewProjection matrix
float4x4 WorldViewProj;
};
The preceding constant buffer declaration is named PerObject and will be loaded using the
first constant buffer register, that is, b0 (also known as slot 0). The buffer consists of a single
4 x 4 affine transform matrix, which is our precalculated WVP matrix. The name itself is of no
consequence. It is the register number/slot number and name of the properties within that
are important.
 
Search WWH ::




Custom Search