Source: lib/teal_book/cuon-utils.js

  1. /**
  2. * @file
  3. *
  4. * Summary.
  5. * <p>This is a class for creating shaders.</p>
  6. *
  7. * @author Kanda and Matsuda
  8. * @copyright © 2012 Kanda and Matsuda
  9. * @since 25/09/2016
  10. * @see <a href="/cwdc/13-webgl/lib/teal_book/cuon-utils.js">source</a>
  11. */
  12. /**
  13. * Create a program object and make it current.
  14. * @param {WebGLRenderingContext} GL context.
  15. * @param {String} vshader a vertex shader program.
  16. * @param {String} fshader a fragment shader program.
  17. * @return {Boolean} true, if the program object was created and successfully made current.
  18. */
  19. function initShaders(gl, vshader, fshader) {
  20. var program = createProgram(gl, vshader, fshader);
  21. if (!program) {
  22. console.log("Failed to create program");
  23. return false;
  24. }
  25. gl.useProgram(program);
  26. gl.program = program;
  27. return true;
  28. }
  29. /**
  30. * Create the linked program object
  31. * @param {WebGLRenderingContext} gl GL context.
  32. * @param {String} vshader a vertex shader program.
  33. * @param {String} fshader a fragment shader program.
  34. * @return {WebGLProgram} created program object, or null if the creation has failed.
  35. * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/createProgram WebGLRenderingContext: createProgram() method}
  36. */
  37. function createProgram(gl, vshader, fshader) {
  38. // Create shader object
  39. var vertexShader = loadShader(gl, gl.VERTEX_SHADER, vshader);
  40. var fragmentShader = loadShader(gl, gl.FRAGMENT_SHADER, fshader);
  41. if (!vertexShader || !fragmentShader) {
  42. return null;
  43. }
  44. // Create a program object
  45. var program = gl.createProgram();
  46. if (!program) {
  47. return null;
  48. }
  49. // Attach the shader objects
  50. gl.attachShader(program, vertexShader);
  51. gl.attachShader(program, fragmentShader);
  52. // Link the program object
  53. gl.linkProgram(program);
  54. // Check the result of linking
  55. var linked = gl.getProgramParameter(program, gl.LINK_STATUS);
  56. if (!linked) {
  57. var error = gl.getProgramInfoLog(program);
  58. console.log("Failed to link program: " + error);
  59. gl.deleteProgram(program);
  60. gl.deleteShader(fragmentShader);
  61. gl.deleteShader(vertexShader);
  62. return null;
  63. }
  64. return program;
  65. }
  66. /**
  67. * Create a shader object
  68. * @param {WebGLRenderingContext} gl GL context.
  69. * @param {gl.VERTEX_SHADER | gl.FRAGMENT_SHADER} type the type of the shader object to be created,
  70. * @param {DOMString} source shader program.
  71. * @return {WebGLShader} created shader object, or null if the creation has failed.
  72. * @see {@link https://udn.realityripple.com/docs/Web/API/WebGLRenderingContext/shaderSource WebGLRenderingContext.shaderSource()}
  73. * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/createShader WebGLRenderingContext: createShader() method}
  74. */
  75. function loadShader(gl, type, source) {
  76. // Create shader object
  77. var shader = gl.createShader(type);
  78. if (shader == null) {
  79. console.log("unable to create shader");
  80. return null;
  81. }
  82. // Set the shader program
  83. gl.shaderSource(shader, source);
  84. // Compile the shader
  85. gl.compileShader(shader);
  86. // Check the result of compilation
  87. var compiled = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
  88. if (!compiled) {
  89. var error = gl.getShaderInfoLog(shader);
  90. console.log("Failed to compile shader: " + error);
  91. gl.deleteShader(shader);
  92. return null;
  93. }
  94. return shader;
  95. }
  96. /**
  97. * Initialize and get the rendering context for WebGL.
  98. * This will make any GL errors show up in your browser JavaScript console.
  99. * @deprecated since WebGL2
  100. * @param {HTMLCanvasElement} canvas &lt;canvas&gt; element.
  101. * @param {Boolean} opt_debug flag to initialize the context for debugging.
  102. * @return {WebGL2RenderingContext} the rendering context for WebGL.
  103. * @see {@link https://www.khronos.org/webgl/wiki/Debugging Debugging}
  104. * @see {@link http://learnwebgl.brown37.net/browser_environment/canvas_and_gl_context.html Getting the WebGL Context}
  105. */
  106. function getWebGLContext(canvas, opt_debug) {
  107. // Get the rendering context for WebGL
  108. var gl = WebGLUtils.setupWebGL(canvas);
  109. if (!gl) return null;
  110. // if opt_debug is explicitly false, create the context for debugging
  111. if (arguments.length < 2 || opt_debug) {
  112. gl = WebGLDebugUtils.makeDebugContext(gl);
  113. }
  114. return gl;
  115. }