Skip to content

michaelhogg/webgol

 
 

Repository files navigation

WebGOL Build Status

Screenshot

http://michaelhogg.github.io/webgol/

A WebGL implementation of Conway's Game of Life (GOL).

Improvements

WebGOL was forked from webgl-game-of-life, and features the following improvements:

  • Dynamically-sized canvas which fills the window (using NPOT textures)
  • Coloured cells (linear interpolation of corner colours)
  • User-configurable cell size, framerate and wraparound
  • Mutations (randomly-added live cells to prevent life from dying out)
  • Glow/bloom visual effect
  • Control panel user interface
  • Comprehensive error handling and troubleshooting
  • Shader source code loaded without using deprecated synchronous Ajax
  • Non-dependent texture reads in most shaders
  • SRP object-oriented code architecture
  • SCSS stylesheet
  • A few Jasmine tests (running on Travis CI)
  • Grunt development tasks for linting, minifying, compiling and testing

What is Conway's Game of Life?

LIFE is a classic computer game. It was invented by John H. Conway in 1970, and has entertained many hackers and wasted many years of computer time ever since. If you're smart and creative, it can be very intellectually stimulating. It's a simulation game which can generate strange and beautiful patterns, sometimes in complex and interesting ways. (ref)

Why not use pure JavaScript?

Tasks such as simulating GOL or rendering fractals require a large number of calculations to be performed. For example, a 1080p GOL simulation contains 2 million cells, so an animation rate of 30 frames-per-second requires 60 million cell updates every second.

JavaScript is a single-threaded environment, so it can only perform one calculation at a time. Web workers can be used to perform a few calculations in parallel, but they are "relatively heavy-weight, and are not intended to be used in large numbers" (spec). Therefore, a pure JavaScript implementation of GOL would be relatively slow.

Why use WebGL?

WebGL executes code on the GPU, which can perform a calculation on hundreds of data elements simultaneously. This kind of hardware-accelerated parallel computing is great for rendering 3D graphics, and can also benefit other tasks (such as a GOL simulation) in which a large amount of data can be processed in parallel.

GOL can be considered to be a pleasingly parallel problem, in that each cell's next state can be calculated independently, and so "little or no effort is required to separate the problem into a number of parallel tasks".

WebGOL is an example of GPGPU (general-purpose computing on GPUs). Check out this excellent article for more information:

GPGPU is a methodology for high-performance computing that uses GPUs to crunch data. Algorithms well-suited to GPGPU implementation are those that exhibit two properties:

  • Data parallel – A processor can execute the operation on different data elements simultaneously.
  • Throughput intensive – The algorithm is going to process lots of data elements, so there will be plenty to operate on in parallel.

Implementation

In WebGOL, the entire GOL simulation state is stored and processed as textures in the GPU. The next state of a cell is calculated by a fragment shader, and hundreds of copies of that shader can be executed in parallel on a modern GPU, enabling hundreds of cells to be updated simultaneously.

With the GPU responsible for all the heavy processing work, the JavaScript code simply needs to send regular commands to the GPU to update the GOL state and render it to the screen.

Even with a low-power GPU in a laptop (eg: Intel HD Graphics 4000), WebGOL can run a 1080p simulation at 60 frames-per-second (120 million cell updates every second).

Libraries used

Thanks

  • A huge thank you to skeeto for creating igloojs (a WebGL wrapper library) and webgl-game-of-life (see his blog post), which provide the foundation for WebGOL.
  • Thanks also to mikeash for creating GPULife, which contains a couple of nice features that have been copied in WebGOL ("corner colours" and random cell mutations).
  • And of course, thanks to John Conway for inventing his wonderful Game of Life :)

License

WebGOL is licensed under the MIT License (Expat).

It's a fork of webgl-game-of-life, which does not contain any license details, but the author's blog indicates that the code is released into the public domain:

All information on this blog, unless otherwise noted, is hereby released into the public domain, with no rights reserved. (1)

Just like all of my code, I made [NativeGuide] public domain (2)

About

Conway's Game of Life, hardware-accelerated with WebGL

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 73.7%
  • CSS 16.6%
  • C 9.6%
  • Ruby 0.1%