Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function init() {
- canvas = document.getElementById('webgl');
- gl = getWebGLContext(canvas, false);
- canvas.onmousedown= onmousedown;
- canvas.onmouseup= onmouseup;
- canvas.onmousemove= onmousemove;
- // Initialize the program object and its uniforms. no error check
- initShaders(gl, document.getElementById('vertexShader').text,
- document.getElementById('fragmentShader').text);
- // Link the shader attributes to the vertex buffer object.
- var projectionMatrixLocation = gl.getUniformLocation(gl.program, 'projectionMatrix');
- var modelMatrixLocation = gl.getUniformLocation(gl.program, 'modelMatrix');
- var lightDirectionLocation = gl.getUniformLocation(gl.program, 'lightDirection');
- var lightColorLocation = gl.getUniformLocation(gl.program, 'lightColor');
- var objectColorLocation = gl.getUniformLocation(gl.program, 'objectColor');
- i0=0;
- i1=1;
- i2=2;
- var n;
- normals = [];
- for (n = 0; n < vertices.length; n++)
- normals.push([0,0,0]);
- for (i= 0; i<triangles.length; i++) {
- triangle = triangles[i];
- a = normalize(sub(vertices[triangle[i1]] , vertices[triangle[i0]]));
- b = normalize(sub(vertices[triangle[i2]] , vertices[triangle[i0]]));
- n = normalize(mult(a,b));
- normals[i0] = add(normals[i0], n);
- normals[i1] = add(normals[i1], n);
- normals[i2] = add(normals[i2], n);
- }
- for (n= 0; n < normals.length; n++) {
- normals[n] = normalize(normals[n]);
- }
- var vertexPositionLocation = gl.getAttribLocation(gl.program, 'vertexPosition');
- gl.enableVertexAttribArray(vertexPositionLocation);
- var vertexNormalLocation = gl.getAttribLocation(gl.program, 'vertexNormal');
- gl.enableVertexAttribArray(vertexNormalLocation);
- var positionArray;
- var normalArray;
- positionArray = new Float32Array(flatten(vertices));
- normalArray = new Float32Array(flatten(normals));
- triangleArray = new Uint16Array (flatten(triangles));
- // Initialize vertex and index buffer objects.
- positionBuffer = gl.createBuffer();
- normalBuffer = gl.createBuffer();
- triangleBuffer = gl.createBuffer();
- gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
- gl.bufferData(gl.ARRAY_BUFFER, positionArray, gl.STATIC_DRAW);
- gl.bindBuffer(gl.ARRAY_BUFFER, normalBuffer);
- gl.bufferData(gl.ARRAY_BUFFER, normalArray , gl.STATIC_DRAW);
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, triangleBuffer);
- gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, triangleArray, gl.STATIC_DRAW);
- // Set up to render.
- gl.clearColor(1.0, 1.0, 0.8, 1.0);
- gl.enable(gl.DEPTH_TEST);
- rotateX = 0.0;
- rotateY = 0.0;
- translateZ = 3.0;
- dragging = false;
- draw();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement