Hello, I'm new to this forum as a poster. I tried making the title as descriptive as possible so here goes:

I'm new to UBOs and I've seen many different implementations and I don't think none of them serves my purpose and/or I can't understand UBOs completely. In my application, I have a number of lights that isn't fixed. I'd like to feed them to the GPU so I can use them in the shader code. Bonus: I'd prefer to have lights stored in the GPU much like the VBOs, where I just need to bind the buffer's ID (GLuint) to use it. The trick is that even if this is possible, you'd have to bind several UBOs, one for each light and bind a certain index to them so that it maps to the array on the shader code. Is there any way to do this?

Right now, I'm looking at having my fragment shader like this:

Code :
struct Light {
   vec3 position;
   float padding;
}
layout (std140) uniform Lights {
   Light light[];
}

and then iterating through the lights. This dynamic allocation of arrays is possible in 4.3 as far as I know. However, I have problems in the C++ code:

Initialization:
Code :
glGenBuffers(1,&m_lightsUBO); // this generates UBO, OK
glBindBuffer(GL_UNIFORM_BUFFER, m_lightsUBO); // this binds it, OK
glBufferData(GL_UNIFORM_BUFFER, /*size*/, /*data*/, GL_DYNAMIC_DRAW); // this allocates space for the UBO. 
glBindBufferRange(GL_UNIFORM_BUFFER, /*buffer index*/, m_lightsUBO, 0, /*size*/); // this binds UBO to Buffer Index

Though I don't need to specify the data at this point, I do need to specify a size... but I don't know how many lights I will need. This is a run-time value but if I move this code to a per-frame function I'll be creating a new UBO every frame and thus defeating the purpose. Also, I'm not sure what this Buffer Index should be.
After the initialization, I can pretty much do this:

Per Frame:
Code :
glBindBuffer(GL_UNIFORM_BUFFER,m_lightsUBO);
glBufferSubData(GL_UNIFORM_BUFFER,0,data.size(),data.data());
glBindBuffer(GL_UNIFORM_BUFFER,0);

Which means I'm actually sending data to the GPU every frame, instead of just referencing a Buffer Object.

I'd like to know if what I'm looking for is possible and if so, how to do it. If not, then I'd like to know the closest possible alternatives to what I want.

Thank you in advance!