Advertisement
Guest User

Untitled

a guest
Feb 6th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var cubeRotation = 0.0;
  2.  
  3. main();
  4.  
  5.  
  6. function main() {
  7.   const canvas = document.querySelector('#glcanvas');
  8.   const gl = canvas.getContext('webgl');
  9.  
  10.   // If we don't have a GL context, give up now
  11.  
  12.   if (!gl) {
  13.     alert('Unable to initialize WebGL. Your browser or machine may not support it.');
  14.     return;
  15.   }
  16.  
  17.   // Vertex shader program
  18.  
  19.   const vsSource = `
  20.     attribute vec4 aVertexPosition;
  21.     attribute vec4 aVertexColor;
  22.     uniform mat4 uModelViewMatrix;
  23.     uniform mat4 uProjectionMatrix;
  24.     varying lowp vec4 vColor;
  25.     void main(void) {
  26.       gl_Position = uProjectionMatrix * uModelViewMatrix * aVertexPosition;
  27.       vColor = aVertexColor;
  28.     }
  29.   `;
  30.  
  31.   // Fragment shader program
  32.  
  33.   const fsSource = `
  34.     varying lowp vec4 vColor;
  35.     void main(void) {
  36.       gl_FragColor = vColor;
  37.     }
  38.   `;
  39.  
  40.   // Initialize a shader program; this is where all the lighting
  41.   // for the vertices and so forth is established.
  42.   const shaderProgram = initShaderProgram(gl, vsSource, fsSource);
  43.  
  44.   // Collect all the info needed to use the shader program.
  45.   // Look up which attributes our shader program is using
  46.   // for aVertexPosition, aVevrtexColor and also
  47.   // look up uniform locations.
  48.   const programInfo = {
  49.     program: shaderProgram,
  50.     attribLocations: {
  51.       vertexPosition: gl.getAttribLocation(shaderProgram, 'aVertexPosition'),
  52.       vertexColor: gl.getAttribLocation(shaderProgram, 'aVertexColor'),
  53.     },
  54.     uniformLocations: {
  55.       projectionMatrix: gl.getUniformLocation(shaderProgram, 'uProjectionMatrix'),
  56.       modelViewMatrix: gl.getUniformLocation(shaderProgram, 'uModelViewMatrix'),
  57.     },
  58.   };
  59.  
  60.   // Here's where we call the routine that builds all the
  61.   // objects we'll be drawing.
  62.   const buffers = initBuffers(gl);
  63.  
  64.   var then = 0;
  65.  
  66.   // Draw the scene repeatedly
  67.   function render(now) {
  68.     now *= 0.001;  // convert to seconds
  69.     const deltaTime = now - then;
  70.     then = now;
  71.  
  72.     drawScene(gl, programInfo, buffers, deltaTime);
  73.  
  74.     requestAnimationFrame(render);
  75.   }
  76.   requestAnimationFrame(render);
  77. }
  78.     var mvMatrix = mat4.create();
  79.     var mvMatrixStack = [];
  80.     var pMatrix = mat4.create();
  81.  
  82.     function mvPushMatrix() {
  83.         var copy = mat4.create();
  84.         mat4.set(mvMatrix, copy);
  85.         mvMatrixStack.push(copy);
  86.     }
  87.  
  88.     function mvPopMatrix() {
  89.         if (mvMatrixStack.length == 0) {
  90.             throw "Invalid popMatrix!";
  91.         }
  92.         mvMatrix = mvMatrixStack.pop();
  93.     }
  94.  
  95.     function setMatrixUniforms() {
  96.         gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, false, pMatrix);
  97.         gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, mvMatrix);
  98.     }
  99.  
  100.     function degToRad(degrees) {
  101.         return degrees * Math.PI / 180;
  102.     }
  103. //
  104. // initBuffers
  105. //
  106. // Initialize the buffers we'll need. For this demo, we just
  107. // have one object -- a simple three-dimensional cube.
  108. //
  109. function initBuffers(gl) {
  110.  
  111.   //Head Cube
  112.   const headPositionBuffer = gl.createBuffer();
  113.   gl.bindBuffer(gl.ARRAY_BUFFER, headPositionBuffer);
  114.  
  115.   const positions = [
  116.     // Front face
  117.     -1.0, -1.0,  1.0,
  118.      1.0, -1.0,  1.0,
  119.      1.0,  1.0,  1.0,
  120.     -1.0,  1.0,  1.0,
  121.  
  122.     // Back face
  123.     -1.0, -1.0, -1.0,
  124.     -1.0,  1.0, -1.0,
  125.      1.0,  1.0, -1.0,
  126.      1.0, -1.0, -1.0,
  127.  
  128.     // Top face
  129.     -1.0,  1.0, -1.0,
  130.     -1.0,  1.0,  1.0,
  131.      1.0,  1.0,  1.0,
  132.      1.0,  1.0, -1.0,
  133.  
  134.     // Bottom face
  135.     -1.0, -1.0, -1.0,
  136.      1.0, -1.0, -1.0,
  137.      1.0, -1.0,  1.0,
  138.     -1.0, -1.0,  1.0,
  139.  
  140.     // Right face
  141.      1.0, -1.0, -1.0,
  142.      1.0,  1.0, -1.0,
  143.      1.0,  1.0,  1.0,
  144.      1.0, -1.0,  1.0,
  145.  
  146.     // Left face
  147.     -1.0, -1.0, -1.0,
  148.     -1.0, -1.0,  1.0,
  149.     -1.0,  1.0,  1.0,
  150.     -1.0,  1.0, -1.0,
  151.   ];
  152.  
  153.   gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);
  154.   const headFaceColors = [
  155.     [1.0,  1.0,  1.0,  1.0],    // Front face: white
  156.     [1.0,  0.0,  0.0,  1.0],    // Back face: red
  157.     [0.0,  1.0,  0.0,  1.0],    // Top face: green
  158.     [0.0,  0.0,  1.0,  1.0],    // Bottom face: blue
  159.     [1.0,  1.0,  0.0,  1.0],    // Right face: yellow
  160.     [1.0,  0.0,  1.0,  1.0],    // Left face: purple
  161.   ];
  162.  
  163.   colors = [];
  164.  
  165.   for (var j = 0; j < headFaceColors.length; ++j) {
  166.     const c = headFaceColors[j];
  167.     colors = colors.concat(c, c, c, c);
  168.   }
  169.  
  170.   const headColorBuffer = gl.createBuffer();
  171.   headColorBuffer.itemSize = 4;
  172.   headColorBuffer.numItems = 24;
  173.   gl.bindBuffer(gl.ARRAY_BUFFER, headColorBuffer);
  174.   gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);
  175.   const headIndexBuffer = gl.createBuffer();
  176.   gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, headIndexBuffer);
  177.   var headIndices = [
  178.     0,  1,  2,      0,  2,  3,    // front
  179.     4,  5,  6,      4,  6,  7,    // back
  180.     8,  9,  10,     8,  10, 11,   // top
  181.     12, 13, 14,     12, 14, 15,   // bottom
  182.     16, 17, 18,     16, 18, 19,   // right
  183.     20, 21, 22,     20, 22, 23,   // left
  184.   ];
  185.   gl.bufferData(gl.ELEMENT_ARRAY_BUFFER,
  186.       new Uint16Array(headIndices), gl.STATIC_DRAW);
  187.   indexBuffer.itemSize = 1;
  188.   indexBuffer.numItems = 36;
  189.  
  190.      
  191.   return;
  192. }
  193.  
  194.     function headCube()
  195.     {  
  196.         mat4.translate(mvMatrix, [-0.3, -0.2, -0.5]);          // CUBE1 CONTROL POSITION
  197.         mvPushMatrix();
  198.  
  199.     }
  200.  
  201.      function headCubeBuffer(){
  202.         gl.bindBuffer(gl.ARRAY_BUFFER, headPositionBuffer);
  203.         gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, headPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
  204.         gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexColorBuffer);
  205.         gl.vertexAttribPointer(shaderProgram.vertexColorAttribute, headColorBuffer.itemSize, gl.FLOAT, false, 0, 0);
  206.         gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, headIndexBuffer);
  207.         setMatrixUniforms();
  208.         gl.drawElements(gl.TRIANGLES, headIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
  209.         mvPopMatrix();
  210.     }
  211. //
  212. // Draw the scene.
  213. //
  214. function drawScene(gl, programInfo, buffers, deltaTime) {
  215.   gl.clearColor(0.0, 0.0, 0.0, 1.0);  // Clear to black, fully opaque
  216.   gl.clearDepth(1.0);                 // Clear everything
  217.   gl.enable(gl.DEPTH_TEST);           // Enable depth testing
  218.   gl.depthFunc(gl.LEQUAL);            // Near things obscure far things
  219.  
  220.   // Clear the canvas before we start drawing on it.
  221.  
  222.   gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
  223.  
  224.   // Create a perspective matrix, a special matrix that is
  225.   // used to simulate the distortion of perspective in a camera.
  226.   // Our field of view is 45 degrees, with a width/height
  227.   // ratio that matches the display size of the canvas
  228.   // and we only want to see objects between 0.1 units
  229.   // and 100 units away from the camera.
  230.  
  231.   const fieldOfView = 45 * Math.PI / 180;   // in radians
  232.   const aspect = gl.canvas.clientWidth / gl.canvas.clientHeight;
  233.   const zNear = 0.1;
  234.   const zFar = 100.0;
  235.   const projectionMatrix = mat4.create();
  236.  
  237.   // note: glmatrix.js always has the first argument
  238.   // as the destination to receive the result.
  239.   mat4.perspective(projectionMatrix,
  240.                    fieldOfView,
  241.                    aspect,
  242.                    zNear,
  243.                    zFar);
  244.   mat4.identity(mvMatrix);
  245.   // Set the drawing position to the "identity" point, which is
  246.   // the center of the scene.
  247.   const modelViewMatrix = mat4.create();
  248.  
  249.   // Now move the drawing position a bit to where we want to
  250.   // start drawing the square.
  251.  
  252.   mat4.translate(modelViewMatrix,     // destination matrix
  253.                  modelViewMatrix,     // matrix to translate
  254.                  [-0.0, 0.0, -6.0]);  // amount to translate
  255.   mat4.rotate(modelViewMatrix,  // destination matrix
  256.               modelViewMatrix,  // matrix to rotate
  257.               cubeRotation,     // amount to rotate in radians
  258.               [0, 0, 0]);       // axis to rotate around (Z)
  259.   mat4.rotate(modelViewMatrix,  // destination matrix
  260.               modelViewMatrix,  // matrix to rotate
  261.               cubeRotation * .7,// amount to rotate in radians
  262.               [0, 1, 0]);       // axis to rotate around (X)
  263.  
  264.   // Tell WebGL how to pull out the positions from the position
  265.   // buffer into the vertexPosition attribute
  266.   {
  267.     const numComponents = 3;
  268.     const type = gl.FLOAT;
  269.     const normalize = false;
  270.     const stride = 0;
  271.     const offset = 0;
  272.     gl.bindBuffer(gl.ARRAY_BUFFER, buffers.position);
  273.     gl.vertexAttribPointer(
  274.         programInfo.attribLocations.vertexPosition,
  275.         numComponents,
  276.         type,
  277.         normalize,
  278.         stride,
  279.         offset);
  280.     gl.enableVertexAttribArray(
  281.         programInfo.attribLocations.vertexPosition);
  282.   }
  283.  
  284.   // Tell WebGL how to pull out the colors from the color buffer
  285.   // into the vertexColor attribute.
  286.   {
  287.     const numComponents = 4;
  288.     const type = gl.FLOAT;
  289.     const normalize = false;
  290.     const stride = 0;
  291.     const offset = 0;
  292.     gl.bindBuffer(gl.ARRAY_BUFFER, buffers.color);
  293.     gl.vertexAttribPointer(
  294.         programInfo.attribLocations.vertexColor,
  295.         numComponents,
  296.         type,
  297.         normalize,
  298.         stride,
  299.         offset);
  300.     gl.enableVertexAttribArray(
  301.         programInfo.attribLocations.vertexColor);
  302.   }
  303.  
  304.   // Tell WebGL which indices to use to index the vertices
  305.   gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffers.indices);
  306.  
  307.   // Tell WebGL to use our program when drawing
  308.  
  309.   gl.useProgram(programInfo.program);
  310.  
  311.   // Set the shader uniforms
  312.  
  313.   gl.uniformMatrix4fv(
  314.       programInfo.uniformLocations.projectionMatrix,
  315.       false,
  316.       projectionMatrix);
  317.   gl.uniformMatrix4fv(
  318.       programInfo.uniformLocations.modelViewMatrix,
  319.       false,
  320.       modelViewMatrix);
  321.  
  322.   {
  323.     const vertexCount = 36;
  324.     const type = gl.UNSIGNED_SHORT;
  325.     const offset = 0;
  326.     gl.drawElements(gl.TRIANGLES, vertexCount, type, offset);
  327.   }
  328.  
  329.   // Update the rotation for the next draw
  330.  
  331.   //cubeRotation += deltaTime;
  332. }
  333.  
  334. //
  335. // Initialize a shader program, so WebGL knows how to draw our data
  336. //
  337. function initShaderProgram(gl, vsSource, fsSource) {
  338.   const vertexShader = loadShader(gl, gl.VERTEX_SHADER, vsSource);
  339.   const fragmentShader = loadShader(gl, gl.FRAGMENT_SHADER, fsSource);
  340.  
  341.   // Create the shader program
  342.  
  343.   const shaderProgram = gl.createProgram();
  344.   gl.attachShader(shaderProgram, vertexShader);
  345.   gl.attachShader(shaderProgram, fragmentShader);
  346.   gl.linkProgram(shaderProgram);
  347.  
  348.   // If creating the shader program failed, alert
  349.  
  350.   if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
  351.     alert('Unable to initialize the shader program: ' + gl.getProgramInfoLog(shaderProgram));
  352.     return null;
  353.   }
  354.  
  355.   return shaderProgram;
  356. }
  357.  
  358. //
  359. // creates a shader of the given type, uploads the source and
  360. // compiles it.
  361. //
  362. function loadShader(gl, type, source) {
  363.   const shader = gl.createShader(type);
  364.  
  365.   // Send the source to the shader object
  366.  
  367.   gl.shaderSource(shader, source);
  368.  
  369.   // Compile the shader program
  370.  
  371.   gl.compileShader(shader);
  372.  
  373.   // See if it compiled successfully
  374.  
  375.   if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
  376.     alert('An error occurred compiling the shaders: ' + gl.getShaderInfoLog(shader));
  377.     gl.deleteShader(shader);
  378.     return null;
  379.   }
  380.  
  381.   return shader;
  382. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement