• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            zgpxgame

            像作詩一樣寫程序,像彈琴一樣敲代碼,像看小說一樣讀開源

            Doom3源碼剖析(2)--架構簡介(譯)

                前段時間看到國外一博客上的幾篇Doom3代碼分析的文章,感覺分析得不錯,就自己翻譯了一下,以下是譯文,有不太理解地方就直接貼了原文。大部分是按自己的理解翻譯的,可能有些地方譯的并不精確。有喜歡的朋友可以在這里看原文:http://fabiensanglard.net/doom3/,這里歡迎各種討論。

             

            簡介

                2011年11月23日,id Software依照慣例發布了前一代引擎的源碼,這次發布的是Doom3引擎,id Tech 4

                Within hours the GitHub repository was forked more than 400 times,大家在不同的平臺上編譯并且查看游戲的內部實現機制。我也馬上開始了Mac版本代碼的學習。

                這份代碼在清晰度和注釋方面,是繼id Software發布Doom iPhone代碼后,最好的一份代碼。我建議每一個愛好者都去讀一讀,并研習這份代碼的實現。

                這里是我的筆記,里面記錄了我理解到的一些內容。像往常一樣,我將其整理地清晰了一些,我希望它能幫助一些朋友節省一些時間,并且我也希望它能促使我們閱讀更多的代碼,并成為更優秀的程序員。

             

            背景

                拿到這份具有開創性的引擎代碼是一件很令人興奮的事情。早在2004年Doom3發行時,它確立了實時3D圖形和音頻的新標準。它首次使技術上允許美術創作人員像好萊塢模式那樣去表達自己的想法。盡管8年過去了,首次在Delta Labs 4遇見HellKnight的情景仍然很震撼:

                視頻在這里

                由卡馬克創造的“統一的照明和陰影”概念在這個視頻里表現得很有活力,我將在后面的渲染部分再介紹這部分內容。

             

            初次的接觸

                Doom3源代碼發布現在開始用github,而不再是以前的ftp server。

                從TTimo發布的原始代碼很容易在Visual Studio 2010上編譯。不幸的是,不能用Visual Studio 2010 Express版編譯,因為Doom3代碼中使用了MFC,而Express版沒有。

                windows代碼在這里:git clone https://github.com/TTimo/doom3.gpl.git

                      

             

                我喜歡在Mac系統下用XCode瀏覽代碼。相比于Visual Studio,它在搜索速度和變量的高亮方面都給我很享受的體驗。我想用XCode來編譯程序,但是項目設置有一些問題。不過很容易通過幾個簡單的步驟修復好。這里有一個github已包含了所有的修改。

                git clone https://github.com/badsector/Doom3-for-MacOSX-

                      

            這兩份代碼都很容易編譯,可以一鍵完成。

          1. 下載代碼
          2. 點F8或Command+B (譯注:大概是xcode構建快捷鍵)
          3. 完成

            雜記:為了運行游戲,base文件夾下需要包含Doom3 的資源。由于我的機器上沒有cd驅動器,所以我下載了Steam版本。似乎id Software也做了同樣的事,在Visual Studio的項目設置里仍然包含了這樣的字符"+set fs_basepath C:\Program Files (x86)\Steam\steamapps\common\doom 3" !

            架構

                解決方案(solution)中項目的劃分反映了整個引擎的架構


             

                下面以更可視化的方式來概括這個架構

            doom3_diagram2

                Doom3的大部分代碼在2004年10月的時候可以通過發布的Doom3 SDK中看到,但是SDK里沒有Doom3執行文件部分代碼。在SDK中可以構建idLib部分和gamex86部分,核心引擎部分那時仍然是閉源的。

            注意:在游戲模塊(gamex86),每一個類都是從idClass繼承的,這樣引擎可以執行內部RTTI,并且也可以通過類名字來實例化類。

            雜記:你仔細觀察上面的架構圖,可能會發現有一些基本框架(例如FileSystem)只在Doom3.exe中。這里就會有一個問題,就是當gamex86也要加載資源的時候。

            問:那么gamex86是如何處理的呢?

            答:這些子系統是從Doom3.exe中動態加載到gamex86.dll中的,這也是指示箭頭出現的原因。

             

                如果我們使用PE explorer等工具查看gamex86.dll,我們會看到gamex86.dll導出了一個函數:GetGameAPI:  

            這是同Quake2加載renderer和game 動態庫方式一模一樣:當Doom3啟動gamex86.dll的時候,會交換對象的指針

            • 通過LoadLibrary將dll加載到進程的內存空間中
            • 通過GetProcAddress獲取GetGameAPI在gamex86.dll中的地址
            • 調用GetGameAPI

            gameExport_t * GetGameAPI_t( gameImport_t *import );

                在最后完成“握手”時,Doom3.exe有一個指針指向idGame對象,gamex86.dll有一個指針指向gameImport_t對象,這個對象包含了gamex86.dll中沒有的子系統,比如idFileSystem。

            typedef struct {

                int                                version;                  // API version

                idSys *                            sys;                      // non-portable system services

                idCommon *                         common;                   // common

                idCmdSystem *                      cmdSystem;                // console command system

                idCVarSystem *                     cvarSystem;               // console variable system

                idFileSystem *                     fileSystem;               // file system

                idNetworkSystem *                  networkSystem;            // network system

                idRenderSystem *                   renderSystem;             // render system

                idSoundSystem *                    soundSystem;              // sound system

                idRenderModelManager *             renderModelManager;       // render model manager

                idUserInterfaceManager *           uiManager;                // user interface manager

                idDeclManager *                    declManager;              // declaration manager

                idAASFileManager *                 AASFileManager;           // AAS file manager

                idCollisionModelManager *          collisionModelManager;    // collision model manager

            } gameImport_t;

             

            typedef struct {

                int            version;     // API version

                idGame *       game;        // interface to run the game

                idGameEdit *   gameEdit;    // interface for in-game editing

            } gameExport_t;

             

            注意:一個好消息是,id tech4的開發者在Doom3 SDK 文檔頁很好的描述了各個子系統。

             

            C… ++ ?!

            • 在id Software歷史上這是第一次用C++代碼代替了C。關于此事我問了卡馬克:

            "

            Hello John,

            I wonder what motivated to move the team to C++ for idtech4.

            Fab

            "

            "

            There was a sense of inevitability to it at that point, but only about half the programmers really had C++ background in the beginning. I had C and Objective-C background, and I sort of "slid into C++" by just looking at the code that the C++ guys were writing. In hindsight, I wish I had budgeted the time to thoroughly research and explore the language before just starting to use it.

            You may still be able to tell that the renderer code was largely developed in C, then sort of skinned into C++.

            Today, I do firmly believe that C++ is the right language for large, multi-developer projects with critical performance requirements, and Tech 5 is a lot better off for the Doom 3 experience.

            John Carmack

            "

            他也在2004年的時候有評論

            "
            Is C++ a better language for developing games than C? Today, most games are developed in C++, and I know that Doom 3 was developed in C++. Having worked with both C and C++ for quite a while, do you think C++ is really a better language for developing games?

            "

            "

            Yes, it is. I'm not a fan of the more complex features of C++, but grouping things into classes is a Good Thing.

            John Carmack

            "

            • 代碼中使用了大量抽象和多態。但是一個漂亮的技巧可以避免某些對象因虛函數表而帶來的性能問題。
            • 所有的資源都使用可讀性很好的文本方式存儲。沒有太多的二進制內容。代碼中廣泛地使用了Lexer/Parser的組合。
            • 模板被用在低層實用類中(idLib),但是從不會在高層代碼中出現,不會像Google的V8代碼那樣刺瞎你的眼。
            • 在代碼注釋方面,它是id Software的代碼中第二好的,最好的是Doom iPhone代碼,可能因為它比Doom3是更近的一段時間編寫的。
            • 看一下編碼約定也是很有趣的事情,在這里

             

            主循環概覽

                這里是主循環展開后的代碼:(更詳細的在這里

            idCommonLocal    commonLocal;                   // OS Specialized object

            idCommon *       common = &commonLocal;         // Interface pointer (since Init is OS dependent it is an abstract method

            int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )

            {

                Sys_SetPhysicalWorkMemory( 192 << 20, 1024 << 20 );   //Min = 201,326,592 Max = 1,073,741,824

                Sys_GetCurrentMemoryStatus( exeLaunchMemoryStats );

                Sys_CreateConsole();

                SetErrorMode( SEM_FAILCRITICALERRORS );

                for ( int i = 0; i < MAX_CRITICAL_SECTIONS; i++ ) {

                    InitializeCriticalSection( &win32.criticalSections[i] );

                }

                Sys_Milliseconds();

                common->Init( 0, NULL, lpCmdLine );               // Assess how much VRAM is available (not done via OpenGL but OS call)

                Sys_StartAsyncThread() // 開啟異步線程

                {                      // 異步線程展開(譯注:異步線程中并沒有找到類似的代碼)

                    while ( 1 )

                    {

                        usleep( 16666 );                         // Run at 60Hz

                        common->Async();                         // Do the job

                        Sys_TriggerEvent( TRIGGER_EVENT_ONE );   // Unlock other thread waiting for inputs

                        pthread_testcancel();                    // Check if we have been cancelled by the main thread (on shutdown).

                    }

                }

             

                Sys_ShowConsole

                while ( 1 )

                {

                    Win_Frame();

                    common->Frame();

                }

            }

             

                這是id Software引擎的標準主循環,除了Sys_StartAsyncThread函數表明Doom3是多線程的,這個線程的目的是為了處理時間敏感的功能,引擎不希望這部分被幀率限制住。

            • 聲音處理
            • 用戶輸入處理

            雜記:id Tech4的高層對象都是抽象類,這將會使程序在運行時從虛函數表中查找虛函數地址而導致性能問題。但是這里有一個“技巧”來避免這個問題。所有的對象被以靜態的方式實例化,如下:

                    idCommonLocal    commonLocal;                 // Implementation

                    idCommon *       common = &commonLocal;       // Interface manipulated in the code

            這樣編譯器在編譯時就可以決定被調用的函數,從而不用去查找虛函數表。這實際上是一個雙贏,since the pointer does not have to be dereferenced at runtime either since its address is known at compile time. 

            雜記:已經讀過6、7款id Software引擎的我發現,有一些函數名從doom1開始就一直沒有改變過:負責處理鼠標和游戲桿輸入的函數仍然叫IN_frame()

             

            渲染器

                Doom3中最令人興奮的當然是渲染器部分。這里有太多的內容,所以我將這部分的筆記分到另一篇文章里了。在這里
                如果你對(場景)預處理和入口系統(portal system)很感興趣,可以看這里

             

            新的標準

                新的標準可以概括為一個詞:Unification

                前一代引擎在渲染所有元素的時候,使用的是不同的方式,經常要花費很長時間的預處理。Quake2中的基于光能傳遞的lightmap,在設計者能看到最終的效果前需要等很長時間。

             

            物理引擎

            (譯注:原文這里沒有內容,大概還沒寫好)

             

            腳本和虛擬機

            (譯注:原文這里沒有內容,大概還沒寫好)

             

            地圖加載

                我記憶中,加載所消耗的時間很長,我一直想知道在加載場景的時候,程序背后都做了些什么。

             

            結語

                It was not always easy to focus...

                But overall it was very educationnal to read most of it.

          4. posted on 2012-04-25 00:07 zgpxgame 閱讀(4508) 評論(4)  編輯 收藏 引用 所屬分類: DOOM3

            評論

            # re: Doom3源碼剖析(2)--架構簡介(譯)[未登錄] 2013-09-27 14:57 lee

            我喜歡在Mac系統下用XCode瀏覽代碼。相比于Visual Studio,它在搜索速度和變量的高亮方面都給我很享受的體驗

            。。。鹵煮B格甚高 膜拜  回復  更多評論   

            # re: Doom3源碼剖析(2)--架構簡介(譯) 2015-01-23 17:19 做游戲的老G

            Mac上面可以使用AppCode開發C++
            直接把XCode爆成渣  回復  更多評論   

            # re: Doom3源碼剖析(2)--架構簡介(譯) 2015-08-08 17:57 cokabear

            @做游戲的老G
            確實,推薦appcode的很多  回復  更多評論   

            伊人久久无码中文字幕| 嫩草伊人久久精品少妇AV| 精品久久久久久综合日本| 久久久综合香蕉尹人综合网| 一本色道久久综合亚洲精品| 久久精品99久久香蕉国产色戒 | 亚洲国产精品一区二区久久hs| 99久久免费国产精品| 久久国产精品99精品国产987| 国产精品成人久久久久三级午夜电影| 久久精品亚洲精品国产色婷| 99久久婷婷国产综合精品草原| 四虎国产精品成人免费久久| 欧美午夜A∨大片久久 | 999久久久免费国产精品播放| 欧美日韩中文字幕久久久不卡 | 久久午夜夜伦鲁鲁片免费无码影视| 色综合久久中文字幕无码 | 亚洲中文字幕无码久久精品1 | 久久精品国产WWW456C0M| 91麻精品国产91久久久久| 久久强奷乱码老熟女网站| 久久久久亚洲av成人网人人软件 | 久久99九九国产免费看小说| 久久美女人爽女人爽| 四虎国产精品免费久久久| 久久综合香蕉国产蜜臀AV| 久久精品国产亚洲AV不卡| 久久久艹| 久久激情亚洲精品无码?V| 精品久久久久久久久中文字幕| 日产精品久久久久久久| 狠狠色狠狠色综合久久| 久久精品国产免费观看三人同眠| 久久精品国产99久久香蕉| 久久久久亚洲AV成人网人人软件| 国产成人综合久久综合| 日本欧美国产精品第一页久久| 国产精品久久久久乳精品爆| 精品久久久久久久中文字幕 | 99久久99这里只有免费费精品|