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:

  • GraphicsDevice, so you have to instantiate them. You can do this through the GeometricPrimitive class. Code: Creating and using a primitive
    1. // creationvar myCube = GeometricPrimitive.Cube.New(GraphicsDevice);var myTorus = GeometricPrimitive.Torus.New(GraphicsDevice);// ...// draw one on screenmyCube.Draw(CommandList, EffectInstance);
    EffectInstance when drawing. For information on loading effects, please see Effects and shaders.

    Custom drawing

    VertexDeclaration has to be defined. A vertex declaration describes the elements of each vertex and their layout.For details, see the VertexElement reference page. VertexDeclaration. VertexBufferBinding can be created. Code: Creating a vertex buffer
    1. // Create a vertex layout with position and texture coordinatevar layout = new VertexDeclaration(VertexElement.Position<Vector3>(), VertexElement.TextureCoordinate<Vector2>()); // Create the vertex buffer from an array of verticesvar vertices = new VertexPositionTexture[vertexCount];var vertexBuffer = Buffer.Vertex.New(GraphicsDevice, vertices);// Create a vertex buffer bindingvar vertexBufferBinding = new VertexBufferBinding(vertexBuffer, layout, vertexCount);
    PrimitiveType to draw have to be included in the pipeline state object. The buffer itself can be set dynamically. Draw(Int32, Int32). Code: Binding and drawing vertex buffers
    1. // Set the pipeline statepipelineStateDescription.InputElements = vertexBufferBinding.Layout.CreateInputElements();pipelineStateDescription.PrimitiveType = PrimitiveType.TriangleStrip;// Create and set a PipelineState object// ...// Bind the vertex buffer to the pipelinecommandList.SetVertexBuffers(0, vertexBuffer, 0, vertexBufferBinding.Stride);// Draw the verticescommandList.Draw(vertexCount);
    DrawIndexed(Int32, Int32, Int32). Code: Drawing indexed vertices
    1. // Create the index buffervar indices = new short[indexCount];var is32Bits = false;var indexBuffer = Buffer.Index.New(GraphicsDevice, indices);// set the VAOcommandList.SetIndexBuffer(indexBuffer, 0, is32Bits);// Draw indexed verticescommandList.DrawIndexed(indexBuffer.ElementCount);