When using OpenGL, generating VBOs is just calling the function glGenBuffer
. You don't need to know the size of the buffer until you're ready to add the data to the buffer. So what you can do is generate your buffers, then calculate the vertices you'll need (and all that other stuff). Once you've calculated the number of vertices, you can call glBufferData
with the correct size.
Unfortunately I've only used OpenGL in C++ (using glm usually), but I believe the same concepts apply to Java as well. Here's an example of what you can do, assuming you store your vertex info in something similar to a std::vector:
//Generate your buffers
GLuint vboID;
GLuint uvboID;
glGenBuffers(1, &vboID);
glGenBuffers(1, &uvboID);
// Calculate your vertex info here
// We'll assume you use the following for storing your data
std::vector<glm::vec3> vertices;
std::vector<glm::vec2> uvs;
// Once you've calculated your vertex info, load it into your buffers
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * vertices.size(), &vertices[0], GL_DYNAMIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glBindBuffer(GL_ARRAY_BUFFER, uvboID);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec2) * uvs.size(), &uvs[0], GL_DYNAMIC_DRAW);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
// Draw your geometry here
glDrawArrays(GL_TRIANGLES, 0, vertices.size());
Of course, this is a somewhat simplified example, as I didn't include any code for setting your current active program or using a Vertex Array Object, but it illustrates the solution to the problem you're having. Note that you don't have to know how big your vertex data is until your call to glBufferData. Keep in mind there's a lot I didn't cover here (such as making sure you don't call glBufferData over and over again if your data didn't change).
Also, I'm a completely self taught programmer, so I'd appreciate it if anyone points out anything that could be done in a more efficient manner. I learned OpenGL from this site:
http://www.opengl-tutorial.org/
You may also want to check out this site as well, as it also covers OpenGL fairly well:
http://ogldev.atspace.co.uk/index.html