-
Notifications
You must be signed in to change notification settings - Fork 1
/
life.html
212 lines (199 loc) · 7.41 KB
/
life.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<html>
<head>
<title>Conway's Game of Life on GPU</title>
<script id="shader-vs" type="x-shader/x-vertex">
attribute vec3 aPos;
attribute vec2 aTexCoord;
varying vec2 vTexCoord;
void main(void) {
gl_Position = vec4(aPos, 1.);
vTexCoord = aTexCoord;
}
</script>
<script id="shader-fs" type="x-shader/x-fragment">
precision mediump float;
uniform sampler2D uTexSamp;
varying vec2 vTexCoord;
const float d = 1./1024.;
void main(void) {
float c = texture2D(uTexSamp, vTexCoord).x;
float s = texture2D(uTexSamp, vec2(vTexCoord.x, vTexCoord.y + d)).x;
s += texture2D(uTexSamp, vec2(vTexCoord.x, vTexCoord.y - d)).x;
s += texture2D(uTexSamp, vec2(vTexCoord.x + d, vTexCoord.y)).x;
s += texture2D(uTexSamp, vec2(vTexCoord.x - d, vTexCoord.y)).x;
s += texture2D(uTexSamp, vec2(vTexCoord.x + d, vTexCoord.y + d)).x;
s += texture2D(uTexSamp, vec2(vTexCoord.x - d, vTexCoord.y - d)).x;
s += texture2D(uTexSamp, vec2(vTexCoord.x + d, vTexCoord.y - d)).x;
s += texture2D(uTexSamp, vec2(vTexCoord.x - d, vTexCoord.y + d)).x;
// if ( > 0.) s += 1.0;
// if (texture2D(uTexSamp, vTexCoord - vec2(0., d)).x > 0.) s += 1.0;
// if (texture2D(uTexSamp, vTexCoord + vec2(d, 0.)).x > 0.) s += 1.0;
// if (texture2D(uTexSamp, vTexCoord - vec2(d, 0.)).x > 0.) s += 1.0;
// if (texture2D(uTexSamp, vTexCoord + vec2(d, d)).x > 0.) s += 1.0;
// if (texture2D(uTexSamp, vTexCoord - vec2(d, d)).x > 0.) s += 1.0;
// if (texture2D(uTexSamp, vTexCoord + vec2(d, -d)).x > 0.) s += 1.0;
// if (texture2D(uTexSamp, vTexCoord - vec2(d, -d)).x > 0.) s += 1.0;
// if (c > 0.0){
// if ((s == 2.0) || (s == 3.0)) gl_FragColor = vec4(1.);
// else gl_FragColor = vec4(vec3(0.),1.);}
// else if (s == 3.0) gl_FragColor = vec4(1.);
// else gl_FragColor = vec4(vec3(0.),1.);
float val = step(7.4, s * (5.5 - s) + c);
gl_FragColor = vec4(val, val, val, 1.0);
}
</script>
<script id="shader-fs-show" type="x-shader/x-fragment">
precision mediump float;
uniform sampler2D uTexSamp;
varying vec2 vTexCoord;
void main(void) {
gl_FragColor = texture2D(uTexSamp, vTexCoord);
}
</script>
<script type="text/javascript">
function getShader ( gl, id ){
var shaderScript = document.getElementById ( id );
var str = "";
var k = shaderScript.firstChild;
while ( k ){
if ( k.nodeType == 3 ) str += k.textContent;
k = k.nextSibling;
}
var shader;
if ( shaderScript.type == "x-shader/x-fragment" )
shader = gl.createShader ( gl.FRAGMENT_SHADER );
else if ( shaderScript.type == "x-shader/x-vertex" )
shader = gl.createShader(gl.VERTEX_SHADER);
else return null;
gl.shaderSource(shader, str);
gl.compileShader(shader);
return shader;
}
var gl, prog, prog_show, FBO, FBO2, texture, texture2, animation,
delay = 0, density = .5, it = 1, frames = 0, time, timer;
function main() {
clearInterval(timer);
var c = document.getElementById("c");
try { gl = c.getContext("experimental-webgl", {depth : false } );
} catch(e) {}
if ( !gl ) {alert("Your browser does not support WebGL"); return;}
prog = gl.createProgram();
gl.attachShader(prog, getShader( gl, "shader-vs" ));
gl.attachShader(prog, getShader( gl, "shader-fs" ));
gl.linkProgram(prog);
prog_show = gl.createProgram();
gl.attachShader(prog_show, getShader( gl, "shader-vs" ));
gl.attachShader(prog_show, getShader( gl, "shader-fs-show" ));
gl.linkProgram(prog_show);
var posBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, posBuffer);
var vertices = new Float32Array([-1,-1,0, 1,-1,0, -1,1,0, 1,1,0]);
var aPosLoc = gl.getAttribLocation(prog, "aPos");
gl.enableVertexAttribArray( aPosLoc );
var aTexLoc = gl.getAttribLocation(prog, "aTexCoord");
gl.enableVertexAttribArray( aTexLoc );
var texCoords = new Float32Array([0,0, 1,0, 0,1, 1,1]);
var texCoordOffset = vertices.byteLength;
gl.bufferData(gl.ARRAY_BUFFER,
texCoordOffset + texCoords.byteLength, gl.STATIC_DRAW);
gl.bufferSubData(gl.ARRAY_BUFFER, 0, vertices);
gl.bufferSubData(gl.ARRAY_BUFFER, texCoordOffset, texCoords);
gl.vertexAttribPointer(aPosLoc, 3, gl.FLOAT, gl.FALSE, 0, 0);
gl.vertexAttribPointer(aTexLoc, 2, gl.FLOAT, gl.FALSE, 0, texCoordOffset);
texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1);
var pixels = [], tSize = 1024;
for(var i = 0; i<tSize; i++)
for(var j = 0; j<tSize; j++)
if(Math.random() > density) pixels.push( 0, 0,0,0 );
else pixels.push( 255, 0,0,0 );
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, tSize, tSize, 0, gl.RGBA,
gl.UNSIGNED_BYTE, new Uint8Array(pixels));
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
texture2 = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture2);
gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, tSize, tSize, 0, gl.RGBA,
gl.UNSIGNED_BYTE, new Uint8Array(pixels));
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
// gl.uniform1i(gl.getUniformLocation(prog, "uTexSamp"), 0);
FBO = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, FBO);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0,
gl.TEXTURE_2D, texture, 0);
FBO2 = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, FBO2);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0,
gl.TEXTURE_2D, texture2, 0);
timer = setInterval(fr, 500);
time = new Date().getTime();
animation = "animate";
anim();
}
function anim(){
draw();
switch ( animation ){
case "animate":
setTimeout("requestAnimationFrame(anim)", delay); break;
case "reset":
main(); break;
}
}
function draw(){
gl.useProgram(prog);
for (i = 0; i < 10; i++) {
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.bindFramebuffer(gl.FRAMEBUFFER, FBO2);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
gl.flush();
gl.bindTexture(gl.TEXTURE_2D, texture2);
gl.bindFramebuffer(gl.FRAMEBUFFER, FBO);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
gl.flush();
}
gl.useProgram(prog_show);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
gl.flush();
it = -it; frames++;
}
function setDelay(v) {
delay = parseInt(v);
}
function setDensity(v) {
density = v.valueOf();
animation = "reset";
}
function fr(){
var ti = new Date().getTime();
var fps = Math.round(1000*frames/(ti - time));
document.getElementById("framerate").value = fps;
frames = 0; time = ti;
}
</script>
<style type="text/css">
H1{ font-size: 120%; color: green}
</style>
</head>
<body onload="main()">
<table><tr><td>
<canvas id="c" width="1024" height="1024"></canvas>
<br><button onclick='animation="reset"'>Reset</button>
delay(ms)<input size="2" value="0" onchange="setDelay( this.value )">
density<input size="3" value="0.5" onchange="setDensity(this.value)">
fps<input size="2" id="framerate">
</td><td>
<h1>Conway's Game of Life on GPU</h1>
<br>One more 2D Life but only 7kb in one piece.
On HD 4850 I've got ~80 frames per sec.
<br>Random life-forms are funny but they freeze quickly :)
<br><a href="http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life">Conway's
Game of Life (wiki)</a>
</td></tr></table>
<hr><a href="../webgl.htm">WebGL</a>
<i>updated</i> 25 Apr 2010
</body>
</html>