Game Development Reference
In-Depth Information
the code is a lot faster. With the way the code is laid out, only a few changes need
to be made to get this performance increase.
A new class is needed to collect all the vertex information before sending it to the
graphics card. A good name for this class is Batch . The Batch will take the
sprites and pack all the vertex information together in some big arrays. It then
gives OpenGL pointers to these arrays and sends a draw command. This new
class will use the Tao framework so remember to include the proper using
statements.
public class Batch
{
const int MaxVertexNumber = 1000;
Vector[] _vertexPositions = new Vector[MaxVertexNumber];
Color[] _vertexColors
= new Color[MaxVertexNumber];
Point[] _vertexUVs
= new Point[MaxVertexNumber];
int _batchSize = 0;
public void AddSprite(Sprite sprite)
{
// If the batch is full, draw it, empty and start again.
if (sprite.VertexPositions.Length + _batchSize > MaxVertexNumber)
{
Draw();
}
// Add the current sprite vertices to the batch.
for (int i = 0; i < sprite.VertexPositions.Length; i++)
{
_vertexPositions[_batchSize + i] = sprite.VertexPositions[i];
_vertexColors[_batchSize + i]
= sprite.VertexColors[i];
_vertexUVs[_batchSize + i]
= sprite.VertexUVs[i];
}
_batchSize += sprite.VertexPositions.Length;
}
}
The max vertex number is how big a batch is allowed to get before it tells
OpenGL to render all the vertices it has. The next members are the arrays that
describe all the vertex information: its position, color, and U,V coordinates. The
_batchSize member tracks how big the batch is becoming. This information
needs to be passed on to OpenGL to draw the arrays. It's also compared with the
max vertex number to decide when to draw the batch.
 
Search WWH ::




Custom Search