Is there any way to load multiple light sources to a shader without defining the max number of lights?

Hi!

I want to be able to add lights dynamically. Or at least pass a variable to the shader (from java) that tells it how many sources there are.

Is there a good way to do this?

There’s no particular reason not to just make the array large enough to hold the maximum number of lights and just pass a count of the number of valid entries as a uniform variable. The performance cost of processing a very large number of lights is likely to be an issue long before the memory consumption becomes significant.

If you don’t want to specify an upper bound on the array size, you can use a shader storage block (requires OpenGL 4.3 or the ARB_shader_storage_buffer_object extension) or a texture.

Uniform arrays (either in the default uniform block or a UBO) must be sized, either explicitly in the declaration or implicitly by indexing only with constant integral expressions. However, you could just recompile the shader if the size changes.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.