Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. //Step 1
  2. "use strict";
  3.  
  4. var vertexShaderSource = `#version 300 es
  5.  
  6. //an attribute is an input (in) to a vertex shader.
  7. //It will receive data from a buffer
  8.  
  9. in vec4 a_position;
  10.  
  11. // all shaders have a main function
  12. void main()
  13. {
  14. // gl_Position is a special variable a vertex shader
  15. // is responsible for setting
  16. gl_Position = a_position;
  17. }
  18. `;
  19.  
  20. //Step 2
  21. var fragmentShaderSource = `#version 300 es
  22. // fragment shaders don't have a default precision so we need
  23. // to pick one. mediump is a good default. It means "medium precision"
  24. precision mediump float;
  25.  
  26. // we need to declare an output for the fragment shader
  27. out vec4 outColor;
  28.  
  29. void main() {
  30. // Just set the output to a constant redish-purple
  31. outColor = vec4(1, 0, 0.5, 1);
  32. }
  33. `;
  34.  
  35.  
  36. //step 3
  37. function createShader(gl, type, source) {
  38. var shader = gl.createShader(type);
  39. gl.shaderSource(shader, source);
  40. gl.compileShader(shader);
  41.  
  42. var success = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
  43. if (success) {
  44. return shader;
  45. }
  46.  
  47. console.log(gl.getShaderInfoLog(shader)); // eslint-disable-line
  48. gl.deleteShader(shader);
  49. return undefined;
  50. }
  51.  
  52. //step 4
  53. function createProgram(gl, vertexShader, fragmentShader) {
  54. var program = gl.createProgram();
  55. gl.attachShader(program, vertexShader);
  56. gl.attachShader(program, fragmentShader);
  57. gl.linkProgram(program);
  58. var success = gl.getProgramParameter(program, gl.LINK_STATUS);
  59.  
  60. if (success) {
  61. return program;
  62. }
  63.  
  64. console.log(gl.getProgramInfoLog(program)); // eslint-disable-line
  65. gl.deleteProgram(program);
  66. return undefined;
  67. }
  68.  
  69.  
  70. //step 5
  71. function main() {
  72.  
  73. var canvas = document.getElementById("c");
  74. var gl = canvas.getContext("webgl2");
  75. if (!gl) {
  76. return;
  77. }
  78.  
  79. // create GLSL shaders, upload the GLSL source, compile the shaders
  80. var vertexShader = createShader(gl, gl.VERTEX_SHADER, vertexShaderSource);
  81. var fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fragmentShaderSource);
  82.  
  83. // Link the two shaders into a program
  84. var program = createProgram(gl, vertexShader, fragmentShader);
  85.  
  86. // look up where the vertex data needs to go.
  87. var positionAttributeLocation = gl.getAttribLocation(program, "a_position");
  88.  
  89. // Create a buffer and put three 2d clip space points in it
  90. var positionBuffer = gl.createBuffer();
  91.  
  92. // Bind it to ARRAY_BUFFER (think of it as ARRAY_BUFFER = positionBuffer)
  93. gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
  94.  
  95. var positions = [
  96. 0, 0,
  97. 0, 0.5,
  98. 0.7, 0,
  99. ];
  100.  
  101. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);
  102.  
  103.  
  104. // Create a vertex array object (attribute state)
  105. var vao = gl.createVertexArray();
  106.  
  107. // and make it the one we're currently working with
  108. gl.bindVertexArray(vao);
  109.  
  110. // Turn on the attribute
  111. gl.enableVertexAttribArray(positionAttributeLocation);
  112.  
  113. // Tell the attribute how to get data out of positionBuffer (ARRAY_BUFFER)
  114. var size = 2; // 2 components per iteration
  115. var type = gl.FLOAT; // the data is 32bit floats
  116. var normalize = false; // don't normalize the data
  117. var stride = 0; // 0 = move forward size * sizeof(type) each iteration to get the next position
  118. var offset = 0; // start at the beginning of the buffer
  119. gl.vertexAttribPointer(
  120. positionAttributeLocation, size, type, normalize, stride, offset);
  121.  
  122.  
  123. webglUtils.resizeCanvasToDisplaySize(gl.canvas);
  124.  
  125. // Tell WebGL how to convert from clip space to pixels
  126. gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
  127.  
  128. // Clear the canvas
  129. gl.clearColor(0, 0, 0, 0);
  130. gl.clear(gl.COLOR_BUFFER_BIT);
  131.  
  132. gl.useProgram(program);
  133.  
  134. // Bind the attribute/buffer set we want.
  135. gl.bindVertexArray(vao);
  136.  
  137. // draw
  138. var primitiveType = gl.TRIANGLES;
  139. var offset = 0;
  140. var count = 3;
  141. gl.drawArrays(primitiveType, offset, count);
  142. }
  143.  
  144. main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement