0

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?

Jorgusss
  • 7
  • 1
  • 3

1 Answers1

0

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.

IceFire
  • 4,016
  • 2
  • 31
  • 51
  • Thanks, but how to create those few more vertex buffers? – Jorgusss Feb 20 '16 at 14:24
  • You already have one vertex buffer. You can just use vbo_create with, e.g. 40 vertices instead of four and, like said, maybe render with GL_TRIANGLES – IceFire Feb 20 '16 at 14:33
  • You mean to do somethin like that? glGenBuffers(40, VertexArrayID); When I call glBindBuffer(GL_TRIANGLES, VertexArrayID[0]); and run the program, after moment it shows that is stopped working. – Jorgusss Feb 20 '16 at 15:24
  • @Jorgusss: No, that "40" is the *number of buffers* not the *size* of those buffers. Please go read a tutorial or something. – Nicol Bolas Feb 20 '16 at 15:27
  • but, it is not the point I am looking for.. I just want to make like that: 1. create buffer 2. draw polygon, 3.save polygon, 4.draw other polygon, 5. save that polygon etc... I need to create each buffer for other polygoin thats all, and I have no idea how to save every polygon on each buffer – Jorgusss Feb 20 '16 at 15:38