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:
dolcel committed 2026-10-20 23:59:02 +00:00
commit c09ce40be0
4048 files changed
+603256

No files matched your search

+215
View File
@@ -0,0 +1,215 @@
"use strict";
var shadedSphereEyeSpace = function() {
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 top =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 ctm;
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 triangle(a, b, c) {
positionsArray.push(a);
positionsArray.push(b);
positionsArray.push(c);
// normals are vectors
normalsArray.push(vec4(a[0],a[1], a[2], 0.0));
normalsArray.push(vec4(b[0],b[1], b[2], 0.0));
normalsArray.push(vec4(c[0],c[1], c[2], 0.0));
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, 4, 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");
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;
positionsArray = [];
normalsArray = [];
init();
};
document.getElementById("Button7").onclick = function(){
if(numTimesToSubdivide) numTimesToSubdivide--;
index = 0;
positionsArray = [];
normalsArray = [];
init();
};
gl.uniform4fv(gl.getUniformLocation(program,
"uAmbientProduct"),flatten(ambientProduct));
gl.uniform4fv(gl.getUniformLocation(program,
"uDiffuseProduct"),flatten(diffuseProduct));
gl.uniform4fv(gl.getUniformLocation(program,
"uSpecularProduct"),flatten(specularProduct));
gl.uniform4fv(gl.getUniformLocation(program,
"uLightPosition"),flatten(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, top, 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);
}
}
shadedSphereEyeSpace();