• <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>
            Cpper
            C/C++高級工程師 Android高級軟件工程師 IT集成工程師 音頻工程師 熟悉c,c++,java,c#,py,js,asp等多種語言 程序猿
            蓋莫游戲引擎是我設(shè)計的一個2/3d游戲引擎,已經(jīng)做了1年多了。
            出于各種考慮我都需要對其進(jìn)行大規(guī)模的改寫,以便在框架上具有良好清晰的架構(gòu)(當(dāng)然也會不斷新加入很多功能)。在參考了irr和其他幾個引擎代碼之后,我把引擎的場景繼承關(guān)系設(shè)計為:
            Object->Transform->SceneNode這樣的關(guān)系
            Object是一般的對象基類
            Transform是控制世界矩陣和局部矩陣的類對象
            其代碼如下:
             1 ///////////////////////////////////////////////////////
             2 /// 用4*4矩陣來定義3d世界物體的位置和角度,并生產(chǎn)一個樹狀繼承
             3 ///////////////////////////////////////////////////////
             4 class G_DLL_API Transform : public Object
             5 {
             6 public:
             7     typedef std::vector<RefPtr<Transform> >           ChildrenList;
             8     typedef std::vector<RefPtr<Transform> >::iterator ChildrenItr;
             9 public:
            10     ///////////////////////////////////////////////////////
            11     /// 構(gòu)造,析構(gòu)變換類
            12     ///////////////////////////////////////////////////////     
            13     inline Transform(): parent(NULL),world_matrix_is_identity(false){}
            14     Transform(const Matrix4f& matrix): parent(NULL), world_matrix_is_identity(false){SetLocalMatrix(matrix);}
            15     virtual ~Transform(){} 
            16 
            17     ///////////////////////////////////////////////////////
            18     /// 增加子節(jié)點
            19     ///////////////////////////////////////////////////////         
            20     void AddChild(RefPtr<Transform> child);
            21     
            22     ///////////////////////////////////////////////////////
            23     /// 設(shè)置子節(jié)點
            24     ///////////////////////////////////////////////////////         
            25     void SetChild(int index, RefPtr<Transform> child);
            26   
            27     ///////////////////////////////////////////////////////
            28     /// 獲取子節(jié)點個數(shù)
            29     ///////////////////////////////////////////////////////      
            30     inline uint GetChildCount() const{return children.size();}
            31     
            32     ///////////////////////////////////////////////////////
            33     /// 獲取指定索引的子節(jié)點
            34     ///////////////////////////////////////////////////////         
            35     inline RefPtr<Transform> GetChild(int i);
            36     inline RefPtr<Transform> GetLastChild();
            37   
            38     ///////////////////////////////////////////////////////
            39     /// 節(jié)點移除
            40     ///////////////////////////////////////////////////////      
            41     void EraseChild(RefPtr<Transform> child);
            42     void EraseChildren(int index, int count);
            43     void EraseAllChildren();
            44     void EraseAllChildrenRecursive();
            45 
            46     RefPtr<Transform>               parent;
            47     
            48     ///////////////////////////////////////////////////////
            49     /// 獲取變換的4*4矩陣
            50     ///////////////////////////////////////////////////////         
            51     inline Matrix4f GetComputedWorldMatrix();
            52     
            53     ///////////////////////////////////////////////////////
            54     /// 設(shè)置,獲取局部矩陣
            55     ///////////////////////////////////////////////////////         
            56     void  SetLocalMatrix(const Matrix4f& matrix){world_matrix = matrix;}
            57     Matrix4f GetLocalMatrix(){return local_matrix;}
            58     
            59     ///////////////////////////////////////////////////////
            60     /// 平移,縮放,旋轉(zhuǎn)
            61     ///////////////////////////////////////////////////////         
            62     void  Translate(float x, float y, float z);
            63     void  Translate(const Vector3f& p);
            64     void  Scale(float x, float y, float z);
            65     void  Rotate(float degrees, float x, float y, float z);
            66 
            67     ///////////////////////////////////////////////////////
            68     /// 重新計算矩陣
            69     ///////////////////////////////////////////////////////    
            70     virtual void ComputeWorldMatrix(RefPtr<Camera> camera);
            71     void  ComputeWorldMatrixRecursive(RefPtr<Camera> camera);
            72     inline Matrix4f  GetWorldMatrix(){return world_matrix;}
            73  
            74      ///////////////////////////////////////////////////////
            75     /// 設(shè)置,獲取是否保持單位世界矩陣
            76     ///////////////////////////////////////////////////////    
            77     inline void SetAlwaysIdentityWorldMatrix(bool i){ world_matrix_is_identity = i; }
            78     inline bool IsAlwaysIdentityWorldMatrix(){ return world_matrix_is_identity; }
            79 
            80 protected:
            81      ///////////////////////////////////////////////////////
            82     /// 設(shè)置世界矩陣
            83     ///////////////////////////////////////////////////////    
            84     void SetWorldMatrix(const Matrix4f& matrix){world_matrix = matrix;}
            85     Matrix4f                        world_matrix; 
            86     Matrix4f                        local_matrix;
            87     ChildrenList                    children;
            88     bool                            world_matrix_is_identity;
            89 };
            然后是基本的場景節(jié)點類SceneNode
             1 //! 2010.02.06
             2 #ifndef SCENENODE_HPP 
             3 #define SNENENODE_HPP
             4 
             5 ///////////////////////////////////////////////////////
             6 /// 頭文件包含
             7 ///////////////////////////////////////////////////////
             8 #include <GEngine/Config.hpp>
             9 #include <GEngine/Geometry.hpp>
            10 #include <GEngine/Transform.hpp>
            11 
            12 namespace core
            13 {
            14 
            15 ///////////////////////////////////////////////////////
            16 /// 定義引擎基本場景節(jié)點類
            17 ///////////////////////////////////////////////////////
            18 class G_DLL_API SceneNode : public Transform
            19 {
            20 public:
            21     ///////////////////////////////////////////////////////
            22     /// 構(gòu)造,析構(gòu)場景節(jié)點
            23     ///////////////////////////////////////////////////////     
            24     SceneNode();
            25     virtual ~SceneNode();
            26 
            27 public:      
            28     ///////////////////////////////////////////////////////
            29     /// 獲取場景aabb盒子,球
            30     ///////////////////////////////////////////////////////         
            31     Box     GetSceneBox();
            32     Spheref GetSceneSphere();
            33     
            34     ///////////////////////////////////////////////////////
            35     /// 場景渲染
            36     ///////////////////////////////////////////////////////        
            37     void  BeginRender();
            38     void  Render();
            39     void  RenderAfter();
            40     
            41     ///////////////////////////////////////////////////////
            42     /// 設(shè)置,獲取是否渲染(顯示)場景
            43     ///////////////////////////////////////////////////////        
            44     void SetVisible(bool visible);
            45     void EnableVisible();
            46     void DisableVisible();
            47     bool IsVisible()const;
            48     
            49     ///////////////////////////////////////////////////////
            50     /// 設(shè)置,獲取是否自動調(diào)用視錐體剔除
            51     ///////////////////////////////////////////////////////        
            52     void SetAutoCulling(bool auto_cull);
            53     bool IsAutoCulling()const;
            54     
            55 protected:
            56     ///////////////////////////////////////////////////////
            57     /// 設(shè)置場景球,盒子 
            58     ///////////////////////////////////////////////////////    
            59     void SetSceneBox(const Box &box);
            60     void SetSceneSphere(const Spheref &sphere);
            61     
            62     ///////////////////////////////////////////////////////
            63     /// 重新計算場景box,shpere
            64     ///////////////////////////////////////////////////////        
            65     void UpdateBox();
            66     void UpdateSphere();
            67  
            68     bool    visible;
            69     bool    auto_culling;    
            70     Spheref spheref;
            71     Box     box;
            72     bool    dirty_sphere;
            73     bool    dirty_box; 
            74 };
            75  
            76 }
            77 
            78 #endif

            場景節(jié)點類在Transform的基礎(chǔ)之上增加了是否顯示,是否設(shè)置為自動調(diào)用視椎體剔除的函數(shù)
            當(dāng)然還有一般的虛擬場景渲染函數(shù)(3個)
            當(dāng)然這只是最基本的功能
            ( virtual void  BeginRender();
             virtual void  Render();
             virtual void  RenderAfter();)
            然后可以在SceneNode的基礎(chǔ)之上增加新的場景類型(比如:BillBoard等等)

            題注:其實在引擎設(shè)計過程中我一直想避免這種復(fù)雜的繼承關(guān)系,但是之后我會發(fā)現(xiàn)當(dāng)引擎功能越來越大的時候,不采用這種方式,引擎邏輯關(guān)系會變得越來越糟
            我在想之后就可以加入一個場景渲染隊列來渲染場景了.
            如果對游戲引擎設(shè)計感興趣 我們可以交流探討下(ccsdu2009@sohu.com)
            posted on 2010-02-07 12:51 ccsdu2009 閱讀(1275) 評論(3)  編輯 收藏 引用
            Comments
            • # re: 蓋莫游戲引擎的Transform類設(shè)計
              ccsdu2009
              Posted @ 2010-02-07 16:48
              這個東西寫的很有問題!  回復(fù)  更多評論   
            • # re: 蓋莫游戲引擎的Transform類設(shè)計
              陳梓瀚(vczh)
              Posted @ 2010-02-07 19:49
              如果你把模型作為純數(shù)據(jù)看待就不會有這個問題了。  回復(fù)  更多評論   
            • # re: 蓋莫游戲引擎的Transform類設(shè)計
              ccsdu2009
              Posted @ 2010-02-07 20:07
              在涉及渲染的時候會有問題
              我修改了下
               1 
               2 ///////////////////////////////////////////////////////
               3 /// 定義引擎場景節(jié)點基類
               4 ///////////////////////////////////////////////////////
               5 class G_DLL_API SceneNode : public Object, public Parent<RefPtr<SceneNode> >public Renderable
               6 {
               7 public:
               8     typedef std::map<const std::string,RefPtr<SceneNode> >           SceneList;
               9     typedef std::map<const std::string,RefPtr<SceneNode> >::iterator SceneItr;
              10 public:
              11     ///////////////////////////////////////////////////////
              12     /// 構(gòu)造,析構(gòu)場景節(jié)點
              13     ///////////////////////////////////////////////////////     
              14     inline SceneNode(){}
              15     inline SceneNode(RefPtr<SceneNode> parent):Parent<RefPtr<SceneNode> >(parent){}
              16     inline SceneNode(const Matrix4f &matrix):world_matrix(matrix){}
              17     inline SceneNode(RefPtr<SceneNode> parent,const Matrix4f &matrix):Parent<RefPtr<SceneNode> >(parent),world_matrix(matrix){}
              18     virtual ~SceneNode();
              19 public:    
              20     ///////////////////////////////////////////////////////
              21     /// 場景渲染虛函數(shù)
              22     ///////////////////////////////////////////////////////            
              23     virtual void BeginRender();
              24     virtual void Render();
              25     virtual void AfterRender(); 
              26     
              27     ///////////////////////////////////////////////////////
              28     /// 節(jié)點操作
              29     ///////////////////////////////////////////////////////         
              30     void PushScene(RefPtr<SceneNode> child);
              31     bool PopScene(RefPtr<SceneNode> child);    
              32     void ClearAllScene();
              33     void ClearAllSceneRecursive();
              34     uint GetSceneCount(){return children.size();}
              35     RefPtr<SceneNode> GetScene(const std::string &name);
              36     
              37     ///////////////////////////////////////////////////////
              38     /// 獲取,設(shè)置場景矩陣
              39     ///////////////////////////////////////////////////////    
              40     inline Matrix4f GetWorldMatrix()const{return world_matrix;}    
              41     //!inline Matrix4f SetWorldMatrix(const Matrix4f &matrix){this->world_matrix = matrix;}
              42     inline Matrix4f GetLocalMatrix()const{return local_matrix;}    
              43     //!inline Matrix4f SetLocalMatrix(const Matrix4f &matrix){this->local_matrix = matrix;}
              44     
              45     ///////////////////////////////////////////////////////
              46     /// 場景平移,縮放,旋轉(zhuǎn)(子場景矩陣也會發(fā)生變化)
              47     ///////////////////////////////////////////////////////    
              48     void Translate(const Vector3f &offset); 
              49     void Scale(const Vector3f &scale); 
              50     void Rotate(float angle, float x, float y, float z);     
              51     
              52     ///////////////////////////////////////////////////////
              53     /// 獲取,設(shè)置場景盒子,球
              54     ///////////////////////////////////////////////////////    
              55     inline Box     GetSceneBox()const{return box;}
              56     inline void    SetSceneBox(const Box &box){this->box = box;}
              57     inline Spheref GetSceneSphere()const{return shpere;}
              58     inline void    SetSceneSphere(const Spheref &sphere){this->shpere = sphere;}    
              59 protected:
              60     SceneList     children;
              61     Matrix4f      world_matrix;
              62     Matrix4f      local_matrix; 
              63     Box           box;
              64     Spheref       shpere;    
              65     
              66     DECLARE_OBJECT(SceneNode);
              67 };
              68  
              如下:
              使用std::map來存儲場景
              當(dāng)然不存在重復(fù)名字的場景是最基本的前體
                回復(fù)  更多評論   

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


             
            婷婷久久香蕉五月综合加勒比| 91久久精品国产成人久久| 久久久久亚洲国产| 国产AⅤ精品一区二区三区久久| 久久久久无码精品国产| 精品久久久久久成人AV| 色婷婷综合久久久中文字幕| 国产精品久久久久国产A级| 久久99久久99小草精品免视看| 久久精品中文字幕久久| 一本色综合久久| 久久久久亚洲AV无码去区首| 无码AV波多野结衣久久| 久久国产免费| 久久青青草原精品国产不卡| 国产精品久久久久久一区二区三区| 国产精品va久久久久久久| 久久精品www| 国产精品免费久久久久影院| 精品熟女少妇a∨免费久久| 亚洲精品tv久久久久久久久| 久久久久久久久66精品片| 亚洲欧美一级久久精品| 狠狠色丁香久久婷婷综合蜜芽五月 | 88久久精品无码一区二区毛片| 久久伊人精品一区二区三区| 亚洲国产成人久久综合碰| 亚洲国产香蕉人人爽成AV片久久| 精品久久久久久国产免费了| 一本久久免费视频| 精品国产乱码久久久久久郑州公司| 99蜜桃臀久久久欧美精品网站 | 久久精品久久久久观看99水蜜桃 | 三级片免费观看久久| 久久国产欧美日韩精品免费| 中文无码久久精品| 99久久免费国产精品| 久久天天婷婷五月俺也去| 国产精品欧美久久久天天影视 | 精品久久久久成人码免费动漫| AV无码久久久久不卡蜜桃 |