Indices in VBO

Hi,

i have a problem. I cant pack Indices into buffer …

This is data to draw:

		NumVertices = 3;
		NumIndices = 3;
		Vertices = new SVertex[NumVertices];
		Colors = new SColor[NumVertices];
		Indices = new GLuint[NumIndices];

		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 = 1;
		Vertices[2].y = 1;
		Vertices[2].z = 0;

		Colors[0].r = 1;
		Colors[0].g = 0;
		Colors[0].b = 0;

		Colors[1].r = 1;
		Colors[1].g = 0;
		Colors[1].b = 0;

		Colors[2].r = 1;
		Colors[2].g = 0;
		Colors[2].b = 0;

		Indices[0]=0;
		Indices[1]=1;
		Indices[2]=2;

This is drawing function:

Ok, i forgot about that. So now mu drawing function looks like this:

glGenBuffersARB( 1, &Colr );
glBindBufferARB( GL_ARRAY_BUFFER_ARB, Colr );
glBufferDataARB( GL_ARRAY_BUFFER_ARB, NumVertices*sizeof(SColor), Colors, GL_STATIC_DRAW_ARB );
glColorPointer(3,GL_FLOAT,0,0);

glGenBuffersARB( 1, &VertV );
glBindBufferARB( GL_ARRAY_BUFFER_ARB, VertV );
glBufferDataARB( GL_ARRAY_BUFFER_ARB, NumVertices*sizeof(Svertex), Vertices, GL_STATIC_DRAW_ARB );
glVertexPointer(3,GL_FLOAT,0,0);

glGenBuffersARB( 1, &Indict );
glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, Indict );
glBufferDataARB( GL_ELEMENT_ARRAY_BUFFER_ARB, NumIndices*sizeof(GLuint), Indices, GL_STATIC_DRAW_ARB );


		glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_COLOR_ARRAY);		
			
			glDrawElements(	GL_TRIANGLES,3,GL_UNSIGNED_INT, 0);  //if I give here "Indices" it works but not drawing everything

		glDisableClientState(GL_COLOR_ARRAY);
		glDisableClientState(GL_VERTEX_ARRAY);


glBindBufferARB( GL_ARRAY_BUFFER_ARB,0);
glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB,0);

EDIT - updated to actual version

You can read about glIndexPointer(GL_UNSIGNED_INT, 0, 0)
glEnableClientState(GL_INDEX_ARRAY)
http://www.opengl.org/wiki/index.php/Common_Mistakes#glEnableClientState.28GL_INDEX_ARRAY.29

and about using VBO + sample code
http://www.opengl.org/wiki/index.php/VBO

http://www.opengl.org/wiki/index.php/Common_Mistakes#glEnableClientState.28GL_INDEX_ARRAY.29

EDIT:
Too slow…

… actual version in 1st post

Rethink NumVertices*sizeof(float).

CatDog

I’ve changed it to struct name (bug from ctrl t -> ctrl v) :wink:

But there is still sth wrong :stuck_out_tongue:

With these:
glDrawElements( GL_TRIANGLES,NumIndices,GL_UNSIGNED_INT, Indices);
it doeasnt draws everything (not this data set i’ve presented).

From what i read - instead of indices it should be 0 ->
glDrawElements( GL_TRIANGLES,NumIndices,GL_UNSIGNED_INT, 0);
but it crashes.

Anyone know what is wrong?

The above code would work if you did not active vertex array client-side capabilities after calling glColorPointer and glVertexPointer. So call

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);

before setting vertex and color data. :slight_smile:

OK. So now i have this code:

		glEnableClientState(GL_VERTEX_ARRAY);
		glEnableClientState(GL_COLOR_ARRAY);

			glBindBufferARB( GL_ARRAY_BUFFER_ARB, VertV );
			glVertexPointer(3, GL_FLOAT, 0, 0);
			glBindBufferARB( GL_ARRAY_BUFFER_ARB, Colr );
			glColorPointer(3, GL_FLOAT, 0, 0);

			//glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 );	//this works only if last parameter of drawElements is Indices - not 0

			glDrawElements(	GL_TRIANGLES,NumIndices,GL_UNSIGNED_INT, 0);

			//glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 );

		glDisableClientState(GL_COLOR_ARRAY);
		glDisableClientState(GL_VERTEX_ARRAY);


		glBindBufferARB( GL_ARRAY_BUFFER_ARB,0);

and before thet i have function to build vbo:
glGenBuffersARB( 1, &Colr );
glBindBufferARB( GL_ARRAY_BUFFER_ARB, Colr );
glBufferDataARB( GL_ARRAY_BUFFER_ARB, NumVertices*sizeof(SColor), (void *) Colors, GL_STATIC_DRAW_ARB );

glGenBuffersARB( 1, &VertV );
glBindBufferARB( GL_ARRAY_BUFFER_ARB, VertV );
glBufferDataARB( GL_ARRAY_BUFFER_ARB, NumVertices*sizeof(SVertex), (void *) Vertices, GL_STATIC_DRAW_ARB );

glGenBuffersARB( 1, &Indict );
glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, Indict );
glBufferDataARB( GL_ELEMENT_ARRAY_BUFFER_ARB, NumIndices*sizeof(GLuint), (void *) Indices, GL_STATIC_DRAW_ARB );

and still sth is wrong :stuck_out_tongue: It displays only fragment of my scene. There isnt problem with numbering vertices, indices etc.

Maybe there still sth wrong with vbo ?

Your vbo setup looks right. There is something I don’t understand:


//glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 ); //this works only if last parameter of drawElements is Indices - not 0

glDrawElements( GL_TRIANGLES,NumIndices,GL_UNSIGNED_INT, 0);

Why did you comment the glBindBufferARB line? If you want to use vbo your have to do:

glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, indict);
glDrawElements( GL_TRIANGLES,NumIndices,GL_UNSIGNED_INT, NULL);

When you set NULL as the last glDrawElements parameter, opengl will use the data of the bound array to the GL_ELEMENT_ARRAY_BUFFER_ARB target and this data is the one managed by the indict vbo in the above piece of code.

If you just want to use vertex arrays and not vbo, you do this:

glDrawElements( GL_TRIANGLES,NumIndices,GL_UNSIGNED_INT, Indices);

But with vertex arrays you do not benefit from the advanced memory management provided by vbos.

Thanks mate.

You’ve been very helpful! It’s working now. It was that what You wrote, and one little bug from my side :slight_smile:

Cheers :slight_smile:

You are welcome, good luck for the future. :slight_smile: