We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi,
Currently I'm passing an array of floats to my shader, but I'd like to pass in more complex items. From my research it seems like I'd use structs here but Java does not support them.
My object is something like this:
struct boid { vec2 position; vec2 velocity; int flock; }
Any advice on how to build an equivalent object in my Processing code to pass in?
Answers
@crummy -- disclaimer -- I am not a shader pro. However:
I think the issue is not Java support. You could create a struct-like class with public instance variables -- as per the Sun Java Coding Guidelines -- http://stackoverflow.com/a/8565711 .
The issue seems to be that PShader.set() only accepts arrays -- not arbitrary objects.
If your goal is concision and coding convenience, you could consider a class like this:
(untested)
Now:
If you wanted to make it even more concise you could add another class constructor which takes the PShader and calls set immediately. Creating your "struct"(-like-object) and configuring your shader are one action.
Now:
That would work fine, but in this case (a flocking simulation) I want to push an array of about a hundred of these things. I could have "uniform vec2[100] positions", etc but with position, velocity, and more I'll run up against max uniforms sooner or later.
Perhaps "encoding" a struct into a texture2d and passing it into the shader would allow greater flexibility?
How to stay under max_uniforms with a large array seems like it is a very different question from how to pass in mixed data types -- or I'm really not understanding your question, and will have to defer to a shaders expert.
Shouldn't MAX_*_UNIFORM_COMPONENTS (or whatever) be a property of your graphics setup?
My understanding was that it doesn't matter how you pass them in -- as PVectors, or parallel float arrays, etc. -- you are either under the memory limit or you aren't. But perhaps there is shader lore or dark magic to work around that.
Instead of passing in 3*100 vec2 and ints (one set of three settings per boid) I would assume that you would Have a single flock object passing in 3 large parallel arrays with three set calls: -- float[100], float[100], int[100]. But memory is memory.
Thanks for the clarification - I read the docs and realized I misunderstood the uniform limitations. I'll pass in arrays of position, velocity etc separately.
Good luck! Interested to hear how it works out.
Forum shader-experts -- do you have any follow-up confirmation or advice for crummy?
https://www.khronos.org/registry/OpenGL/specs/gl/GLSLangSpec.3.30.pdf
@nabr -- Thanks! How does your response relate to the question of how to pass large amounts of structured data to a shader from Processing?
Yes i just read the question without the following conversation
struct boid { vec2 position; vec2 velocity; int flock; }B_[200]; will create 200x vec2 positions, 200x vec2 velocity, 200x int flock;
Man sorry, i just type something, maybe its related, when somebody google how to use structs :)
As i know @crummy your question is related to lights in native OpenGL mostly this kind of data is passed as array into the shader. Take a look here. http://www.tomdalling.com/blog/modern-opengl/08-even-more-lighting-directional-lights-spotlights-multiple-lights/
Also make an internet research maybe you find better examples that fits your needs.
Basicly the same as jeremydouglass already suggested.