Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.05 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <!-- saved from url=(0082)https://sorbus.if.uj.edu.pl/~pbialas/3DComputerGraphics/Tutorial/WebGL/basics.html -->
  3. <html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  4.  
  5. <title>Triangle</title>
  6.  
  7. <script type="text/javascript" src="gl-matrix-min.js"></script>
  8. <style>
  9. body {
  10. margin: 100px;
  11. }
  12. </style>
  13.  
  14. <script>
  15.  
  16. /*
  17. This functions check if the WebGL context gl
  18. did throw any error and if so displays it's number.
  19. Probably not neccessery as it's is dislayed in console anyway ...
  20. */
  21.  
  22. function checkError(gl, msg) {
  23. let info = gl.getError();
  24. if (info !== gl.NO_ERROR) {
  25. console.log("Error " + info + " : " + msg);
  26. }
  27. }
  28.  
  29. /*
  30. Tak samo jak powyższa, ale zamiast wypisywać
  31. komunikat na konsoli wyświetla go w okienku
  32. dialogowym
  33. */
  34. function alertError(gl, msg) {
  35. let info = gl.getError();
  36. if (info !== gl.NO_ERROR) {
  37. alert("Error " + info + ": " + msg);
  38. }
  39. }
  40.  
  41. /*
  42. Tu następuje seria pomocniczych funkcji, które
  43. ułatwiają napisanie programu WebGL. W każdej z nich
  44. pierwszy parametr to WebGL context.
  45. */
  46.  
  47. /*
  48. Funkcja kompiluje kod zawarty w parametrze 'src'
  49. dołaczając go do shadera 'shader'. W przypadku błędów kompilacji
  50. wyświetla stosowny komunikat na konsoli.
  51. */
  52.  
  53. function compileShader(gl, shader, src) {
  54.  
  55. gl.shaderSource(shader, src);
  56. gl.compileShader(shader);
  57.  
  58. //Check for compilation error and display them.
  59. if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
  60. console.log(gl.getShaderInfoLog(shader));
  61. return null;
  62. }
  63. return shader;
  64. }
  65.  
  66. /*
  67. Główna funkcja tworząca program cieniujący.
  68. Żródła szaderów są przekazywane za pomoca obiektu 'src'
  69. jako jego pola 'vertex' i 'fragment'.
  70. */
  71.  
  72. function makeProgram(gl, src) {
  73. let vshader = gl.createShader(gl.VERTEX_SHADER);
  74. let fshader = gl.createShader(gl.FRAGMENT_SHADER);
  75.  
  76. compileShader(gl, vshader, src.vertex);
  77. compileShader(gl, fshader, src.fragment);
  78.  
  79. let program = gl.createProgram();
  80.  
  81. gl.attachShader(program, vshader);
  82. gl.attachShader(program, fshader);
  83.  
  84. gl.linkProgram(program);
  85. let info = gl.getProgramParameter(program, gl.LINK_STATUS);
  86. if (!info) {
  87. console.log("Could not link shaders\n" + info);
  88. }
  89.  
  90. return program;
  91. }
  92.  
  93. /*
  94. Function creating an ARRAY BUFFER and loading data into it.
  95. */
  96. function initBuffer(gl, data) {
  97.  
  98. let buffer = gl.createBuffer();
  99.  
  100. gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
  101. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(data), gl.STATIC_DRAW);
  102. gl.bindBuffer(gl.ARRAY_BUFFER, null);
  103. return buffer;
  104. }
  105.  
  106. </script>
  107. </head>
  108.  
  109. <body onload="onLoad()">
  110. <canvas id="canvas" width="400px" height="400px" style="border: solid thin black;"></canvas>
  111.  
  112.  
  113. <script>
  114.  
  115. /* Those two variables contain
  116. the source coe for both shaders.
  117. */
  118.  
  119. const vertex_src = `
  120. attribute vec4 aVertexPosition;
  121. uniform mat4 uM;
  122.  
  123. attribute vec4 aVertexColor;
  124.  
  125. varying vec4 vVertexColor;
  126.  
  127.  
  128.  
  129. void main() {
  130. gl_Position = aVertexPosition;
  131.  
  132. gl_Position = uM*aVertexPosition;
  133.  
  134. vVertexColor=aVertexColor;
  135. }
  136. `;
  137.  
  138. const fragment_src = `
  139.  
  140. precision mediump float;
  141.  
  142. varying vec4 vVertexColor;
  143. void main() {
  144. gl_FragColor = vVertexColor;
  145. }
  146. `;
  147.  
  148. const VertexCoordinates = [
  149. 0.0 , 0.0, 0.0,
  150. -1.0, 0.0, 0.0,
  151. -0.5, -1.0, 0.0,
  152. 0.0, -1.0, 0.0,
  153. 0.5, -1.0, 0.0,
  154. 1.0, 0.0, 0.0,
  155. 0.5, 1.0, 0.0,
  156. 0.0, 1.0, 0.0,
  157. -0.5, 1.0, 0.0,
  158. -1.0, 0.0, 0.0];
  159.  
  160.  
  161. const VertexColors = [
  162. 0.0 , 0.0, 0.0,
  163. 1.0, 0.0, 0.0,
  164. 0.5, 1.0, 0.0,
  165. 0.0, 1.0, 0.0,
  166. 0.5, 1.0, 0.0,
  167. 1.0, 0.0, 0.0,
  168. 0.5, 1.0, 0.0,
  169. 0.0, 1.0, 0.0,
  170. 0.5, 1.0, 0.0,
  171. 1.0, 0.0, 0.0];
  172.  
  173. function onLoad() {
  174. let canvas = document.getElementById("canvas");
  175. if (!canvas) alert("cannot access canvas");
  176. let gl = canvas.getContext("webgl");
  177. if (!gl) alert("cannot get WebGL context");
  178.  
  179. gl.viewport(0, 0, canvas.width, canvas.height);
  180.  
  181. let colorBuffer = initBuffer(gl, VertexColors);
  182.  
  183. let program = makeProgram(gl, {vertex: vertex_src, fragment: fragment_src});
  184.  
  185. let vertexBuffer = initBuffer(gl, VertexCoordinates);
  186.  
  187. // Set's the background color
  188. gl.clearColor(0.75, 0.75, 0.75, 1.0);
  189. // Enables the depth test.
  190. gl.enable(gl.DEPTH_TEST);
  191.  
  192. //Clear canvas (fill with background color) and depth buffer.
  193. gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
  194.  
  195. gl.useProgram(program);
  196.  
  197. var uMlocation = gl.getUniformLocation(program,'uM');
  198. var T = mat4.create();
  199. mat4.translate(T,T,[0.0,0.5,0.0])
  200.  
  201. var R = mat4.create();
  202. mat4.rotate(R,R,Math.PI/2, [0,0.0,1.0]);
  203.  
  204. gl.uniformMatrix4fv(uMlocation,false , T );
  205. gl.uniformMatrix4fv(uMlocation,false , R );
  206.  
  207. gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
  208. gl.enableVertexAttribArray(gl.getAttribLocation(program, 'aVertexPosition'));
  209. gl.vertexAttribPointer(gl.getAttribLocation(program, 'aVertexPosition'), 3, gl.FLOAT, false, 0, 0);
  210.  
  211. gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
  212. gl.enableVertexAttribArray(gl.getAttribLocation(program, 'aVertexColor'));
  213. gl.vertexAttribPointer(gl.getAttribLocation(program, 'aVertexColor'),
  214. 3, gl.FLOAT, false, 0, 0);
  215.  
  216. gl.drawArrays(gl.TRIANGLE_FAN, 1, 9);
  217.  
  218. gl.bindBuffer(gl.ARRAY_BUFFER, null);
  219. gl.disableVertexAttribArray(gl.getAttribLocation(program, 'aVertexPosition'));
  220.  
  221. }
  222.  
  223. </script>
  224.  
  225. </body></html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement