引入RenderState设计

This commit is contained in:
HuYingzhuo 2019-03-27 22:13:49 +08:00
parent 403499e1f1
commit 514a4b37ca
4 changed files with 216 additions and 52 deletions

View File

@ -3,6 +3,7 @@
#include<hgl/graph/RenderWindow.h>
#include<iostream>
#include<hgl/graph/Renderable.h>
#include<hgl/graph/RenderState.h>
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();

View File

@ -2,54 +2,152 @@
#define HGL_RENDER_STATE_INCLUDE
#include<hgl/type/Color4f.h>
#include<hgl/CompOperator.h>
//#include<hgl/CompOperator.h>
#include<hgl/type/Set.h>
#include<GLEWCore/glew.h>
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 DepthStatus
struct RenderStateBlock
{
float near_depth =0,
far_depth =1;
virtual void Apply()=0;
};
bool depth_mask =true;
float clear_depth =far_depth;
struct ColorState:public RenderStateBlock
{
bool red =true;
bool green =true;
bool blue =true;
bool alpha =true;
DEPTH_TEST_FUNC depth_func =DEPTH_TEST_LESS;
bool clear_color=false;
GLfloat clear_color_value[4]={0,0,0,0};
public:
void Apply();
};//struct ColorState:public RenderStateBlock
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 DepthState:public RenderStateBlock
{
GLfloat near_depth=0,
far_depth=1;
bool depth_mask=true;
GLfloat clear_depth_value=far_depth; //清空深度时所用的值
bool clear_depth; //是否要清空深度
DEPTH_TEST depth_func=DEPTH_TEST::LESS;
bool depth_test;
public:
CompOperatorMemcmp(struct DepthStatus &);
};//
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<RenderStateBlock *> 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;i<count;i++)
{
(*rsb)->Apply();
++rsb;
}
}
};//class RenderStateData:public RenderStateBlock
/**
*
*/
struct RenderState
class RenderState
{
bool color_mask[4];
Color4f clear_color;
RenderStateData state_data;
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; ///<深度遮罩
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

View File

@ -7,6 +7,7 @@ SET(GRAPH_SRC_FILES OpenGLDebug.cpp
BufferObject.cpp
VertexArray.cpp
Renderable.cpp
RenderState.cpp
TextureFormat.cpp
Texture1D.cpp
Texture1DDSA.cpp

View File

@ -0,0 +1,46 @@
#include<hgl/graph/RenderState.h>
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