Newer
Older
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
213
214
215
216
217
218
219
220
221
222
//used a lot of ideas from https://bl.ocks.org/robinhouston/ed597847175cf692ecce to clean this code up
var gl;
var canvas;
var frameBuffers;
var states = [null, null];
var resizedLastState;
var resizedCurrentState;
var width;
var height;
var stepProgram;
var renderProgram;
var textureSizeLocation;
var textureSizeLocationRender;
var mouseCoordLocation;
var mouseCoordinates = [null, null];
var mouseEnableLocation;
var mouseEnable = true;
var paused = false;//while window is resizing
window.onload = initGL;
function initGL() {
$("#about").click(function(e){
e.preventDefault();
$("#aboutModal").modal('show');
});
// Get A WebGL context
canvas = document.getElementById("glcanvas");
canvas.width = canvas.clientWidth;
canvas.height = canvas.clientHeight;
canvas.onmousemove = onMouseMove;
canvas.onmousedown = onMouseDown;
// canvas.onmouseup = onMouseUp;
canvas.onmouseout = onMouseUp;
canvas.onmouseenter = function(){
mouseEnable = 1;
};
window.onresize = onResize;
gl = canvas.getContext("webgl", {antialias:false}) || canvas.getContext("experimental-webgl", {antialias:false});
if (!gl) {
alert('Could not initialize WebGL, try another browser');
return;
}
gl.disable(gl.DEPTH_TEST);
gl.getExtension('OES_texture_float');
// setup a GLSL program
stepProgram = createProgramFromScripts(gl, "2d-vertex-shader", "2d-fragment-shader");
renderProgram = createProgramFromScripts(gl, "2d-vertex-shader", "2d-render-shader");
gl.useProgram(renderProgram);
loadVertexData(gl, renderProgram);
textureSizeLocationRender = gl.getUniformLocation(renderProgram, "u_textureSize");
gl.useProgram(stepProgram);
loadVertexData(gl, stepProgram);
textureSizeLocation = gl.getUniformLocation(stepProgram, "u_textureSize");
mouseCoordLocation = gl.getUniformLocation(stepProgram, "u_mouseCoord");
mouseEnableLocation = gl.getUniformLocation(stepProgram, "u_mouseEnable");
frameBuffers = [makeFrameBuffer(), makeFrameBuffer()];
resetWindow();
gl.bindTexture(gl.TEXTURE_2D, states[0]);//original texture
render();
}
function loadVertexData(gl, program) {
gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ -1,-1, 1,-1, -1, 1, 1, 1]), gl.STATIC_DRAW);
// look up where the vertex data needs to go.
var positionLocation = gl.getAttribLocation(program, "a_position");
gl.enableVertexAttribArray(positionLocation);
gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);
}
function makeFrameBuffer(){
return gl.createFramebuffer();
}
function makeRandomArray(rgba){
for (var x=0;x<width;x++) {
for (var y=0;y<height;y++) {
var ii = (y*width + x) * 4;
var central_square = (x > width/2-10 && x < width/2+10 && y > height/2-10 && y < height/2+10);
if (central_square) {
rgba[ii] = 0.5 + Math.random() * 0.02 - 0.01;
rgba[ii + 1] = 0.25 + Math.random() * 0.02 - 0.01;
} else {
rgba[ii] = 1.0;
rgba[ii + 1] = 0;
}
}
}
return rgba;
}
function makeTexture(gl, data){
var texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
// Set the parameters so we can render any size image.
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.FLOAT, data);
return texture;
}
function render(){
if (!paused) {
gl.useProgram(stepProgram);
if (mouseEnable){
gl.uniform1f(mouseEnableLocation, 1);
gl.uniform2f(mouseCoordLocation, mouseCoordinates[0], mouseCoordinates[1]);
} else gl.uniform1f(mouseEnableLocation, 0);
if (resizedLastState) {
states[0] = resizedLastState;
gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffers[0]);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, states[0], 0);
resizedLastState = null;
}
if (resizedCurrentState) {
states[1] = resizedCurrentState;
gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffers[1]);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, states[1], 0);
resizedCurrentState = null;
}
for (var i=0;i<2;i++) {
if (paused) {
window.requestAnimationFrame(render);
return;
}
step(i);
}
gl.useProgram(renderProgram);
//draw to canvas
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
gl.bindTexture(gl.TEXTURE_2D, states[0]);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
} else resetWindow();
window.requestAnimationFrame(render);
}
function step(i){
gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffers[(i+1)%2]);
gl.bindTexture(gl.TEXTURE_2D, states[i%2]);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);//draw to framebuffer
}
function onResize(){
paused = true;
}
function resetWindow(){
canvas.width = canvas.clientWidth;
canvas.height = canvas.clientHeight;
width = canvas.clientWidth;
height = canvas.clientHeight;
gl.viewport(0, 0, width, height);
// set the size of the texture
gl.useProgram(stepProgram);
gl.uniform2f(textureSizeLocation, width, height);
gl.useProgram(renderProgram);
gl.uniform2f(textureSizeLocationRender, width, height);
//texture for saving output from frag shader
resizedCurrentState = makeTexture(gl, null);
//fill with random pixels
var rgba = new Float32Array(width*height*4);
resizedLastState = makeTexture(gl, makeRandomArray(rgba));
paused = false;
}
function onMouseMove(e){
mouseCoordinates = [e.clientX, height-e.clientY];
}
function onMouseDown(e){
// gl.useProgram(stepProgram);
mouseCoordinates = [e.clientX, height-e.clientY];
}
function onMouseUp(){
mouseEnable = 0;
}