stackgl / shader-school

:mortar_board: A workshopper for GLSL shaders and graphics programming
Other
4.28k stars 252 forks source link

#24-gpgpu-1 : Game of Life fix #89

Closed mdda closed 10 years ago

mdda commented 10 years ago

The 'ideal solution' is a really neat 'logic compression'. However, because the initial conditions are not {0,255}, it's almost impossible to write an explicit version that works.

The attached pull request changes the initial state to be {0,255} instead, so that the following kernel also works (boringly written, I know):

float state(vec2 coord) {
  return texture2D(prevState, fract(coord / stateSize)).r;
}

void main() {
  vec2 coord = gl_FragCoord.xy;

  float count = state(coord+vec2(-1,-1)) + state(coord+vec2(-1, 0)) + state(coord+vec2(-1,+1)) + 
                state(coord+vec2( 0,-1)) +                          + state(coord+vec2( 0,+1)) + 
                state(coord+vec2(+1,-1)) + state(coord+vec2(+1, 0)) + state(coord+vec2(+1,+1));
  float current = state(coord);

  float s = (current==0.0 && count==3.0)?1.0:((current==1.0 && (count==2.0 || count==3.0))?1.0:0.0);

  gl_FragColor = vec4(s,s,s, 1.0);
}

All the Best Martin :-)

mikolalysenko commented 10 years ago

Thanks, this is a good improvement.