[examples] Add shaders_game_of_life (#5394)
* [examples] Add `shaders_game_of_life` * Declaration hides another variable same name
BIN
examples/shaders/resources/game_of_life/acorn.png
Normal file
|
After Width: | Height: | Size: 218 B |
BIN
examples/shaders/resources/game_of_life/breeder.png
Normal file
|
After Width: | Height: | Size: 1.9 KiB |
BIN
examples/shaders/resources/game_of_life/glider.png
Normal file
|
After Width: | Height: | Size: 216 B |
BIN
examples/shaders/resources/game_of_life/glider_gun.png
Normal file
|
After Width: | Height: | Size: 291 B |
BIN
examples/shaders/resources/game_of_life/oscillators.png
Normal file
|
After Width: | Height: | Size: 463 B |
BIN
examples/shaders/resources/game_of_life/puffer_train.png
Normal file
|
After Width: | Height: | Size: 1.3 KiB |
BIN
examples/shaders/resources/game_of_life/r_pentomino.png
Normal file
|
After Width: | Height: | Size: 213 B |
BIN
examples/shaders/resources/game_of_life/spaceships.png
Normal file
|
After Width: | Height: | Size: 828 B |
BIN
examples/shaders/resources/game_of_life/still_lifes.png
Normal file
|
After Width: | Height: | Size: 615 B |
44
examples/shaders/resources/shaders/glsl100/game_of_life.fs
Normal file
@ -0,0 +1,44 @@
|
||||
#version 100
|
||||
|
||||
precision highp float;
|
||||
|
||||
// Input vertex attributes (from vertex shader)
|
||||
varying vec2 fragTexCoord;
|
||||
varying vec4 fragColor;
|
||||
|
||||
// Input uniform values
|
||||
uniform sampler2D texture0;
|
||||
uniform vec4 colDiffuse;
|
||||
|
||||
// Input size in pixels of the textures
|
||||
uniform vec2 resolution;
|
||||
|
||||
void main()
|
||||
{
|
||||
// Size of one pixel in texture coordinates (from 0.0 to 1.0)
|
||||
float x = 1.0/resolution.x;
|
||||
float y = 1.0/resolution.y;
|
||||
|
||||
// Status of the current cell (1 = alive, 0 = dead)
|
||||
int origValue = (texture2D(texture0, fragTexCoord).r < 0.1)? 1 : 0;
|
||||
|
||||
// Sum of alive neighbors
|
||||
int sumValue = (texture2D(texture0, vec2(fragTexCoord.x - x, fragTexCoord.y - y)).r < 0.1)? 1 : 0; // Top-left
|
||||
sumValue += (texture2D(texture0, vec2(fragTexCoord.x - x, fragTexCoord.y )).r < 0.1)? 1 : 0; // Top
|
||||
sumValue += (texture2D(texture0, vec2(fragTexCoord.x - x, fragTexCoord.y + y)).r < 0.1)? 1 : 0; // Top-right
|
||||
|
||||
sumValue += (texture2D(texture0, vec2(fragTexCoord.x, fragTexCoord.y - y)).r < 0.1)? 1 : 0; // Left
|
||||
sumValue += (texture2D(texture0, vec2(fragTexCoord.x, fragTexCoord.y + y)).r < 0.1)? 1 : 0; // Right
|
||||
|
||||
sumValue += (texture2D(texture0, vec2(fragTexCoord.x + x, fragTexCoord.y - y)).r < 0.1)? 1 : 0; // Bottom-left
|
||||
sumValue += (texture2D(texture0, vec2(fragTexCoord.x + x, fragTexCoord.y )).r < 0.1)? 1 : 0; // Bottom
|
||||
sumValue += (texture2D(texture0, vec2(fragTexCoord.x + x, fragTexCoord.y + y)).r < 0.1)? 1 : 0; // Bottom-right
|
||||
|
||||
// Game of life rules:
|
||||
// Current cell remains alive when 2 or 3 neighbors are alive, dies otherwise
|
||||
// Current cell goes from dead to alive when exactly 3 neighbors are alive
|
||||
if ((origValue == 1 && sumValue == 2) || sumValue == 3)
|
||||
gl_FragColor = vec4(0.0, 0.0, 0.0, 255.0); // Alive: draw the pixel black
|
||||
else
|
||||
gl_FragColor = fragColor; // Dead: draw the pixel with the background color, RAYWHITE
|
||||
}
|
||||
42
examples/shaders/resources/shaders/glsl120/game_of_life.fs
Normal file
@ -0,0 +1,42 @@
|
||||
#version 120
|
||||
|
||||
// Input vertex attributes (from vertex shader)
|
||||
varying vec2 fragTexCoord;
|
||||
varying vec4 fragColor;
|
||||
|
||||
// Input uniform values
|
||||
uniform sampler2D texture0;
|
||||
uniform vec4 colDiffuse;
|
||||
|
||||
// Input size in pixels of the textures
|
||||
uniform vec2 resolution;
|
||||
|
||||
void main()
|
||||
{
|
||||
// Size of one pixel in texture coordinates (from 0.0 to 1.0)
|
||||
float x = 1.0/resolution.x;
|
||||
float y = 1.0/resolution.y;
|
||||
|
||||
// Status of the current cell (1 = alive, 0 = dead)
|
||||
int origValue = (texture2D(texture0, fragTexCoord).r < 0.1)? 1 : 0;
|
||||
|
||||
// Sum of alive neighbors
|
||||
int sumValue = (texture2D(texture0, vec2(fragTexCoord.x - x, fragTexCoord.y - y)).r < 0.1)? 1 : 0; // Top-left
|
||||
sumValue += (texture2D(texture0, vec2(fragTexCoord.x - x, fragTexCoord.y )).r < 0.1)? 1 : 0; // Top
|
||||
sumValue += (texture2D(texture0, vec2(fragTexCoord.x - x, fragTexCoord.y + y)).r < 0.1)? 1 : 0; // Top-right
|
||||
|
||||
sumValue += (texture2D(texture0, vec2(fragTexCoord.x, fragTexCoord.y - y)).r < 0.1)? 1 : 0; // Left
|
||||
sumValue += (texture2D(texture0, vec2(fragTexCoord.x, fragTexCoord.y + y)).r < 0.1)? 1 : 0; // Right
|
||||
|
||||
sumValue += (texture2D(texture0, vec2(fragTexCoord.x + x, fragTexCoord.y - y)).r < 0.1)? 1 : 0; // Bottom-left
|
||||
sumValue += (texture2D(texture0, vec2(fragTexCoord.x + x, fragTexCoord.y )).r < 0.1)? 1 : 0; // Bottom
|
||||
sumValue += (texture2D(texture0, vec2(fragTexCoord.x + x, fragTexCoord.y + y)).r < 0.1)? 1 : 0; // Bottom-right
|
||||
|
||||
// Game of life rules:
|
||||
// Current cell remains alive when 2 or 3 neighbors are alive, dies otherwise
|
||||
// Current cell goes from dead to alive when exactly 3 neighbors are alive
|
||||
if (((origValue == 1) && (sumValue == 2)) || sumValue == 3)
|
||||
gl_FragColor = vec4(0.0, 0.0, 0.0, 255.0); // Alive: draw the pixel black
|
||||
else
|
||||
gl_FragColor = fragColor; // Dead: draw the pixel with the background color, RAYWHITE
|
||||
}
|
||||
45
examples/shaders/resources/shaders/glsl330/game_of_life.fs
Normal file
@ -0,0 +1,45 @@
|
||||
#version 330
|
||||
|
||||
// Input vertex attributes (from vertex shader)
|
||||
in vec2 fragTexCoord;
|
||||
in vec4 fragColor;
|
||||
|
||||
// Input uniform values
|
||||
uniform sampler2D texture0;
|
||||
uniform vec4 colDiffuse;
|
||||
|
||||
// Output fragment color
|
||||
out vec4 finalColor;
|
||||
|
||||
// Input size in pixels of the textures
|
||||
uniform vec2 resolution;
|
||||
|
||||
void main()
|
||||
{
|
||||
// Size of one pixel in texture coordinates (from 0.0 to 1.0)
|
||||
float x = 1.0/resolution.x;
|
||||
float y = 1.0/resolution.y;
|
||||
|
||||
// Status of the current cell (1 = alive, 0 = dead)
|
||||
int origValue = (texture(texture0, fragTexCoord).r < 0.1)? 1 : 0;
|
||||
|
||||
// Sum of alive neighbors
|
||||
int sumValue = (texture(texture0, vec2(fragTexCoord.x - x, fragTexCoord.y - y)).r < 0.1)? 1 : 0; // Top-left
|
||||
sumValue += (texture(texture0, vec2(fragTexCoord.x - x, fragTexCoord.y )).r < 0.1)? 1 : 0; // Top
|
||||
sumValue += (texture(texture0, vec2(fragTexCoord.x - x, fragTexCoord.y + y)).r < 0.1)? 1 : 0; // Top-right
|
||||
|
||||
sumValue += (texture(texture0, vec2(fragTexCoord.x, fragTexCoord.y - y)).r < 0.1)? 1 : 0; // Left
|
||||
sumValue += (texture(texture0, vec2(fragTexCoord.x, fragTexCoord.y + y)).r < 0.1)? 1 : 0; // Right
|
||||
|
||||
sumValue += (texture(texture0, vec2(fragTexCoord.x + x, fragTexCoord.y - y)).r < 0.1)? 1 : 0; // Bottom-left
|
||||
sumValue += (texture(texture0, vec2(fragTexCoord.x + x, fragTexCoord.y )).r < 0.1)? 1 : 0; // Bottom
|
||||
sumValue += (texture(texture0, vec2(fragTexCoord.x + x, fragTexCoord.y + y)).r < 0.1)? 1 : 0; // Bottom-right
|
||||
|
||||
// Game of life rules:
|
||||
// Current cell remains alive when 2 or 3 neighbors are alive, dies otherwise
|
||||
// Current cell goes from dead to alive when exactly 3 neighbors are alive
|
||||
if (((origValue == 1) && (sumValue == 2)) || sumValue == 3)
|
||||
finalColor = vec4(0.0, 0.0, 0.0, 255.0); // Alive: draw the pixel black
|
||||
else
|
||||
finalColor = fragColor; // Dead: draw the pixel with the background color, RAYWHITE
|
||||
}
|
||||