#version 330
//interpolated texture coordinates for current pixel
in vec2 uv;
//output pixel
out vec4 frag_color;
//previous generation of CGoL
uniform sampler2D u_source_texture;
void main()
{
//fetch each neighbor texel and current texel.
//top row
int r00 = int(textureOffset(u_source_texture, uv, ivec2(-1, 1)).r);
int r01 = int(textureOffset(u_source_texture, uv, ivec2( 0, 1)).r);
int r02 = int(textureOffset(u_source_texture, uv, ivec2( 1, 1)).r);
//middle row
int r10 = int(textureOffset(u_source_texture, uv, ivec2(-1, 0)).r);
int r11 = int(texture(u_source_texture, uv).r);
int r12 = int(textureOffset(u_source_texture, uv, ivec2( 1, 0)).r);
//bottom row
int r20 = int(textureOffset(u_source_texture, uv, ivec2(-1,-1)).r;
int r21 = int(textureOffset(u_source_texture, uv, ivec2( 0,-1)).r);
int r22 = int(textureOffset(u_source_texture, uv, ivec2( 1,-1)).r);
//conways game of life rules:
//if neighbor count including self is 3, the next generation cell is alive
//if neighbor count including self is 4, and current cell is alive, the next generation cell is alive
//otherwise, the next generation cell is dead.
int finalSum = (r00 + r10 + r20) +
(r01 + r11 + r21) +
(r02 + r12 + r22);
if(finalSum == 3)
frag_color = vec4(1,1,1,1);
else if(finalSum == 4 && r11 == 1)
frag_color = vec4(1,1,1,1);
else
frag_color = vec4(0,0,0,1);
}