|
1339 | 1339 | // draw 9 points
|
1340 | 1340 | gl.drawArrays(gl.POINTS, 0, 9);
|
1341 | 1341 | </script>
|
| 1342 | +<script id="disabled-vertex-attrib" type="not-js"> |
| 1343 | +'use strict'; |
| 1344 | + |
| 1345 | +const canvas = document.querySelector('canvas'); |
| 1346 | +const gl = canvas.getContext('webgl'); |
| 1347 | + |
| 1348 | +const vsGLSL = ` |
| 1349 | + attribute vec4 position; |
| 1350 | + attribute vec4 color; |
| 1351 | +
|
| 1352 | + uniform vec4 offset; |
| 1353 | +
|
| 1354 | + varying vec4 v_color; |
| 1355 | +
|
| 1356 | + void main() { |
| 1357 | + gl_Position = position + offset; |
| 1358 | + v_color = color; |
| 1359 | + } |
| 1360 | +`; |
| 1361 | + |
| 1362 | +const fsGLSL = ` |
| 1363 | + precision highp float; |
| 1364 | + varying vec4 v_color; |
| 1365 | + void main() { |
| 1366 | + gl_FragColor = v_color; |
| 1367 | + } |
| 1368 | +`; |
| 1369 | + |
| 1370 | +const createShader = function(gl, type, glsl) { |
| 1371 | + const shader = gl.createShader(type) |
| 1372 | + gl.shaderSource(shader, glsl) |
| 1373 | + gl.compileShader(shader) |
| 1374 | + if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) { |
| 1375 | + throw new Error(gl.getShaderInfoLog(shader)) |
| 1376 | + } |
| 1377 | + return shader |
| 1378 | +}; |
| 1379 | + |
| 1380 | +const compileShadersAndLinkProgram = function(gl, prg, vsGLSL, fsGLSL) { |
| 1381 | + const vertexShader = createShader(gl, gl.VERTEX_SHADER, vsGLSL) |
| 1382 | + const fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fsGLSL) |
| 1383 | + gl.attachShader(prg, vertexShader) |
| 1384 | + gl.attachShader(prg, fragmentShader) |
| 1385 | + gl.linkProgram(prg) |
| 1386 | + if (!gl.getProgramParameter(prg, gl.LINK_STATUS)) { |
| 1387 | + throw new Error(gl.getProgramInfoLog(prg)) |
| 1388 | + } |
| 1389 | + // NOTE! These are only here to unclutter the diagram. |
| 1390 | + // It is safe to detach and delete shaders once |
| 1391 | + // a program is linked though it is arguably not common. |
| 1392 | + // and I usually don't do it. |
| 1393 | + gl.detachShader(prg, vertexShader) |
| 1394 | + gl.deleteShader(vertexShader) |
| 1395 | + gl.detachShader(prg, fragmentShader) |
| 1396 | + gl.deleteShader(fragmentShader) |
| 1397 | + return prg |
| 1398 | +}; |
| 1399 | + |
| 1400 | +const prog = gl.createProgram(); |
| 1401 | +compileShadersAndLinkProgram(gl, prog, vsGLSL, fsGLSL); |
| 1402 | +const positionLoc = gl.getAttribLocation(prog, 'position'); |
| 1403 | +const colorLoc = gl.getAttribLocation(prog, 'color'); |
| 1404 | +const offsetLoc = gl.getUniformLocation(prog, 'offset'); |
| 1405 | + |
| 1406 | +// vertex positions for triangle |
| 1407 | +const vertexPositions = new Float32Array([ |
| 1408 | + 0.0, 0.4, |
| 1409 | + -0.4, -0.4, |
| 1410 | + 0.4, -0.4, |
| 1411 | +]); |
| 1412 | + |
| 1413 | +const vertexColors = new Float32Array([ |
| 1414 | + 1, 1, 0, 1, // yellow |
| 1415 | + 0, 1, 1, 1, // cyan |
| 1416 | + 1, 0, 1, 1, // magenta |
| 1417 | +]); |
| 1418 | + |
| 1419 | +const positionBuffer = gl.createBuffer(); |
| 1420 | +gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer); |
| 1421 | +gl.bufferData(gl.ARRAY_BUFFER, vertexPositions, gl.STATIC_DRAW); |
| 1422 | + |
| 1423 | +const colorBuffer = gl.createBuffer(); |
| 1424 | +gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer); |
| 1425 | +gl.bufferData(gl.ARRAY_BUFFER, vertexColors, gl.STATIC_DRAW); |
| 1426 | + |
| 1427 | +// this is not needed. It's just here to unclutter the diagram |
| 1428 | +gl.bindBuffer(gl.ARRAY_BUFFER, null); |
| 1429 | + |
| 1430 | +// above this line is initialization code |
| 1431 | +// -------------------------------------- |
| 1432 | +// below is rendering code. |
| 1433 | + |
| 1434 | +// -------------------------------------- |
| 1435 | +// First draw a triangle with a different color for each vertex |
| 1436 | + |
| 1437 | +// set the attributes |
| 1438 | +gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer); |
| 1439 | +gl.enableVertexAttribArray(positionLoc); |
| 1440 | +gl.vertexAttribPointer( |
| 1441 | + positionLoc, // location |
| 1442 | + 2, // size (components per iteration) |
| 1443 | + gl.FLOAT, // type of to get from buffer |
| 1444 | + false, // normalize |
| 1445 | + 0, // stride (bytes to advance each iteration) |
| 1446 | + 0, // offset (bytes from start of buffer) |
| 1447 | +); |
| 1448 | + |
| 1449 | +gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer); |
| 1450 | +gl.enableVertexAttribArray(colorLoc); |
| 1451 | +gl.vertexAttribPointer( |
| 1452 | + colorLoc, // location |
| 1453 | + 4, // size (components per iteration) |
| 1454 | + gl.FLOAT, // type of to get from buffer |
| 1455 | + false, // normalize |
| 1456 | + 0, // stride (bytes to advance each iteration) |
| 1457 | + 0, // offset (bytes from start of buffer) |
| 1458 | +); |
| 1459 | + |
| 1460 | +// this is not needed. It's just here to unclutter the diagram |
| 1461 | +gl.bindBuffer(gl.ARRAY_BUFFER, null); |
| 1462 | + |
| 1463 | +gl.useProgram(prog); |
| 1464 | + |
| 1465 | +// draw on left |
| 1466 | +gl.uniform4fv(offsetLoc, [-0.5, 0, 0, 0]); |
| 1467 | + |
| 1468 | +// draw 3 vertices (1 triangle) |
| 1469 | +gl.drawArrays(gl.TRIANGLES, 0, 3); |
| 1470 | + |
| 1471 | +// Now draw the triangle again in a solid color |
| 1472 | +// by turning off the color attribute and setting |
| 1473 | +// an attribute values |
| 1474 | +gl.disableVertexAttribArray(colorLoc); |
| 1475 | +gl.vertexAttrib4fv(colorLoc, [0.3, 0.6, 0.9, 1]); |
| 1476 | + |
| 1477 | +// draw on right |
| 1478 | +gl.uniform4fv(offsetLoc, [0.5, 0, 0, 0]); |
| 1479 | + |
| 1480 | +// draw 3 vertices (1 triangle) |
| 1481 | +gl.drawArrays(gl.TRIANGLES, 0, 3); |
| 1482 | +</script> |
1342 | 1483 | <script src="/webgl/resources/chroma.min.js"></script>
|
1343 | 1484 | <script src="/3rdparty/showdown.min.js"></script>
|
1344 | 1485 | <script src="/3rdparty/prism.js" data-manual></script>
|
|
1446 | 1587 | { note: 'c-buffer', base: 'position2Buffer',x: 'left:left-0', y: 'top:bottom+10', },
|
1447 | 1588 | ],
|
1448 | 1589 | },
|
| 1590 | + 'disabled-vertex-attrib': { |
| 1591 | + name: 'Disabled Vertex Attrib', |
| 1592 | + adjust: [ |
| 1593 | + {cmd: 'expand', id: 'globalUI.attribValueState'}, |
| 1594 | + ], |
| 1595 | + windowPositions: [ |
| 1596 | + { note: 'canvas', base: '#diagram', x: 'right:right-10', y: 'top:top+10', }, |
| 1597 | + { note: 'vertex-array', base: '#diagram', x: 'left:left+460', y: 'bottom:bottom-10', }, |
| 1598 | + { note: 'global-state', base: '#diagram', x: 'left:left+10', y: 'top:top+10', }, |
| 1599 | + { note: 'program', base: 'global state', x: 'left:right+60', y: 'top:top+0', }, |
| 1600 | + { note: 'v-shader', base: 'canvas', x: 'left:left-50', y: 'top:bottom+10', }, |
| 1601 | + { note: 'f-shader', base: 'vertexShader', x: 'left:left-10', y: 'top:bottom-90', }, |
| 1602 | + { note: 'p-buffer', base: 'canvas', x: 'left:left+70', y: 'top:bottom+10', }, |
| 1603 | + { note: 'c-buffer', base: 'positionBuffer', x: 'left:left-0', y: 'top:bottom+10', }, |
| 1604 | + ], |
| 1605 | + }, |
1449 | 1606 | },
|
1450 | 1607 | });
|
1451 | 1608 | </script>
|
|
0 commit comments