Cpper
C/C++高级工程师 Android高级软件工程师 IT集成工程师 音频工程师 熟悉c,c++,java,c#,py,js,asp等多种语言 程序猿
设计引擎的时候一直在考虑什么时候才可以支持多渲染器,比如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
当然其他相关的函数会陆续加入的

posted on 2010-04-01 21:05 ccsdu2009 阅读(303) 评论(3)  编辑 收藏 引用 所属分类: Game引擎
Comments
  • # re: 盖莫游戏引擎2.1.1之渲染器设计
    Davy.xu
    Posted @ 2010-04-01 22:56
    不支持RenderTarget?
    Shader怎么办?  回复  更多评论   
  • # re: 盖莫游戏引擎2.1.1之渲染器设计
    ccsdu2009
    Posted @ 2010-04-02 08:33
    先简后难啊  回复  更多评论   
  • # re: 盖莫游戏引擎2.1.1之渲染器设计
    陈梓瀚(vczh)
    Posted @ 2010-04-02 11:22
    我认为最严重的问题是不同的渲染API的最有效工作方法是不一样的,你得连这些都抽象掉。不能仅仅抽象功能。

    这样才能给机会给那些为你的引擎开发新渲染API支持的时候,不会因为你封装的问题导致他们丧失了利用该API的最有效率的惯用法的权利。

    所以一般来说,你只需要在场景里面实现BSP树,然后让model.Draw变虚就好了,全过程让渲染插件自己处理。  回复  更多评论   

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理