Initial transfer of larger repo from git. IF full history and branches are needed must go to github at https://github.com/DulcisL/CCUCoding.git
This commit is contained in:
commit
c09ce40be0
4048 files changed
+603256
No files matched your search
@@ -0,0 +1,242 @@
|
||||
"use strict";
|
||||
|
||||
var canvas;
|
||||
var gl;
|
||||
|
||||
var numTimesToSubdivide = 3;
|
||||
|
||||
var index = 0;
|
||||
|
||||
var positionsArray = [];
|
||||
var normalsArray = [];
|
||||
|
||||
|
||||
var near = -10;
|
||||
var far = 10;
|
||||
var radius = 1.5;
|
||||
var theta = 0.0;
|
||||
var phi = 0.0;
|
||||
var dr = 5.0 * Math.PI/180.0;
|
||||
|
||||
var left = -3.0;
|
||||
var right = 3.0;
|
||||
var ytop =3.0;
|
||||
var bottom = -3.0;
|
||||
|
||||
var va = vec4(0.0, 0.0, -1.0,1);
|
||||
var vb = vec4(0.0, 0.942809, 0.333333, 1);
|
||||
var vc = vec4(-0.816497, -0.471405, 0.333333, 1);
|
||||
var vd = vec4(0.816497, -0.471405, 0.333333,1);
|
||||
|
||||
var lightPosition = vec4(1.0, 1.0, 1.0, 0.0 );
|
||||
var lightAmbient = vec4(0.2, 0.2, 0.2, 1.0 );
|
||||
var lightDiffuse = vec4( 1.0, 1.0, 1.0, 1.0 );
|
||||
var lightSpecular = vec4( 1.0, 1.0, 1.0, 1.0 );
|
||||
|
||||
var materialAmbient = vec4( 1.0, 0.0, 1.0, 1.0 );
|
||||
var materialDiffuse = vec4( 1.0, 0.8, 0.0, 1.0 );
|
||||
var materialSpecular = vec4( 1.0, 1.0, 1.0, 1.0 );
|
||||
var materialShininess = 20.0;
|
||||
|
||||
var red = new Uint8Array([255, 0, 0, 255]);
|
||||
var green = new Uint8Array([0, 255, 0, 255]);
|
||||
var blue = new Uint8Array([0, 0, 255, 255]);
|
||||
var cyan = new Uint8Array([0, 255, 255, 255]);
|
||||
var magenta = new Uint8Array([255, 0, 255, 255]);
|
||||
var yellow = new Uint8Array([255, 255, 0, 255]);
|
||||
|
||||
var cubeMap;
|
||||
|
||||
var ambientColor, diffuseColor, specularColor;
|
||||
|
||||
var modelViewMatrix, projectionMatrix;
|
||||
var modelViewMatrixLoc, projectionMatrixLoc;
|
||||
|
||||
var nMatrix, nMatrixLoc;
|
||||
|
||||
var eye;
|
||||
var at = vec3(0.0, 0.0, 0.0);
|
||||
var up = vec3(0.0, 1.0, 0.0);
|
||||
|
||||
function configureCubeMap() {
|
||||
|
||||
cubeMap = gl.createTexture();
|
||||
|
||||
gl.bindTexture(gl.TEXTURE_CUBE_MAP, cubeMap);
|
||||
gl.texImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X ,0,gl.RGBA,
|
||||
1,1,0,gl.RGBA,gl.UNSIGNED_BYTE, red);
|
||||
gl.texImage2D(gl.TEXTURE_CUBE_MAP_NEGATIVE_X ,0,gl.RGBA,
|
||||
1,1,0,gl.RGBA,gl.UNSIGNED_BYTE, green);
|
||||
gl.texImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_Y ,0,gl.RGBA,
|
||||
1,1,0,gl.RGBA,gl.UNSIGNED_BYTE, blue);
|
||||
gl.texImage2D(gl.TEXTURE_CUBE_MAP_NEGATIVE_Y ,0,gl.RGBA,
|
||||
1,1,0,gl.RGBA,gl.UNSIGNED_BYTE, cyan);
|
||||
gl.texImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_Z ,0,gl.RGBA,
|
||||
1,1,0,gl.RGBA,gl.UNSIGNED_BYTE, yellow);
|
||||
gl.texImage2D(gl.TEXTURE_CUBE_MAP_NEGATIVE_Z ,0,gl.RGBA,
|
||||
1,1,0,gl.RGBA,gl.UNSIGNED_BYTE, magenta);
|
||||
|
||||
|
||||
gl.texParameteri(gl.TEXTURE_CUBE_MAP,gl.TEXTURE_MAG_FILTER,gl.NEAREST);
|
||||
gl.texParameteri(gl.TEXTURE_CUBE_MAP,gl.TEXTURE_MIN_FILTER,gl.NEAREST);
|
||||
}
|
||||
|
||||
|
||||
|
||||
function triangle(a, b, c) {
|
||||
|
||||
positionsArray.push(a);
|
||||
positionsArray.push(b);
|
||||
positionsArray.push(c);
|
||||
|
||||
// normals are vectors
|
||||
|
||||
normalsArray.push(vec3(a[0],a[1], a[2]));
|
||||
normalsArray.push(vec3(b[0],b[1], b[2]));
|
||||
normalsArray.push(vec3(c[0],c[1], c[2]));
|
||||
|
||||
index += 3;
|
||||
}
|
||||
|
||||
function divideTriangle(a, b, c, count) {
|
||||
if (count > 0) {
|
||||
|
||||
var ab = mix( a, b, 0.5);
|
||||
var ac = mix( a, c, 0.5);
|
||||
var bc = mix( b, c, 0.5);
|
||||
|
||||
ab = normalize(ab, true);
|
||||
ac = normalize(ac, true);
|
||||
bc = normalize(bc, true);
|
||||
|
||||
divideTriangle( a, ab, ac, count - 1 );
|
||||
divideTriangle( ab, b, bc, count - 1 );
|
||||
divideTriangle( bc, c, ac, count - 1 );
|
||||
divideTriangle( ab, bc, ac, count - 1 );
|
||||
}
|
||||
else {
|
||||
triangle( a, b, c );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function tetrahedron(a, b, c, d, n) {
|
||||
divideTriangle(a, b, c, n);
|
||||
divideTriangle(d, c, b, n);
|
||||
divideTriangle(a, d, b, n);
|
||||
divideTriangle(a, c, d, n);
|
||||
}
|
||||
|
||||
window.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 );
|
||||
|
||||
gl.enable(gl.DEPTH_TEST);
|
||||
|
||||
//
|
||||
// Load shaders and initialize attribute buffers
|
||||
//
|
||||
var program = initShaders( gl, "vertex-shader", "fragment-shader" );
|
||||
gl.useProgram( program );
|
||||
|
||||
|
||||
var ambientProduct = mult(lightAmbient, materialAmbient);
|
||||
var diffuseProduct = mult(lightDiffuse, materialDiffuse);
|
||||
var specularProduct = mult(lightSpecular, materialSpecular);
|
||||
|
||||
|
||||
tetrahedron(va, vb, vc, vd, numTimesToSubdivide);
|
||||
|
||||
var nBuffer = gl.createBuffer();
|
||||
gl.bindBuffer( gl.ARRAY_BUFFER, nBuffer);
|
||||
gl.bufferData( gl.ARRAY_BUFFER, flatten(normalsArray), gl.STATIC_DRAW );
|
||||
|
||||
var normalLoc = gl.getAttribLocation( program, "aNormal" );
|
||||
gl.vertexAttribPointer( normalLoc, 3, gl.FLOAT, false, 0, 0 );
|
||||
gl.enableVertexAttribArray( normalLoc);
|
||||
|
||||
var vBuffer = gl.createBuffer();
|
||||
gl.bindBuffer(gl.ARRAY_BUFFER, vBuffer);
|
||||
gl.bufferData(gl.ARRAY_BUFFER, flatten(positionsArray), gl.STATIC_DRAW);
|
||||
|
||||
var positionLoc = gl.getAttribLocation( program, "aPosition");
|
||||
gl.vertexAttribPointer(positionLoc, 4, gl.FLOAT, false, 0, 0);
|
||||
gl.enableVertexAttribArray(positionLoc);
|
||||
|
||||
modelViewMatrixLoc = gl.getUniformLocation( program, "uModelViewMatrix" );
|
||||
projectionMatrixLoc = gl.getUniformLocation( program, "uProjectionMatrix" );
|
||||
nMatrixLoc = gl.getUniformLocation( program, "uNormalMatrix" );
|
||||
|
||||
configureCubeMap();
|
||||
gl.activeTexture( gl.TEXTURE0 );
|
||||
gl.uniform1i(gl.getUniformLocation(program, "uTexMap"),0);
|
||||
|
||||
document.getElementById("Button0").onclick = function(){radius *= 2.0;};
|
||||
document.getElementById("Button1").onclick = function(){radius *= 0.5;};
|
||||
document.getElementById("Button2").onclick = function(){theta += dr;};
|
||||
document.getElementById("Button3").onclick = function(){theta -= dr;};
|
||||
document.getElementById("Button4").onclick = function(){phi += dr;};
|
||||
document.getElementById("Button5").onclick = function(){phi -= dr;};
|
||||
|
||||
document.getElementById("Button6").onclick = function(){
|
||||
numTimesToSubdivide++;
|
||||
index = 0;
|
||||
pointsArray = [];
|
||||
normalsArray = [];
|
||||
init();
|
||||
};
|
||||
document.getElementById("Button7").onclick = function(){
|
||||
if(numTimesToSubdivide) numTimesToSubdivide--;
|
||||
index = 0;
|
||||
positionsArray = [];
|
||||
normalsArray = [];
|
||||
init();
|
||||
};
|
||||
|
||||
|
||||
gl.uniform4fv( gl.getUniformLocation(program,
|
||||
"uAmbientProduct"),ambientProduct );
|
||||
gl.uniform4fv( gl.getUniformLocation(program,
|
||||
"uDiffuseProduct"),diffuseProduct );
|
||||
gl.uniform4fv( gl.getUniformLocation(program,
|
||||
"uSpecularProduct"),specularProduct );
|
||||
gl.uniform4fv( gl.getUniformLocation(program,
|
||||
"uLightPosition"),lightPosition) ;
|
||||
gl.uniform1f( gl.getUniformLocation(program,
|
||||
"uShininess"),materialShininess );
|
||||
|
||||
render();
|
||||
}
|
||||
|
||||
|
||||
function render() {
|
||||
|
||||
gl.clear( gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
|
||||
|
||||
eye = vec3(radius*Math.sin(theta)*Math.cos(phi),
|
||||
radius*Math.sin(theta)*Math.sin(phi), radius*Math.cos(theta));
|
||||
|
||||
modelViewMatrix = lookAt(eye, at , up);
|
||||
projectionMatrix = ortho(left, right, bottom, ytop, near, far);
|
||||
|
||||
// normal matrix only really need if there is nonuniform scaling
|
||||
// it's here for generality but since there is
|
||||
// no scaling in this example we could just use modelView matrix in shaders
|
||||
|
||||
nMatrix = normalMatrix(modelViewMatrix, true);
|
||||
|
||||
gl.uniformMatrix4fv(modelViewMatrixLoc, false, flatten(modelViewMatrix));
|
||||
gl.uniformMatrix4fv(projectionMatrixLoc, false, flatten(projectionMatrix));
|
||||
gl.uniformMatrix3fv(nMatrixLoc, false, flatten(nMatrix) );
|
||||
|
||||
for( var i=0; i<index; i+=3)
|
||||
gl.drawArrays(gl.TRIANGLES, i, 3);
|
||||
|
||||
requestAnimationFrame(render);
|
||||
}
|
||||
Reference in new issue
Block a user