I’m attempting to draw two objects - a tilemap and a player sprite. The tilemap is drawn using two vertex buffers (one static VertexPosition buffer for geometry; one dynamic Vector2 buffer for uvs). The player sprite is then drawn on top of the tilemap with a single VertexPositionTexture buffer.
Individually each object is drawn correctly. When drawn together, the first frame is rendered as expected, but any frame after the tilemap appears to be missing it’s texture data (though the geometry is drawn correctly). If I wrap the player sprite’s VertexPositionTexture buffer in a VertexBufferBinding - the tilemap works again. But if I call SetVertexBuffer(…player sprite data…) after calling SetVertexBuffers(…tilemap data…), it breaks.
Tilemap Constructor:
this.vertexPositions = new VertexPosition[tileCount * 4];
this.vertexUvs = new Vector2[tileCount * 4];
this.indices = new int[tileCount * 6];
this.vertexPositionBuffer = new VertexBuffer(
graphicsDevice,
VertexPosition.VertexDeclaration,
this.vertexPositions.Length,
BufferUsage.WriteOnly);
vertexUvDeclaration = new VertexDeclaration(
new VertexElement[]
{
new VertexElement(
0,
VertexElementFormat.Vector2,
VertexElementUsage.TextureCoordinate,
0)
});
this.vertexUvBuffer = new DynamicVertexBuffer(
graphicsDevice,
vertexUvDeclaration,
this.vertexPositions.Length,
BufferUsage.WriteOnly);
this.indexBuffer = new IndexBuffer(
graphicsDevice,
IndexElementSize.ThirtyTwoBits,
this.indices.Length,
BufferUsage.WriteOnly);
this.vertexBufferBindings = new VertexBufferBinding[]
{
new VertexBufferBinding(this.vertexPositionBuffer),
new VertexBufferBinding(this.vertexUvBuffer)
};
Tilemap Draw()
this.vertexUvBuffer.SetData(this.vertexUvs);
basicEffect.GraphicsDevice.SetVertexBuffers(vertexBufferBindings);
basicEffect.GraphicsDevice.Indices = this.indexBuffer;
basicEffect.Texture = texture;
basicEffect.View = camera.View;
basicEffect.Projection = camera.Projection;
basicEffect.World = Matrix.CreateTranslation(position);
foreach (var pass in basicEffect.CurrentTechnique.Passes)
{
pass.Apply();
basicEffect.GraphicsDevice.DrawIndexedPrimitives(
PrimitiveType.TriangleList,
0,
0,
this.indexBuffer.IndexCount / 3);
}
Player Sprite Constructor
this.vertexBuffer = new VertexBuffer(
graphicsDevice,
VertexPositionTexture.VertexDeclaration,
this.vertices.Length,
BufferUsage.WriteOnly);
this.indexBuffer = new IndexBuffer(
graphicsDevice,
IndexElementSize.ThirtyTwoBits,
this.indices.Length,
BufferUsage.WriteOnly);
Player Sprite Draw
basicEffect.GraphicsDevice.SetVertexBuffer(this.vertexBuffer);
basicEffect.GraphicsDevice.Indices = this.indexBuffer;
basicEffect.Texture = texture;
basicEffect.View = camera.View;
basicEffect.Projection = camera.Projection;
basicEffect.World = Matrix.CreateTranslation(playerPosition);
foreach (var pass in basicEffect.CurrentTechnique.Passes)
{
pass.Apply();
basicEffect.GraphicsDevice.DrawIndexedPrimitives(
PrimitiveType.TriangleList,
0,
0,
this.indexBuffer.IndexCount / 3);
}