引入RenderState设计
This commit is contained in:
parent
403499e1f1
commit
514a4b37ca
@ -3,6 +3,7 @@
|
|||||||
#include<hgl/graph/RenderWindow.h>
|
#include<hgl/graph/RenderWindow.h>
|
||||||
#include<iostream>
|
#include<iostream>
|
||||||
#include<hgl/graph/Renderable.h>
|
#include<hgl/graph/Renderable.h>
|
||||||
|
#include<hgl/graph/RenderState.h>
|
||||||
|
|
||||||
using namespace hgl;
|
using namespace hgl;
|
||||||
using namespace hgl::graph;
|
using namespace hgl::graph;
|
||||||
@ -59,10 +60,39 @@ ArrayBuffer *vb_vertex=nullptr;
|
|||||||
ArrayBuffer *vb_color=nullptr;
|
ArrayBuffer *vb_color=nullptr;
|
||||||
VertexArray *va=nullptr;
|
VertexArray *va=nullptr;
|
||||||
Renderable *render_obj=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 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 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()
|
void InitVertexBuffer()
|
||||||
{
|
{
|
||||||
vb_vertex=CreateVBO(VB2f(3,vertex_data));
|
vb_vertex=CreateVBO(VB2f(3,vertex_data));
|
||||||
@ -79,21 +109,9 @@ void InitVertexBuffer()
|
|||||||
render_obj=new Renderable(GL_TRIANGLES,va);
|
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()
|
void draw()
|
||||||
{
|
{
|
||||||
glClearBufferfv(GL_COLOR,0,clear_color);
|
render_state.Apply();
|
||||||
glClearBufferfv(GL_DEPTH,0,&clear_depth);
|
|
||||||
|
|
||||||
render_obj->Draw();
|
render_obj->Draw();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,6 +149,7 @@ int main(void)
|
|||||||
return -3;
|
return -3;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
InitRenderState();
|
||||||
InitVertexBuffer();
|
InitVertexBuffer();
|
||||||
|
|
||||||
win->Show();
|
win->Show();
|
||||||
|
@ -2,54 +2,152 @@
|
|||||||
#define HGL_RENDER_STATE_INCLUDE
|
#define HGL_RENDER_STATE_INCLUDE
|
||||||
|
|
||||||
#include<hgl/type/Color4f.h>
|
#include<hgl/type/Color4f.h>
|
||||||
#include<hgl/CompOperator.h>
|
//#include<hgl/CompOperator.h>
|
||||||
|
#include<hgl/type/Set.h>
|
||||||
|
#include<GLEWCore/glew.h>
|
||||||
namespace hgl
|
namespace hgl
|
||||||
{
|
{
|
||||||
enum DEPTH_TEST_FUNC
|
namespace graph
|
||||||
{
|
{
|
||||||
DEPTH_TEST_NEVER=0,
|
struct RenderStateBlock
|
||||||
DEPTH_TEST_LESS,
|
{
|
||||||
DEPTH_TEST_EQUAL,
|
virtual void Apply()=0;
|
||||||
DEPTH_TEST_LEQUAL,
|
};
|
||||||
DEPTH_TEST_GREATER,
|
|
||||||
DEPTH_TEST_NOTEQUAL,
|
|
||||||
DEPTH_TEST_GEQUAL,
|
|
||||||
DEPTH_TEST_ALWAYS
|
|
||||||
};//
|
|
||||||
|
|
||||||
struct DepthStatus
|
struct ColorState:public RenderStateBlock
|
||||||
{
|
{
|
||||||
float near_depth =0,
|
bool red =true;
|
||||||
far_depth =1;
|
bool green =true;
|
||||||
|
bool blue =true;
|
||||||
|
bool alpha =true;
|
||||||
|
|
||||||
bool depth_mask =true;
|
bool clear_color=false;
|
||||||
float clear_depth =far_depth;
|
GLfloat clear_color_value[4]={0,0,0,0};
|
||||||
|
|
||||||
DEPTH_TEST_FUNC depth_func =DEPTH_TEST_LESS;
|
public:
|
||||||
bool depth_test;
|
|
||||||
|
|
||||||
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 DepthState:public RenderStateBlock
|
||||||
* 渲染状态
|
{
|
||||||
*/
|
GLfloat near_depth=0,
|
||||||
struct RenderState
|
far_depth=1;
|
||||||
{
|
|
||||||
bool color_mask[4];
|
|
||||||
Color4f clear_color;
|
|
||||||
|
|
||||||
DepthStatus depth;
|
bool depth_mask=true;
|
||||||
// uint draw_face; ///<绘制的面
|
GLfloat clear_depth_value=far_depth; //清空深度时所用的值
|
||||||
// uint fill_mode; ///<填充方式
|
bool clear_depth; //是否要清空深度
|
||||||
//
|
|
||||||
// uint cull_face; ///<裁剪面,0:不裁(双面材质),FACE_FRONT:正面,FACE_BACK:背面
|
DEPTH_TEST depth_func=DEPTH_TEST::LESS;
|
||||||
//
|
bool depth_test;
|
||||||
// bool depth_test; ///<深度测试
|
|
||||||
// uint depth_func; ///<深度处理方式
|
public:
|
||||||
// bool depth_mask; ///<深度遮罩
|
|
||||||
};//class RenderState
|
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
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 渲染状态
|
||||||
|
*/
|
||||||
|
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
|
}//namespace hgl
|
||||||
#endif//HGL_RENDER_STATE_INCLUDE
|
#endif//HGL_RENDER_STATE_INCLUDE
|
||||||
|
@ -7,6 +7,7 @@ SET(GRAPH_SRC_FILES OpenGLDebug.cpp
|
|||||||
BufferObject.cpp
|
BufferObject.cpp
|
||||||
VertexArray.cpp
|
VertexArray.cpp
|
||||||
Renderable.cpp
|
Renderable.cpp
|
||||||
|
RenderState.cpp
|
||||||
TextureFormat.cpp
|
TextureFormat.cpp
|
||||||
Texture1D.cpp
|
Texture1D.cpp
|
||||||
Texture1DDSA.cpp
|
Texture1DDSA.cpp
|
||||||
|
46
src/RenderDriver/RenderState.cpp
Normal file
46
src/RenderDriver/RenderState.cpp
Normal 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
|
Loading…
x
Reference in New Issue
Block a user