改进VertexArray,在添加数据时,自动bind

This commit is contained in:
hyzboy 2019-03-09 16:52:23 +08:00
parent d4b3783efa
commit e26d56907a
5 changed files with 33 additions and 49 deletions

View File

@ -65,14 +65,6 @@ VertexArray *va=nullptr;
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 };
void BindVBO2VAO(const int vao,const int binding_index,const int shader_location,VertexBufferBase *vb)
{
glVertexArrayAttribBinding(vao,shader_location,binding_index);
glVertexArrayAttribFormat(vao,shader_location,vb->GetComponent(),vb->GetDataType(),GL_FALSE,0);
glEnableVertexArrayAttrib(vao,shader_location);
glVertexArrayVertexBuffer(vao,binding_index,vb->GetBufferIndex(),0,vb->GetStride());
}
void InitVertexBuffer()
{
vb_vertex=new VB2f(3,vertex_data);
@ -84,16 +76,8 @@ void InitVertexBuffer()
const int vertex_location=shader.GetAttribLocation("Vertex"); ///<取得顶点数据输入流对应的shader地址
const int color_location=shader.GetAttribLocation("Color"); ///<取得颜色数据输入流对应的shader地址
int binding_index=0; //绑定点
const int vao=va->GetVAO();
va->SetVertexBuffer(vb_vertex);
va->SetColorBuffer(vb_color,HGL_PC_RGB);
BindVBO2VAO(vao,binding_index,vertex_location,vb_vertex);
++binding_index;
BindVBO2VAO(vao,binding_index,color_location,vb_color);
va->SetVertexBuffer(vertex_location,vb_vertex);
va->SetColorBuffer(color_location,vb_color,HGL_PC_RGB);
}
constexpr GLfloat clear_color[4]=

View File

@ -85,15 +85,6 @@ constexpr float texcoord_data[]={ 0,0,
1,0,
1,1 };
void BindVBO2VAO(const int vao,const int binding_index,const int shader_location,VertexBufferBase *vb)
{
glVertexArrayAttribBinding(vao,shader_location,binding_index);
glVertexArrayAttribFormat(vao,shader_location,vb->GetComponent(),vb->GetDataType(),GL_FALSE,0);
glEnableVertexArrayAttrib(vao,shader_location);
glVertexArrayVertexBuffer(vao,binding_index,vb->GetBufferIndex(),0,vb->GetStride());
}
void InitVertexBuffer()
{
vb_vertex=new VB2f(4,vertex_data);
@ -105,16 +96,8 @@ void InitVertexBuffer()
const int vertex_location=shader.GetAttribLocation("Vertex"); ///<取得顶点数据输入流对应的shader地址
const int texcoord_location=shader.GetAttribLocation("TexCoord"); ///<取得纹理坐标数据输入流对应的shader地址
int binding_index=0; //绑定点
const int vao=va->GetVAO();
va->SetVertexBuffer(vb_vertex);
va->AddVertexAttribBuffer(vb_texcoord);
BindVBO2VAO(vao,binding_index,vertex_location,vb_vertex);
++binding_index;
BindVBO2VAO(vao,binding_index,texcoord_location,vb_texcoord);
va->SetVertexBuffer(vertex_location,vb_vertex);
va->AddVertexAttribBuffer(texcoord_location,vb_texcoord);
}
bool InitTexture()

View File

@ -26,6 +26,8 @@ namespace hgl
//4通道
HGL_PC_RGBA,
//4通道 3+1模式
HGL_PC_RGB_SPECULAR,
HGL_PC_RGB_DEPTH,
HGL_PC_NORMAL_HEIGHT,

View File

@ -40,7 +40,7 @@ namespace hgl
public: //通用顶点缓冲区设置
int AddVertexAttribBuffer (VertexBufferBase *); ///<设置顶点缓冲区数据
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();} ///<清除所有顶点缓冲区数据
@ -48,8 +48,8 @@ namespace hgl
public: //特殊缓冲区独立设置函数
bool SetElementBuffer (VertexBufferBase *eb); ///<设置索引缓冲区数据
bool SetVertexBuffer (VertexBufferBase *vb); ///<设置顶点缓冲区数据
bool SetColorBuffer (VertexBufferBase *vb,PixelCompoment cf); ///<设置颜色缓冲区数据
bool SetVertexBuffer (int shader_location,VertexBufferBase *vb); ///<设置顶点缓冲区数据
bool SetColorBuffer (int shader_location,VertexBufferBase *vb,PixelCompoment cf); ///<设置颜色缓冲区数据
int GetVertexCompoment ()const{return vertex_compoment;} ///<取得顶点数据成分数量
PixelCompoment GetColorCompoment ()const{return color_compoment;} ///<取得顶点颜色格式

View File

@ -43,15 +43,28 @@ namespace hgl
/**
*
* @param vb
* @return
* @return
* @return -1
*/
int VertexArray::AddVertexAttribBuffer(VertexBufferBase *vb)
int VertexArray::AddVertexAttribBuffer(int shader_location, VertexBufferBase *vb)
{
if(!vb)return(false);
if(vb->GetBufferType()!=GL_ARRAY_BUFFER)return(false);
return vertex_buffer_list.Add(vb);
const int binding_index = vertex_buffer_list.GetCount(); //一个VAO中的绑定点必须从0开始而且必须紧密排列
glVertexArrayAttribBinding(vao, shader_location, binding_index);
if(vb->GetDataType()==GL_INT ) glVertexArrayAttribIFormat( vao,shader_location,vb->GetComponent(),vb->GetDataType(),0);else
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());
vertex_buffer_list.Add(vb);
return binding_index;
}
bool VertexArray::SetElementBuffer(VertexBufferBase *eb)
@ -59,14 +72,16 @@ namespace hgl
if(!eb)return(false);
if(eb->GetBufferType()!=GL_ELEMENT_ARRAY_BUFFER)return(false);
element_buffer=eb;
glVertexArrayElementBuffer(vao, eb->GetBufferIndex());
return(true);
}
bool VertexArray::SetVertexBuffer(VertexBufferBase *vb)
bool VertexArray::SetVertexBuffer(int shader_location, VertexBufferBase *vb)
{
if(!vb)return(false);
if(!AddVertexAttribBuffer(vb)<0)
if(!AddVertexAttribBuffer(shader_location,vb)<0)
return(false);
vertex_compoment=vb->GetComponent();
@ -74,12 +89,12 @@ namespace hgl
return(true);
}
bool VertexArray::SetColorBuffer(VertexBufferBase *vb,PixelCompoment cf)
bool VertexArray::SetColorBuffer(int shader_location, VertexBufferBase *vb,PixelCompoment cf)
{
if(!vb)return(false);
if(cf<=HGL_PC_NONE||cf>=HGL_PC_END)return(false);
if(AddVertexAttribBuffer(vb)<0)
if(AddVertexAttribBuffer(shader_location,vb)<0)
return(false);
color_compoment=cf;
@ -107,8 +122,8 @@ namespace hgl
{
glBindVertexArray(vao);
if(element_buffer)
glDrawElements(primitive,element_buffer->GetCount(),element_buffer->GetDataType(),nullptr);
if (element_buffer)
glDrawElements(primitive, element_buffer->GetCount(), element_buffer->GetDataType(), nullptr);
else
if(vertex_buffer)
glDrawArrays(primitive,0,vertex_buffer->GetCount());