| 1 |
/* -*- Mode: C -*- */ |
|---|
| 2 |
uniform sampler2DRect textureIn; |
|---|
| 3 |
|
|---|
| 4 |
void main(void) |
|---|
| 5 |
{ |
|---|
| 6 |
vec2 texCoord = gl_TexCoord[0].xy; |
|---|
| 7 |
|
|---|
| 8 |
const float offset = 1.0; |
|---|
| 9 |
|
|---|
| 10 |
// Consider only the red channel in the input image, if > 0.5 assume alive. |
|---|
| 11 |
|
|---|
| 12 |
// center |
|---|
| 13 |
int c = texture2DRect(textureIn, texCoord).r > 0.5 ? 1 : 0; |
|---|
| 14 |
// bottom left |
|---|
| 15 |
int bl = texture2DRect(textureIn, texCoord + vec2(-offset, -offset)).r > 0.5 ? 1 : 0; |
|---|
| 16 |
// left |
|---|
| 17 |
int l = texture2DRect(textureIn, texCoord + vec2(-offset, 0.0)).r > 0.5 ? 1 : 0; |
|---|
| 18 |
// top left |
|---|
| 19 |
int tl = texture2DRect(textureIn, texCoord + vec2(-offset, offset)).r > 0.5 ? 1 : 0; |
|---|
| 20 |
// top |
|---|
| 21 |
int t = texture2DRect(textureIn, texCoord + vec2( 0.0, offset)).r > 0.5 ? 1 : 0; |
|---|
| 22 |
// top right |
|---|
| 23 |
int tr = texture2DRect(textureIn, texCoord + vec2( offset, offset)).r > 0.5 ? 1 : 0; |
|---|
| 24 |
// right |
|---|
| 25 |
int r = texture2DRect(textureIn, texCoord + vec2( offset, 0.0)).r > 0.5 ? 1 : 0; |
|---|
| 26 |
// bottom right |
|---|
| 27 |
int br = texture2DRect(textureIn, texCoord + vec2( offset, -offset)).r > 0.5 ? 1 : 0; |
|---|
| 28 |
// bottom |
|---|
| 29 |
int b = texture2DRect(textureIn, texCoord + vec2( 0.0, -offset)).r > 0.5 ? 1 : 0; |
|---|
| 30 |
|
|---|
| 31 |
// sum neighbours |
|---|
| 32 |
int sum = bl + l + tl + t + tr + r + br + b; |
|---|
| 33 |
|
|---|
| 34 |
// the rules of Conway's game of life |
|---|
| 35 |
vec4 outval; |
|---|
| 36 |
if (c == 1) { // cell alive |
|---|
| 37 |
if (sum < 2 ) { // loneliness |
|---|
| 38 |
outval = vec4(0,0,0,1); |
|---|
| 39 |
} else if (sum > 3) { // overcrowding |
|---|
| 40 |
outval = vec4(0,0,0,1); |
|---|
| 41 |
} else { // unchanged |
|---|
| 42 |
outval = vec4(1,1,1,1); |
|---|
| 43 |
} |
|---|
| 44 |
} else { // cell dead |
|---|
| 45 |
if (sum == 3) { // come to life |
|---|
| 46 |
outval = vec4(1,1,1,1); |
|---|
| 47 |
} else { // unchanged |
|---|
| 48 |
outval = vec4(0,0,0,1); |
|---|
| 49 |
} |
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
gl_FragColor = outval; |
|---|
| 53 |
} |
|---|