Sign in to follow this  
try_catch_this

Using indexed lists in vbo's

Recommended Posts

Creating a buffer glGenBuffersARB(1, &buffer); glBindBufferARB(GL_ARRAY_BUFFER_ARB, buffer); glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(data), data, GL_STATIC_DRAW_ARB); Using the buffer glEnableClientState( ... ); glBindBufferARB(GL_ARRAY_BUFFER_ARB, buffer); glVertexPointer( 3, GL_FLOAT, 0, 0); glDrawArrays(GL_TRIANGLE_STRIP, 0, sizeof(data)); I have managed to get this done for vertex and texture coord arrays. But I cant seem to figure out how to do this for indexed lists of triangles. any code samples would be nice.

Share this post


Link to post
Share on other sites
Have you done it with normal vertex arrays using index'd data?
If so, its the same, you use the functions which take a pointer to the index data or a VBO offset (if you put your indices into a VBO)

Share this post


Link to post
Share on other sites
::glDrawElements( GL_TRIANGLES, m_MapMesh.m_Indices.size(), GL_UNSIGNED_INT, &m_MapMesh.m_Indices[0] );

The problem here is that the indices is on the clientside. I need to get it on the server side. Thats why you have vbo's.

m_MapMesh.m_Indices is a std::vector<GLuint>

This is stored on the clientside. I need to find out how to get my indices on the server side.

It is not the same for vertex data, thats why i think I need some sort of a simple code sample.

thanks

Share this post


Link to post
Share on other sites
// create vertex buffer
glGenBuffersARB(1, &mapbuffer);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, mapbuffer);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, width*height*3*sizeof(GLfloat), p, GL_STATIC_DRAW_ARB);

// create index buffer
glGenBuffersARB(1, &index_buf);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, index_buf);
glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, isiz*sizeof(GLushort), tet_index, GL_STATIC_DRAW_ARB);


You then just use drawElements and pass 0 as the pointer to the index data after you have bound the element_array buffer...

Google for GL_ELEMENT_ARRAY_BUFFER_ARB - I think I learned how to use this extension from an example on flipcode or somewhere. There is a thread in a forum about exactly this topic as well as using texture coord buffers etc...

good luck,
mike

Share this post


Link to post
Share on other sites
ok here is a very basic example using triangle lists,we are just drawing a mesh with no y value.


struct Vertex
{
float x, y, z;
};

int vertexCount;
int indicecount;
unsigned int *indices; //indice array
Vertex *Vertices; //vertex array
int m_iSize; //size of our mesh

unsigned int m_VBOIndices; // VBO Vertex Object
unsigned int m_VBOVertices;
#define BUFFER_OFFSET(i) ((char *)NULL + (i))

void init()
{
//create vertex array
vertexCount = m_iSize * m_iSize*3;
Vertices = new Vertex[vertexCount];
for(int z=0; z<m_iSize; z++ )
{
for( int x=0; x<m_iSize; x++ )
{
Vertices[z*m_iSize+x].x = x;
Vertices[z*m_iSize+x].y = 0;
Vertices[z*m_iSize+x].z = z;
}

}

//create indice array
int i=0;
indicecount = (m_iSize-1)*(m_iSize-1)*6;
indices = new unsigned int[indicecount];
for(int z=0; z<m_iSize-1; z++ )
{
for( int x=0; x<m_iSize-1; x++ )
{
//triangle 1
indices[i] = x +(m_iSize *z);
i++;
indices[i] = x + (m_iSize * (z+1));
i++;
indices[i] = (x+1) + (m_iSize * (z+1));
i++;
//triangle 2
indices[i] = (x+1) + (m_iSize * (z+1));
i++;
indices[i] = (x+1) + (m_iSize * z);
i++;
indices[i] = x +(m_iSize *z);
i++;

}
}

//create vertex VBO
glGenBuffersARB( 1, &m_VBOVertices );
glBindBufferARB( GL_ARRAY_BUFFER_ARB, m_VBOVertices );
glBufferDataARB( GL_ARRAY_BUFFER_ARB, vertexCount*3*sizeof(float), Vertices, GL_STATIC_DRAW_ARB );
delete [] Vertices; Vertices=0;
//create indice VBO
glGenBuffersARB(1, &m_VBOIndices);
glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, m_VBOIndices );
glBufferDataARB( GL_ELEMENT_ARRAY_BUFFER_ARB,indicecount*sizeof(float),indices,GL_STATIC_DRAW_ARB);
delete[] indices;indices=0;
}

void render()
{
glEnableClientState(GL_VERTEX_ARRAY);
glBindBufferARB( GL_ARRAY_BUFFER_ARB, m_VBOVertices );
glVertexPointer( 3, GL_FLOAT, 0, BUFFER_OFFSET(0) );
glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, m_VBOIndices );
//render the whole mesh
glDrawElements(GL_TRIANGLES,indicecount,GL_UNSIGNED_INT,BUFFER_OFFSET(0));

glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 ); //unbind the VBO
glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB,0 );
glDisableClientState(GL_VERTEX_ARRAY);
}
void cleanup()
{
glDeleteBuffersARB(1, &m_VBOVertices);
glDeleteBuffersARB(1, &m_VBOIndices);
}





hope that helps

Share this post


Link to post
Share on other sites

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