struct Vector3;
struct Vector4;
struct Vertex
{
Vector3 position;
Vector4 color;
}
{
// Create a triangle using three vertexes.
Vertex vertexData[] = {
// Position , Color
{ Vector3(0, 0, 0), Vector4(1, 0, 0, 1) }, // Red
{ Vector3(1, 0, 0), Vector4(0, 1, 0, 1) }, // Green
{ Vector3(1, 1, 0), Vector4(0, 0, 1, 1) }, // Blue
};
...
}
When loading vertex data to the graphics device it is necessary to describe the properties of the data so it may be interpreted correctly by the input assembler.
#include
{
// Each vertex consists of two elements, a position and a color.
// First we describe the position element.
VertexElement positionElement = {
0, // There is zero bytes offset in the structure before a position element
DataFormat::Float, // We use floating point data for our coordinates
3, // We use three components, x, y, z, for the position.
ElementSemantic::Position, // The intended use of this element is as a position.
0 // It is the first element using the position semantic.
};
// Then we describe the color element.
VertexElement colorElement = {
3 * sizeof(float), // There are three floats, the position element, preceding the color element in our vertex structure.
DataFormat::Float, // We use floating point data for our colors.
4, // We have four components, rgba, for our color.
ElementSemantic::Color, // The intended use of this element is coloring.
0 // It is the first element using the color semantic.
};
// Create a new vertex format to hold our element descriptions.
VertexFormat vertexFormat;
vertexFormat.elements.push_back(positionElement);
vertexFormat.elements.push_back(colorElement);
...
}
#include
{
...
// Get a pointer to the buffer management interface. The device must be initialized previously.
auto buffers = device->getBuffers();
// Load our vertex data into a new vertex buffer.
auto vertexBufferHandle = buffers->loadVertexBuffer(
vertexData, // Pointer to our vertex data in memory
3, // The number of vertexes in our data set.
vertexFormat // Description of a vertex in the buffer.
);
if (HandleIsValid(vertexBufferHandle)) {
// Success!
}
...
}