PDA

View Full Version : VBO don't render



Riskz
04-08-2009, 02:06 PM
I'm having some trouble to get the VBO working. Here is the code which generates de VBO and the code which draws it:



glGenBuffers(1 , &obj->VBO[0]);
glBindBuffer(GL_ARRAY_BUFFER, obj->VBO[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*obj->v_total, vertex_array, GL_STATIC_DRAW);
glGenBuffers(1 , &obj->VBO[1]);
glBindBuffer(GL_ARRAY_BUFFER, obj->VBO[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*obj->v_total, normal_array, GL_STATIC_DRAW);
glGenBuffers(1 , &obj->VBO[3]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, obj->VBO[3]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int)*3*obj->p_total, vertex_index, GL_STATIC_DRAW);



glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_INDEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, ball.VBO[0]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ball.VBO[3]);
glVertexPointer(3, GL_FLOAT, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, ball.VBO[1]);
glNormalPointer(GL_FLOAT, 0,0);

//glDrawArrays(GL_TRIANGLES, 0, ball.v_total);
glDrawElements( GL_TRIANGLES, ball.p_total, GL_INT,0 );

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_INDEX_ARRAY);

I'd also like to know wether I should call glDrawArray() or glDrawElements.

Thanks in advance

Rosario Leonardi
04-09-2009, 04:36 AM
Look almost fine to me, try to check if you get some error with glError.
The glDrawElements shouldn't work with GL_INT, try with GL_UNSIGNED_INT


I'd also like to know wether I should call glDrawArray() or glDrawElements.
With the glDrawArray you draw your array/buffer sequentially, with the glDrawElements you can specify the polygon indexes.

Using glDrawArray is like to use the glDrawElements with the indexes array 0 1 2 3 4 5... ect..

Riskz
04-09-2009, 09:27 AM
thanks man, that worked fine. The problem, as you said, was the "GL_INT", I changed it to "GL_UNSIGNED_INT" and worked fine.

Now I have some trouble with texturing. The texture isn't rendered.

Here is the code:


...
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_INDEX_ARRAY);
if(obj->mat[0].tex_id>-1) {
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, ball.VBO[2]);
glClientActiveTexture(GL_TEXTURE0);
glTexCoordPointer(2, GL_FLOAT, 0, 0);
}
glBindBuffer(GL_ARRAY_BUFFER, obj->VBO[0]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, obj->VBO[3]);
glVertexPointer(3, GL_FLOAT, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, obj->VBO[1]);
glNormalPointer(GL_FLOAT, 0,0);

glDrawElements( GL_TRIANGLES, obj->p_total*3, GL_UNSIGNED_INT,0 );

glDisableClientState(GL_VERTEX_ARRAY);
if(obj->mat[0].tex_id>-1) glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_INDEX_ARRAY);
...


Thanks again

ephtracy
04-09-2009, 09:40 AM
did you set the texture parameters ( especially filter ) correctly and have the GL_TEXTURE_2D enabled?

Riskz
04-09-2009, 09:43 AM
I'd say yes. Becouse although I'm implementing VBO, I have a copy of my objects stored in the RAM, so that I can disable/enable VBO with a key. And when VBO is off the texture is drawn ok, but when I enable VBO the texture is not drawn. I will look deeper in my code and post all the code related to textures.

EDIT: Does "glClientActiveTexture(GL_TEXTURE0)" replace "glBindTexture(GL_TEXTURE_2D, 0" ?

ephtracy
04-09-2009, 09:55 AM
EDIT: Does "glClientActiveTexture(GL_TEXTURE0)" replace "glBindTexture(GL_TEXTURE_2D, 0" ?


no.

'glClientActiveTexture' is just setting the currebt active texture unit( here is GL_TEXTURE0 ) you have your textures binded to.

'glBindTexture' is binding your texture object to certain target ( here is GL_TEXTURE_2D ) and certain unit ( you set with 'glActiveTexture', while 'glClientActiveTexture' is used for client );

glActiveTexture( GL_TEXTURE0 );
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, handle );

//set texture parameters
...

// draw primitives
...

glBindTexture( GL_TEXTURE_2D, 0 );
glDisable( GL_TEXTURE_2D );

Riskz
04-09-2009, 09:59 AM
So what would be the code to activate and bind a texture to use it with VBO?

something like


glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glClientActiveTexture(GL_TEXTURE0);
glBindBuffer(GL_ARRAY_BUFFER, ball.VBO[2]);
glBindTexture(GL_TEXTURE_2D, obj->mat[0].tex_id);
glTexCoordPointer(2, GL_FLOAT, 0, 0);

?

Thanks

ephtracy
04-09-2009, 10:17 AM
'glBindTexture' has no effect on client array
i did in this way :



// setup texture
glActiveTexture( GL_TEXTURE0 );
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, obj->mat[0].tex_id );

// draw triangles
...

glEnableClientState( GL_TEXTURE_COORD_ARRAY );
glClientActiveTexture( GL_TEXTURE0 );
glBindBuffer( GL_ARRAY_BUFFER, ball.VBO[2] );
glTexCoordPointer( 2, GL_FLOAT, 0, 0 );

...

Riskz
04-09-2009, 10:28 AM
I've just found the error, it was a very stupid error:

glBindBuffer(GL_ARRAY_BUFFER, ball.VBO[2]);
I was always binding the VBO with the ball texture coordinates (this is because at first I was only rendering the ball using VBO, and forgot to change this when I wrote the general VBO drawing function)
The right code was this:

glBindBuffer(GL_ARRAY_BUFFER, obj->VBO[2]);
I'm only posting it to tell you how stupid I am.

Thank you both
=)