Jump to content
  • Advertisement
Sign in to follow this  
EmptyVoid

How to use Index Buffers in OpenGL?

This topic is 3667 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

Basically as I understand it GL_ELEMENT_ARRAY_BUFFER is the OpenGL Index buffer but how do I create and render one? Current initialization function:
void load_mesh()
{
CVert*			   vertices;
int*                indices;

// This is where I create a array of vertices for the vertex buffer.
vertices = new CVert[3];
vertices[0].x=0;
vertices[0].y=0;
vertices[0].z=0;
vertices[1].x=1;
vertices[1].y=0;
vertices[1].z=0;
vertices[2].x=0;
vertices[2].y=1;
vertices[2].z=0;

// This is where I create a array of indices for the index buffer
indices = new int[3];
indices[0]=2;
indices[1]=1;
indices[2]=0;

// Creating the vertex buffer.
glGenBuffers( 1, &vertex_buffer );
glBindBuffer( GL_ARRAY_BUFFER, vertex_buffer );
glBufferData( GL_ARRAY_BUFFER, 9*sizeof(float), vertices, GL_STATIC_DRAW );

// Creating the index buffer.
glGenBuffers( 1, &index_buffer );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, index_buffer );
glBufferData( GL_ELEMENT_ARRAY_BUFFER, 3*sizeof(int), indices, GL_STATIC_DRAW );

// Cleaning up.
delete [] vertices;
delete [] indices;
}












Current Render function:
void render()
{
// Clear screen and point the view in the right direction.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0.0f, 25.0f, -45.0f, 0.0f, 0.0f, 0.0f, 0, 1, 0);
cgGLSetStateMatrixParameter(modelViewMatrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);

// Enabling and Binding shaders.
cgGLEnableProfile(cgVertexProfile);
cgGLBindProgram(cg_vs);
cgGLEnableProfile(cgPixelProfile);
cgGLBindProgram(cg_ps);

// Setting color.
cgGLSetParameter4f(color, 0.0f, 1.0f, 0.0f, 1.0f);

// Enabling Client States
glEnableClientState( GL_VERTEX_ARRAY );

// Binding Vertex buffer.
glBindBuffer( GL_ARRAY_BUFFER, vertex_buffer );
glVertexPointer( 3, GL_FLOAT, 0, (char *) NULL );

// Binding Index buffer.
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, index_buffer );

// Here is where I am trying to render with the index buffer but I don't think I'm using the correct command. 
glDrawElements(GL_TRIANGLES,1,GL_UNSIGNED_SHORT,0);

//Disabling Client States.
glDisableClientState( GL_VERTEX_ARRAY );

// Disabling Shaders.
cgGLDisableProfile(cgPixelProfile);
cgGLDisableProfile(cgVertexProfile);

// Displaying rendered scene.
SwapBuffers(hDC);
}













[Edited by - EmptyVoid on August 1, 2008 3:55:48 PM]

Share this post


Link to post
Share on other sites
Advertisement
glIndexPointer is the indexed color equivalent of glColorPointer and has nothing to do with indexed vertex arrays. The vertex index array is passed to glDrawElements. glDrawArrays are for consecutive vertices only.

Share this post


Link to post
Share on other sites
Quote:
Original post by Brother Bob
glIndexPointer is the indexed color equivalent of glColorPointer and has nothing to do with indexed vertex arrays. The vertex index array is passed to glDrawElements. glDrawArrays are for consecutive vertices only.


How would I use glDrawElements? I edited the code above to reflect the changes I made.

Share this post


Link to post
Share on other sites
Looks correct, except that you only draw one vertex. The second parameter to glDrawElements is the number of indices to reference, so if you want to draw a triangle, you need to reference three vertices.

Share this post


Link to post
Share on other sites
Quote:
Original post by Brother Bob
Looks correct, except that you only draw one vertex. The second parameter to glDrawElements is the number of indices to reference, so if you want to draw a triangle, you need to reference three vertices.


Ok it's working thanks!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!