Facebook
From Ungracious Monkey, 4 Years ago, written in JavaScript.
Embed
Download Paste or View Raw
Hits: 137
  1.  
  2. main();
  3.  
  4. function main() {
  5.   const canvas = document.querySelector('#glcanvas');
  6.   const gl = canvas.getContext('webgl');
  7.  
  8.  
  9.   if (!gl) {
  10.     alert('Unable to initialize WebGL. Your browser or machine may not support it.');
  11.     return;
  12.   }
  13.  
  14.  
  15.   const shaderProgramFactory = new ShaderProgramFactory(gl);
  16.   const shaderProgram = shaderProgramFactory.createShaderProgram();
  17.  
  18.   // Collect all the info needed to use the shader program.
  19.   // Look up which attributes our shader program is using
  20.   // for aVertexPosition, aVertexNormal, aTextureCoord,
  21.   // and look up uniform locations.
  22.   const programInfo = {
  23.     program: shaderProgram,
  24.     attribLocations: {
  25.       vertexPosition: gl.getAttribLocation(shaderProgram, 'aVertexPosition'),
  26.       vertexNormal: gl.getAttribLocation(shaderProgram, 'aVertexNormal'),
  27.       textureCoord: gl.getAttribLocation(shaderProgram, 'aTextureCoord'),
  28.     },
  29.     uniformLocations: {
  30.       projectionMatrix: gl.getUniformLocation(shaderProgram, 'uProjectionMatrix'),
  31.       modelViewMatrix: gl.getUniformLocation(shaderProgram, 'uModelViewMatrix'),
  32.       normalMatrix: gl.getUniformLocation(shaderProgram, 'uNormalMatrix'),
  33.       uSampler: gl.getUniformLocation(shaderProgram, 'uSampler'),
  34.     },
  35.   };
  36.  
  37.  
  38.   const cubeBuffer = new CubeBuffer(gl);
  39.   const buffers = cubeBuffer.startBuffer();
  40.  
  41.   const imageLoader = new ImageLoader(gl);
  42.   const texture = imageLoader.loadImageIntoTexture( 'cubetexture.png');
  43.  
  44.   var then = 0;
  45.  
  46.   // Draw the scene repeatedly
  47.   function render(now) {
  48.     now *= 0.001;  // convert to seconds
  49.     const deltaTime = now - then;
  50.     then = now;
  51.  
  52.     drawScene(gl, programInfo, buffers, texture, deltaTime);
  53.  
  54.     requestAnimationFrame(render);
  55.   }
  56.   requestAnimationFrame(render);
  57. }
  58.  
  59.  
  60.