設計引擎的時候一直在考慮什么時候才可以支持多渲染器,比如d3d9比如軟件渲染器
為了盡可能的讓opengl的API盡可能的與引擎代碼分離
我加上了一個初步封裝了opengl API的類VideoDriver
當前只是簡單的封裝了很多opengl的函數
代碼如下:
1 public:
2 ////////////////////////////////////////////////////////
3 /// 構造,析構引擎視頻驅動(渲染器)
4 ////////////////////////////////////////////////////////
5 VideoDriver();
6 virtual ~VideoDriver();
7 public:
8 ////////////////////////////////////////////////////////
9 /// 獲取當前視頻驅動器類型
10 ////////////////////////////////////////////////////////
11 virtual ENGINE_VIDEODERIVER GetVideoDriverType()const
12 {
13 return ENGINE_VIDEODERIVER_NULL;
14 }
15
16 ////////////////////////////////////////////////////////
17 /// 設置,獲取是否啟用豎直同步
18 ////////////////////////////////////////////////////////
19 virtual void UseVerticalSync(bool enabled) = 0;
20 virtual bool IsUseVerticalSync()const = 0;
21
22 ////////////////////////////////////////////////////////////
23 /// 獲取當前幀速
24 ////////////////////////////////////////////////////////////
25 virtual float GetFPS() const = 0;
26
27 /////////////////////////////////////////////////////////
28 /// 獲取視頻模式列表,獲取桌面視頻模式
29 /////////////////////////////////////////////////////////
30 virtual int GetVideoMode(VideoMode* mode, int number) = 0;
31 virtual VideoMode GetDesktopVideoMode()const = 0;
32
33 ////////////////////////////////////////////////////////
34 /// 設置,獲取視口
35 ////////////////////////////////////////////////////////
36 virtual void SetViewPort(const Recti &rect) = 0;
37 virtual Recti GetViewPort()const = 0;
38
39 //////////////////////////////////////////////////////////
40 /// 設置投影模式下的視景體
41 //////////////////////////////////////////////////////////
42 virtual void SetPerspective(float fov, float aspect, float near, float far) = 0;
43
44 //////////////////////////////////////////////////////////
45 /// 啟用,禁止和查詢剔除功能
46 //////////////////////////////////////////////////////////
47 virtual void EnableCulling() = 0;
48 virtual void DisableCulling() = 0;
49 virtual bool IsEnabledCulling() = 0;
50 virtual void SetCulling(ENGEIN_CULLING_TYPE type) = 0;
51
52
53 //////////////////////////////////////////////////////////
54 /// 平移,旋轉,縮放
55 //////////////////////////////////////////////////////////
56 virtual void Translate(float x, float y, float z) = 0;
57 virtual void Translate(const Vector3f &offset) = 0;
58 virtual void Rotate(float angle, float x, float y,float z) = 0;
59 virtual void Scale(float x, float y, float z) = 0;
60 virtual void Scale(const Vector3f& scale) = 0;
61
62 //////////////////////////////////////////////////////////
63 /// 設置飛行模式參數
64 //////////////////////////////////////////////////////////
65 virtual void SetPilotView(float x,float y,float z,float roll,float pitch,float heading) = 0;
66
67 ////////////////////////////////////////////////////////
68 /// 設置,獲取渲染器矩陣操作
69 ////////////////////////////////////////////////////////
70 virtual void SetTransform(const Matrix4f& mat, ENGINE_MATRIX_TYPE type) = 0;
71 virtual const Matrix4f& GetTransform(ENGINE_MATRIX_TYPE type)const = 0;
72
73 ////////////////////////////////////////////////////////
74 /// 矩陣保存
75 ////////////////////////////////////////////////////////
76 virtual void PushMatrix() = 0;
77 virtual void PopMatrix() = 0;
78
79 ////////////////////////////////////////////////////////
80 /// 啟用2d渲染
81 ////////////////////////////////////////////////////////
82 virtual void Ortho2D() = 0;
83
84 //////////////////////////////////////////////////////////
85 /// 設置當前渲染模式
86 //////////////////////////////////////////////////////////
87 virtual void SetRenderMode(RENDER_MODE mode) = 0;
88
89 //////////////////////////////////////////////////////////
90 /// 設置,獲取清屏色
91 //////////////////////////////////////////////////////////
92 virtual void SetClearColor(const Color& color) = 0;
93 virtual Color GetClearColor()const = 0;
94
95 ////////////////////////////////////////////////////////////
96 /// 設置要清理的緩存類型(見:ENGINE_CLEAR_ACCUM,ENGINE_CLEAR_INDEX等)
97 ////////////////////////////////////////////////////////////
98 virtual void SetClearBuffer(long buffer_mask) = 0;
99
100 //////////////////////////////////////////////////////////
101 /// 渲染色設置
102 //////////////////////////////////////////////////////////
103 virtual void SetRenderColor(const Color& color) = 0;
104 void SetColor(const Color& color){SetRenderColor(color);}
105
106 //////////////////////////////////////////////////////////
107 /// 啟用,禁止混合
108 //////////////////////////////////////////////////////////
109 virtual void SetBlending(bool blend) = 0;
110 virtual bool GetBlending()const = 0;
111
112 //////////////////////////////////////////////////////////
113 /// 線寬
114 //////////////////////////////////////////////////////////
115 virtual void SetLineWidth(float width) = 0;
116 virtual float GetMaxLineWidth()const = 0;
117 virtual float GetLineWidth()const = 0;
118
119 //////////////////////////////////////////////////////////
120 /// 點繪制
121 //////////////////////////////////////////////////////////
122 virtual void DrawPoint(const Vector3f &point) = 0;
123 virtual void DrawPoint(float x, float y, float z) = 0;
124
125 //////////////////////////////////////////////////////////
126 /// 線繪制
127 //////////////////////////////////////////////////////////
128 virtual void DrawLine(const Point &from, const Point &to) = 0;
129 virtual void DrawLine(const Vector2f &p1, const Vector2f &p2) =0;
130 virtual void DrawLine(float x1, float y1, float x2, float y2) = 0;
131 virtual void DrawLine(const Vector3f &p1, const Vector3f &p2) = 0;
132
133 //////////////////////////////////////////////////////////
134 /// 矩形繪制
135 //////////////////////////////////////////////////////////
136 virtual void DrawRect(float x, float y, float width, float height) = 0;
137 virtual void DrawRect(const Rectf& rect) = 0;
138 virtual void DrawRect(const Recti& rect) = 0;
139 virtual void FillRect(float x, float y, float width, float height)= 0;
140 virtual void FillRect(const Rectf& rect) = 0;
141 virtual void FillRect(const Recti& rect) = 0;
142
143 //////////////////////////////////////////////////////////
144 /// 格子繪制
145 //////////////////////////////////////////////////////////
146 virtual void DrawGrid(const Point &pos, const Point &unit, const Point &num) = 0;
147 virtual void DrawGrid(const Point& pos, const int &row, const int &col, const Point &num) = 0;
148
149 //////////////////////////////////////////////////////////
150 /// 三角形繪制
151 //////////////////////////////////////////////////////////
152 virtual void DrawTriangle(const Trianglef& tri, bool filled) = 0;
153 virtual void DrawTriangle(const Vector2f& p1,const Vector2f& p2,const Vector2f& p3,bool filled) = 0;
154
155 //////////////////////////////////////////////////////////
156 /// 園繪制
157 //////////////////////////////////////////////////////////
158 virtual void DrawCircle(const Vector2f& center, float radius, float segments) = 0;
159 virtual void DrawCircleSegment(const Vector2f& center,float radius,float t1,float t2,float segments,bool filled) = 0;
160
161 ////////////////////////////////////////////////////////////
162 /// 繪制長方體(參數紋理id,長方體位置,變換矩陣,大小)
163 ////////////////////////////////////////////////////////////
164 virtual void RenderCube(int texture,const float pos[3],const float mat[12],const float sides[3]) = 0;
165 public:
166 ////////////////////////////////////////////////////////////
167 /// 設備預(后)更新
168 ////////////////////////////////////////////////////////////
169 virtual void PreUpdate() = 0;
170 virtual void PostUpdate() = 0;
171 private:
172
173 DECLARE_OBJECT(VideoDriver)
這是初步設計的結果
以后估計會有software video driver
當然其他相關的函數會陸續加入的