Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass multiple uniforms efficiently and dynamically to GLSL

Tags:

c++

opengl

glsl

I want to pass an array of struct I have in my program, I know there are a few way's to do this but I want to do it efficiently, here is what I know I can do:

  • I can use simply create a struct with the things I want to pass to the shaders and create an array of how many can be passed:

    struct Light
    {
        vec3 Position;
        vec3 diffuse;
        float Intensity;
    };
    
    #define NUM_OF_LIGHTS 5
    uniform Light lights[NUM_OF_LIGHTS];
    

    Pros: Really easy to do.

    Cons: It is not dynamic, I need to choose a maximum amount of lights, which I want to avoid doing.

  • I can use Uniform Blocks Objects (UBO) which I read on opengl.org that I can use array for them from OpenGL 4.0 (Which is my target by the way, (considering 4.1)).

    Pros: Cleaner(?) to do. might be faster(?)1

    Cons: Still, not dynamic.

  • Shader Storage Blocks Objects (SSBO) is another option but I can't use them because they are core on version 4.3. bad for me because, as far as I read, they are dynamic and that is what I want.

I couldn't find anything else about how to pass things as I want to do.

If I can't do it, how can game engines create multiple lighting without knowing how much lights the user will enter (considering they using OpenGL and they target 3.X up to 4.0, which seem reasonable to me)?

Thanks in advance.

Footnotes:

  1. I might be wrong here, so if I do let me know and I will delete those.
like image 427
Zik332 Avatar asked Mar 30 '17 10:03

Zik332


1 Answers

How about your first solution with a little modification:

struct Light
{
    vec3 Position;
    vec3 diffuse;
    float Intensity;
};

#define MAX_NUM_OF_LIGHTS 50            //define max number of lights
uniform Light lights[MAX_NUM_OF_LIGHTS];
uniform int number_of_lights            //the actual number of lights, for loops 

Still not exactly what you want but it works well.

like image 112
eldo Avatar answered Nov 15 '22 00:11

eldo