"use strict"; // teapot subdvision // vertices.js and patches.js read in first // vertex data in vertices.js // provides array vertices of 306 vec3's // numTeapotVertices set to 306 // patch data in patches.js // provies array indices of 32 arrays // of 16 vertex indices // numTeapotPatches set to 32 var render, canvas, gl; var NumTimesToSubdivide = 1; var index = 0; var points =[]; var divideCurve = function( c, r , l) { // divides c into left (l) and right ( r ) curve data var mid = mix(c[1], c[2], 0.5); l[0] = vec4(c[0]); l[1] = mix(c[0], c[1], 0.5 ); l[2] = mix(l[1], mid, 0.5 ); r[3] = vec4(c[3]); r[2] = mix(c[2], c[3], 0.5 ); r[1] = mix( mid, r[2], 0.5 ); r[0] = mix(l[2], r[1], 0.5 ); l[3] = vec4(r[0]); return; } //---------------------------------------------------------------------------- var drawPatch = function(p) { // Draw the quad (as two triangles) bounded by the corners of the // Bezier patch points.push(p[0][0]); points.push(p[0][3]); points.push(p[3][3]); points.push(p[0][0]); points.push(p[3][3]); points.push(p[3][0]); index+=6; return; } //---------------------------------------------------------------------------- var dividePatch = function (p, count ) { //console.log(count, p[0], p[1], p[2], p[3]) //for(var i =0; i<4; i++) console.log(count, i, p[i][0], p[i][1], p[i][2], p[i][3]) if ( count > 0 ) { var a = patch(); var b = patch(); var t = patch(); var q = patch(); var r = patch(); var s = patch(); // subdivide curves in u direction, transpose results, divide // in u direction again (equivalent to subdivision in v) for ( var k = 0; k < 4; ++k ) { divideCurve( p[k], a[k], b[k] ); } a = transpose(a); b = transpose(b); for ( var k = 0; k < 4; ++k ) { divideCurve( a[k], q[k], r[k] ); divideCurve( b[k], s[k], t[k] ); } // recursive division of 4 resulting patches dividePatch( q, count - 1 ); dividePatch( r, count - 1 ); dividePatch( s, count - 1 ); dividePatch( t, count - 1 ); } else { drawPatch( p ); } return; } // onload = function init() { canvas = document.getElementById( "gl-canvas" ); gl = canvas.getContext('webgl2'); if (!gl) { alert( "WebGL 2.0 isn't available" ); } gl.viewport( 0, 0, canvas.width, canvas.height ); gl.clearColor( 1.0, 1.0, 1.0, 1.0 ); var patch1 = new Array(numTeapotPatches); for(var i=0; i