【1. 概覽】 Ogre支持Quake3的bsp格式。相關(guān)的代碼在“Plugin_BSPSceneManager”工程中。主要的類(lèi)有以下幾個(gè):
Class BspSceneNode:
BspSceneNode是SceneNode的派生類(lèi),是專(zhuān)門(mén)提供給BSPSceneManager使用的。主要是提供針對(duì)于BSP tree的可見(jiàn)性判斷。這個(gè)類(lèi)并不是BSP tree的node,BSP tree中的node使用BspNode。BspSceneNode會(huì)放入BSP tree的leaf節(jié)點(diǎn)中。由于SceneNode使用包裹盒的方法,不可分割,所以一個(gè)BspSceneNode可能放入多個(gè)Bsp tree的leaf節(jié)點(diǎn)中。
從類(lèi)的定義看,BspSceneNode并沒(méi)有額外的保存什么數(shù)據(jù)。重寫(xiě)的幾個(gè)虛函數(shù)主要是用來(lái)通知BspSceneMapager, BspSceneNode::_update()會(huì)調(diào)用BspSceneManager::_notifyObjectMoved(),detach objcect會(huì)調(diào)用BspSceneManager::_notifyObjectDetached()。
Class BspSceneManager:
粗略的看BspSceneManager與OctreeSceneManager類(lèi)似。首先保存了一個(gè)BspLevel的指針,然后使用一個(gè) walkTree()函數(shù),用來(lái)遍歷tree結(jié)構(gòu)。由于Quake使用BSP leaf tree,所以多了一個(gè)processVisibleLeaf()函數(shù)。另外一個(gè)明顯的不同是有一個(gè)renderStaticGeometry()函數(shù), “Renders the static level geometry tagged in walkTree”。此函數(shù)渲染“mMatFaceGroupMap”中的所有數(shù)據(jù)。BSP一個(gè)好處是不透明面可以front-back的順序來(lái)渲染,而透明面back-front來(lái)渲染,OGRE是如何將此特性保存到MaterialFaceGroupMap的呢?
Class BspLevel:
這是一個(gè)核心的class。他存儲(chǔ)了BSP的所有數(shù)據(jù),關(guān)鍵的數(shù)據(jù)有:
<!--[if !supportLists]-->1. <!--[endif]-->“BspNode* mRootNode;”――BSP tree的根節(jié)點(diǎn)
<!--[if !supportLists]-->2. <!--[endif]-->“VertexData* mVertexData;”――整個(gè)level的所有頂點(diǎn);
<!--[if !supportLists]-->3. <!--[endif]-->“StaticFaceGroup* mFaceGroups;”――faces
<!--[if !supportLists]-->4. <!--[endif]-->“BspNode::Brush *mBrushes;”――用來(lái)做碰撞檢測(cè)的Brush,是QuakeBSP除了渲染以外的另外一個(gè)精華!Brush的名字有點(diǎn)怪,其實(shí)就是一個(gè)convex volume,可以減少CD的運(yùn)算量。
<!--[if !supportLists]-->5. <!--[endif]-->“VisData mVisData;”――PVS數(shù)據(jù),是又一個(gè)Quake中的精華!當(dāng)初Carmark在設(shè)計(jì)Quake的時(shí)候還使用軟件渲染,hiden surface removal和減少over draw是最另他頭痛的問(wèn)題。BSP的思想應(yīng)該是他從網(wǎng)上看來(lái)的,不過(guò)PVS應(yīng)該是他所創(chuàng)。PVS大大減少了over draw。(見(jiàn)《Michael Abrash's Graphics Programming Black Book》)
<!--[if !supportLists]-->6. <!--[endif]-->“PatchMap mPatches;”――Quake3支持貝賽爾曲面
關(guān)鍵的函數(shù):
<!--[if !supportLists]-->1. <!--[endif]-->bool isLeafVisible(const BspNode* from, const BspNode* to) const;使用PVS來(lái)檢測(cè)可見(jiàn)性。
<!--[if !supportLists]-->2. <!--[endif]-->void _notifyObjectMoved(const MovableObject* mov, const Vector3& pos); void _notifyObjectDetached(const MovableObject* mov); à void tagNodesWithMovable(BspNode* node, const MovableObject* mov, const Vector3& pos); 把MovableObject(注意:不是SceneNode)掛到BSP的leaves上。
Class BspNode:
這是Bsp中的另外一個(gè)重要的類(lèi)了。Node和Leaf都使用這個(gè)類(lèi)。
重要數(shù)據(jù):
<!--[if !supportLists]-->1. <!--[endif]-->Plane mSplitPlane; BspNode* mFront; BspNode* mBack; 分割平面和前后節(jié)點(diǎn);
<!--[if !supportLists]-->2. <!--[endif]-->int mVisCluster; 每個(gè)cluster占pvs的一個(gè)bit,這是為了減少pvs占用的內(nèi)存。
<!--[if !supportLists]-->3. <!--[endif]-->int mNumFaceGroups; int mFaceGroupStart; 用來(lái)找到BspLevel中哪些face group是屬于我這個(gè)leaf的,這樣做也是為了優(yōu)化存儲(chǔ);
<!--[if !supportLists]-->4. <!--[endif]-->IntersectingObjectSet mMovables; 和本節(jié)點(diǎn)相交的movable對(duì)象
<!--[if !supportLists]-->5. <!--[endif]-->NodeBrushList mSolidBrushes; 本節(jié)點(diǎn)包含的brush。
另外剩下的OgreQuake3Level.h、OgreQuake3Shader.h、OgreQuake3ShaderManager.h、 OgreQuake3Type.h主要是為了把Quake3格式的bsp,shader信息讀入,并轉(zhuǎn)換成Ogre本地的bsp定義以及 Material。現(xiàn)在quake3的源碼已經(jīng)公開(kāi)(非常感謝id software以及carmark),可以結(jié)合quake3的源碼來(lái)看。
【2. Quake3 bsp的加載】 以Demo_BSP為例,首先需要修改“quake3settings.cfg”,兩個(gè)參數(shù),“Pak0Location”是pk包的路徑(是一個(gè)zip文件),“Map”為想要加載的地圖。
OGRE使用BspLevel來(lái)存儲(chǔ)Bsp場(chǎng)景信息,這個(gè)類(lèi)是與文件格式無(wú)關(guān)的。所以需要另外一個(gè)類(lèi)來(lái)把Quake3的bsp文件讀入。
Quake3Level的讀盤(pán)的主要由兩個(gè)函數(shù)完成:
1、“void Quake3Level::loadHeaderFromStream()”。調(diào)用的流程是:
BspApplication::loadResources()
à ResourceGroupManager::loadResourceGroup()【A】
à BspSceneManager::estimateWorldGeometry()
à BspLevel::calculateLoadingStages()
àQuake3Level::loadHeaderFromStream()
Quake3 BSP的文件格式很簡(jiǎn)單明了,前面是一個(gè)文件頭,后面是幾個(gè)數(shù)據(jù)塊。文件頭主要存儲(chǔ)了幾個(gè)lump,包含數(shù)據(jù)塊的起始位置和大小,通過(guò)lump,可以直接seek到對(duì)于的數(shù)據(jù)塊。 此函數(shù)在加載了文件頭之后,調(diào)用了Quake3Level:: initialiseCounts ()函數(shù),主要是計(jì)算了每個(gè)lump包含的對(duì)象的個(gè)數(shù),例如face,vertex,bursh等等。
2、第二個(gè)函數(shù)“void Quake3Level::loadFromStream()”。調(diào)用的過(guò)程是:
ResourceGroupManager::loadResourceGroup()【A】
àBspSceneManager::setWorldGeometry()
àBspResourceManager::load()
àResourceManager::load()
àResource::load()
àBspLevel::loadImpl()
àQuake3Level::loadFromStream()
在這地方OGRE延續(xù)了他羅嗦的風(fēng)格,BspSceneManager要通過(guò)BspResourceManager來(lái)加載場(chǎng)景,BspLevel實(shí)現(xiàn)為一種 Resource,BspResourceManager通過(guò)標(biāo)準(zhǔn)的ResourceManager――》Resource來(lái)找到BspLevel,然后調(diào)用其加載函數(shù)。
此函數(shù)首先構(gòu)造了一個(gè)“MemoryDataStream”對(duì)象,在 MemoryDataStream的構(gòu)造函數(shù)中把文件數(shù)據(jù)全部讀入其緩沖中(Quake也是這樣干的),然后調(diào)用“void Quake3Level::initialisePointers(void)”函數(shù),得到所有l(wèi)ump索引的對(duì)象的指針。
Quake3Level 把文件讀入并明確了所有數(shù)據(jù)的指針之后,在void BspLevel::loadImpl()中調(diào)用“BspLevel::loadQuake3Level()”函數(shù)講Quake3level中的數(shù)據(jù)拷貝到自己的數(shù)據(jù)對(duì)象中。主要執(zhí)行了以下幾個(gè)操作:
<!--[if !supportLists]-->1. <!--[endif]-->“BspLevel::loadEntities()”,這個(gè)lump存的是一個(gè)字符串,用來(lái)描述一些游戲信息,Ogre的這個(gè)函數(shù)只讀取了Player start信息(位置和角度)。
<!--[if !supportLists]-->2. <!--[endif]-->“Quake3Level:: extractLightmaps()”。Quake3 BSP的每個(gè)light map都是128×128大,此函數(shù)將Light map lump中的數(shù)據(jù)逐個(gè)調(diào)用“TextureManager::loadImage()”創(chuàng)建成Texture對(duì)象(class D3D9Texture for D3D9 RenderSystem)。
<!--[if !supportLists]-->3. <!--[endif]-->創(chuàng)建VertexData: [Create vertex declaration] OGRE BspLevel使用的頂點(diǎn)格式為:Position3,Normal3,Diffuse,uv0,uv1; [Build initial patches] 調(diào)用BspLevel::initQuake3Patches()。此函數(shù)遍歷Quake3Level中的所有faces,對(duì)于每個(gè)face type為“BSP_FACETYPE_PATCH”的face,創(chuàng)建一個(gè)PatchSurface對(duì)象,并調(diào)用PatchSurface:: defineSurface()函數(shù)進(jìn)行,然后保存到BspLevel:: mPatches數(shù)組中。此函數(shù)還計(jì)算了BspLevel:: mPatchVertexCount和BspLevel:: mPatchIndexCount; [硬件頂點(diǎn)緩沖] 調(diào)用HardwareBufferManager創(chuàng)建HardwareVertexBuffer對(duì)象;使用“BspLevel:: quakeVertexToBspVertex()”函數(shù)把q3 bsp頂點(diǎn)格式轉(zhuǎn)換為Ogre BSPLevel的頂點(diǎn)格式。然后綁定到BspLevel::mVertexData;
<!--[if !supportLists]-->4. <!--[endif]-->創(chuàng)建Faces:創(chuàng)建BspLevel:: mLeafFaceGroups數(shù)組;創(chuàng)建BspLevel:: mFaceGroups數(shù)組,此數(shù)組的數(shù)據(jù)在后面一步中填充;創(chuàng)建indexbuffer,并將Quake3Level::mElements拷貝進(jìn)來(lái);
<!--[if !supportLists]-->5. <!--[endif]-->Create materials for shaders:對(duì)于Quake3Level::mFaces每一個(gè)bsp_face_t,找到它索引的Quake3Shader,并創(chuàng)建 Material,如果沒(méi)有找到Quake3Shader的話(huà)則使用shader name去查找貼圖文件; 在此循環(huán)中還進(jìn)行了“Copy face data”的操作,填充BspLevel:: mFaceGroups中的數(shù)據(jù);
<!--[if !supportLists]-->6. <!--[endif]-->Nodes:創(chuàng)建BspLevel:: mRootNode數(shù)組,并將數(shù)據(jù)拷貝進(jìn)來(lái)。
<!--[if !supportLists]-->7. <!--[endif]-->Brushes:將數(shù)據(jù)拷貝到BspLevel:: mBrushes中;
<!--[if !supportLists]-->8. <!--[endif]-->Leaves:設(shè)置每個(gè)leaf節(jié)點(diǎn)的數(shù)據(jù),主要包括包裹盒,mFaceGroupStart,mNumFaceGroups,mVisCluster,mSolidBrushes。參見(jiàn)BspNode類(lèi);
<!--[if !supportLists]-->9. <!--[endif]-->Vis data:將數(shù)據(jù)拷貝到BspLevel:: mVisData中。
Quake3 BSP的load流程基本上就是這些了。
【3. Bsp tree scene的渲染】 仍然以Demo_BSP為例來(lái)分析。渲染的核心操作流程從SceneManager::_renderScene()開(kāi)始(參見(jiàn)“Ogre學(xué)習(xí)筆記(3):Mesh的渲染流程”),接下來(lái)還有SceneManager:: _updateSceneGraph(),SceneManager::prepareRenderQueue(),BspSceneManager沒(méi)有重寫(xiě)這幾個(gè)函數(shù)。不過(guò),有一點(diǎn)需要注意,SceneManager:: _updateSceneGraph()調(diào)用了BspSceneNode::_update()與OctreeSceneManager類(lèi)似的,如果有必要的話(huà),會(huì)調(diào)用BspSceneManager:: _notifyObjectMoved()--》BspLevel:: _notifyObjectMoved(),將SceneNode中的MovableObject attach到正確的leaf node中。 接下來(lái)是BspSceneManager:: findVisibleObjects(),這是一個(gè)從SceneManager重寫(xiě)的函數(shù)。順理成章的,這個(gè)函數(shù)調(diào)用了 BspSceneManager::walkTree()。在這個(gè)函數(shù)中,首先找到了camera所在的leaf node(通過(guò)BspLevel::findleaf()函數(shù));然后遍歷BspLevel中的每個(gè)leaf node,先使用PVS檢測(cè)可見(jiàn)性(通過(guò)BspLevel::isLeafVisible()函數(shù)),如果可見(jiàn)再使用camera――bounding box檢測(cè),如果還是可見(jiàn)的,則對(duì)此leaf node調(diào)用BspSceneManager::processVisibleLeaf()函數(shù)。此函數(shù)主要執(zhí)行兩個(gè)操作,一個(gè)是把World Geometry加入到渲染數(shù)據(jù)表中(mFaceGroupSet和mMatFaceGroupMap),另外一個(gè)是把與此leaf node相交的MoveableObject加到渲染隊(duì)列中(mMovablesForRendering)。一件比較有疑問(wèn)的事情是,walkTree 是循環(huán)遍歷所有l(wèi)eaf node,而沒(méi)有按照BSP tree遞歸遍歷,這大大削弱了BSP的提前排序的優(yōu)勢(shì)。
然后是BspSceneManager重寫(xiě)了另外一個(gè)重要的函數(shù)_renderVisibleObjects()。此函數(shù)包含兩個(gè)操作,一個(gè)是 renderStaticGeometry(),另外一個(gè)是調(diào)用父類(lèi)的SceneManager::_renderVisibleObjects()。前者循環(huán)遍歷mMatFaceGroupMap,然后調(diào)用RenderSystem::_rendr();后者已經(jīng)在“Ogre學(xué)習(xí)筆記(3):Mesh的渲染流程”中詳細(xì)分析過(guò)了。

|
posted on 2007-07-18 02:44
七星重劍 閱讀(911)
評(píng)論(0) 編輯 收藏 引用 所屬分類(lèi):
Game Engine 、
OGRE