14.2.1 繪制粒子系統
因為粒子系統是動態的,在每一個幀中我們需要更新系統中的粒子,對于渲染粒子系統的一種直觀但效率低下的方法如下:
創建一個足夠大的頂點緩存保存最大數量的粒子。
每一幀里執行:
A. 更新所有粒子。
B. COPY所有活著的粒子到頂點緩存。
C. 繪制頂點緩存。
這個方法正確,不過不是最有效率的。第一,頂點緩沖必須足夠大以保存系統中所有粒子。但是非常重要的是,當我們從列表拷貝所有粒子到頂點緩沖(步驟B)時,顯卡卻什么也不做。舉個例子,假設我們系統有10,000個粒子,首先我們需要一個能容納10,000個粒子的頂點緩沖,這是一個很大的內存。另外顯卡將停著什么也不做直到列表中的10,000個粒子拷到頂點緩沖,直到我們調用DrawPrimitive。這個特定情況是CPU與顯卡不同時工作的一個很好的例子。
更好的辦法(SDK中點精靈例程中用到的方法)就象這樣:
提示:這是一個簡單的描述,但它說明了這一思想。它假定我們總是有500個粒子以填充一個緩存片段,但是這是不可能發生的,因為我們經常殺死并創建粒子,所以從一幀到另一幀粒子數量是變化的。舉個例子,假設我們只剩下200個粒子要在當前幀拷貝并渲染。因為200個粒子不能填充整個緩存片段,我們用代碼處理這個特定情形。這個特定情形只有在最后的緩存片段中才會出現,因為如果不是最后的片斷,就意味著必然有500個粒子將被移到下一緩存片段。
創建一個合適尺寸的頂點緩存(能夠保存2000個粒子),然后我們劃分頂點緩存為幾個小的塊,就像這個例子,我們設置每個緩存片斷的尺寸為500個粒子。

l然后創建一個全局變量 i = 0 ,用來記錄片段。
每一幀里執行:
A. 更新所有粒子。
B. 直到所有粒子渲染完畢。
1. 如果頂點緩存沒有滿:
a 用D3DLOCK_NOOVERWRITE標記鎖定緩存片段i
b COPY 500個粒子到片段i
2. 如果頂點緩存滿了:
a 從起始的地方開始頂點緩沖: i=0
b 用D3DLOCK_NOOVERWRITE標記鎖定緩存段i
c COPY 500個粒子到片段i
3. 渲染片段i.
4. 下一片段: i+ +
備注:頂點緩存是動態的, 因此我們能利用動態鎖定標記D3DLOCK_NOOVERWRITE和 D3DLOCK_DISCARD。這兩個標記允許我們鎖定頂點緩存的某一部分。當頂點緩存中的其他部分被渲染時,它是不能渲染的。例如,假如我們正在使用D3DLOCK_NOOVERWRITE標記渲染片段0時, 當渲染片段0的時候我們能鎖定并填充片段1。這樣可以防止渲染的延遲。
這個方法更有效率。首先,我們減少頂點緩存的尺寸;然后, CPU與顯卡在協調的工作。也就是說,當我們繪制一小批粒子時(graphics card work),同時拷貝另一小批粒子到頂點緩存 (CPU work)。這個動作是連續執行的,直到所有的粒子都被渲染完畢,就像你了解的一樣, 顯卡在全部頂點緩存被填充的時候是不用處于空閑狀態的。
我們現在將注意力轉向這一個渲染方案的實現,為了方便使用這個粒子系統的渲染方案, 我們使用 cParticleSystem 類中的下列數據成員:
m_vb_num—在給定時間內我們的頂點緩存能夠保存的粒子數量。這個值與實際的粒子系統中的粒子數無關。
m_vb_offset—這個變量是頂點緩存中的偏移,在頂點緩存里我們將用它開始COPY下一批粒子,例如,如果第一批在緩存中是0到499,偏移到第二批COPY的開始處將是500。
m_vb_batch_size—定義一批緩存中的粒子數量。
我們現在介紹渲染方法的代碼:
void cParticleSystem::render()
{
// The render method works by filling a section of the vertex buffer with data, then we render that section.
// While that section is rendering we lock a new section and begin to fill that section.
// Once that sections filled we render it. This process continues until all the particles have been drawn.
// The benifit of this method is that we keep the video card and the CPU busy.
if(m_particles.empty())
return;
// set render states
pre_render();
m_device->SetTexture(0, m_texture);
m_device->SetFVF(PARTICLE_FVF);
m_device->SetStreamSource(0, m_vertex_buffer, 0, sizeof(sParticle));
//
// render batches one by one
//
// start at beginning if we're at the end of the vertex buffer
if(m_vb_offset >= m_vb_num)
m_vb_offset = 0;
sParticle* v;
m_vertex_buffer->Lock(
m_vb_offset * sizeof(sParticle),
m_vb_batch_num * sizeof(sParticle),
(void**)&v,
m_vb_offset ? D3DLOCK_NOOVERWRITE : D3DLOCK_DISCARD);
DWORD num_particles_in_batch = 0;
// until all particles have been rendered
for(list<sParticleAttribute>::iterator iter = m_particles.begin(); iter != m_particles.end(); iter++)
{
if(! iter->is_alive)
continue;
// copy a batch of the living particles to the next vertex buffer segment
v->position = iter->position;
v->color = (D3DCOLOR) iter->color;
v++; // next element
num_particles_in_batch++;
// if this batch full?
if(num_particles_in_batch == m_vb_batch_num)
{
// draw the last batch of particles that was copied to the vertex buffer
m_vertex_buffer->Unlock();
m_device->DrawPrimitive(D3DPT_POINTLIST, m_vb_offset, m_vb_batch_num);
//
// While that batch is drawing, start filling the next batch with particles.
//
// move the offset to the start of the next batch
m_vb_offset += m_vb_batch_num;
// Don't offset into memory that is outside the vb's range.
// If we're at the end, start at the beginning.
if(m_vb_offset >= m_vb_num)
m_vb_offset = 0;
m_vertex_buffer->Lock(
m_vb_offset * sizeof(sParticle),
m_vb_batch_num * sizeof(sParticle),
(void**)&v,
m_vb_offset ? D3DLOCK_NOOVERWRITE : D3DLOCK_DISCARD);
num_particles_in_batch = 0; // reset for new batch
}
}
m_vertex_buffer->Unlock();
// Its possible that the LAST batch being filled never got rendered because the condition
// (num_particles_in_batch == m_vb_batch_num) would not have been satisfied.
// We draw the last partially filled batch now.
if(num_particles_in_batch)
m_device->DrawPrimitive(D3DPT_POINTLIST, m_vb_offset, num_particles_in_batch);
m_vb_offset += m_vb_batch_num; // next block
post_render(); // reset render states
}
14.2.2 隨機
如果我們模擬雪花,不能讓所有雪花以完全相同的方式落下。我們要讓它們按相似的方式落下而不是完全相同的方式。為了使粒子系統的隨機功能更簡單,我們增加了下列兩個函數到d3dUtility.h/cpp文件。
第一個函數在[low_bound, high_bound]區間內隨機的返回一個float類型值:
float get_random_float(float low_bound, float high_bound)
{
if(low_bound >= high_bound) // bad input
return low_bound;
// get random float in [0, 1] interval
float f = (rand() % 10000) * 0.0001f;
// return float in [low_bound, high_bound] interval
return f * (high_bound - low_bound) + low_bound;
}
第二個函數在邊界盒的范圍內,輸出一個隨機的向量。
void get_random_vector(D3DXVECTOR3* out, D3DXVECTOR3* min, D3DXVECTOR3* max)
{
out->x = get_random_float(min->x, max->x);
out->y = get_random_float(min->y, max->y);
out->z = get_random_float(min->z, max->z);
}