Ofshader set uniform once instead of each frame

Hi, I am trying to implement a worley noise using ofshader. My ofDraw() code looks like this:

        ofSetBackgroundColor(0, 0, 0);

	z += 1; if (z > 400) z = 0;

	shader.begin();
	shader.setUniform1f("u_z", z);
	shader.setUniform2f("u_resolution", ofGetWidth(), ofGetHeight());
	shader.setUniform3fv("u_p", (float*)&a[0].x, nmax);
	ofRect(0, 0, ofGetWidth(), ofGetHeight());
	shader.end();

This is my shader.frag code:

#ifdef GL_ES
precision mediump float;
#endif

#define nmax 1000

uniform vec2 u_resolution;
uniform float u_z;
uniform vec3 u_p[nmax];

void main() {
	vec2 xy = gl_FragCoord.xy;
	float cmin=u_resolution.x*u_resolution.x;
	float c=0;
	vec3 dp;
	for(int i=0;i<nmax; i++)
	{
		dp=u_p[i] - vec3(xy,u_z);
		c=dot(dp,dp);
		if(c<cmin) cmin=c;
	}

	cmin = 1.5*sqrt(cmin)/255.0;

    gl_FragColor = vec4(cmin,cmin,cmin,1.0);
}

In the code, the nmax=1000 and the contents of the array a(in the shader.setUniform3fv) does not change. Is there a way to set it to the shader once rather than setting it each frame?

Hi @luffy8naruto,

Assuming your list is created like so :


    vector < glm::vec3 > a;
    int amnt = 1000;
    for( int i = 0; i < amnt; i++){
        a.push_back( glm::vec3(ofNoise(i * .1 * ofGetElapsedTimef()), float(i) / float(amnt), 0));
    }

you would need to pass the argument like so :

    shader.setUniform3fv("u_p", (float*)&a[0], 1000);

so the error would come from here :

because you only pass .x

I tested it on my machine and it works.
Let me know,

Best,

P