Game Development Reference
In-Depth Information
one for U and one for V. Color is defined by four elements: red, green, blue, and
alpha. These pointers will be used by OpenGL to fetch the graphics information
that it's going to render.
The pointers need to point to the memory address at the start of each of the
arrays. The memory from that point on will be read sequentially. It then uses the
dimension information to decide when to stop reading the memory. This is why
the Vector , Color , and Point structures each have the [StructLayout
(LayoutKind.Sequential)] attribute in their definitions. The order of
their members is very important; swap them around and the rendering won't
work as expected.
To tell OpenGL to read from these pointers and render the data, the execution is
returned to the Draw method. The glDrawArrays method is called; this takes
in a type, a stride, and the number of vertices to draw. The type is GL_TRIAN-
GLES , as each sprite is made from two triangles and this is how the vertex in-
formation should be interpreted. The stride is zero. The stride is how much
memory to skip after reading each vertex. Sometimes for efficiency reasons dif-
ferent vertex information is all packed together in one continuous stretch of
memory. The stride ensures the irrelevant bits can be skipped over. In the batch,
all the data is relevant so no stride is needed. The final argument is how many
vertices are to be rendered; this information has been recorded in the _batch-
Size member.
The final command in Draw is to reset the _batchSize . The data in the arrays
doesn't need to be emptied as none of it will be drawn without overwriting it
with new sprite data.
Modifying the Renderer
Batched drawing has a little more setup than using glBegin and glEnd , but it's
not that much more complicated. The final task is to replace the old glBegin ,
glEnd rendering in the Renderer with the new batch method.
class Renderer
{
Batch _batch = new Batch();
public void DrawSprite(Sprite sprite)
{
_batch.AddSprite(sprite);
}
 
Search WWH ::




Custom Search