#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2009 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;
namespace Nuclex.Graphics.Batching {
/// Draws batched vertices using DrawUserPrimitive() calls
internal class UserPrimitiveBatchDrawer :
IBatchDrawer, IDisposable
where VertexType : struct, IVertexType {
/// Initializes a new DrawUserPrimitive()-based batch drawer
///
/// Graphics device that will be used for rendering
///
public UserPrimitiveBatchDrawer(GraphicsDevice graphicsDevice) {
this.graphicsDevice = graphicsDevice;
}
/// Immediately releases all resources owned by the instance
public void Dispose() { }
///
/// Maximum number of vertices or indices a single batch is allowed to have
///
public int MaximumBatchSize {
get { return PrimitiveBatch.BatchSize; }
}
/// Selects the vertices that will be used for drawing
/// Primitive vertices
/// Number of vertices to draw
/// Indices of the vertices to draw
/// Number of vertex indices to draw
public void Select(
VertexType[] vertices, int vertexCount,
short[] indices, int indexCount
) {
this.vertices = vertices;
this.indices = indices;
}
/// Selects the vertices that will be used for drawing
/// Primitive vertices
/// Number of vertices to draw
public void Select(
VertexType[] vertices, int vertexCount
) {
this.vertices = vertices;
}
/// Draws a batch of indexed primitives
///
/// Index of the first vertex in the vertex array. This vertex will become
/// the new index 0 for the index buffer.
///
/// Number of vertices used in the call
///
/// Position at which to begin processing the index array
///
/// Number of indices that will be processed
/// Type of primitives to draw
/// Desired graphics device settings for the primitives
public void Draw(
int startVertex, int vertexCount,
int startIndex, int indexCount,
PrimitiveType type, DrawContext context
) {
int primitiveCount = VertexHelper.GetPrimitiveCount(indexCount, type);
// If the DrawContext requires more than one pass, we have to draw the
// primitives multiple times
for (int pass = 0; pass < context.Passes; ++pass) {
// Enter the effect pass and send all primitives to the graphics card
context.Apply(pass);
this.graphicsDevice.DrawUserIndexedPrimitives(
type, // type of primitives to render
this.vertices, // vertex array
startVertex, // offset that will be added to all vertex indices
vertexCount, // number of vertices used in the call
this.indices, // index array
startIndex, // where in the index array to begin processing
primitiveCount // number of primitives
);
} // for
}
/// Draws a batch of primitives
/// Index of vertex to begin drawing with
/// Number of vertices used
/// Type of primitives that will be drawn
/// Desired graphics device settings for the primitives
public void Draw(
int startVertex, int vertexCount,
PrimitiveType type, DrawContext context
) {
int primitiveCount = VertexHelper.GetPrimitiveCount(vertexCount, type);
// If the DrawContext requires more than one pass, we have to draw the
// primitives multiple times
for (int pass = 0; pass < context.Passes; ++pass) {
// Enter the effect pass and send all primitives to the graphics card
context.Apply(pass);
this.graphicsDevice.DrawUserPrimitives(
type, // type of primitives to render
this.vertices, // vertex array
startVertex, // where in the vertex array to begin processing
primitiveCount // number of primitives
);
} // for
}
/// Graphics device being used for rendering
private GraphicsDevice graphicsDevice;
/// Vertices being used for drawing
private VertexType[] vertices;
/// Indices being used for drawing
private short[] indices;
}
} // namespace Nuclex.Graphics.Batching