问题
I am trying to draw a square with VBO in my native blackberry 10 application. My implementation is,
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glGenBuffers(1,&decompressTileImage->VBOId);
glBindBuffer(GL_ARRAY_BUFFER,decompressTileImage->VBOId);
glBufferData(GL_ARRAY_BUFFER,sizeof(tileCoordList)+sizeof(tileTextureCoordList),0,GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER,0,sizeof(tileCoordList),tileCoordList);
glBufferSubData(GL_ARRAY_BUFFER,sizeof(tileCoordList),sizeof(tileTextureCoordList),tileTextureCoordList);
glTexCoordPointer(3, GL_FLOAT, sizeof(tileTextureCoordList), (void*)sizeof(this->tileCoordList));
glVertexPointer(3, GL_FLOAT, sizeof(tileCoordList), 0);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDisable(GL_BLEND);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_TEXTURE_2D);
swapBuffers();
Here tileCoordList contains the co-ordinates for the square and tileTextureCoordList contains the corresponding texture. tileCoordList
is a Square3D
type structure.
typedef struct
{
Vertex3D lowerLeft;
Vertex3D lowerRight;
Vertex3D upperLeft;
Vertex3D upperRight;
}Square3D;
typedef struct
{
GLfloat x;
GLfloat y;
GLfloat z;
} Vertex3D;
4 Vertex3D
represents a square in tileCoordList. Same goes for tileTextureCoordList
. I can draw fine without the VBO though. I am using opengl-es 1.1
Another question.
If I create two VBOs. Suppose I bound the first one with its id and copied the data and drew it. And then comes the second one and I also bind it and copy the data and draw it. Now if I want to draw the first one again do I need to bind it again? If I need to bind again do I need to copy the data to buffer for that VBO again also?
回答1:
It has transpired in the comments that tileCoordList
and tileTextureCoordList
are pointers to Square3D
structures.
This means that the sizeof
operator will give you the size of a pointer, not the size of the struct.
You could use e.g.
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(*tileCoordList), tileCoordList);
instead.
回答2:
You are doing something weird to your buffer. Without knowing what tileCoordList
is it's hard to say if you are adding the data correctly.
In general, regarding both of your concerns here: VBOs store the data for you. It is not only possible, but encouraged to do:
vbo_1, vbo_2;
vbo1.LoadData(data_1);
vbo2.LoadData(data_2);
main_loop {
vbo1.Bind();
Draw();
vbo2.Bind();
Draw();
}
I have used pseudocode here, but you should get the idea. I don't understand what the SubData
call is supposed to do; in general, one simple glBufferData
should be enough. Bind
call sets the buffer as an active one.
This line made me cringe - why is VBOId
public? Smells like a poorly designed abstraction to me:
glGenBuffers(1,&decompressTileImage->VBOId);
And oh, please don't use this->
. The only thing it does here is worsen readability.
来源:https://stackoverflow.com/questions/15310777/vbo-doesnt-draw-anything