Draw vertices

AdvancedProgrammer

When loading a scene, Xenko automatically handles the draw calls to display the scene throughout the entity system. This page introduces manual drawing.

Primitives

Xenko provides the following set of built-in primitives:

  • Plane
  • Cube
  • Sphere
  • Geosphere
  • Cylinder
  • Torus
  • TeapotThey aren't automatically created along with the GraphicsDevice, so you have to instantiate them. You can do this through the GeometricPrimitive class.

Code: Creating and using a primitive

  1. // creation
  2. var myCube = GeometricPrimitive.Cube.New(GraphicsDevice);
  3. var myTorus = GeometricPrimitive.Torus.New(GraphicsDevice);
  4. // ...
  5. // draw one on screen
  6. myCube.Draw(CommandList, EffectInstance);

They have no effect associated with them, so the user has to provide an EffectInstance when drawing. For information on loading effects, please see Effects and shaders.

Custom drawing

Outside of built-in primitives, any geometry can be drawn by creating custom vertex buffers. To create a vertex buffer, first a VertexDeclaration has to be defined. A vertex declaration describes the elements of each vertex and their layout.For details, see the VertexElement reference page.

Next, a vertex buffer can be created from an array of vertices. The vertex data type has to match the VertexDeclaration.

Given vertex buffer and declaration, a VertexBufferBinding can be created.

Code: Creating a vertex buffer

  1. // Create a vertex layout with position and texture coordinate
  2. var layout = new VertexDeclaration(VertexElement.Position<Vector3>(), VertexElement.TextureCoordinate<Vector2>());
  3. // Create the vertex buffer from an array of vertices
  4. var vertices = new VertexPositionTexture[vertexCount];
  5. var vertexBuffer = Buffer.Vertex.New(GraphicsDevice, vertices);
  6. // Create a vertex buffer binding
  7. var vertexBufferBinding = new VertexBufferBinding(vertexBuffer, layout, vertexCount);

To draw the newly created vertex buffer, it has to be bound to the pipeline. The vertex layout and the PrimitiveType to draw have to be included in the pipeline state object. The buffer itself can be set dynamically.

Afterwards, the vertices are ready to be rendered using Draw(Int32, Int32).

Code: Binding and drawing vertex buffers

  1. // Set the pipeline state
  2. pipelineStateDescription.InputElements = vertexBufferBinding.Layout.CreateInputElements();
  3. pipelineStateDescription.PrimitiveType = PrimitiveType.TriangleStrip;
  4. // Create and set a PipelineState object
  5. // ...
  6. // Bind the vertex buffer to the pipeline
  7. commandList.SetVertexBuffers(0, vertexBuffer, 0, vertexBufferBinding.Stride);
  8. // Draw the vertices
  9. commandList.Draw(vertexCount);

It is also possible to draw indexed geometry. To use an index buffer, first create it similarly to the vertex buffer and bind it to the pipeline.It can then be used for drawing using DrawIndexed(Int32, Int32, Int32).

Code: Drawing indexed vertices

  1. // Create the index buffer
  2. var indices = new short[indexCount];
  3. var is32Bits = false;
  4. var indexBuffer = Buffer.Index.New(GraphicsDevice, indices);
  5. // set the VAO
  6. commandList.SetIndexBuffer(indexBuffer, 0, is32Bits);
  7. // Draw indexed vertices
  8. commandList.DrawIndexed(indexBuffer.ElementCount);