考慮到在應(yīng)用中有好幾個(gè)坐標(biāo)系統(tǒng)比如opengl,3dx,當(dāng)然用戶也可能有自己的坐標(biāo)系統(tǒng)
考慮如此,我覺得還是把引擎的坐標(biāo)系統(tǒng)不要局限于特定的一個(gè)坐標(biāo)系
這是我寫的引擎坐標(biāo)系統(tǒng)如下:
1 ///////////////////////////////////////////////////////////
2 /// 頭文件包含
3 ///////////////////////////////////////////////////////////
4
5 namespace core
6 {
7
8 ////////////////////////////////////////////////////////////
9 /// 坐標(biāo)軸枚舉
10 ////////////////////////////////////////////////////////////
11 enum AXIS_TYPE
12 {
13 AXIS_X = 1;
14 AXIS_Y = 1 << 2;
15 AXIS_Z = 1 << 4;
16 };
17
18 ////////////////////////////////////////////////////////////
19 /// 坐標(biāo)軸方向枚舉
20 ////////////////////////////////////////////////////////////
21 enum AXIS_DIRECTION
22 {
23 AXIS_DIRECTION_UP = 1,
24 AXIS_DIRECTION_DOWN = -1
25 };
26
27 ////////////////////////////////////////////////////////////
28 /// 定義引擎坐標(biāo)系統(tǒng)
29 ////////////////////////////////////////////////////////////
30 ////////////////////////////////////////////////////////////
31 /// 引擎的坐標(biāo)系統(tǒng)定義如下:
32 /// 以指向屏幕右方的坐標(biāo)軸為第一坐標(biāo)軸(1)
33 /// 以指向屏幕下方的坐標(biāo)軸為第二坐標(biāo)軸(2)
34 /// 以垂直屏幕向外的坐標(biāo)系統(tǒng)為第三坐標(biāo)軸(3)
35 /// 以方向相反的坐標(biāo)軸其方向?yàn)樨?fù)(-1)
36 ////////////////////////////////////////////////////////////
37 struct CoordinateSystem
38 {
39 CoordinateSystem()
40 {
41 type[0] = AXIS_X;
42 type[1] = AXIS_Y;
43 type[2] = AXIS_Z;
44 dir[0] = dir[1] = dir[2] = AXIS_DIRECTION_UP
45 }
46
47 CoordinateSystem(AXIS_TYPE type1, AXIS_DIRECTION dir1,
48 AXIS_TYPE type2, AXIS_DIRECTION dir2,
49 AXIS_TYPE type3, AXIS_DIRECTION dir3)
50 {
51 //! 必須保證參數(shù)坐標(biāo)軸的合法性
52 ASSERT(type1 + type2 + type3 == 13);
53
54 type[0] = type1;
55 type[1] = type2;
56 type[2] = type3;
57 dir[0] = dir1;
58 dir[1] = dir2;
59 dir[2] = dir3;
60 }
61
62 AXIS_TYPE type[3];
63 AXIS_DIRECTION dir[3];
64 };
65
66 }
67
68 #define COORDINATE_ENGINE core::CoordinateSystem(AXIS_X,1,AXIS_Y, 1,AXIS_Z, 1)
69 #define COORDINATE_OPENGL core::CoordinateSystem(AXIS_X,1,AXIS_Y,-1,AXIS_Z, 1)
70 #define COORDINATE_3DMAX core::CoordinateSystem(AXIS_X,1,AXIS_Z,-1,AXIS_Y,-1)
首先給坐標(biāo)系統(tǒng)排一個(gè)次序,然后定義正向的坐標(biāo)軸方向,然后就可以抽象出萬能的直角坐標(biāo)系統(tǒng)了
當(dāng)然為了方便起見這里給定了3個(gè)常見的坐標(biāo)系統(tǒng)分別為引擎坐標(biāo)系,OPENGL坐標(biāo)系統(tǒng)和3dmax坐標(biāo)系統(tǒng)
摘要: 這是蓋莫游戲引擎的音頻測試代碼具體如下:
1#include <cstdlib> 2#include <iostream> 3#include <GEngine/Main.hpp> 4using namespace std; 5 6const&nbs...
閱讀全文
蓋莫游戲引擎是我設(shè)計(jì)的一個(gè)2/3d游戲引擎,已經(jīng)做了1年多了。
出于各種考慮我都需要對其進(jìn)行大規(guī)模的改寫,以便在框架上具有良好清晰的架構(gòu)(當(dāng)然也會不斷新加入很多功能)。在參考了irr和其他幾個(gè)引擎代碼之后,我把引擎的場景繼承關(guān)系設(shè)計(jì)為:
Object->Transform->SceneNode這樣的關(guān)系
Object是一般的對象基類
Transform是控制世界矩陣和局部矩陣的類對象
其代碼如下:
1 ///////////////////////////////////////////////////////
2 /// 用4*4矩陣來定義3d世界物體的位置和角度,并生產(chǎn)一個(gè)樹狀繼承
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é)點(diǎn)
19 ///////////////////////////////////////////////////////
20 void AddChild(RefPtr<Transform> child);
21
22 ///////////////////////////////////////////////////////
23 /// 設(shè)置子節(jié)點(diǎn)
24 ///////////////////////////////////////////////////////
25 void SetChild(int index, RefPtr<Transform> child);
26
27 ///////////////////////////////////////////////////////
28 /// 獲取子節(jié)點(diǎn)個(gè)數(shù)
29 ///////////////////////////////////////////////////////
30 inline uint GetChildCount() const{return children.size();}
31
32 ///////////////////////////////////////////////////////
33 /// 獲取指定索引的子節(jié)點(diǎn)
34 ///////////////////////////////////////////////////////
35 inline RefPtr<Transform> GetChild(int i);
36 inline RefPtr<Transform> GetLastChild();
37
38 ///////////////////////////////////////////////////////
39 /// 節(jié)點(diǎn)移除
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 /// 重新計(jì)算矩陣
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é)點(diǎn)類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é)點(diǎn)類
17 ///////////////////////////////////////////////////////
18 class G_DLL_API SceneNode : public Transform
19 {
20 public:
21 ///////////////////////////////////////////////////////
22 /// 構(gòu)造,析構(gòu)場景節(jié)點(diǎn)
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 /// 重新計(jì)算場景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é)點(diǎn)類在Transform的基礎(chǔ)之上增加了是否顯示,是否設(shè)置為自動調(diào)用視椎體剔除的函數(shù)
當(dāng)然還有一般的虛擬場景渲染函數(shù)(3個(gè))
當(dāng)然這只是最基本的功能
( virtual void BeginRender();
virtual void Render();
virtual void RenderAfter();)
然后可以在SceneNode的基礎(chǔ)之上增加新的場景類型(比如:BillBoard等等)
題注:其實(shí)在引擎設(shè)計(jì)過程中我一直想避免這種復(fù)雜的繼承關(guān)系,但是之后我會發(fā)現(xiàn)當(dāng)引擎功能越來越大的時(shí)候,不采用這種方式,引擎邏輯關(guān)系會變得越來越糟
我在想之后就可以加入一個(gè)場景渲染隊(duì)列來渲染場景了.
如果對游戲引擎設(shè)計(jì)感興趣 我們可以交流探討下(
ccsdu2009@sohu.com)
通常的單態(tài)實(shí)現(xiàn)大都市如下的形式:
1 class Object
2 {
3 public:
4 Object* Instance();
5 protected:
6 };
7
8
(應(yīng)該是Object* Instance())
獲取可以采用如下的模板寫法:
1 template<clas T>
2 class Singleton
3 {
4 public:
5 static T* Instance();
6 };
7
其實(shí)可以對其模板寫法做一下變形如下:
1 ////////////////////////////////////////////////////////////
2 /// 單態(tài)模板類(非線程安全的)
3 ////////////////////////////////////////////////////////////
4 template <typename Base, typename T>
5 class Singleton
6 {
7 public:
8
9 //! 獲取對象指針
10 /*!
11 這里暫時(shí)不需要線程安全的版本:)
12 */
13 static T* Instance()
14 {
15 if( NULL == instance )
16 instance = new T;
17 assert(instance = 0);
18 return instance;
19 };
20
21 //! 對象指針的析構(gòu)
22 static void Deinit()
23 {
24 delete instance;
25 instance = NULL;
26 };
27
28 private:
29 static Base* instance;
30 };
采用雙模板參數(shù)的手法
這樣做的一個(gè)好處就是可以通過特定的環(huán)境來使用合適的單態(tài)版本
摘要: matrix22實(shí)現(xiàn)如下:
1 2 //////////////////////////////////////////////////////////// 3 ///定義2*2矩陣模板類 4 ////////////////////////////////////...
閱讀全文
代碼如下:
1 #include <GEngine/Main.hpp>
2
3 using namespace std;
4
5
6 int main(int argc, char *argv[])
7 {
8 core::Device* device = core::InitDevice("蓋莫引擎框架");
9 device->SetClearColor(core::Color(80,100,0));
10
11 BEGIN_LOOP(device)
12
13 END_LOOP(device)
14
15 device->Close();
16 device->Drop();
17
18 system("PAUSE");
19 return EXIT_SUCCESS;
20 }
21
22
顯示結(jié)果如下:
當(dāng)然作為最基本的要求,蓋莫引擎可以再vc,devc++,codeblock下使用O(∩_∩)O~
使用蓋莫游戲引擎做的天空面例子(當(dāng)然可以使用天空盒,天空頂)
代碼如下:
1 #include <cstdlib>
2 #include <iostream>
3 #include <GEngine/Main.hpp>
4
5 using namespace std;
6
7 core::RefPtr<core::Image> skyimage[1];
8 core::RefPtr<core::Texture> skytexture[1];
9 core::RefPtr<core::Image> terrainimage[2];
10 core::RefPtr<core::Texture> terraintexture[2];
11
12 int main(int argc, char *argv[])
13 {
14 core::Device* device = core::InitDevice("蓋莫引擎場景測試");
15
16 //! 獲取資源管理器
17 core::ResourceManager *resourcemanager = device->GetResourceManager();
18
19 //! 獲取場景管理器
20 core::RefPtr<core::SceneManager> scenemanager = core::SceneManager::GetSceneManager();
21
22 //! 獲取新的攝像機(jī)并設(shè)置為活動攝像機(jī)
23 core::RefPtr<core::Camera> camera = scenemanager->CreateCamera("mycamera",
24 Vector3f(500,20,400),
25 Vector3f(503,20,400),
26 Vector3f(0,1,0));
27
28 //! 設(shè)置當(dāng)前活動攝像機(jī)
29 scenemanager->SetActiveCamera(camera);
30
31 //! 獲取天空面圖形和紋理
32 skyimage[0] = resourcemanager->GetImage("sky_image","..\\image//sky//top.jpg");
33 skytexture[0] = resourcemanager->GetTexture("sky_texture",skyimage[0]);
34 core::RefPtr<core::SkyPlane> skyplane = scenemanager->GetSkyPlane(32,500,900,skytexture[0]);
35 skyplane->SetRotationStep(0.03f);
36
37 //! 設(shè)置地形數(shù)據(jù)
38 terrainimage[0] = resourcemanager->GetImage("terrain_image","..\\terrain//terrain.bmp");
39 terrainimage[1] = resourcemanager->GetImage("terrain_detail","..\\terrain//terrainflat.jpg");
40 terraintexture[0] = resourcemanager->GetTexture("terrain_texture1",terrainimage[0]);
41 terraintexture[1] = resourcemanager->GetTexture("terrain_texture2",terrainimage[1]);
42
43 core::RefPtr<core::Terrain> terrain = scenemanager->GetTerrain("terrain1");
44 bool flag = terrain->Load("..\\terrain//terrain2.raw",1024);
45 terrain->SetStepSize(16);
46 terrain->SetTexture(terraintexture[1],terraintexture[0]);
47
48 //! 設(shè)置攝像機(jī)數(shù)據(jù)
49 camera->SetViewport(0,0,640,480);
50 camera->SetPerspective(45,640.0f/480.0f,0.1f,1000);
51
52 //! 獲取輸入系統(tǒng)指針
53 core::Input* input = device->GetInput();
54
55 //! 獲取霧指針
56 core::RefPtr<core::Fog> fog = scenemanager->GetFog();
57 fog->SetColor(core::Color(0.3f,0.3f,0.2f));
58 fog->SetDensity(0.001f);
59 fog->SetQuality(0.003f);
60 fog->SetBound(0.1f,1000.0f);
61 fog->Render();
62
63 BEGIN_LOOP(device)
64 camera->SetPerspective(45,640.0f/480.0f,0.1f,1000);
65 camera->Render();
66 skyplane->Render();
67 terrain->Render();
68 END_LOOP(device)
69
70 terrain->Deinit();
71
72 device->Close();
73 device->Drop();
74
75 system("PAUSE");
76 return EXIT_SUCCESS;
77 }
78
79
貼圖如下:

編譯:devc++,vc2003
如果對蓋莫引擎感興趣可以通過郵件和我聯(lián)系
ccsdu2009@sohu.com
代碼如下:
1 #include <cstdlib>
2 #include <iostream>
3 #include <GEngine/Main.hpp>
4
5 using namespace std;
6
7 core::RefPtr<core::Image> skyimage[1];
8 core::RefPtr<core::Texture> skytexture[1];
9 core::RefPtr<core::Image> terrainimage[2];
10 core::RefPtr<core::Texture> terraintexture[2];
11
12 int main(int argc, char *argv[])
13 {
14 core::Device* device = core::InitDevice("蓋莫引擎場景測試");
15
16 //! 獲取資源管理器
17 core::ResourceManager *resourcemanager = device->GetResourceManager();
18
19 //! 獲取場景管理器
20 core::RefPtr<core::SceneManager> scenemanager = core::SceneManager::GetSceneManager();
21
22 //! 獲取新的攝像機(jī)并設(shè)置為活動攝像機(jī)
23 core::RefPtr<core::Camera> camera = scenemanager->CreateCamera("mycamera",
24 Vector3f(500,20,400),
25 Vector3f(503,20,400),
26 Vector3f(0,1,0));
27
28 //! 設(shè)置當(dāng)前活動攝像機(jī)
29 scenemanager->SetActiveCamera(camera);
30
31 //! 獲取天空頂圖形和紋理
32 skyimage[0] = resourcemanager->GetImage("sky_image","..\\image//sky//top.jpg");
33 skytexture[0] = resourcemanager->GetTexture("sky_texture",skyimage[0]);
34 core::RefPtr<core::SkyDome> skydome = scenemanager->GetSkyDome(250,3,3,skytexture[0]);
35 skydome->SetRotationStep(0.03f);
36
37 //! 設(shè)置地形數(shù)據(jù)
38 terrainimage[0] = resourcemanager->GetImage("terrain_image","..\\terrain//terrain.bmp");
39 terrainimage[1] = resourcemanager->GetImage("terrain_detail","..\\terrain//detail.bmp");
40 terraintexture[0] = resourcemanager->GetTexture("terrain_texture1",terrainimage[0]);
41 terraintexture[1] = resourcemanager->GetTexture("terrain_texture2",terrainimage[1]);
42
43 ASSERT(terrainimage[0] || terrainimage[1]);
44 core::RefPtr<core::Terrain> terrain = scenemanager->GetTerrain("terrain1");
45 bool flag = terrain->Load("..\\terrain//terrain2.raw",1024);
46 terrain->SetStepSize(16);
47 terrain->SetTexture(terraintexture[1],terraintexture[0]);
48
49 //! 設(shè)置攝像機(jī)數(shù)據(jù)
50 camera->SetViewport(0,0,640,480);
51 camera->SetPerspective(45,640.0f/480.0f,0.1f,1000);
52
53 //! 獲取輸入系統(tǒng)指針
54 core::Input* input = device->GetInput();
55
56 //! 獲取霧指針
57 core::RefPtr<core::Fog> fog = scenemanager->GetFog();
58 fog->SetColor(core::Color(0.3f,0.3f,0.2f));
59 fog->SetDensity(0.001f);
60 fog->SetQuality(0.003f);
61 fog->SetBound(0.1f,1000.0f);
62 fog->Render();
63
64 BEGIN_LOOP(device)
65 camera->SetPerspective(45,640.0f/480.0f,0.1f,1000);
66 camera->Render();
67 skydome->Render();
68 terrain->Render();
69 END_LOOP(device)
70
71 terrain->Deinit();
72
73 device->Close();
74 device->Drop();
75
76 system("PAUSE");
77 return EXIT_SUCCESS;
78 }
79
80
下面的是貼圖

如果對蓋莫引擎感興趣,可通過郵件和我聯(lián)系:ccsdu2009@sohu.com