How to send an array to a shader frag on oF0.9.0

Hi, forum.

I’m trying to understand how to send an array to a shader frag.

I want to send float array to frag. but i couldn’t.
I wrote follows In the ofApp:

void ofApp::draw(){

    myArray[0] = 1.0;
    myArray[1] = 0.0;
    myArray[2] = 0.0;

    printf("%f \n", myArray[0]); //  1.0

    shader.load("","shader.frag");
    shader.begin();
    shader.setUniform1fv("uniArray", myArray, 3);
    ofRect(0,0,ofGetWidth(), ofGetHeight());
    shader.end();
}

in the frag.

uniform float uniArray[3];

void main() {
    gl_FragColor = vec4(uniArray[0], uniArray[1], uniArray[2], 1.0); // uniArray[0] seems not to be 1.0; 
}

I expected red rect is drawn. but it was black.

When I use setUniform3f and an vec3 instead of an array, A red rect is drawn.

What went wrong?

thanks.

hey @masakick,

I do believe what you’re missing is a reference to the first element of myArray:

shader.setUniform1fv("uniArray", &myArray[0], 3);

Hope that does the trick.

I’m having the exact same problem! Came across this while trying to add a second light to a sketch and instead it came out black, so I wrote a very similar debug sketch. Strangely, using ofSetUniform[X]fv with a count of 1, and reading the result as a normal variable in the shader seems to work, but using a higher count and declaring an array in the shader fails…

ofapp.cpp

    void ofApp::setup(){
	float colors[] = { 1,0.5,0,0,0.5,1 };
	shader.load("test");
	shader.begin();
	shader.setUniform3fv("inColor", colors , 1);    //works
    //shader.setUniform3fv("inColor", colors , 2);  //fails
	shader.end();
	cout << colors;
    }

    //--------------------------------------------------------------
    void ofApp::draw(){
    	shader.begin();
    	ofDrawRectangle(100, 100, ofGetWidth() -200, ofGetHeight() -200);
    	shader.end();
    }

test.frag

#version 430

uniform vec3 inColor;         //works
//uniform vec3 inColor[2];    //fails
out vec4 outColor;

void main() {
	outColor = vec4(inColor, 1);         //works
    //outColor = vec4(inColor[0], 1);   //fails for both array positions
}

Using the 3 working lines draws an orange rectangle, using the 3 broken lines draws a black rectangle whether using array position 0 or 1 in the shader. Changing color to &color[0] as described in the previous answer had no effect on behaviour.

Not sure if this is a 0.9 bug or a GPU driver bug (I know GLSL can be funny with arrays at times). Anyone have any ideas?

OK, still not sure where the bug comes from, but it seems to work fine if you use the underlying OpenGL commands.

void ofApp::setup(){
	float colors[] = { 1,0.5,0,0,0.5,1 };
	shader.load("test");
	GLuint program = shader.getProgram();
	GLint loc = glGetUniformLocation(program, "inColor");
	glUseProgram(program);
	glUniform3fv(loc, 2, colors);
}

Hope that helps somebody :slight_smile:

can you open a bug in github? we use an internal cache of uniforms to make things faster in some platforms and it might be causing some problems with this specific call