webgl-0004-movement.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>WebGL Movement</title>
  6. <style type="text/css">
  7. body { background-color: grey;}
  8. canvas { background-color: white;}
  9. </style>
  10. <script src="../../dist/skulpt.js" type="text/javascript"></script>
  11. <script src="../../dist/builtin.js" type="text/javascript"></script>
  12. </head>
  13. <body>
  14. <h1>WebGL Movement</h1>
  15. <form>
  16. <textarea id="code" name="code" cols="120" rows="40">
  17. import webgl
  18. from math import sin
  19. gl = webgl.Context("my-canvas")
  20. trianglesVerticeBuffer = gl.createBuffer()
  21. trianglesColorBuffer = gl.createBuffer()
  22. program = None
  23. def setup():
  24. global program
  25. vs = gl.createShader(gl.VERTEX_SHADER)
  26. gl.shaderSource(vs, "attribute vec3 aVertexPosition; attribute vec3 aVertexColor; varying highp vec4 vColor; void main(void) { gl_Position = vec4(aVertexPosition, 1.0); vColor = vec4(aVertexColor, 1.0); }")
  27. gl.compileShader(vs)
  28. print "Vertex shader COMPILE_STATUS: " + str(gl.getShaderParameter(vs, gl.COMPILE_STATUS))
  29. fs = gl.createShader(gl.FRAGMENT_SHADER)
  30. gl.shaderSource(fs, "varying highp vec4 vColor; void main(void) { gl_FragColor = vColor; }")
  31. gl.compileShader(fs)
  32. print "Fragment shader COMPILE_STATUS: " + str(gl.getShaderParameter(fs, gl.COMPILE_STATUS))
  33. program = gl.createProgram()
  34. gl.attachShader(program, vs)
  35. gl.attachShader(program, fs)
  36. gl.linkProgram(program)
  37. print "Program LINK_STATUS: " + str(gl.getProgramParameter(program, gl.LINK_STATUS))
  38. gl.useProgram(program)
  39. triangleVerticeColors = [1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0]
  40. gl.bindBuffer(gl.ARRAY_BUFFER, trianglesColorBuffer)
  41. gl.bufferData(gl.ARRAY_BUFFER, webgl.Float32Array(triangleVerticeColors), gl.STATIC_DRAW)
  42. def draw(gl, elapsed):
  43. gl.clearColor(0.1, 0.5, 0.1, 1.0);
  44. gl.clear(gl.COLOR_BUFFER_BIT);
  45. gl.viewport(0, 0, 400, 300);
  46. translation = sin(elapsed * 2.0 * 3.14159 / 10000.0)/2.0;
  47. triangleVertices = [-0.5 + translation, 0.5, 0.0,
  48. 0.0 + translation, 0.0, 0.0,
  49. -0.5 + translation, -0.5, 0.0,
  50. 0.5 + translation, 0.5, 0.0,
  51. 0.0 + translation, 0.0, 0.0,
  52. 0.5 + translation, -0.5, 0.0]
  53. gl.bindBuffer(gl.ARRAY_BUFFER, trianglesVerticeBuffer)
  54. gl.bufferData(gl.ARRAY_BUFFER, webgl.Float32Array(triangleVertices), gl.DYNAMIC_DRAW)
  55. vertexPositionAttribute = gl.getAttribLocation(program, "aVertexPosition")
  56. gl.enableVertexAttribArray(vertexPositionAttribute)
  57. gl.bindBuffer(gl.ARRAY_BUFFER, trianglesVerticeBuffer)
  58. gl.vertexAttribPointer(vertexPositionAttribute, 3, gl.FLOAT, False, 0, 0)
  59. vertexColorAttribute = gl.getAttribLocation(program, "aVertexColor")
  60. gl.enableVertexAttribArray(vertexColorAttribute)
  61. gl.bindBuffer(gl.ARRAY_BUFFER, trianglesColorBuffer)
  62. gl.vertexAttribPointer(vertexColorAttribute, 3, gl.FLOAT, False, 0, 0)
  63. gl.drawArrays(gl.TRIANGLES, 0, 6)
  64. setup()
  65. gl.setDrawFunc(draw);
  66. </textarea>
  67. <button onclick="runit()" type="button">Run</button>
  68. </form>
  69. <canvas id="my-canvas" height="300" width="400">
  70. Your browser does not support the HTML5 canvas element.
  71. </canvas>
  72. <pre id="my-output" ></pre>
  73. <script>
  74. function outputHandler(text) {
  75. var output = document.getElementById("my-output");
  76. output.innerHTML = output.innerHTML + text.replace(/</g, '&lt;');
  77. }
  78. function builtinRead(x) {
  79. if (Sk.builtinFiles === undefined || Sk.builtinFiles["files"][x] === undefined) {
  80. throw "File not found: '" + x + "'";
  81. }
  82. return Sk.builtinFiles["files"][x];
  83. }
  84. function runit() {
  85. var prog = document.getElementById("code").value;
  86. // Clear the output
  87. document.getElementById("my-output").innerHTML = '';
  88. Sk.canvas = "my-canvas";
  89. Sk.pre = "my-output";
  90. Sk.configure({"output":outputHandler, "read":builtinRead});
  91. try {
  92. eval(Sk.importMainWithBody("<stdin>", false, prog));
  93. }
  94. catch(e) {
  95. alert(e);
  96. }
  97. }
  98. </script>
  99. </body>
  100. </html>