1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| function initSphere() { canvas = document.getElementById("gl-canvas");
gl = canvas.getContext("webgl2"); if (!gl) { alert("WebGL isn't available"); }
gl.viewport(0, 0, canvas.width, canvas.width); gl.clearColor(1.0, 1.0, 1.0, 1.0);
var program = initShaders(gl, "vertex-shader", "fragment-shader"); gl.useProgram(program);
vBuffer = gl.createBuffer(); nBuffer = gl.createBuffer();
divideTetra(va, vb, vc, vd, numOfSubdivides); gl.bindBuffer(gl.ARRAY_BUFFER, vBuffer); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(points), gl.STATIC_DRAW);
vPosition = gl.getAttribLocation(program, "vPosition"); gl.vertexAttribPointer(vPosition, 4, gl.FLOAT, false, 0, 0); gl.enableVertexAttribArray(vPosition);
gl.bindBuffer(gl.ARRAY_BUFFER, nBuffer); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(normals), gl.STATIC_DRAW);
vNormal = gl.getAttribLocation(program, "vNormal"); gl.vertexAttribPointer(vNormal, 3, gl.FLOAT, false, 0, 0); gl.enableVertexAttribArray(vNormal);
modelViewMatrixLoc = gl.getUniformLocation(program, "modelViewMatrix"); projectionMatrixLoc = gl.getUniformLocation(program, "projectionMatrix"); var normalMatrixLoc = gl.getUniformLocation(program, "normalMatrix");
var ambientProduct = vec4.create(); vec4.multiply(ambientProduct, lightAmbient, materialAmbient); var diffuseProduct = vec4.create(); vec4.multiply(diffuseProduct, lightDiffuse, materialDiffuse); var specularProduct = vec4.create(); vec4.multiply(specularProduct, lightSpecular, materialSpecular);
gl.uniform4fv(gl.getUniformLocation(program, "ambientProduct"), ambientProduct); gl.uniform4fv(gl.getUniformLocation(program, "diffuseProduct"), diffuseProduct); gl.uniform4fv(gl.getUniformLocation(program, "specularProduct"), specularProduct); gl.uniform4fv(gl.getUniformLocation(program, "lightPosition"), lightPosition); gl.uniform1f(gl.getUniformLocation(program, "shininess"), materialShininess);
modelViewMatrix = mat4.create(); projectionMatrix = mat4.create();
document.onkeydown = handleKeyDown; document.onkeyup = handleKeyUp;
render(); }
|