webgl-0005-3d.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>WebGL 3D</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 3D</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. uViewMatrix = None
  24. uProjMatrix = None
  25. viewMatrix = webgl.Matrix4x4([1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0])
  26. projMatrix = webgl.Matrix4x4([1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0])
  27. def setup():
  28. print viewMatrix
  29. print projMatrix
  30. global program, uViewMatrix, uProjMatrix
  31. vs = gl.createShader(gl.VERTEX_SHADER)
  32. gl.shaderSource(vs, "" +
  33. "attribute vec3 aVertexPosition;" +
  34. "attribute vec3 aVertexColor;" +
  35. "" +
  36. "uniform mat4 uViewMatrix;" +
  37. "uniform mat4 uProjMatrix;" +
  38. "" +
  39. "varying highp vec4 vColor;" +
  40. "void main(void) {" +
  41. " gl_Position = uProjMatrix * uViewMatrix * vec4(aVertexPosition, 1.0);" +
  42. " vColor = vec4(aVertexColor, 1.0);" +
  43. "}")
  44. gl.compileShader(vs)
  45. print "Vertex shader COMPILE_STATUS: " + str(gl.getShaderParameter(vs, gl.COMPILE_STATUS))
  46. fs = gl.createShader(gl.FRAGMENT_SHADER)
  47. gl.shaderSource(fs, "" +
  48. "varying highp vec4 vColor;" +
  49. "void main(void) {" +
  50. " gl_FragColor = vColor;" +
  51. "}")
  52. gl.compileShader(fs)
  53. print "Fragment shader COMPILE_STATUS: " + str(gl.getShaderParameter(fs, gl.COMPILE_STATUS))
  54. program = gl.createProgram()
  55. gl.attachShader(program, vs)
  56. gl.attachShader(program, fs)
  57. gl.linkProgram(program)
  58. print "Program LINK_STATUS: " + str(gl.getProgramParameter(program, gl.LINK_STATUS))
  59. gl.useProgram(program)
  60. 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]
  61. gl.bindBuffer(gl.ARRAY_BUFFER, trianglesColorBuffer)
  62. gl.bufferData(gl.ARRAY_BUFFER, webgl.Float32Array(triangleVerticeColors), gl.STATIC_DRAW)
  63. uViewMatrix = gl.getUniformLocation(program, "uViewMatrix")
  64. uProjMatrix = gl.getUniformLocation(program, "uProjMatrix")
  65. projMatrix.perspective(45, 4.0/3.0, 0.1, 100.0)
  66. viewMatrix.identity()
  67. viewMatrix.translate([0.0, 0.0, -2.0])
  68. gl.clearColor(0.0, 0.0, 0.0, 1.0);
  69. gl.viewport(0, 0, 400, 300);
  70. def draw(gl, elapsed):
  71. gl.clear(gl.COLOR_BUFFER_BIT);
  72. translation = sin(elapsed * 2.0 * 3.14159 / 10000.0)/2.0;
  73. triangleVertices = [-0.5 + translation, 0.5, -0.5,
  74. 0.0 + translation, 0.0, -0.5,
  75. -0.5 + translation, -0.5, -0.5,
  76. 0.5 + translation, 0.5, 0.5,
  77. 0.0 + translation, 0.0, 0.5,
  78. 0.5 + translation, -0.5, 0.5]
  79. gl.bindBuffer(gl.ARRAY_BUFFER, trianglesVerticeBuffer)
  80. gl.bufferData(gl.ARRAY_BUFFER, webgl.Float32Array(triangleVertices), gl.DYNAMIC_DRAW)
  81. gl.uniformMatrix4fv(uProjMatrix, False, projMatrix)
  82. gl.uniformMatrix4fv(uViewMatrix, False, viewMatrix)
  83. vertexPositionAttribute = gl.getAttribLocation(program, "aVertexPosition")
  84. gl.enableVertexAttribArray(vertexPositionAttribute)
  85. gl.bindBuffer(gl.ARRAY_BUFFER, trianglesVerticeBuffer)
  86. gl.vertexAttribPointer(vertexPositionAttribute, 3, gl.FLOAT, False, 0, 0)
  87. vertexColorAttribute = gl.getAttribLocation(program, "aVertexColor")
  88. gl.enableVertexAttribArray(vertexColorAttribute)
  89. gl.bindBuffer(gl.ARRAY_BUFFER, trianglesColorBuffer)
  90. gl.vertexAttribPointer(vertexColorAttribute, 3, gl.FLOAT, False, 0, 0)
  91. gl.drawArrays(gl.TRIANGLES, 0, 6)
  92. setup()
  93. gl.setDrawFunc(draw);
  94. </textarea>
  95. <button onclick="runit()" type="button">Run</button>
  96. </form>
  97. <canvas id="my-canvas" height="300" width="400">
  98. Your browser does not support the HTML5 canvas element.
  99. </canvas>
  100. <pre id="my-output" ></pre>
  101. <script>
  102. function outputHandler(text) {
  103. var output = document.getElementById("my-output");
  104. output.innerHTML = output.innerHTML + text.replace(/</g, '&lt;');
  105. }
  106. function builtinRead(x) {
  107. if (Sk.builtinFiles === undefined || Sk.builtinFiles["files"][x] === undefined) {
  108. throw "File not found: '" + x + "'";
  109. }
  110. return Sk.builtinFiles["files"][x];
  111. }
  112. function runit() {
  113. var prog = document.getElementById("code").value;
  114. // Clear the output
  115. document.getElementById("my-output").innerHTML = '';
  116. Sk.canvas = "my-canvas";
  117. Sk.pre = "my-output";
  118. Sk.configure({"output":outputHandler, "read":builtinRead});
  119. try {
  120. eval(Sk.importMainWithBody("<stdin>", false, prog));
  121. }
  122. catch(e) {
  123. alert(e);
  124. }
  125. }
  126. </script>
  127. </body>
  128. </html>