Skip to content

Commit 366300a

Browse files
authored
[examples] Add shaders_game_of_life (#5394)
* [examples] Add `shaders_game_of_life` * Declaration hides another variable same name
1 parent 215ad78 commit 366300a

File tree

15 files changed

+1050
-0
lines changed

15 files changed

+1050
-0
lines changed
218 Bytes
Loading
1.87 KB
Loading
216 Bytes
Loading
291 Bytes
Loading
463 Bytes
Loading
1.35 KB
Loading
213 Bytes
Loading
828 Bytes
Loading
615 Bytes
Loading
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#version 100
2+
3+
precision highp float;
4+
5+
// Input vertex attributes (from vertex shader)
6+
varying vec2 fragTexCoord;
7+
varying vec4 fragColor;
8+
9+
// Input uniform values
10+
uniform sampler2D texture0;
11+
uniform vec4 colDiffuse;
12+
13+
// Input size in pixels of the textures
14+
uniform vec2 resolution;
15+
16+
void main()
17+
{
18+
// Size of one pixel in texture coordinates (from 0.0 to 1.0)
19+
float x = 1.0/resolution.x;
20+
float y = 1.0/resolution.y;
21+
22+
// Status of the current cell (1 = alive, 0 = dead)
23+
int origValue = (texture2D(texture0, fragTexCoord).r < 0.1)? 1 : 0;
24+
25+
// Sum of alive neighbors
26+
int sumValue = (texture2D(texture0, vec2(fragTexCoord.x - x, fragTexCoord.y - y)).r < 0.1)? 1 : 0; // Top-left
27+
sumValue += (texture2D(texture0, vec2(fragTexCoord.x - x, fragTexCoord.y )).r < 0.1)? 1 : 0; // Top
28+
sumValue += (texture2D(texture0, vec2(fragTexCoord.x - x, fragTexCoord.y + y)).r < 0.1)? 1 : 0; // Top-right
29+
30+
sumValue += (texture2D(texture0, vec2(fragTexCoord.x, fragTexCoord.y - y)).r < 0.1)? 1 : 0; // Left
31+
sumValue += (texture2D(texture0, vec2(fragTexCoord.x, fragTexCoord.y + y)).r < 0.1)? 1 : 0; // Right
32+
33+
sumValue += (texture2D(texture0, vec2(fragTexCoord.x + x, fragTexCoord.y - y)).r < 0.1)? 1 : 0; // Bottom-left
34+
sumValue += (texture2D(texture0, vec2(fragTexCoord.x + x, fragTexCoord.y )).r < 0.1)? 1 : 0; // Bottom
35+
sumValue += (texture2D(texture0, vec2(fragTexCoord.x + x, fragTexCoord.y + y)).r < 0.1)? 1 : 0; // Bottom-right
36+
37+
// Game of life rules:
38+
// Current cell remains alive when 2 or 3 neighbors are alive, dies otherwise
39+
// Current cell goes from dead to alive when exactly 3 neighbors are alive
40+
if ((origValue == 1 && sumValue == 2) || sumValue == 3)
41+
gl_FragColor = vec4(0.0, 0.0, 0.0, 255.0); // Alive: draw the pixel black
42+
else
43+
gl_FragColor = fragColor; // Dead: draw the pixel with the background color, RAYWHITE
44+
}

0 commit comments

Comments
 (0)