Is it possible to pass a list of arrays to a shader Frag

I have a non-defined list of x,y i would like to pass to my frag shader for it to draw some stuff based on those.

How can i do it? So far looks like i’m able to pass vec1-4, but my list could range from 0-Xamount.

Thanks

Hi,

Not 100% sure I’m following, but you can pass ofShader an array of vec2 using setUniform2fv

In your shader you would have something like

#define MAX_ITEMS 5
uniform vec2 myData[MAX_ITEMS ]

And in your c++

ofVec2f someStuff[5];
...
myShader.setUniform2fv("myData", &someStuff[0].x, 5 );

Or alternatively myShader.setUniform2fv(“myData[0]”, &someStuff[0].x, 5 ); if that doesn’t work. (I’m on a mongrel OF version )

There will be a limit to the size of what you can send over this way though. If you need to send large amounts of data you want OpenGL 4.5 and ofBufferObject, with this combo you can send up large buffers to the GPU that you can read and write to from shaders and it’s fucking amazing.

What i meant was, i have an array of points i need to use for calculation. The number of this points is unknown from the start and might increase or decrease over time.

I’m not sure what’s the best way to do it.
I can’t set #define MAX_ITEMS 5 cause I won’t know the length of that array. It will depend on what is being tracked at runtime.

Then if the maximum amount is something that will fit in a uniform array, not sure what the limits are, you could on the shader side allocate enough space for the maximum and have another uniform that tells you how many of those data points are valid.

That sounds like a method. How can i tell how what is the maximum?

I’m not sure if it’s working.
In my frag

#define COUNT 50
uniform int u_pointsCount;
uniform vec2 u_points[COUNT];
float u_circles[COUNT];

void main() {
   //some code before
   for(int i=0;i< u_pointsCount; ++i)
    { 
      u_circles[i] = drawPoly( st, u_points[i], size, 16.);
      
    }
}

In my of App code do i set the values to the array like this?

for (int i = 0; i< 10; i++) {
   shader.setUniform2f("u_points[i]",  ofRandom(1), ofRandom(1) );
}

When i run my app, it draws, but it does not seem to be getting any values from the u_points array… from what i can tell, it looks like it got back 0,0…

Hi!

Like @hahakid said, if you want to pass a lot of values, you should work with ofBufferObject. You can check an example in examples/gl/textureBufferInstancedExample.

If you want to set an array as an uniform, you should keep it low in terms of length. I usually don’t go higher than 16.

I do it like this, ofApp.h:

static const int arrayLength = 16;
float yourArray[arrayLength];

You assign whatever values you want, and then:

shader.setUniform1fv("uniArray", yourArray, arrayLength);

And on your fragment shader, you declare it like so:

uniform float uniArray[16];

Thanks for your reply.
I’m looking at the textureBufferInstancedExample but i don’t really
understand how to use it.

For my project i would need more than 16.

Let me try to figure out what i should be doing, I need a
vector<ofMatrix4x4> matrices; which i use to store my list of
points? Depending on how many points i have, i do a
matrices.resize(noOfPoints); on update.

In my update method, i then set the points xy, info i want to pass to
shader, on the martices by using a loop

for(size_t i=0;i<matrices.size();i++){
   matrices[i] = pointsData; //?? How do use this exactly??
}

Not familiar with using matrices, i just need to pass in 2 values per
point. Don’t really understand the ofMatrix4x4 documentation. Is it like
so?

for(size_t i=0;i<matrices.size();i++){
   ofMatrix4x4 tempMat = ofMatrix4x4 ( myPoint.x, myPoint.y, 
0,0,0,0,0,0,0,0,0,0,0,0,0,0 );
   matrices[i] = tempMat =; //Is this right?
}

Do i next run the buffer.updateData(0,matrices); method?

Also how do i access the matrices info from the frag shader? I only see
it in the vertex shader.
Can i put this in the frag shader uniform mat4 modelViewProjectionMatrix; ? or do i get it from the tex?

Thanks!

I managed to try it out.
Would you know if this is a v0.9.0 feature? v0.8.4 is giving me errors.