glsl - input structure of points into shaders

bernardo's picture

hello how can I input structure of points into and Glsl shader so that it reads the xy position of an array of points?

cybero's picture
Re: glsl - input structure of points into shaders

Make QC run GLSL 1.5 so you can run geometry shaders as per OpenGL specification 3.2 or higher. That's if I've understood you correctly. Otherwise couldn't you work upon an objects vertices? [GL Tools Line Structure patch for instance]

bernardo's picture
Re: glsl - input structure of points into shaders

yes cybero. i just want to input into the shader a structure(array) of values with xy coords to eval:

the frag shader

uniform float x1;
uniform float y1;
uniform float x2;
uniform float y2;
uniform float x3;
uniform float y3;
uniform float x4;
uniform float y4;
uniform int pointsLength;
vec2 points[10];
float distances[10];
float rPoints[10];
//Declare a 2D texture as a uniform variable
vec3 red= vec3(1.0,0.0,0.0);
vec3 green= vec3(0.0,1.0,0.0);
vec3 blue= vec3(0.0,0.0,1.0);
vec3 yellow= vec3(1.0,1.0,0.0);
vec2 p1= vec2( x1, y1);
vec2 p2= vec2( x2, y2);
vec2 p3= vec2( x3, y3);
vec2 p4= vec2( x4, y4);
varying vec4 p;
void main() {
 //float r1= ((p.x-p1.x)*(p.x-p1.x))+((p.y-p1.y)*(p.y-p1.y));
 //float r2= ((p.x-p2.x)*(p.x-p2.x))+((p.y-p2.y)*(p.y-p2.y));
 //float r3= ((p.x-p3.x)*(p.x-p3.x))+((p.y-p3.y)*(p.y-p3.y));
 float r1= distance(p.xy, p1);
 float r2= distance(p.xy, p2);
 float r3= distance(p.xy, p3);
 float r4= distance(p.xy, p4);
for (int i=0;i<pointsLength;i=i++){
    distances[i] = distance(p.xy, points[i].xy);
 vec3 color;
  if (r1<r2 && r1<r3 && r1<r4) 
  color= red;
  else if (r2<r1 && r2<r3 && r2<r4) 
  color= green;
  else if (r3<r2 && r3<r4 && r3<r1) 
  color= yellow;
  color= blue;
    gl_FragColor = vec4(color, 1.0);

its from GToledo and inputs 4 points but I wann input a hole bunch of them LOL

gtoledo3's picture
Re: glsl - input structure of points into shaders

I hadn't replied to your fb stat yet :-)... This gives more context.

There's a few issues...

QCStructure, is unique to QC. One way to look at it, is that it's kind of like a wrapper that can hold certain types of data.

GLSL is a cross platform technology. There's no such thing in GLSL as "QCStructure", which is unfortunate from the standpoint of ease of use.

QC does a little wedging to make GLSL work ok in the context of QC programming. For example, there's some niceties like being able to make a dedicated Color port, which is also a QC data type. (You can do that by writing uniform vec4 Color in your shader, or if you need multiple, you can do something like uniform vec4 a_Color, uniform vec4 b_Color, etc. A vec4 is still a GLSL data type though, and the there's other places in QC where a color data type converts to a vec4.)

So, there's no way to make a color port on a GLSL shader. Something like an NSUInteger, NSString, NSArray, NSDictionary, GFList, etc. (parts of QCStructure), doesn't mean anything in shader language, and there's no kind of conversion presented. I don't think there could legitimately be one, so the choice not to weld one on probably made sense.

With that particular shader, there are multiple things that would need to be changed; it's not programmed to have a dynamic structure count. I think what could be reasonably done to affect something like what you're talking about are to manually add more float inputs, and update all of the code to yield whatever result you're going for.

My other thought, is that you might be able to create a texture input on the shader, and use the r/g/b or r/g/b/a pixel intensities to control stuff in the shader, as if it was a structure. However, you're going to be limited in steps of resolution, and unless you recode the shader to work in a loop, it's not going to work if you change the texture w/h. You'll have to add your points in, update the equation, and keep your w/h static. It would be a little more natural if QC's GLSL supported 1D texture, but it does not.

bernardo's picture
Re: glsl - input structure of points into shaders

but it does support a 2d texture. I read somewhere Cwgrith telling about a 2d texture with a single line of pixels (1d) and the rest black or whatever. could that help?