Loading...
Logo
Processing Forum
I was trying to write a fragment shader for the quick generation of multi-scale fractal turing patterns, as described in Jonathan McCabes paper  here and all those wonderful sketches at openprocessing. For some reason, when I try to run th ketch, the entire computer freezes? I can't really figure it out, and I'm not getting any error messages. I feel like I'm probably just missing something simple in the shader, but would you mind looking at the code?

The Processing portion:

Copy code
  1. float[] aR= {
  2.  50.0, 5.0,2.0
  3. };
  4. float[] iR= {
  5.   100.0, 10.0,4.0
  6. };
  7. float[] small= {
  8.   .4, .2,.1
  9. };

  10. PShader s;  
  11. float i;
  12.     
  13. void setup() {
  14.   size(640, 360,P2D);
  15.   s = loadShader("turing.glsl");
  16.   s.set("activatorRadii",aR);
  17.   s.set("inhibitorRadii",iR);
  18.   s.set("smallAmount",small);
  19.   i=1;
  20.   loadPixels();
  21.   randomize();
  22. }
  23. void randomize(){
  24.   for(int i=0;i<pixels.length;i++)
  25.     pixels[i]=color(random(0,255));
  26.   updatePixels();
  27. }

  28. void draw() {
  29.   s.set("iterations",i);
  30.   filter(s);
  31.   i++;
  32. }
And the GLSL portion, saved in the data folder as "turing.glsl":

Copy code
  1. #ifdef GL_ES
  2. precision mediump float;
  3. precision mediump int;
  4. #endif
  5. #define LEVELS 3
  6. uniform sampler2D textureSampler;
  7. uniform vec2 texcoordOffset;
  8. uniform float activatorRadii[LEVELS];
  9. uniform float inhibitorRadii[LEVELS];
  10. uniform float smallAmount[LEVELS];
  11. uniform float iterations;

  12. varying vec4 vertColor;
  13. varying vec4 vertTexcoord;
  14. float grid(vec4 v){ //maps the red value of the texture to between -1 and 1
  15.     return v.r*2.0-1.0;
  16. }
  17. vec4 unGrid(float g){ //maps the new value (which is most of the time between -1.4 and 1.4) to between -1 and 1
  18.     return vec4(vec3(g+1.4)/2.8,1.0);
  19. }
  20. vec4 tex(float x, float y){ //simply gets the color of the texture at (x,y).
  21.     return texture2D(textureSampler,vec2(x,y));
  22. }
  23. int constrained(float x, float y){ //makes checks to make certain that it is only using pixels on the texture
  24.     if(x>=0.0&&x<=1.0&&y>=0.0&&y<=1.0)
  25.         return 1;
  26.     return 0;
  27. }
  28. void main(void) {
  29.   vec4 pixelColor = texture2D(textureSampler,vertTexcoord.xy);
  30.   vec2 c=vertTexcoord.xy;
  31.   vec2 d=texcoordOffset;
  32.   float activator[LEVELS]; 
  33.   float inhibitor[LEVELS];
  34.   float variations[LEVELS];
  35.   int bestVariation=0;
  36.   for(int i=0;i<LEVELS;i++){
  37.     activator[i]=0.0;
  38.     inhibitor[i]=0.0;
  39.     float count=0.0;
  40.     //these lines make activator[i] equal to the average value of the pixels in a radius of activatorRadii[i] 
  41.     for(float x=c.x-activatorRadii[i]*d.x;x<c.x+activatorRadii[i]*d.x;x+=d.x){ 
  42. for(float y=c.y-activatorRadii[i]*d.y;y<c.y+activatorRadii[i]*d.y;y+=d.y){
  43.    if(constrained(x,y)==0){
  44. activator[i]+=grid(tex(x,y));
  45. count+=1.0;
  46.    }
  47. }
  48.     }
  49.     activator[i]/=count;
  50.     count=0.0;

  51.     //these lines make inhibito[i] equal to the average value of the pixels in a radius of inhibitorRadii[i] 
  52.     for(float x=c.x-inhibitorRadii[i]*d.x;x<c.x+inhibitorRadii[i]*d.x;x+=d.x){
  53. for(float y=c.y-inhibitorRadii[i]*d.y;y<c.y+inhibitorRadii[i]*d.y;y+=d.y){
  54.    if(constrained(x,y)==0){
  55. inhibitor[i]+=grid(tex(x,y));
  56. count+=1.0;
  57.    }
  58. }
  59.     }
  60.     inhibitor[i]/=count;
  61.     //these lines seek out the level of the turing pattern with the smallest difference between the activator and inhibitor
  62.     variations[i]=abs(activator[i]-inhibitor[i]);
  63.     if(variations[i]<variations[bestVariation])
  64. bestVariation=i;
  65.   }
  66.   float g=grid(tex(c.x,c.y));
  67.   if(activator[bestVariation]>inhibitor[bestVariation])
  68.     g+=smallAmount[bestVariation]/iterations;//dividing by iterations ensures that as time goes on, there will be less impact. This is how I adjusted to the difficulty of mapping the grid between -1 and 1 each time.
  69.   else
  70.     g-=smallAmount[bestVariation]/iterations;
  71.     
  72.   gl_FragColor = unGrid(g);
  73. }

Also, if this has any impact, I'm using Processing 2.0b6 on a mac os x 10.8.2 with a NVIDIA GeForce 320M graphics card.
Any help would be wonderful!

Replies(2)

This is very interesting. But I just switched to Processing 2.0b8 which is again a GLSL shader game changer (luckily for the better at first glance)...

Has this worked at any point? It seems like kamikaze to write the whole thing at once. Since debugging is the biggest challenge with shaders, I think incremental development is the safest route.
I know nothing about shaders, alas, but I tried the sketch as you gave it.
On Windows 7, medium to low level graphics card (ATI Radeon HD 5450), Processing 2.0b7 (2.0b8 don't want to start on my computer), it froze my computer for some seconds and reset my graphics (all windows went like negative photo). So, no success here either.