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

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