0

I tried rendering a cube using a vertex array on a VBO, with position and color, and an index to especify, but when I try to draw it shows nothing on the screen.

This is my data:

    Vertex VertexData[8];

    GLfloat CubeVertexData[] = 
    {   
     // front
    -1.0, -1.0,  1.0,
     1.0, -1.0,  1.0,
     1.0,  1.0,  1.0,
    -1.0,  1.0,  1.0,
    // back
    -1.0, -1.0, -1.0,
     1.0, -1.0, -1.0,
     1.0,  1.0, -1.0,
    -1.0,  1.0, -1.0 };

    GLubyte CubeColors[] = {
       // Front
       0, 255, 0, 255,
       255, 255, 0, 255,
       0, 255, 0, 255,
       0, 0, 255, 255,

       // Back
       255, 0, 0, 255,
       255, 255, 0, 255,
       0, 255, 0, 255,
       0, 255, 0, 255
     };

     GLushort CubeElementData[] = {
        // front
        0, 1, 2,
        2, 3, 0,
        // top
        3, 2, 6,
        6, 7, 3,
        // back
        7, 6, 5,
        5, 4, 7,
        // bottom
        4, 5, 1,
        1, 0, 4,
        // left
        4, 0, 3,
        3, 7, 4,
        // right
        1, 5, 6,
        6, 2, 1
    };

So I'm using my own Vertex struct to store the positions and colors together.

    glGenBuffers(1, &myVboID);
    glBindBuffer(GL_ARRAY_BUFFER, myVboID);
    glBufferData(GL_ARRAY_BUFFER, sizeof(VertexData), &VertexData[0], GL_STATIC_DRAW);

    glGenBuffers(1, &vertElemID);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertElemID);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(CubeElementData), &CubeElementData[0], GL_STATIC_DRAW);

`

And when I try to draw, it just shows nothing on screem. I'm really confused, because I have another similar program that does exactly the same, but uses glut, and it works fine.

//Bind vbo and indices
glBindBuffer(GL_ARRAY_BUFFER, myVboID);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(Vertex, position));

glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_UNSIGNED_INT, GL_TRUE, sizeof(Vertex), (void*) offsetof(Vertex, color));

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertElemID);
int size;  
glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);

glDrawElements(GL_TRIANGLES, size / sizeof(GLushort), GL_UNSIGNED_SHORT, 0);

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

This is how I created the VBO for each array when it worked

// Vertex data
glGenBuffers(1, &vertBuffID);
glBindBuffer(GL_ARRAY_BUFFER, vertBuffID);
glBufferData(GL_ARRAY_BUFFER, sizeof(CubeVertexData), CubeVertexData, GL_STATIC_DRAW);

// Color data
glGenBuffers(1, &vertColorID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertColorID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(CubeColors), CubeColors, GL_STATIC_DRAW);

// Index array of vertex / color data 
glGenBuffers(1, &vertElemID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertElemID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(CubeElementData), &CubeElementData[0], GL_STATIC_DRAW);

And this is how I draw, this works perfectly fine, but I just can't get it to work with the vertex array for some reason.

glBindBuffer(GL_ARRAY_BUFFER, vertBuffID);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(0);

glBindBuffer(GL_ARRAY_BUFFER, vertColorID);
glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0);
glEnableVertexAttribArray(1);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertElemID);
int size;  
glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);

glDrawElements(GL_TRIANGLES, size / sizeof(GLushort), GL_UNSIGNED_SHORT, 0);

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  • Not sure if this is your main problem, but the way you specify the vertex attribute for the colors looks fishy. If you have color values up to 255, and normalize them as an attribute of type `GL_UNSIGNED_INT`, the maximum value will be 255 / (2^32 - 1). Which is a very small number. You may want to set your background color to something other than black if it's currently black. Then you can tell if the cube is drawn in black, or not drawn at all. – Reto Koradi Mar 19 '15 at 04:09
  • Hi! Thanks for the answer! Sadly that's not it, when I render it using two separate VBOs (one for position and one for color) it works perfectly, so the issue can't be with the colors. The problem is only when I try to use an array of Vertices in one VBO and access the data with offsets. – Claudio Enrique Bustamante Gal Mar 19 '15 at 04:34
  • 1
    But the working code you posted uses `GL_UNSIGNED_BYTE` for the color attribute, while the new code uses `GL_UNSIGNED_INT`. That's not the same thing at all. – Reto Koradi Mar 19 '15 at 04:55
  • Yes, you're rigth, it's because I was trying to change some things, but actually, both data types are 4 bytes, so it makes no difference. I tried anyways, but it didn't solve the problem. Thanks! – Claudio Enrique Bustamante Gal Mar 19 '15 at 06:20

0 Answers0