#version 330

precision highp float;

uniform sampler2D previousState;
uniform float life;
uniform int ruleStay[9];
uniform int ruleCome[9];
uniform float size[2];
uniform int nFrame;

smooth in vec2 vTexCoord;

out vec4 myColor;

void main()
{
    myColor = vec4(0.0,0.0,0.0,1.0);
	float pre = texture(previousState, vTexCoord.st).r;
    int nachbarn = 0;
    for(int i = -1; i < 2; i++)
    {
        for(int j = -1; j < 2; j++)
        {
            if((i != 0 || j !=  0) && texture(previousState,vec2(vTexCoord.s+(i*(1.0/size[0])),vTexCoord.t+(j*(1.0/size[1])))).r == 1.0)
                nachbarn++;
        }
    }
    if ((pre == 1.0 && ruleStay[nachbarn] == 1) || (pre == 0.0 && ruleCome[nachbarn] == 1)) {
        myColor.rgb = vec3(1.0);
    }
    else if (pre != 0.0) {
        myColor.rgb = vec3(pre-1.0/life);
    }
    
}