Advertisement
drockw2

init

Nov 20th, 2014
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function init() {
  2.    
  3.     canvas = document.getElementById('webgl');
  4.     gl     = getWebGLContext(canvas, false);
  5.    
  6.     canvas.onmousedown= onmousedown;
  7.     canvas.onmouseup= onmouseup;
  8.     canvas.onmousemove= onmousemove;
  9.    
  10.     // Initialize the program object and its uniforms. no error check
  11.  
  12.     initShaders(gl, document.getElementById('vertexShader').text,
  13.                     document.getElementById('fragmentShader').text);
  14.    
  15.   // Link the shader attributes to the vertex buffer object.
  16.  
  17.     var projectionMatrixLocation = gl.getUniformLocation(gl.program, 'projectionMatrix');
  18.     var modelMatrixLocation = gl.getUniformLocation(gl.program, 'modelMatrix');
  19.     var lightDirectionLocation = gl.getUniformLocation(gl.program, 'lightDirection');
  20.     var lightColorLocation = gl.getUniformLocation(gl.program, 'lightColor');
  21.     var objectColorLocation = gl.getUniformLocation(gl.program, 'objectColor');
  22.  
  23.    
  24.  
  25.  
  26.  
  27.     i0=0;
  28.     i1=1;
  29.     i2=2;
  30.     var n;
  31.     normals = [];
  32.    
  33.     for (n = 0; n < vertices.length; n++)
  34.         normals.push([0,0,0]);
  35.  
  36.    
  37.     for (i= 0; i<triangles.length; i++) {
  38.         triangle = triangles[i];
  39.         a = normalize(sub(vertices[triangle[i1]] , vertices[triangle[i0]]));
  40.         b = normalize(sub(vertices[triangle[i2]] , vertices[triangle[i0]]));
  41.  
  42.         n = normalize(mult(a,b));
  43.        
  44.         normals[i0] = add(normals[i0], n);
  45.         normals[i1] = add(normals[i1], n);
  46.         normals[i2] = add(normals[i2], n);
  47.        
  48.        
  49.     }
  50.    
  51.     for (n= 0; n < normals.length; n++) {
  52.         normals[n] = normalize(normals[n]);
  53.     }
  54.    
  55.    
  56.    
  57.    
  58.    
  59.     var vertexPositionLocation = gl.getAttribLocation(gl.program, 'vertexPosition');
  60.     gl.enableVertexAttribArray(vertexPositionLocation);
  61.     var vertexNormalLocation = gl.getAttribLocation(gl.program, 'vertexNormal');
  62.     gl.enableVertexAttribArray(vertexNormalLocation);
  63.    
  64.     var positionArray;
  65.     var normalArray;
  66.    
  67.    
  68.     positionArray = new Float32Array(flatten(vertices));
  69.     normalArray = new Float32Array(flatten(normals));
  70.     triangleArray = new Uint16Array (flatten(triangles));
  71.    
  72.     // Initialize vertex and index buffer objects.
  73.  
  74.     positionBuffer = gl.createBuffer();
  75.     normalBuffer  = gl.createBuffer();
  76.     triangleBuffer   = gl.createBuffer();
  77.  
  78.     gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
  79.     gl.bufferData(gl.ARRAY_BUFFER, positionArray, gl.STATIC_DRAW);
  80.  
  81.     gl.bindBuffer(gl.ARRAY_BUFFER, normalBuffer);
  82.     gl.bufferData(gl.ARRAY_BUFFER, normalArray , gl.STATIC_DRAW);
  83.  
  84.     gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, triangleBuffer);
  85.     gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, triangleArray, gl.STATIC_DRAW);
  86.    
  87.      // Set up to render.
  88.  
  89.     gl.clearColor(1.0, 1.0, 0.8, 1.0);
  90.     gl.enable(gl.DEPTH_TEST);
  91.    
  92.     rotateX = 0.0;
  93.     rotateY = 0.0;
  94.     translateZ = 3.0;
  95.    
  96.     dragging = false;
  97.    
  98.     draw();
  99.  
  100.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement