diff --git a/example/DirectGLRender/main.cpp b/example/DirectGLRender/main.cpp index 21665a6e..9fe46ece 100644 --- a/example/DirectGLRender/main.cpp +++ b/example/DirectGLRender/main.cpp @@ -3,6 +3,7 @@ #include #include #include +#include using namespace hgl; using namespace hgl::graph; @@ -59,10 +60,39 @@ ArrayBuffer *vb_vertex=nullptr; ArrayBuffer *vb_color=nullptr; VertexArray *va=nullptr; Renderable *render_obj=nullptr; +RenderState render_state; constexpr float vertex_data[]={0.0f,0.5f, -0.5f,-0.5f, 0.5f,-0.5f }; constexpr float color_data[]={1,0,0, 0,1,0, 0,0,1 }; +constexpr GLfloat clear_color[4]= +{ + 77.f/255.f, + 78.f/255.f, + 83.f/255.f, + 1.f +}; + +void InitRenderState() +{ + DepthState *ds=new DepthState(); + + ds->clear_depth=true; //要求清除depth缓冲区 + ds->clear_depth_value=1.0f; //depth清除所用值 + + ds->depth_mask=false; //关闭depth mask + ds->depth_test=false; //关闭depth test + + render_state.Add(ds); + + ColorState *cs=new ColorState(); + + cs->clear_color=true; + memcpy(cs->clear_color_value,clear_color,sizeof(GLfloat)*4); + + render_state.Add(cs); +} + void InitVertexBuffer() { vb_vertex=CreateVBO(VB2f(3,vertex_data)); @@ -79,21 +109,9 @@ void InitVertexBuffer() render_obj=new Renderable(GL_TRIANGLES,va); } -constexpr GLfloat clear_color[4]= -{ - 77.f/255.f, - 78.f/255.f, - 83.f/255.f, - 1.f -}; - -constexpr GLfloat clear_depth=1.0f; - void draw() { - glClearBufferfv(GL_COLOR,0,clear_color); - glClearBufferfv(GL_DEPTH,0,&clear_depth); - + render_state.Apply(); render_obj->Draw(); } @@ -131,6 +149,7 @@ int main(void) return -3; } + InitRenderState(); InitVertexBuffer(); win->Show(); diff --git a/inc/hgl/graph/RenderState.h b/inc/hgl/graph/RenderState.h index 8fe18629..c040d207 100644 --- a/inc/hgl/graph/RenderState.h +++ b/inc/hgl/graph/RenderState.h @@ -2,54 +2,152 @@ #define HGL_RENDER_STATE_INCLUDE #include -#include +//#include +#include +#include namespace hgl { - enum DEPTH_TEST_FUNC + namespace graph { - DEPTH_TEST_NEVER=0, - DEPTH_TEST_LESS, - DEPTH_TEST_EQUAL, - DEPTH_TEST_LEQUAL, - DEPTH_TEST_GREATER, - DEPTH_TEST_NOTEQUAL, - DEPTH_TEST_GEQUAL, - DEPTH_TEST_ALWAYS - };// + struct RenderStateBlock + { + virtual void Apply()=0; + }; - struct DepthStatus - { - float near_depth =0, - far_depth =1; + struct ColorState:public RenderStateBlock + { + bool red =true; + bool green =true; + bool blue =true; + bool alpha =true; - bool depth_mask =true; - float clear_depth =far_depth; + bool clear_color=false; + GLfloat clear_color_value[4]={0,0,0,0}; - DEPTH_TEST_FUNC depth_func =DEPTH_TEST_LESS; - bool depth_test; + public: - public: + void Apply(); + };//struct ColorState:public RenderStateBlock - CompOperatorMemcmp(struct DepthStatus &); - };// + enum class DEPTH_TEST + { + NEVER =GL_NEVER, + LESS =GL_LESS, + EQUAL =GL_EQUAL, + LEQUAL =GL_LEQUAL, + GREATER =GL_GREATER, + NOTEQUAL =GL_NOTEQUAL, + GEQUAL =GL_GEQUAL, + ALWAYS =GL_ALWAYS, + };//enum class DEPTH_TEST_FUNC - /** - * 渲染状态 - */ - struct RenderState - { - bool color_mask[4]; - Color4f clear_color; + struct DepthState:public RenderStateBlock + { + GLfloat near_depth=0, + far_depth=1; - DepthStatus depth; - // uint draw_face; ///<绘制的面 - // uint fill_mode; ///<填充方式 - // - // uint cull_face; ///<裁剪面,0:不裁(双面材质),FACE_FRONT:正面,FACE_BACK:背面 - // - // bool depth_test; ///<深度测试 - // uint depth_func; ///<深度处理方式 - // bool depth_mask; ///<深度遮罩 - };//class RenderState + bool depth_mask=true; + GLfloat clear_depth_value=far_depth; //清空深度时所用的值 + bool clear_depth; //是否要清空深度 + + DEPTH_TEST depth_func=DEPTH_TEST::LESS; + bool depth_test; + + public: + + void Apply(); + + //CompOperatorMemcmp(struct DepthState &); + };//struct DepthState + + enum class FACE + { + FRONT =GL_FRONT, + BACK =GL_BACK, + FRONT_AND_BACK =GL_FRONT_AND_BACK, + };//enum class CULL_FACE_MODE + + struct CullFaceState:public RenderStateBlock + { + bool enabled =true; + + FACE mode =FACE::BACK; + public: + + void Apply(); + };//struct CullFaceState + + enum class FILL_MODE + { + POINT =GL_POINT, + LINE =GL_LINE, + FACE =GL_FILL, + };//enum class FILL_MODE + + struct PolygonModeState:public RenderStateBlock + { + FACE face=FACE::FRONT_AND_BACK; + FILL_MODE mode=FILL_MODE::FACE; + + public: + + void Apply(); + };//struct FillModeState + + /** + * 具体渲染状态数据 + */ + class RenderStateData:public RenderStateBlock + { + Set state_list; + + public: + + void Add(RenderStateBlock *rsb) + { + if(!rsb) + return; + + state_list.Add(rsb); + } + + void Apply() override + { + const int count=state_list.GetCount(); + + if(count<=0) + return; + + RenderStateBlock **rsb=state_list.GetData(); + + for(int i=0;iApply(); + ++rsb; + } + } + };//class RenderStateData:public RenderStateBlock + + /** + * 渲染状态 + */ + class RenderState + { + RenderStateData state_data; + + public: + + RenderState()=default; + RenderState(const RenderStateData &rsd){state_data=rsd;} + virtual ~RenderState()=default; + + virtual void Add(RenderStateBlock *rsb) { state_data.Add(rsb); } + + virtual void Apply() + { + state_data.Apply(); + } + };//class RenderState + }//namespace graph }//namespace hgl #endif//HGL_RENDER_STATE_INCLUDE diff --git a/src/RenderDriver/CMakeLists.txt b/src/RenderDriver/CMakeLists.txt index 994875db..18caafe5 100644 --- a/src/RenderDriver/CMakeLists.txt +++ b/src/RenderDriver/CMakeLists.txt @@ -7,6 +7,7 @@ SET(GRAPH_SRC_FILES OpenGLDebug.cpp BufferObject.cpp VertexArray.cpp Renderable.cpp + RenderState.cpp TextureFormat.cpp Texture1D.cpp Texture1DDSA.cpp diff --git a/src/RenderDriver/RenderState.cpp b/src/RenderDriver/RenderState.cpp new file mode 100644 index 00000000..f710f2cb --- /dev/null +++ b/src/RenderDriver/RenderState.cpp @@ -0,0 +1,46 @@ +#include + +namespace hgl +{ + namespace graph + { + void ColorState::Apply() + { + glColorMask(red,green,blue,alpha); + + if(clear_color) + glClearBufferfv(GL_COLOR,0,clear_color_value); + } + + void DepthState::Apply() + { + glDepthMask((GLenum)depth_mask); + glDepthFunc((GLenum)depth_func); + + if(depth_test) + glEnable(GL_DEPTH_TEST); + else + glDisable(GL_DEPTH_TEST); + + if(clear_depth) + glClearBufferfv(GL_DEPTH,0,&clear_depth_value); + } + + void CullFaceState::Apply() + { + if(enabled) + { + glEnable(GL_CULL_FACE); + glCullFace((GLenum)mode); + } + else + glDisable(GL_CULL_FACE); + } + + void PolygonModeState::Apply() + { + glPolygonMode((GLenum)face, + (GLenum)mode); + } + }//namespace graph +}//namespace hgl