Advertisement
Matt_23

Game of Life fragment shader

Mar 16th, 2023
2,354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OpenGL Shading 1.86 KB | Source Code | 0 0
  1. #version 330 core
  2.  
  3. in vec2 pos;
  4.  
  5. layout (location = 0) out vec3 frag_col;
  6.  
  7. uniform sampler2D input_texture;
  8. uniform bool generate_random;
  9. uniform int width;
  10. uniform int height;
  11.  
  12. float random (in vec2 st) {
  13.     return fract(sin(dot(st.xy,
  14.                          vec2(12.9898,78.233)))
  15.                  * 43758.5453123);
  16. }
  17.  
  18. void main() {
  19.     int pixel_size = 2;
  20.     ivec2 screen_pos = ivec2(pos.x * width, pos.y * height);
  21.     if(generate_random) {
  22.         if (random(vec2(screen_pos.x - (screen_pos.x%pixel_size), screen_pos.y - (screen_pos.y%pixel_size))) < 0.5) {
  23.             frag_col = vec3(0.0);
  24.         } else {
  25.             frag_col = vec3(1.0);
  26.         }
  27.         return;
  28.     }
  29.  
  30.     int wyn = 0;
  31.     wyn += int(texture(input_texture, vec2(pos.x + (pixel_size/float(width)), pos.y + (pixel_size/float(height)))).r);
  32.     wyn += int(texture(input_texture, vec2(pos.x - (pixel_size/float(width)), pos.y + (pixel_size/float(height)))).r);
  33.     wyn += int(texture(input_texture, vec2(pos.x - (pixel_size/float(width)), pos.y - (pixel_size/float(height)))).r);
  34.     wyn += int(texture(input_texture, vec2(pos.x + (pixel_size/float(width)), pos.y - (pixel_size/float(height)))).r);
  35.     wyn += int(texture(input_texture, vec2(pos.x + (pixel_size/float(width)), pos.y)).r);
  36.     wyn += int(texture(input_texture, vec2(pos.x - (pixel_size/float(width)), pos.y)).r);
  37.     wyn += int(texture(input_texture, vec2(pos.x, pos.y + (pixel_size/float(height)))).r);
  38.     wyn += int(texture(input_texture, vec2(pos.x, pos.y - (pixel_size/float(height)))).r);
  39.  
  40.     int state = int(texture(input_texture, vec2(pos.x, pos.y)).r);
  41.  
  42.     if(wyn < 2 && state == 1) frag_col = vec3(0.0);
  43.     else if(wyn < 4 && state == 1) frag_col = vec3(1.0);
  44.     else if(state == 1) frag_col = vec3(0.0);
  45.     else if(wyn == 3 && state == 0) frag_col = vec3(1.0f);
  46.     else frag_col = vec3(0.0);
  47.  
  48.  
  49. }
  50.  
Tags: glsl
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement