Passing an array to a vertex shader

Hi oF coders ! I’m very new to GLSL shaders and I try to understand how they works…

I’ve implemented several heightmap generators (generated data are a 2D ofImage), and currently I use the ofImage’s pixels arrays to generate a terrain bloc as an ofMesh. It works pretty well but turns to become slow when several high definition terrains (speaking about the number of vertices used) are handled continuously AND the heightmap are regenerated each frame (evolving terrains).

So, each point in the heightmap produces a vertex in 3D space.

Instead of using the CPU, I would like to use a vertex shader to modify the Y coordinate of each vertex.
Is ofShader::setUniform1iv the right way to pass an array of integers to the shader ? And how do I access this array in the shader’s code ? How can I index this array for each vertex processed in the shader ? Is there any kind of static variable I can initialize shader’s side before its execution and increment for each vertex being proceeded ?

May be another solution would be to pass the pixels array as a texture to the GPU and then access the texture pixels from the vertex shader ? Dunno if possible but seems more complex to me…

you can just pass the image as a texture to the shader using ofShader::setUniformTexture and read it in the vertex shader. to know which pixel to read you can for example use gl_VertexID which gives you the id for every vertex and then do:

x = gl_VertexID % int(imgW);
y = gl_VertexID / int(imgH);

(you might need to use uint instead of int)

and then use those as texture coordinates to read the value from the texture with:

h = texture(tex, vec2(x,y)).r;

or you could simply pregenerate the mesh with x,y coordinates for each vertex matching the size of the image and then use those to get the texture color

1 Like

Thanks Arturo ! With your hints it seems a little bit less complicated that I thought :slight_smile: Will go the texture way…

If it’s helpful, found this example useful to see how to pass an array of points into a fragment shader:

Cool ! Thank you !