• <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)--架構(gòu)簡(jiǎn)介(譯)

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

             

            簡(jiǎn)介

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

                Within hours the GitHub repository was forked more than 400 times,大家在不同的平臺(tái)上編譯并且查看游戲的內(nèi)部實(shí)現(xiàn)機(jī)制。我也馬上開始了Mac版本代碼的學(xué)習(xí)。

                這份代碼在清晰度和注釋方面,是繼id Software發(fā)布Doom iPhone代碼后,最好的一份代碼。我建議每一個(gè)愛好者都去讀一讀,并研習(xí)這份代碼的實(shí)現(xiàn)。

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

             

            背景

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

                視頻在這里

                由卡馬克創(chuàng)造的“統(tǒng)一的照明和陰影”概念在這個(gè)視頻里表現(xiàn)得很有活力,我將在后面的渲染部分再介紹這部分內(nèi)容。

             

            初次的接觸

                Doom3源代碼發(fā)布現(xiàn)在開始用github,而不再是以前的ftp server。

                從TTimo發(fā)布的原始代碼很容易在Visual Studio 2010上編譯。不幸的是,不能用Visual Studio 2010 Express版編譯,因?yàn)镈oom3代碼中使用了MFC,而Express版沒有。

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

                      

             

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

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

                      

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

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

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

            架構(gòu)

                解決方案(solution)中項(xiàng)目的劃分反映了整個(gè)引擎的架構(gòu)


             

                下面以更可視化的方式來概括這個(gè)架構(gòu)

            doom3_diagram2

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

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

            雜記:你仔細(xì)觀察上面的架構(gòu)圖,可能會(huì)發(fā)現(xiàn)有一些基本框架(例如FileSystem)只在Doom3.exe中。這里就會(huì)有一個(gè)問題,就是當(dāng)gamex86也要加載資源的時(shí)候。

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

            答:這些子系統(tǒng)是從Doom3.exe中動(dòng)態(tài)加載到gamex86.dll中的,這也是指示箭頭出現(xiàn)的原因。

             

                如果我們使用PE explorer等工具查看gamex86.dll,我們會(huì)看到gamex86.dll導(dǎo)出了一個(gè)函數(shù):GetGameAPI:  

            這是同Quake2加載renderer和game 動(dòng)態(tài)庫(kù)方式一模一樣:當(dāng)Doom3啟動(dòng)gamex86.dll的時(shí)候,會(huì)交換對(duì)象的指針

            • 通過LoadLibrary將dll加載到進(jìn)程的內(nèi)存空間中
            • 通過GetProcAddress獲取GetGameAPI在gamex86.dll中的地址
            • 調(diào)用GetGameAPI

            gameExport_t * GetGameAPI_t( gameImport_t *import );

                在最后完成“握手”時(shí),Doom3.exe有一個(gè)指針指向idGame對(duì)象,gamex86.dll有一個(gè)指針指向gameImport_t對(duì)象,這個(gè)對(duì)象包含了gamex86.dll中沒有的子系統(tǒng),比如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;

             

            注意:一個(gè)好消息是,id tech4的開發(fā)者在Doom3 SDK 文檔頁很好的描述了各個(gè)子系統(tǒng)。

             

            C… ++ ?!

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

            "

            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年的時(shí)候有評(píng)論

            "
            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

            "

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

             

            主循環(huán)概覽

                這里是主循環(huán)展開后的代碼:(更詳細(xì)的在這里

            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引擎的標(biāo)準(zhǔn)主循環(huán),除了Sys_StartAsyncThread函數(shù)表明Doom3是多線程的,這個(gè)線程的目的是為了處理時(shí)間敏感的功能,引擎不希望這部分被幀率限制住。

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

            雜記:id Tech4的高層對(duì)象都是抽象類,這將會(huì)使程序在運(yùn)行時(shí)從虛函數(shù)表中查找虛函數(shù)地址而導(dǎo)致性能問題。但是這里有一個(gè)“技巧”來避免這個(gè)問題。所有的對(duì)象被以靜態(tài)的方式實(shí)例化,如下:

                    idCommonLocal    commonLocal;                 // Implementation

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

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

            雜記:已經(jīng)讀過6、7款id Software引擎的我發(fā)現(xiàn),有一些函數(shù)名從doom1開始就一直沒有改變過:負(fù)責(zé)處理鼠標(biāo)和游戲桿輸入的函數(shù)仍然叫IN_frame()

             

            渲染器

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

             

            新的標(biāo)準(zhǔn)

                新的標(biāo)準(zhǔn)可以概括為一個(gè)詞:Unification

                前一代引擎在渲染所有元素的時(shí)候,使用的是不同的方式,經(jīng)常要花費(fèi)很長(zhǎng)時(shí)間的預(yù)處理。Quake2中的基于光能傳遞的lightmap,在設(shè)計(jì)者能看到最終的效果前需要等很長(zhǎng)時(shí)間。

             

            物理引擎

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

             

            腳本和虛擬機(jī)

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

             

            地圖加載

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

             

            結(jié)語

                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 閱讀(4509) 評(píng)論(4)  編輯 收藏 引用 所屬分類: DOOM3

            評(píng)論

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

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

            。。。鹵煮B格甚高 膜拜  回復(fù)  更多評(píng)論   

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

            Mac上面可以使用AppCode開發(fā)C++
            直接把XCode爆成渣  回復(fù)  更多評(píng)論   

            # re: Doom3源碼剖析(2)--架構(gòu)簡(jiǎn)介(譯) 2015-08-08 17:57 cokabear

            @做游戲的老G
            確實(shí),推薦appcode的很多  回復(fù)  更多評(píng)論   

            久久久久亚洲av无码专区喷水 | 日本亚洲色大成网站WWW久久| 99久久中文字幕| 久久精品视频91| 久久久久青草线蕉综合超碰| 久久亚洲春色中文字幕久久久 | 久久久噜噜噜久久中文字幕色伊伊| 免费一级欧美大片久久网| 亚洲狠狠婷婷综合久久蜜芽| 97久久久久人妻精品专区| 香蕉99久久国产综合精品宅男自 | 99久久777色| 伊人伊成久久人综合网777| 国内精品久久久久久久97牛牛| 精品国产91久久久久久久a| 久久夜色精品国产欧美乱| 久久综合九色综合久99| 色88久久久久高潮综合影院| 青青青青久久精品国产h久久精品五福影院1421 | 99久久免费国产特黄| 欧美亚洲国产精品久久高清 | 性做久久久久久久久老女人| 国产精品久久久久影视不卡| 久久婷婷五月综合国产尤物app| 久久精品国产清自在天天线| 女人香蕉久久**毛片精品| 国内精品久久久久影院日本| 久久午夜福利无码1000合集| 色婷婷狠狠久久综合五月| 国产亚洲精久久久久久无码AV| 国内精品伊人久久久久AV影院| 亚洲国产精品无码久久久蜜芽| 亚洲综合久久久| 99久久这里只精品国产免费| 欧美久久一区二区三区| 久久国产成人午夜aⅴ影院| 亚洲成人精品久久| 狠狠色综合网站久久久久久久| 国产精品免费久久| 久久精品国产亚洲AV不卡| 久久丝袜精品中文字幕|