How to add objects to buffer? opengl

763 views Asked by At

In this program I want to draw polygons.

Firstly I made sth like that:

GLuint VertexArrayID;

example of drawing polygon:

if (figure == RECTANGLE)
{
    data[0][0] = px1;   data[0][1] = py1;
    data[1][0] = px2;   data[1][1] = py1;
    data[2][0] = px2;   data[2][1] = py2;
    data[3][0] = px1;   data[3][1] = py2;
    vertex_count = 4;
}
vbo_create(vertex_count);

And here is vbo crreating function

void vbo_create(int vertex_count)
{
    if (vertex_count > 0)
    {
        glGenBuffers(3, VertexArrayID);
        glBindBuffer(GL_ARRAY_BUFFER, VertexArrayID[0]);
        glBufferData(GL_ARRAY_BUFFER, 2 * sizeof(float), data, GL_STATIC_DRAW);
    //GLfloat* data = (GLfloat*)glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);

        glVertexPointer(2, GL_FLOAT, 0, NULL);
        glEnableClientState(GL_VERTEX_ARRAY);
        glDrawArrays(GL_TRIANGLE_FAN, 0, vertex_count);
        glDisableClientState(GL_VERTEX_ARRAY);
    }
}

The point is that I want to save all created objects in one buffer, but I have no idea how to do it. I tried to make an array of VertexArrayID, but it doesn't worked correctly. Any suggestions?

1

There are 1 answers

5
IceFire On BEST ANSWER

Let's say, you have 10 triangles. Then, you would just put 30 vertices in this one buffer and render all of it with GL_TRIANGLES. You could also add four vertices and use GL_QUADS. It is a good idea to have rather few vertex buffers if that works for you.