PDA

View Full Version : Indices in VBO



aistee
01-10-2009, 05:41 PM
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

V-man
01-10-2009, 05:48 PM
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_ARR AY.29

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

dletozeun
01-10-2009, 05:51 PM
http://www.opengl.org/wiki/index.php/Common_Mistakes#glEnableClientState.28GL_INDEX_ARR AY.29

EDIT:
Too slow...

aistee
01-10-2009, 05:57 PM
... actual version in 1st post

CatDog
01-10-2009, 06:18 PM
Rethink NumVertices*sizeof(float).

CatDog

aistee
01-10-2009, 06:34 PM
I've changed it to struct name (bug from ctrl t -> ctrl v) ;)

But there is still sth wrong :p

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?

dletozeun
01-11-2009, 04:57 AM
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. :)

aistee
01-11-2009, 09:13 AM
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 :p It displays only fragment of my scene. There isnt problem with numbering vertices, indices etc.

Maybe there still sth wrong with vbo ?

dletozeun
01-11-2009, 01:57 PM
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.

aistee
01-11-2009, 03:21 PM
Thanks mate.

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

Cheers :)

dletozeun
01-11-2009, 04:04 PM
You are welcome, good luck for the future. :)