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

This commit is contained in:
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()