?? 構建高級粒子系統.htm
字號:
particles within the specific system will have the same
texture assigned.</TD></TR>
<TR bgColor=#ffffff>
<TD class=ColorCode vAlign=top width="18%">BlendMode</TD>
<TD class=ColorCode vAlign=top width="19%">blendMode</TD>
<TD class=EtoC vAlign=top width="63%">The blend mode you want
to use for the particles. Smoke will probably have a different
blend mode from blood-that's the reason you also store the
blend mode for each particle system.</TD></TR>
<TR bgColor=#ffffff>
<TD class=ColorCode vAlign=top width="18%">int</TD>
<TD class=ColorCode vAlign=top width="19%">systemType</TD>
<TD class=EtoC vAlign=top width="63%">A unique ID, which
represents the type of system (smoke or sparks, for example).
The systemType identifier is also required, since you may want
to check for a specific type of particle system within the
collection of all systems. For example, to remove all smoke
systems, you need to know whether a given system is a smoke
system or not.</TD></TR>
<TR bgColor=#ffffff>
<TD class=ColorCode vAlign=top width="18%">Array Particle</TD>
<TD class=ColorCode vAlign=top width="19%">particles</TD>
<TD class=EtoC vAlign=top width="63%">The collection of
particles within this system. This may also be a linked list
instead of an array.</TD></TR>
<TR bgColor=#ffffff>
<TD class=ColorCode vAlign=top width="18%">Array PShape</TD>
<TD class=ColorCode vAlign=top width="19%">shapes</TD>
<TD class=EtoC vAlign=top width="63%">A collection of shapes,
describing the shapes of the particles. The shape descriptions
of the particles usually consist of four positions in 3D
camera-space. These four positions are used to draw the two
triangles for our particle. As you can see in Table 1, a
particle is only stored as a single position, but it requires
four positions (vertices) to draw the texture-mapped shape of
the particle.</TD></TR>
<TR bgColor=#ffffff>
<TD class=ColorCode vAlign=top width="18%">int</TD>
<TD class=ColorCode vAlign=top width="19%">nrAlive</TD>
<TD class=EtoC vAlign=top width="63%">Number of particles in
the system which are still alive. If this value is zero, it
means all particles are dead and the system can be
removed.</TD></TR>
<TR bgColor=#ffffff>
<TD class=ColorCode vAlign=top width="18%">BoundingBox3</TD>
<TD class=ColorCode vAlign=top width="19%">boundingBox</TD>
<TD class=EtoC vAlign=top width="63%">The 3D axis-aligned
bounding box (AABB), used for visibility determination. We can
use this for frustum, portal, and anti-portal checks.</TD></TR>
<TR bgColor=#ffffff>
<TD class=BGRed colSpan=3 vAlign=top>
<DIV align=center><B><FONT color=#cccccc>表2
粒子系統基類的屬性</FONT></B></DIV></TD></TR></TBODY></TABLE><BR> 下面將講述如何計算經常面對活動鏡頭的粒子的四個位置。首先,把粒子世界空間坐標變換到鏡頭空間(用世界空間的位置坐標乘以活動鏡頭矩陣)使用粒子的大小屬性來計算四個頂點。<BR><BR>
<TABLE align=center border=0 cellPadding=2 cellSpacing=0
class=BGRed>
<TBODY>
<TR align=middle>
<TD><IMG height=180 src="AParSystem-3.gif"
width=187></TD></TR>
<TR align=middle>
<TD><FONT color=#cccccc><B>圖3
為渲染粒子的建立形狀</B></FONT></TD></TR></TBODY></TABLE><BR> 我們使用這四個形成形狀的頂點來渲染粒子,雖然一個粒子只有一個位置:xyz。為了渲染一個粒子(例如一個火花),我們需要建立一個形狀(用4個頂點)。然后在四個頂點之間我們得到兩個三角形。想象一個非伸展的粒子總是面對你前面的鏡頭,就像圖3所描述的一樣。對我們來說,粒子總是面對活動鏡頭的,這意味著我們可以簡單的加減粒子的鏡頭空間坐標的x和y值。換句話說,保留z值不變就好像你在作2D一樣。你可以在清單1中看到一個計算的例子。<BR><BR> <SPAN
class=ColorCatchword>// 清單1 — Calculating the shape of a particle
facing the camera.</SPAN><BR> <SPAN class=ColorCode>void
ParticleSystem::SetupShape(int nr)<BR> {<BR> assert(nr <
shapes.Length());</SPAN> <SPAN class=ColorCatchword>// make sure we
don't try to shape anything we<BR> // don't
have<BR><BR> // calculate cameraspace
position<BR></SPAN> <SPAN class=ColorCode>Vector3 csPos =
gCamera->GetViewMatrix() *
particles[nr].position;<BR></SPAN><BR> <SPAN
class=ColorCatchword>// set up shape vertex
positions<BR></SPAN> <SPAN class=ColorCode>shapes[nr].vertex[0] =
csPos + Vector3(-particles[nr].size, particles[nr].size,
0);<BR> shapes[nr].vertex[1] = csPos + Vector3(
particles[nr].size, particles[nr].size,
0);<BR> shapes[nr].vertex[2] = csPos + Vector3(
particles[nr].size, -particles[nr].size,
0);<BR> shapes[nr].vertex[3] = csPos +
Vector3(-particles[nr].size, -particles[nr].size,
0);<BR> }</SPAN><BR><A name=3.0></A><BR><B>3、函數</B>(<SPAN
class=English>The
Functions</SPAN>)<BR> 現在我們知道在粒子系統的基類中需要什么屬性,我們能夠開始思考需要什么樣的函數。既然是基類大多數的函數被聲明為虛函數。每種粒子系統都用不同的方式來更新粒子屬性,所以我們需要一個虛擬的更新函數。這個更新函數將完成以下任務:
<UL>
<LI>更新所有粒子的位置和其他屬性。
<LI>如果我們不能預先計算綁定盒,則需更新它
<LI>計算存活的粒子數量。如果沒有存活的粒子則返回FALSE,反之返回TRUE。返回值將被用于判定系統是否可被刪除。
</LI></UL> 現在我們的基類已有能力來更新粒子,我們也將要建立可用新的(也可能是舊的)位置來構建的形狀。這個函數<SPAN
class=ColorCode>,SetupShape</SPAN>,必須是個虛擬函數,因為某些粒子系統會要伸展粒子,而有些系統不會這么做。你可以在清單1中看到這樣一個函數。<BR> 要向給定的系統加入一個粒子或是重新生成它,建立一個做這件事的函數將是十分有益的。同樣,這應該是個虛擬函數,并應該如此定義:<BR><BR> <SPAN
class=ColorCode>virtual void SetParticleDefault(Particle
&p);</SPAN><BR><BR> 就像我在前面解釋過的,這個函數初始化給定粒子的屬性值。但如果你想要改變煙的速度或是影響你的煙系統的風向。這是我們接觸下一個主題:粒子系統的構造函數。許多粒子系統需要他們獨特的構造函數,強迫我們在基類中建立一個虛擬構造函數和析構函數。在基類的構造函數中你應該輸入以下信息:
<UL>
<LI>你一開始想在系統中構建的粒子數量。
<LI>粒子系統的位置。
<LI>你想在系統中使用的混合模式。
<LI>你想在系統中使用的底紋或底紋文件名。
<LI>系統方式(即ID)。 </LI></UL> 在我的引擎中,粒子系統的基類中的構造函數是這樣的:<BR><BR> <SPAN
class=ColorCode>virtual ParticleSystem( int nr,<BR> rcVector3
centerPos,<BR> BlendMode blend=Blend_AddAlpha,<BR> rcString
file name=óEffects/Particles/
green_particleó,<BR> ParticleSystemType
type=PS_Manual<BR> );</SPAN><BR><BR> 那么應該在哪兒做例如煙系統的風向的各種設置呢?你可以在構造函數中加入根據特定系統的設定值,也可以在每個類中建立一個名為InitInfo的結構體(<SPAN
class=English>structure</SPAN>),其中包含所有的必須的設定值。如果你使用后一種方法,確保在構造函數中加入一個新的參數,即指向新結構的指針。如果指針為空(NULL),使用默認的設定值。<BR> 正如你所想象的,第一種方法要在構造函數中加入很多參數,而這對于程序員來說過于繁瑣。這是我不使用第一種方法的主要原因。使用第二種方法則簡單得多,我們可以在每一個粒子系統類中建立一個函數以用默認值來初始化它的結構。這段代碼的例子和演示程序可在這里得到。<BR><A
name=4.0></A><BR><B>4、粒子管理器</B>(<SPAN class=English>The Particle
Manager</SPAN>)<BR> 現在我們已經發現了每個粒子系統背后的隱藏的技術,該是創建一個管理類來控制我們的各種粒子系統時候。一個管理類將掌管創建、釋放、更新及渲染所有的系統。這樣,管理類必須有一個屬性是指向粒子系統的指針。我強烈推薦你建立或是使用一個數組模板(<SPAN
class=English>Array
Template</SPAN>),因為這將會簡單些。<BR> 你制作的粒子系統的使用者或許會希望較容易地增加系統。他們也不想跟蹤所有的系統以保證所有粒子均已死亡從而可以從內存中釋放它們。這就是設計管理類的目。管理器將會在需要時自動更新和渲染系統,并刪除已死亡的系統。<BR> 當使用不定時系統(系統將在給定的時間后死亡)時,有一個檢查系統是否已被刪除的函數是很有用的(例如,是否它還存在于粒子管理器中)。想象你創建了一個系統并存儲了指向系統的指針。你通過這個指針每一幀訪問系統。如果系統恰巧在你使用指針之前死亡,會發生什么?崩潰。這就是為什么我們需要一個檢查系統是否存活還是已經被管理類刪除的函數。粒子管理類中需要的函數列表如表3所示。<BR><BR>
<TABLE align=center bgColor=#000000 border=0 cellPadding=2
cellSpacing=1 width="95%">
<TBODY>
<TR class=BGRed>
<TD width="18%"><B><FONT color=#cccccc>函數</FONT></B></TD>
<TD colSpan=2><B></B><B><FONT
color=#cccccc>描述</FONT></B></TD></TR>
<TR bgColor=#ffffff>
<TD class=ColorCode vAlign=top width="18%">Init</TD>
<TD class=EtoC colSpan=2 vAlign=top>Initializes the particle
manager.</TD></TR>
<TR bgColor=#ffffff>
<TD class=ColorCode vAlign=top width="18%">AddSystem</TD>
<TD class=EtoC colSpan=2 vAlign=top>Adds a specified particle
system to the manager.</TD></TR>
<TR bgColor=#ffffff>
<TD class=ColorCode vAlign=top width="18%">RemoveSystem</TD>
<TD class=EtoC colSpan=2 vAlign=top>Removes a specified
particle system.</TD></TR>
<TR bgColor=#ffffff>
<TD class=ColorCode vAlign=top width="18%">Update</TD>
<TD class=EtoC colSpan=2 vAlign=top>Updates all active
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -