How to feed array from processing to GLSL fragment shader?

edited October 2013 in GLSL / Shaders

I have

GLSL: uniform vec3 myArray[index];

Processing: How do I set individual value in myArray from processing using .set method?

Tagged:

Answers

  • edited October 2013 Answer ✓

    PShader includes a few set() methods that allow to pass int, float or boolean arrays to the shader:

    • set(String name, int[] vec)
    • set(String name, int[] vec, int ncoords)
    • set(String name, float[] vec)
    • set(String name, float[] vec, int ncoords)
    • set(String name, int[] vec)
    • set(String name, int[] vec, int ncoords)

    the ncoords argument can be used to set the number of coordinates per element (i.e.: the array uniform in the shader is of type vec2, vec3, etc).

  • thanks now I got it to work but there is a limit uniform array size in glsl shader is there an alternative way?

  • edited November 2013 Answer ✓

    The fastest way to upload large amounts of information to a shader is to pack the data in a texture, and set the texture as a uniform. Then sample the texture in the shader for the values you are looking for.

  • Thanks Bill, would you kindly show an example how to pack data in a texture in processing. I could not find that in processing 2 reference.

  • edited November 2013 Answer ✓

    The texture is a PImage. Each pixel in the PImage could hold 3 or 4 elements in the "Array" you are uploading.

    You use set(String name, PImage texture) to set the PImage/Texture as a uniform.

    Then in the shader you use texture2D to get the value from the pixel in the texture.

    The CustomBlend example shipped with Processing does a better job explaining this than I could.

  • The texture is 32bit RGBA and can only store value from 0-255 range. I need accurate per pixel precision for movement of my particles. I googled and found floating point texture thing. But I don't have knowledge about OPENGL and do not know how to implement this in processing. How do I make floating point texture in processing?

  • Processing only handles RGBA textures internally. You can still create floating point textures and pass them to the shaders in your sketch, but you will need to write all the appropriate opengl handling code yourself.

  • Answer ✓

    A workaround for this is to pack your 32 bit data using the four 8 bit channels of each pixel in the texture and unpack it in your shader.

Sign In or Register to comment.