0tamega

segitiga_sejajar

Jan 25th, 2023
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <canvas id="canvas" width="300" height="300"></canvas>
  5.  
  6.  
  7. <script id="vertex" type="x-shader/x-vertex">
  8. #version 300 es
  9.  
  10.  
  11. in vec4 vertexPosition;
  12.  
  13.  
  14. void main() {
  15. gl_Position = vertexPosition;
  16. }
  17. </script>
  18.  
  19.  
  20. <script id="fragment" type="x-shader/x-fragment">
  21. #version 300 es
  22. precision highp float;
  23.  
  24.  
  25. out vec4 fragColor;
  26.  
  27.  
  28. void main() {
  29. fragColor = vec4(1, 0, 0, 1);
  30. }
  31. </script>
  32.  
  33.  
  34. <script>
  35. const canvas = document.getElementById("canvas");
  36. const vertexCode = document.getElementById("vertex").textContent;
  37. const fragmentCode = document.getElementById("fragment").textContent;
  38.  
  39.  
  40. const gl = canvas.getContext("webgl2");
  41. if (!gl) throw "WebGL2 not supported";
  42.  
  43.  
  44. function createShader(shaderType, sourceCode) {
  45. const shader = gl.createShader(shaderType);
  46. gl.shaderSource(shader, sourceCode.trim());
  47. gl.compileShader(shader);
  48. if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
  49. throw gl.getShaderInfoLog(shader);
  50. }
  51. return shader;
  52. }
  53.  
  54.  
  55. const program = gl.createProgram();
  56. gl.attachShader(program, createShader(gl.VERTEX_SHADER, vertexCode));
  57. gl.attachShader(program, createShader(gl.FRAGMENT_SHADER, fragmentCode));
  58. gl.linkProgram(program);
  59. if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
  60. throw gl.getProgramInfoLog(program);
  61. }
  62. gl.useProgram(program);
  63.  
  64.  
  65. const segitiga1 = [
  66. [0, 0],
  67. [-1, 0.5],
  68. [-1, 0],
  69. ];
  70. const vertexData = new Float32Array(segitiga1.flat());
  71. gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());
  72. gl.bufferData(gl.ARRAY_BUFFER, vertexData, gl.STATIC_DRAW);
  73. const vertexPosition = gl.getAttribLocation(program, "vertexPosition");
  74. gl.enableVertexAttribArray(vertexPosition);
  75. gl.vertexAttribPointer(vertexPosition, 2, gl.FLOAT, false, 0, 0);
  76. const canvasSizeUniform = gl.getUniformLocation(program, "canvasSize");
  77. gl.uniform2f(canvasSizeUniform, canvas.width, canvas.height);
  78. gl.drawArrays(gl.TRIANGLE_STRIP, 0, segitiga1.length);
  79.  
  80.  
  81. const segitiga2 = [
  82. [0, 0.5],
  83. [1, 0],
  84. [0, 0],
  85. ];
  86. const vertexData1 = new Float32Array(segitiga2.flat());
  87. gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());
  88. gl.bufferData(gl.ARRAY_BUFFER, vertexData1, gl.STATIC_DRAW);
  89. const vertexPosition1 = gl.getAttribLocation(program, "vertexPosition");
  90. gl.enableVertexAttribArray(vertexPosition1);
  91. gl.vertexAttribPointer(vertexPosition1, 2, gl.FLOAT, false, 0, 0);
  92. const canvasSizeUniform1 = gl.getUniformLocation(program, "canvasSize");
  93. gl.uniform2f(canvasSizeUniform1, canvas.width, canvas.height);
  94. gl.drawArrays(gl.TRIANGLE_STRIP, 0, segitiga2.length);
  95. </script>
  96. </body>
  97. </html>
  98.  
Advertisement
Add Comment
Please, Sign In to add comment