Game Development Reference
In-Depth Information
This approach works, but it is not the most efficient. For one, the ver-
tex buffer must be big enough to hold all the particles in the system.
But more significant is that the graphics card is idling while we copy all
the particles from the list to the vertex buffer (step B). For example,
suppose our system has 10,000 particles; first we need a vertex buffer
that can hold 10,000 particles, which is quite a bit of memory. In addi-
tion the graphics card will sit and do nothing until all 10,000 particles in
the list are copied to the vertex buffer and we call DrawPrimitive .
This scenario is a good example of the CPU and graphics card not work-
ing together.
A better approach (and the approach that the Point Sprite sample
on the SDK uses) goes something like this:
Note: This is a simplified description, but it illustrates the idea. It
assumes that we will always have 500 particles to fill an entire seg-
ment, which in reality doesn't happen because we are constantly
killing and creating particles so the number of particles existing varies
from frame to frame. For example, suppose we only have 200 particles
left to copy over and render in the current frame. Because 200 parti-
cles won't fill an entire segment, we handle this scenario as a special
case in the code. This scenario can only happen on the last segment
being filled for the current frame because if it's not the last segment,
that implies there must be at least 500 particles to move onto the next
segment.
Create a fair-sized vertex buffer (say, one that can hold 2,000 parti-
cles). We then divide the vertex buffer into segments; as an exam-
ple, we set the segment size to 500 particles.
Figure 14.1: Vertex buffer with segments labeled
Then create the global variable i = 0 to keep track of the segment
that we're in.
For each frame:
A. Update all particles.
B. Until all living particles have been rendered:
1. If the vertex buffer is not full, then:
a. Lock segment i with the D3DLOCK_NOOVER-
WRITE flag.
b. Copy 500 particles to segment i .
2. If the vertex buffer is full, then:
Search WWH ::




Custom Search