'Render a Square Using VBO

Hi i'am trying to render a square using VBO. Here is my code

void square() {
    if (!squareUpdate) {
        GLfloat squarevertices[] = {-0.75f,  .75f,   0.0f,   0.75f,
                                     0.75f,  0.0f,   0.75f, -0.75f,
                                     0.0f,  -0.75f, -0.75f,  0.0 };

        glGenBuffers(1, &VBOid);
        glBindBuffer(GL_ARRAY_BUFFER, VBOid);
        glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 9, squarevertices,
                     GL_DYNAMIC_DRAW);

        glEnableClientState(GL_VERTEX_ARRAY);

        glVertexPointer(3, GL_FLOAT, 0, 0);
        glDrawArrays(GL_POLYGON, 0, 9);

    } else {
        glVertexPointer(3, GL_FLOAT, 0, 0);
        glDrawArrays(GL_POLYGON, 0, 9);
    }
}

but it only renders half of a square it looks like a triangle

enter image description here

how can i make a box using a vbo?Im sure im missing something



Solution 1:[1]

Your polygon consists of 4 vertices with 3 components (3*4 = 12). The buffer size is sizeof(GLfloat) * 12 bytes:

glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 9, squarevertices, GL_DYNAMIC_DRAW);

glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 12, squarevertices, GL_DYNAMIC_DRAW);

However, in the draw call you need to specify the number of vertices:

glDrawArrays(GL_POLYGON, 0, 9);

glDrawArrays(GL_POLYGON, 0, 4);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1