新的VertexArray对象,删除Draw代码。

This commit is contained in:
HuYingzhuo 2019-03-27 14:12:47 +08:00
parent 8a8339a431
commit 1327e52a3f
3 changed files with 32 additions and 104 deletions

View File

@ -61,7 +61,7 @@ namespace hgl
const VertexBufferData *GetVertexBufferData()const { return vertex_buffer_data; }
#define VBD_FUNC_COPY(type,name) type Get##name()const{vertex_buffer_data?vertex_buffer_data->Get##name():0;}
#define VBD_FUNC_COPY(type,name) type Get##name()const{return vertex_buffer_data?vertex_buffer_data->Get##name():0;}
VBD_FUNC_COPY(GLenum,DataType)
VBD_FUNC_COPY(uint,Component)
@ -74,6 +74,8 @@ namespace hgl
VertexBufferObject *CreateVertexBufferObject(const GLenum &type,const GLenum &user_pattern=0,VertexBufferData *buf=nullptr); ///<创建一个顶点缓冲区对象
VertexBufferObject *CreateVertexBufferObject(const GLenum &buf_type,const GLenum &user_pattern,const GLsizeiptr &total_bytes); ///<创建一个顶点缓冲区对象
VertexBufferObject *CreateVertexBufferObject(const GLenum &buf_type,const GLenum &user_pattern,const GLsizeiptr &total_bytes,void *data); ///<创建一个顶点缓冲区对象
using ElementBufferObject=VertexBufferObject;
}//namespace graph
}//namespace hgl
#endif//HGL_GRAPH_BUFFER_OBJECT_INCLUDE

View File

@ -2,7 +2,7 @@
#define HGL_GRAPH_VERTEX_ARRAY_INCLUDE
#include<hgl/type/List.h>
#include<hgl/graph/VertexBuffer.h>
#include<hgl/graph/BufferObject.h>
#include<hgl/graph/PixelCompoment.h>
namespace hgl
{
@ -17,55 +17,37 @@ namespace hgl
GLuint vao;
uint primitive; ///<绘制的图元类型
ObjectList<VertexBufferObject> vbo_list; ///<顶点数据缓冲区
ObjectList<VertexBufferBase> vertex_buffer_list; ///<顶点数据缓冲区
ElementBufferObject *element_buffer;
VertexBufferBase *element_buffer;
VertexBufferBase *position_buffer;
int position_compoment; ///<位置属性格式
List<Pair<VertexBufferBase *,PixelCompoment>> color_buffer;
PixelCompoment color_compoment; ///<颜色属性格式
VertexBufferBase *color_buffer;
VertexBufferObject *position_buffer;
int position_compoment; ///<位置属性格式
public:
VertexArray(uint prim,uint max_vertex_attrib);
VertexArray(uint max_vertex_attrib);
~VertexArray();
static int GetMaxVertexAttrib();
static int GetMaxVertexAttrib();
const uint GetPrimitive ()const{return primitive;} ///<取得要绘制的图元类型
const GLuint GetVAO ()const{return vao;} ///<取得VAO对象
const GLuint GetVAO ()const{return vao;} ///<取得VAO对象
public: //通用顶点缓冲区设置
int AddVertexAttribBuffer (int shader_location,VertexBufferBase *); ///<设置顶点缓冲区数据
VertexBufferBase * GetVertexAttribBuffer (int index){return vertex_buffer_list[index];} ///<取得顶点缓冲区数据
bool ClearVertexAttribBuffer (int index){return vertex_buffer_list.Delete(index);} ///<清除顶点缓冲区数据
void ClearVertexAttribBuffers (){vertex_buffer_list.Clear();} ///<清除所有顶点缓冲区数据
int AddBuffer (int shader_location,VertexBufferObject *); ///<设置顶点缓冲区对象
VertexBufferObject *GetBuffer (int index){return vbo_list[index];} ///<取得顶点缓冲区对象
bool ClearBuffer (int index){return vbo_list.Delete(index);} ///<清除顶点缓冲区对象
void ClearBuffers(){ vbo_list.Clear();} ///<清除所有顶点缓冲区对象
public: //特殊缓冲区独立设置函数
bool SetElementBuffer (VertexBufferBase *eb); ///<设置索引缓冲区数据
bool SetPositionBuffer (int shader_location,VertexBufferBase *vb); ///<设置位置缓冲区数据
bool SetElement (ElementBufferObject *eb); ///<设置索引缓冲区对象
bool SetPosition (int shader_location,VertexBufferObject *vb); ///<设置位置缓冲区对象
bool AddColorBuffer (int shader_location,VertexBufferBase *vb,PixelCompoment cf); ///<添加一个颜色缓冲区数据
int GetPositionCompoment()const{return position_compoment;} ///<取得位置数据成分数量
PixelCompoment GetColorCompoment ()const{return color_compoment;} ///<取得颜色数据成份格式
public:
int GetDrawCount (); ///<取得可绘制的数据总数量
bool Draw(); ///<绘制
ElementBufferObject * GetElement (){return element_buffer;} ///<获取索引缓冲区对象
VertexBufferObject * GetPosition (){return position_buffer;} ///<获取位置缓冲区对象
};//class VertexArray
//新设计内容如碰到此处编译失败请在GIT上退回到上一版本
//1.ColorBuffer可能存在多个所以上面的SetColorBuffer可能要考虑多个的情况
//2.将VertexArray类拆分成独立的VAO类和VAOCreater类所有创建VAO相关的全部放到VAOCreater类中创建完删除自身并返回VAO对象
}//namespace graph
}//namespace hgl
#endif//HGL_GRAPH_VERTEX_ARRAY_INCLUDE

View File

@ -7,7 +7,7 @@ namespace hgl
{
namespace
{
static int HGL_MAX_VERTEX_ATTRIBS=0;
static GLint HGL_MAX_VERTEX_ATTRIBS=0;
}
int VertexArray::GetMaxVertexAttrib()
@ -18,18 +18,13 @@ namespace hgl
return HGL_MAX_VERTEX_ATTRIBS;
}
VertexArray::VertexArray(uint prim,uint max_vertex_attrib)
VertexArray::VertexArray(uint max_vertex_attrib)
{
if(max_vertex_attrib>GetMaxVertexAttrib())
max_vertex_attrib=HGL_MAX_VERTEX_ATTRIBS;
primitive=prim;
vertex_buffer_list.PreMalloc(max_vertex_attrib);
position_compoment=-1;
color_compoment=HGL_PC_NONE;
vbo_list.PreMalloc(max_vertex_attrib);
element_buffer=nullptr;
glCreateVertexArrays(1,&vao);
@ -47,12 +42,12 @@ namespace hgl
* @return
* @return -1
*/
int VertexArray::AddVertexAttribBuffer(int shader_location, VertexBufferBase *vb)
int VertexArray::AddBuffer(int shader_location,VertexBufferObject *vb)
{
if(!vb)return(false);
if(vb->GetBufferType()!=GL_ARRAY_BUFFER)return(false);
const int binding_index = vertex_buffer_list.GetCount(); //一个VAO中的绑定点必须从0开始而且必须紧密排列
const int binding_index = vbo_list.GetCount(); //一个VAO中的绑定点必须从0开始而且必须紧密排列
glVertexArrayAttribBinding(vao, shader_location, binding_index);
@ -60,10 +55,10 @@ namespace hgl
if(vb->GetDataType()==GL_DOUBLE ) glVertexArrayAttribLFormat( vao,shader_location,vb->GetComponent(),vb->GetDataType(),0);else
glVertexArrayAttribFormat( vao,shader_location,vb->GetComponent(),vb->GetDataType(),GL_FALSE,0);
glEnableVertexArrayAttrib(vao, shader_location);
glVertexArrayVertexBuffer(vao, binding_index, vb->GetBufferIndex(), 0, vb->GetStride());
glEnableVertexArrayAttrib(vao,shader_location);
glVertexArrayVertexBuffer(vao,binding_index,vb->GetBufferIndex(),0,vb->GetStride());
vertex_buffer_list.Add(vb);
vbo_list.Add(vb);
return binding_index;
}
@ -71,13 +66,13 @@ namespace hgl
/**
*
*/
bool VertexArray::SetElementBuffer(VertexBufferBase *eb)
bool VertexArray::SetElement(ElementBufferObject *eb)
{
if(!eb)return(false);
if(eb->GetBufferType()!=GL_ELEMENT_ARRAY_BUFFER)return(false);
element_buffer=eb;
glVertexArrayElementBuffer(vao, eb->GetBufferIndex());
glVertexArrayElementBuffer(vao,eb->GetBufferIndex());
return(true);
}
@ -86,66 +81,15 @@ namespace hgl
* @param shader_location SHADER地址
* @param vb
*/
bool VertexArray::SetPositionBuffer(int shader_location, VertexBufferBase *vb)
bool VertexArray::SetPosition(int shader_location,VertexBufferObject *vb)
{
if(!vb)return(false);
if(!AddVertexAttribBuffer(shader_location,vb)<0)
if(!AddBuffer(shader_location,vb)<0)
return(false);
position_compoment=vb->GetComponent();
position_buffer=vb;
return(true);
}
/**
*
* @param shader_location SHADER地址
* @param vb
* @param cf
*/
bool VertexArray::AddColorBuffer(int shader_location, VertexBufferBase *vb,PixelCompoment cf)
{
if(!vb)return(false);
if(cf<=HGL_PC_NONE||cf>=HGL_PC_END)return(false);
if(AddVertexAttribBuffer(shader_location,vb)<0)
return(false);
color_compoment=cf;
color_buffer=vb;
return(true);
}
/**
*
* @return
* @return -1
*/
int VertexArray::GetDrawCount()
{
if(element_buffer)
return element_buffer->GetCount();
if(position_buffer)
return position_buffer->GetCount();
return(-1);
}
bool VertexArray::Draw()
{
glBindVertexArray(vao);
if (element_buffer)
glDrawElements(primitive, element_buffer->GetCount(), element_buffer->GetDataType(), nullptr);
else
if(position_buffer)
glDrawArrays(primitive,0,position_buffer->GetCount());
else
return(false);
return(true);
}
}//namespace graph
}//namespace hgl