Sign in to follow this  

Has anyone tried using generic vertex attributes with a VBO and glDrawElements?

This topic is 3869 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Has anyone tried using generic vertex attributes with a VBO and glDrawElements? I currently have a app which renders objects from VBOs. Each object has it's own VBO, which objects having the same geometry reusing a VBO. I am looking for an example of how to do that, and pass an array of data to the GPU for use as a generic vertex attribute. Thanks, Ed

Share this post


Link to post
Share on other sites
Hey, yeah, I used a VBO-class which handled all of these and I used vertex attributes for tangents and bitangents. I stored this data as a VBO, too.

But AFAIK now, it would have been better to use offsets and larger VBOs as this would be faster, but I did not yet try..

Here are some parts of my code which should show you how to do this:

//When setting up the VBO:

glGenBuffers(1,&m_VBO_vert);
glBindBuffer(GL_ARRAY_BUFFER,m_VBO_vert);
glBufferData(GL_ARRAY_BUFFER,m_vertCount*3*sizeof(float),&mc_vertices[0],GL_STATIC_DRAW);


glGenBuffers(1,&m_VBO_tex);
glBindBuffer(GL_ARRAY_BUFFER,m_VBO_tex);
glBufferData(GL_ARRAY_BUFFER,m_vertCount*2*sizeof(float),&mc_texcoords[0],GL_STATIC_DRAW);

if(m_useNormals)
{
glGenBuffers(1,&m_VBO_norm);
glBindBuffer(GL_ARRAY_BUFFER,m_VBO_norm);
glBufferData(GL_ARRAY_BUFFER,m_vertCount*3*sizeof(float),&mc_normals[0],GL_STATIC_DRAW);
}

if(m_useTangents)
{
glGenBuffers(1,&m_VBO_tan);
glBindBuffer(GL_ARRAY_BUFFER,m_VBO_tan);
glBufferData(GL_ARRAY_BUFFER,m_vertCount*3*sizeof(float),&mc_tangents[0],GL_STATIC_DRAW);

glGenBuffers(1,&m_VBO_bitan);
glBindBuffer(GL_ARRAY_BUFFER,m_VBO_bitan);
glBufferData(GL_ARRAY_BUFFER,m_vertCount*3*sizeof(float),&mc_bitangents[0],GL_STATIC_DRAW);
}

//for drawing
// the attribLocations must be known (glGetAttribLocation...)

if(m_useTangents && attribLocation>=0)
glEnableVertexAttribArray(attribLocation);//Enable the vertex attrib array
if(m_useTangents && attribLocation2>=0)
glEnableVertexAttribArray(attribLocation2);

if(supported)
{
glBindBuffer(GL_ARRAY_BUFFER,m_VBO_vert);
glVertexPointer(3,GL_FLOAT,0,(char *)NULL);

glBindBuffer(GL_ARRAY_BUFFER,m_VBO_tex);
glTexCoordPointer(2,GL_FLOAT,0,(char *)NULL);

if(m_useNormals)
{

glBindBuffer(GL_ARRAY_BUFFER,m_VBO_norm);
glNormalPointer(GL_FLOAT,0,(char *)NULL);
}
if(m_useTangents && attribLocation>=0)
{
glBindBuffer(GL_ARRAY_BUFFER,m_VBO_tan);
glVertexAttribPointer(attribLocation,4,GL_FLOAT,0,0,(char *)NULL);//set the pointer to the array
}
if(m_useTangents && attribLocation2>=0)
{
glBindBuffer(GL_ARRAY_BUFFER,m_VBO_bitan);
glVertexAttribPointer(attribLocation2,4,GL_FLOAT,0,0,(char *)NULL);
}

//and then draw using
glDrawArrays(GL_TRIANGLES,0,m_vertCount);

//and unbind all vbos and arrays..
glBindBuffer(GL_ARRAY_BUFFER,0);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
if(m_useNormals)
glDisableClientState(GL_NORMAL_ARRAY);
if(m_useTangents && attribLocation>=0)
glDisableVertexAttribArray(attribLocation);
if(m_useTangents && attribLocation2>=0)
glDisableVertexAttribArray(attribLocation2);




Sorry, this is just a bunch of code but I think it should show how to do what you want to.
Feel free to ask what I did there [wink]

Share this post


Link to post
Share on other sites
Thanks for the reply....

I think I see where I messed up.... I did make calls to glBufferData(...) with my attribute data...

Ed


Quote:
Original post by Caste
Hey, yeah, I used a VBO-class which handled all of these and I used vertex attributes for tangents and bitangents. I stored this data as a VBO, too.

But AFAIK now, it would have been better to use offsets and larger VBOs as this would be faster, but I did not yet try..

Here are some parts of my code which should show you how to do this:
*** Source Snippet Removed ***

Sorry, this is just a bunch of code but I think it should show how to do what you want to.
Feel free to ask what I did there [wink]


Share this post


Link to post
Share on other sites

This topic is 3869 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this