AkademiksQc
AkademiksQc

Reputation: 698

OpenGL ES 1.1: Fail to convert to VBO

i try to convert my vertex data to VBO and i always get a black screen...i can't figure out whats wrong with my implementation. In my rendering, if i remove the bindBuffer call and i pass my vertices pointer to glVertexPointer instead of 0 it works, so nbVertices and vertices are properly populated.

Vec3 definition:

struct vec3
{
    float x;
    float y;
    float z;
}

Creation :

glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vec3) * this->nbVertices, vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER,0);

Rendering :

glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

// draw cube
glScalef(scaleX, scaleY, scaleZ);

glRotatef(rotX, 1, 0, 0);
glRotatef(rotY, 0, 1, 0);
glRotatef(rotZ, 0, 0, 1);

for (int i=0; i<nbTextures; i++)
{
    glBindTexture(GL_TEXTURE_2D, textureIds[i]);

    glBindBuffer(GL_ARRAY_BUFFER,vertexBuffer);
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, 0/*this->vertices*/);

    glEnableClientState(GL_NORMAL_ARRAY);
    glNormalPointer(GL_FLOAT, 0, this->normals);

    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glTexCoordPointer(2, GL_FLOAT, 0, this->texCoords);

    glDrawArrays(GL_TRIANGLE_STRIP, 0, this->nbVertices);
}

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER,0);

Upvotes: 1

Views: 372

Answers (1)

Christian Rau
Christian Rau

Reputation: 45968

The problem is that you are mixing VBOs with client side arrays. Whereas this is no problem, you cannot specify a pointer to a client side array (as you do for every other array except the vertex array) when a VBO is bound. The pointer will just be treated as an offset into the buffer.

So either unbind the buffer after the call to glVertexPointer:

glBindBuffer(GL_ARRAY_BUFFER,vertexBuffer);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, 0/*this->vertices*/);
glBindBuffer(GL_ARRAY_BUFFER,0);

glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, 0, this->normals);

glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 0, this->texCoords);

Or, even better don't mix VBOs and client side arrays and use VBOs for every attribute. You can use the same VBO for each attribute and either put the individual arrays one after the other, in which case you have to play with the offsets a bit, or put the attribute data for each vertex one after the other (often the best approach), in which way you have to play with the stride parameters, too. But you can also use a different VBO for each attribute, but in this case remember to bind the correct VBO before each call to gl...Pointer, like above.

Upvotes: 2

Related Questions