user1687498
user1687498

Reputation: 71

opengl es VBO usage - no geometry rendering

I'm trying to set up VBO for my rendering code to get more fps. It worked for separate VBO's for vertex position, color and texture coords but after transferring to interleaved vertex data there is no geometry rendering. Here is my setup func:

const GLsizeiptr data_size = NUMBER_OF_CUBE_VERTICES * 9 *sizeof(float);
// allocate a new buffer
glGenBuffers(1, &cubeVBO);
glBindBuffer(GL_ARRAY_BUFFER, cubeVBO);
glBufferData(GL_ARRAY_BUFFER, data_size, data, GL_STATIC_DRAW);

float* ptr = (float*)data;
glVertexAttribPointer(ATTRIB_VERTEX, 3, GL_FLOAT, GL_FALSE, sizeof(struct Vertex), (ptr + 0));
glEnableVertexAttribArray(ATTRIB_VERTEX);

glVertexAttribPointer(ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, sizeof(struct Vertex), (ptr + 3));
glEnableVertexAttribArray(ATTRIB_COLOR);

glVertexAttribPointer(ATTRIB_TEXCOORD0, 2, GL_FLOAT, GL_FALSE, sizeof(struct Vertex), (ptr + 7));
glEnableVertexAttribArray(ATTRIB_TEXCOORD0);

glGenBuffers(1, &cubeIBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cubeIBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, NUMBER_OF_CUBE_INDICES*sizeof(GLubyte), s_cubeIndices, GL_STATIC_DRAW);

The data array look like this:

static float data[] =
{
//  position  //   // color // //UV//
-1.0, +1.0, +1.0,  255,0,0,255, 0,0,
-1.0, -1.0, +1.0,  0,255,0,255, 0,0,
+1.0, +1.0, +1.0,  255,0,255,255, 0,0,
+1.0, -1.0, +1.0,  255,0,0,255, 0,0,

+1.0, +1.0, +1.0,  255,0,0,255, 0,0,
+1.0, -1.0, +1.0,  255,0,0,255, 0,0,
+1.0, +1.0, -1.0,  255,255,0,255, 0,0,
+1.0, -1.0, -1.0,  255,0,0,255, 0,0,

+1.0, +1.0, -1.0,  255,0,255,255, 0,0,
+1.0, -1.0, -1.0,  255,255,0,255, 0,0,
-1.0, +1.0, -1.0,  0,255,0,255, 0,0,
-1.0, -1.0, -1.0,  255,0,0,255, 0,0,

-1.0, +1.0, -1.0,  0,0,255,255, 0,0,
-1.0, -1.0, -1.0,  255,0,0,255, 0,0,
-1.0, +1.0, +1.0,  255,255,0,255, 0,0,
-1.0, -1.0, +1.0,  255,0,0,255, 0,0,
};

And this is my render code:

glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glBindBuffer(GL_ARRAY_BUFFER, cubeVBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cubeIBO);
glDrawElements(GL_TRIANGLE_STRIP, NUMBER_OF_CUBE_INDICES, GL_UNSIGNED_BYTE, s_cubeIndices);

Also I tried to use DrawArrays function without index buffer but result was the same - no geometry rendered. There is also GLError 1282 in output window while my program runs. I'd appreciate any help on my problem, thanks.

Upvotes: 4

Views: 684

Answers (2)

user1687498
user1687498

Reputation: 71

Ok, I got it working using glVertexPointer and glEnableClientState functions. But for glVertexAttribPointer and glEnableVertexAttribArray there is still no geometry rendering for some reason. Now the code looks like this: VBO init:

struct Vertex
{
  GLfloat x, y, z;
  GLubyte r, g, b, a;
};
......

const GLsizeiptr data_size = NUMBER_OF_CUBE_VERTICES *sizeof(struct Vertex);
glGenBuffers(1, &cubeVBO);
glBindBuffer(GL_ARRAY_BUFFER, cubeVBO);

glBufferData(GL_ARRAY_BUFFER, data_size, vertices, GL_STATIC_DRAW);

glVertexPointer(3, GL_FLOAT, sizeof(struct Vertex), (GLvoid*)((char*)NULL));
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(struct Vertex), (GLvoid*)offsetof(struct Vertex, r));
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);

rendering:

glBindBuffer(GL_ARRAY_BUFFER, cubeVBO);
glDrawArrays(GL_TRIANGLE_STRIP, 0, NUMBER_OF_CUBE_VERTICES);

I have no idea why this code not work if I switch to the glVertexAttribPointer / glEnableVertexAttribArray. Any Ideas? Maybe I need to move pointing and enabling functions from initialization part to render part?

Upvotes: 0

Srđan Rašić
Srđan Rašić

Reputation: 1787

When you are using buffer object, last parameter of glVertexAttribPointer should be offset to data you need. For example, yours ATTRIB_VERTEX array would start at offset 0, ATTRIB_COLOR array at offset sizeof(float) * 3 (because position takes three floats), etc...

When you are not using buffer objects, but rather vertex array, you have to unbind currently bound buffer object to GL_ARRAY_BUFFER target by calling

glBindBuffer(GL_ARRAY_BUFFER, 0);

Upvotes: 4

Related Questions