Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #version 330
  2.  
  3. //interpolated texture coordinates for current pixel
  4. in vec2 uv;
  5.  
  6. //output pixel
  7. out vec4 frag_color;
  8.  
  9. //previous generation of CGoL
  10. uniform sampler2D u_source_texture;
  11.  
  12. void main()
  13. {
  14.     //fetch each neighbor texel and current texel.
  15.     //top row
  16.     int r00 = int(textureOffset(u_source_texture, uv, ivec2(-1, 1)).r);
  17.     int r01 = int(textureOffset(u_source_texture, uv, ivec2( 0, 1)).r);
  18.     int r02 = int(textureOffset(u_source_texture, uv, ivec2( 1, 1)).r);
  19.  
  20.     //middle row
  21.     int r10 = int(textureOffset(u_source_texture, uv, ivec2(-1, 0)).r);
  22.     int r11 = int(texture(u_source_texture, uv).r);
  23.     int r12 = int(textureOffset(u_source_texture, uv, ivec2( 1, 0)).r);
  24.    
  25.     //bottom row
  26.     int r20 = int(textureOffset(u_source_texture, uv, ivec2(-1,-1)).r;
  27.     int r21 = int(textureOffset(u_source_texture, uv, ivec2( 0,-1)).r);
  28.     int r22 = int(textureOffset(u_source_texture, uv, ivec2( 1,-1)).r);
  29.  
  30.     //conways game of life rules:
  31.     //if neighbor count including self is 3, the next generation cell is alive
  32.     //if neighbor count including self is 4, and current cell is alive, the next generation cell is alive
  33.     //otherwise, the next generation cell is dead.
  34.     int finalSum = (r00 + r10 + r20) +
  35.                    (r01 + r11 + r21) +
  36.                    (r02 + r12 + r22);
  37.  
  38.     if(finalSum == 3)
  39.             frag_color = vec4(1,1,1,1);
  40.     else if(finalSum == 4 && r11 == 1)
  41.             frag_color = vec4(1,1,1,1);
  42.     else
  43.             frag_color = vec4(0,0,0,1);
  44.  
  45. }