Pass Array to Shader

edited April 2014 in GLSL / Shaders

Hello All,

I am just starting with shaders in Processing, but have a little knowledge of them from Three.js and WebGL. I've scoured online but can't find an answer, and some of the GLSL language is pretty impregnable.

I want to pass an array of floats to the shaders (eventually two arrays, one for x values one for y values relating to a set of points) so I can calculate which point is closest for a given pixel in the fragment shader. I'm assuming that's something you can do in the fragment shader?!?!?!

I am passing the array of 10 floats using shader.set("px", xArray); and that seems fine, but I am not sure how to assign this as a varying in the vertex shader, I get this error which I guess is to do with assigning arrays:

java.lang.RuntimeException: Cannot compile vertex shader: ERROR: 0:26: '=' disallowed on array types

Here is the code for the vertex shader:


uniform mat4 modelview;
uniform mat4 transform;
uniform mat3 normalMatrix;

attribute vec4 vertex;
attribute vec3 normal;

uniform float mx;
varying float mxx;

uniform float px[10];
varying float pxx[10];

void main() {
  mxx = mx;
  pxx = px;

  // Vertex in clip coordinates
  gl_Position = transform * vertex;

So any advice on how to handle arrays (I wouldn't have thought it necessary to start putting values into textures for this) would be appreciated!





  • Answer ✓

    The processing code would help people to get the example running a bit quicker.

    I don't know if you removed any comments above but is line 0:26 pxx = px;?

    If so maybe try to copy the values over in a loop.

  • w h y d i d i n o t t h i n k o f t h a t !

    thanks so much clankill3r, that seems to have worked.

  • Why are you using varying anyway? Aren't the uniforms also available to the fragment shader?

  • Good tip. As I said I don't have much experience with shaders! Thanks.

Sign In or Register to comment.