• <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等多種語言 程序猿
            蓋莫游戲引擎是我設計的一個2/3d游戲引擎,已經做了1年多了。
            出于各種考慮我都需要對其進行大規模的改寫,以便在框架上具有良好清晰的架構(當然也會不斷新加入很多功能)。在參考了irr和其他幾個引擎代碼之后,我把引擎的場景繼承關系設計為:
            Object->Transform->SceneNode這樣的關系
            Object是一般的對象基類
            Transform是控制世界矩陣和局部矩陣的類對象
            其代碼如下:
             1 ///////////////////////////////////////////////////////
             2 /// 用4*4矩陣來定義3d世界物體的位置和角度,并生產一個樹狀繼承
             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     /// 構造,析構變換類
            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     /// 增加子節點
            19     ///////////////////////////////////////////////////////         
            20     void AddChild(RefPtr<Transform> child);
            21     
            22     ///////////////////////////////////////////////////////
            23     /// 設置子節點
            24     ///////////////////////////////////////////////////////         
            25     void SetChild(int index, RefPtr<Transform> child);
            26   
            27     ///////////////////////////////////////////////////////
            28     /// 獲取子節點個數
            29     ///////////////////////////////////////////////////////      
            30     inline uint GetChildCount() const{return children.size();}
            31     
            32     ///////////////////////////////////////////////////////
            33     /// 獲取指定索引的子節點
            34     ///////////////////////////////////////////////////////         
            35     inline RefPtr<Transform> GetChild(int i);
            36     inline RefPtr<Transform> GetLastChild();
            37   
            38     ///////////////////////////////////////////////////////
            39     /// 節點移除
            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     /// 設置,獲取局部矩陣
            55     ///////////////////////////////////////////////////////         
            56     void  SetLocalMatrix(const Matrix4f& matrix){world_matrix = matrix;}
            57     Matrix4f GetLocalMatrix(){return local_matrix;}
            58     
            59     ///////////////////////////////////////////////////////
            60     /// 平移,縮放,旋轉
            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     /// 設置,獲取是否保持單位世界矩陣
            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     /// 設置世界矩陣
            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 };
            然后是基本的場景節點類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 /// 定義引擎基本場景節點類
            17 ///////////////////////////////////////////////////////
            18 class G_DLL_API SceneNode : public Transform
            19 {
            20 public:
            21     ///////////////////////////////////////////////////////
            22     /// 構造,析構場景節點
            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     /// 設置,獲取是否渲染(顯示)場景
            43     ///////////////////////////////////////////////////////        
            44     void SetVisible(bool visible);
            45     void EnableVisible();
            46     void DisableVisible();
            47     bool IsVisible()const;
            48     
            49     ///////////////////////////////////////////////////////
            50     /// 設置,獲取是否自動調用視錐體剔除
            51     ///////////////////////////////////////////////////////        
            52     void SetAutoCulling(bool auto_cull);
            53     bool IsAutoCulling()const;
            54     
            55 protected:
            56     ///////////////////////////////////////////////////////
            57     /// 設置場景球,盒子 
            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

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

            題注:其實在引擎設計過程中我一直想避免這種復雜的繼承關系,但是之后我會發現當引擎功能越來越大的時候,不采用這種方式,引擎邏輯關系會變得越來越糟
            我在想之后就可以加入一個場景渲染隊列來渲染場景了.
            如果對游戲引擎設計感興趣 我們可以交流探討下(ccsdu2009@sohu.com)
            posted on 2010-02-07 12:51 ccsdu2009 閱讀(1265) 評論(3)  編輯 收藏 引用
            Comments
            • # re: 蓋莫游戲引擎的Transform類設計
              ccsdu2009
              Posted @ 2010-02-07 16:48
              這個東西寫的很有問題!  回復  更多評論   
            • # re: 蓋莫游戲引擎的Transform類設計
              陳梓瀚(vczh)
              Posted @ 2010-02-07 19:49
              如果你把模型作為純數據看待就不會有這個問題了。  回復  更多評論   
            • # re: 蓋莫游戲引擎的Transform類設計
              ccsdu2009
              Posted @ 2010-02-07 20:07
              在涉及渲染的時候會有問題
              我修改了下
               1 
               2 ///////////////////////////////////////////////////////
               3 /// 定義引擎場景節點基類
               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     /// 構造,析構場景節點
              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     /// 場景渲染虛函數
              22     ///////////////////////////////////////////////////////            
              23     virtual void BeginRender();
              24     virtual void Render();
              25     virtual void AfterRender(); 
              26     
              27     ///////////////////////////////////////////////////////
              28     /// 節點操作
              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     /// 獲取,設置場景矩陣
              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     /// 場景平移,縮放,旋轉(子場景矩陣也會發生變化)
              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     /// 獲取,設置場景盒子,球
              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來存儲場景
              當然不存在重復名字的場景是最基本的前體
                回復  更多評論   
             
            97久久国产露脸精品国产| 久久久久久久久久久久中文字幕| 久久精品中文字幕无码绿巨人| 久久免费99精品国产自在现线| 青青草原综合久久大伊人精品| 日韩精品久久无码人妻中文字幕| 亚洲国产精品无码久久九九| 欧美激情精品久久久久久久九九九 | 99久久国产综合精品网成人影院| 77777亚洲午夜久久多喷| 久久香综合精品久久伊人| 一本久久知道综合久久| 亚洲精品无码久久久影院相关影片| 99久久香蕉国产线看观香| 久久久久久免费视频| 久久精品国产清自在天天线| 国产成人综合久久精品红| 四虎久久影院| 久久久久久精品无码人妻| 亚洲伊人久久成综合人影院| 一本色道久久综合狠狠躁篇| 精品久久久一二三区| 国产激情久久久久久熟女老人| 欧美熟妇另类久久久久久不卡| 精品无码久久久久国产| 99久久久久| 香蕉久久夜色精品国产2020| 99久久99久久精品国产片果冻| 久久精品无码午夜福利理论片| 久久久精品免费国产四虎| 久久久91人妻无码精品蜜桃HD| 91麻豆国产精品91久久久| 久久精品国产亚洲77777| 国产精品99久久精品爆乳| 国产69精品久久久久APP下载 | 怡红院日本一道日本久久| 久久国产免费直播| 精品综合久久久久久97| 久久97精品久久久久久久不卡| 久久综合狠狠综合久久97色| 久久久久久亚洲精品成人|