|
153 | 153 |
|
154 | 154 | var shader; |
155 | 155 | if (shaderScript.type == "x-shader/x-fragment") { |
156 | | - shader = gl.createShader(gl.FRAGMENT_SHADER); |
| 156 | + shader = { id: gl.createShader(gl.FRAGMENT_SHADER) }; |
157 | 157 | } else if (shaderScript.type == "x-shader/x-vertex") { |
158 | | - shader = gl.createShader(gl.VERTEX_SHADER); |
| 158 | + shader = { id: gl.createShader(gl.VERTEX_SHADER) }; |
159 | 159 | } else { |
160 | 160 | return null; |
161 | 161 | } |
162 | 162 |
|
163 | | - gl.shaderSource(shader, str); |
164 | | - gl.compileShader(shader); |
| 163 | + gl.shaderSource(shader.id, str); |
| 164 | + gl.compileShader(shader.id); |
165 | 165 |
|
166 | | - if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) { |
167 | | - alert(gl.getShaderInfoLog(shader)); |
| 166 | + if (!gl.getShaderParameter(shader.id, gl.COMPILE_STATUS)) { |
| 167 | + alert(gl.getShaderInfoLog(shader.id)); |
168 | 168 | return null; |
169 | 169 | } |
170 | 170 |
|
|
176 | 176 | var fragmentShader = getShader(gl, fragmentShaderName); |
177 | 177 | var vertexShader = getShader(gl, vertexShaderName); |
178 | 178 |
|
179 | | - var program = gl.createProgram(); |
180 | | - gl.attachShader(program, vertexShader); |
181 | | - gl.attachShader(program, fragmentShader); |
182 | | - gl.linkProgram(program); |
| 179 | + var program = { id: gl.createProgram() }; |
| 180 | + gl.attachShader(program.id, vertexShader.id); |
| 181 | + gl.attachShader(program.id, fragmentShader.id); |
| 182 | + gl.linkProgram(program.id); |
183 | 183 |
|
184 | | - if (!gl.getProgramParameter(program, gl.LINK_STATUS)) { |
| 184 | + if (!gl.getProgramParameter(program.id, gl.LINK_STATUS)) { |
185 | 185 | alert("Could not initialise color shaders"); |
186 | 186 | } |
187 | 187 |
|
188 | | - program.vertexPositionAttribute = gl.getAttribLocation(program, "aVertexPosition"); |
| 188 | + program.vertexPositionAttribute = gl.getAttribLocation(program.id, "aVertexPosition"); |
189 | 189 | gl.enableVertexAttribArray(program.vertexPositionAttribute); |
190 | 190 |
|
191 | | - program.pMatrixUniform = gl.getUniformLocation(program, "uPMatrix"); |
192 | | - program.mvMatrixUniform = gl.getUniformLocation(program, "uMVMatrix"); |
| 191 | + program.pMatrixUniform = gl.getUniformLocation(program.id, "uPMatrix"); |
| 192 | + program.mvMatrixUniform = gl.getUniformLocation(program.id, "uMVMatrix"); |
193 | 193 |
|
194 | 194 | return program; |
195 | 195 | } |
|
199 | 199 | var mandelbrotProgram; |
200 | 200 | function initPrograms() { |
201 | 201 | colorProgram = initProgram("color-shader-fs", "color-shader-vs"); |
202 | | - colorProgram.vertexColorAttribute = gl.getAttribLocation(colorProgram, "aColor"); |
| 202 | + colorProgram.vertexColorAttribute = gl.getAttribLocation(colorProgram.id, "aColor"); |
203 | 203 | gl.enableVertexAttribArray(colorProgram.vertexColorAttribute); |
204 | 204 |
|
205 | 205 | mandelbrotProgram = initProgram("mandelbrot-shader-fs", "mandelbrot-shader-vs"); |
206 | | - mandelbrotProgram.vertexPlotPositionAttribute = gl.getAttribLocation(mandelbrotProgram, "aPlotPosition"); |
| 206 | + mandelbrotProgram.vertexPlotPositionAttribute = gl.getAttribLocation(mandelbrotProgram.id, "aPlotPosition"); |
207 | 207 | gl.enableVertexAttribArray(mandelbrotProgram.vertexPlotPositionAttribute); |
208 | 208 | } |
209 | 209 |
|
210 | 210 |
|
211 | 211 | var currentProgram; |
212 | 212 | function setCurrentProgram(program) { |
213 | 213 | currentProgram = program; |
214 | | - gl.useProgram(program); |
| 214 | + gl.useProgram(program.id); |
215 | 215 | } |
216 | 216 |
|
217 | 217 |
|
|
321 | 321 | mat4.rotate(mvMatrix, degToRad(this.yRot), [0, 1, 0]); |
322 | 322 | mat4.rotate(mvMatrix, degToRad(this.zRot), [0, 0, 1]); |
323 | 323 |
|
324 | | - gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexPositionBuffer); |
| 324 | + gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexPositionBuffer.id); |
325 | 325 | gl.vertexAttribPointer(mandelbrotProgram.vertexPositionAttribute, cubeVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0); |
326 | 326 |
|
327 | | - var positionBuffer = gl.createBuffer(); |
328 | | - gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer); |
| 327 | + var positionBuffer = { id: gl.createBuffer() }; |
| 328 | + gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer.id); |
329 | 329 | var positions = []; |
330 | 330 | for (var i = 0; i < 6; i++) { |
331 | 331 | positions.push(-2.2 / this.zooms[i] + this.centerOffsetsX[i], -1.2 / this.zooms[i] + this.centerOffsetsY[i]);; |
|
336 | 336 | gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW); |
337 | 337 | gl.vertexAttribPointer(mandelbrotProgram.vertexPlotPositionAttribute, 2, gl.FLOAT, false, 0, 0); |
338 | 338 |
|
339 | | - gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeVertexIndexBuffer); |
| 339 | + gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeVertexIndexBuffer.id); |
340 | 340 | setMatrixUniforms(); |
341 | 341 | gl.drawElements(gl.TRIANGLES, cubeVertexIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0); |
342 | 342 |
|
|
350 | 350 | var cubeVertexPositionBuffer; |
351 | 351 | var cubeVertexIndexBuffer; |
352 | 352 | function initBuffers() { |
353 | | - boundingBoxVertexPositionBuffer = gl.createBuffer(); |
354 | | - gl.bindBuffer(gl.ARRAY_BUFFER, boundingBoxVertexPositionBuffer); |
| 353 | + boundingBoxVertexPositionBuffer = { id: gl.createBuffer() }; |
| 354 | + gl.bindBuffer(gl.ARRAY_BUFFER, boundingBoxVertexPositionBuffer.id); |
355 | 355 | var vertices = [ |
356 | 356 | -5, -5, -10, |
357 | 357 | -5, -5, -25, |
|
382 | 382 | boundingBoxVertexPositionBuffer.itemSize = 3; |
383 | 383 | boundingBoxVertexPositionBuffer.numItems = 20; |
384 | 384 |
|
385 | | - boundingBoxVertexColorBuffer = gl.createBuffer(); |
386 | | - gl.bindBuffer(gl.ARRAY_BUFFER, boundingBoxVertexColorBuffer); |
| 385 | + boundingBoxVertexColorBuffer = { id: gl.createBuffer() }; |
| 386 | + gl.bindBuffer(gl.ARRAY_BUFFER, boundingBoxVertexColorBuffer.id); |
387 | 387 | var colors = [ |
388 | 388 | 0.9, 0.9, 0.9, 1, |
389 | 389 | 0.9, 0.9, 0.9, 1, |
|
414 | 414 | boundingBoxVertexColorBuffer.itemSize = 4; |
415 | 415 | boundingBoxVertexColorBuffer.numItems = 20; |
416 | 416 |
|
417 | | - boundingBoxVertexIndexBuffer = gl.createBuffer(); |
418 | | - gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, boundingBoxVertexIndexBuffer); |
| 417 | + boundingBoxVertexIndexBuffer = { id: gl.createBuffer() }; |
| 418 | + gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, boundingBoxVertexIndexBuffer.id); |
419 | 419 | var vertexIndices = [ |
420 | 420 | 0, 1, 2, 0, 2, 3, |
421 | 421 | 4, 5, 6, 4, 6, 7, |
|
427 | 427 | boundingBoxVertexIndexBuffer.itemSize = 1; |
428 | 428 | boundingBoxVertexIndexBuffer.numItems = 30; |
429 | 429 |
|
430 | | - cubeVertexPositionBuffer = gl.createBuffer(); |
431 | | - gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexPositionBuffer); |
| 430 | + cubeVertexPositionBuffer = { id: gl.createBuffer() }; |
| 431 | + gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexPositionBuffer.id); |
432 | 432 | var vertexPositions = [ |
433 | 433 | // Front face |
434 | 434 | -1.0, -1.0, 1.0, |
|
470 | 470 | cubeVertexPositionBuffer.itemSize = 3; |
471 | 471 | cubeVertexPositionBuffer.numItems = 24; |
472 | 472 |
|
473 | | - cubeVertexIndexBuffer = gl.createBuffer(); |
474 | | - gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeVertexIndexBuffer); |
| 473 | + cubeVertexIndexBuffer = { id: gl.createBuffer() }; |
| 474 | + gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeVertexIndexBuffer.id); |
475 | 475 | var vertexIndices = [ |
476 | 476 | 0, 1, 2, 0, 2, 3, // Front face |
477 | 477 | 4, 5, 6, 4, 6, 7, // Back face |
|
499 | 499 | setCurrentProgram(colorProgram); |
500 | 500 | mat4.identity(mvMatrix); |
501 | 501 |
|
502 | | - gl.bindBuffer(gl.ARRAY_BUFFER, boundingBoxVertexPositionBuffer); |
| 502 | + gl.bindBuffer(gl.ARRAY_BUFFER, boundingBoxVertexPositionBuffer.id); |
503 | 503 | gl.vertexAttribPointer(colorProgram.vertexPositionAttribute, boundingBoxVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0); |
504 | 504 |
|
505 | | - gl.bindBuffer(gl.ARRAY_BUFFER, boundingBoxVertexColorBuffer); |
| 505 | + gl.bindBuffer(gl.ARRAY_BUFFER, boundingBoxVertexColorBuffer.id); |
506 | 506 | gl.vertexAttribPointer(colorProgram.vertexColorAttribute, boundingBoxVertexColorBuffer.itemSize, gl.FLOAT, false, 0, 0); |
507 | 507 |
|
508 | | - gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, boundingBoxVertexIndexBuffer); |
| 508 | + gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, boundingBoxVertexIndexBuffer.id); |
509 | 509 | setMatrixUniforms(); |
510 | 510 | gl.drawElements(gl.TRIANGLES, boundingBoxVertexIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0); |
511 | 511 | } |
|
0 commit comments