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,321 @@
|
||||
/*
|
||||
Modify buttons to menus
|
||||
- menu 1: Subdivisions / R
|
||||
- menu 2: increase / decrease theta and phi
|
||||
- menu 3: move light L/R(x), move light up/down(y), move light Fwd / Back (z)
|
||||
Notes:
|
||||
light movement only on one axis at a time (begins movement from current position)
|
||||
increase light by 0.05 until +-3.0 then reverse direction
|
||||
initial position of light = (0.0, 0.0, 1.0)
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
var shadedSphere3 = 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(0.0, 0.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 lightPositionLoc
|
||||
|
||||
var nMatrix, nMatrixLoc;
|
||||
|
||||
var eye;
|
||||
var at = vec3(0.0, 0.0, 0.0);
|
||||
var up = vec3(0.0, 1.0, 0.0);
|
||||
|
||||
//Flags
|
||||
var direction = true;
|
||||
var lightAxis = 0;
|
||||
var maxTravel = 3.0;
|
||||
var lightMove = false;
|
||||
|
||||
const displacement = 0.05;
|
||||
|
||||
|
||||
function triangle(a, b, c) {
|
||||
|
||||
var t1 = subtract(b, a);
|
||||
var t2 = subtract(c, a);
|
||||
var normal = normalize(cross(t2, t1));
|
||||
|
||||
normalsArray.push(vec4(normal[0], normal[1], normal[2], 0.0));
|
||||
normalsArray.push(vec4(normal[0], normal[1], normal[2], 0.0));
|
||||
normalsArray.push(vec4(normal[0], normal[1], normal[2], 0.0));
|
||||
|
||||
positionsArray.push(a);
|
||||
positionsArray.push(b);
|
||||
positionsArray.push(c);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
function moveLight() {
|
||||
//move light
|
||||
//check if light is supposed to be moving
|
||||
if (lightMove) {
|
||||
console.log(lightPosition);
|
||||
//check if light is moving or left or right
|
||||
if (direction) {
|
||||
//check if the lights current position is passed max travel distance
|
||||
if (lightPosition[lightAxis] < maxTravel) {
|
||||
//increment the value at that axis
|
||||
lightPosition[lightAxis] += displacement;
|
||||
}
|
||||
if (lightPosition[lightAxis] >= maxTravel) {
|
||||
direction = !direction;
|
||||
}
|
||||
}
|
||||
if (!direction) {
|
||||
if (lightPosition[lightAxis] > -maxTravel) {
|
||||
lightPosition[lightAxis] -= displacement;
|
||||
}
|
||||
if (lightPosition[lightAxis] <= -maxTravel) {
|
||||
direction = !direction;
|
||||
}
|
||||
}
|
||||
}
|
||||
return lightPosition;
|
||||
}
|
||||
|
||||
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");
|
||||
|
||||
lightPositionLoc = gl.getUniformLocation(program, "uLightPosition");
|
||||
|
||||
|
||||
document.getElementById("Subdivisions").onclick = function (event) {
|
||||
switch (event.target.index) {
|
||||
case 0:
|
||||
//increase subdivisions
|
||||
numTimesToSubdivide++;;
|
||||
index = 0;
|
||||
positionsArray = [];
|
||||
normalsArray = [];
|
||||
init();
|
||||
|
||||
break;
|
||||
case 1:
|
||||
//decrease subdivisions
|
||||
if (numTimesToSubdivide) numTimesToSubdivide--;
|
||||
index = 0;
|
||||
positionsArray = [];
|
||||
normalsArray = [];
|
||||
init();
|
||||
|
||||
break;
|
||||
case 2:
|
||||
//increase radius
|
||||
radius *= 2.0;
|
||||
break;
|
||||
case 3:
|
||||
//decrease radius
|
||||
radius /= 2.0;
|
||||
break;
|
||||
}
|
||||
};
|
||||
document.getElementById("Theta/ Phi").onclick = function (event) {
|
||||
switch (event.target.index) {
|
||||
case 0:
|
||||
//increase theta
|
||||
theta += dr;
|
||||
break;
|
||||
case 1:
|
||||
//decrease theta
|
||||
theta -= dr;
|
||||
break;
|
||||
case 2:
|
||||
//increase phi
|
||||
phi += dr;
|
||||
break;
|
||||
case 3:
|
||||
//decrease phi
|
||||
phi -= dr;
|
||||
break;
|
||||
}
|
||||
};
|
||||
document.getElementById("Light movement").onclick = function (event) {
|
||||
switch (event.target.index) {
|
||||
case 0:
|
||||
//move light on x axis
|
||||
lightMove = true;
|
||||
lightAxis = 0;
|
||||
break;
|
||||
case 1:
|
||||
//move light on y axis
|
||||
lightAxis = 1;
|
||||
lightMove = true;
|
||||
break;
|
||||
case 2:
|
||||
//move light on z axis
|
||||
lightAxis = 2;
|
||||
lightMove = true;
|
||||
break;
|
||||
case 3:
|
||||
//reset light
|
||||
lightMove = false;
|
||||
lightPosition = vec4(0.0, 0.0, 1.0, 0.0);
|
||||
console.log(lightPosition);
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
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);
|
||||
|
||||
//Moved to function to move the logic around easier
|
||||
moveLight();
|
||||
|
||||
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);
|
||||
|
||||
nMatrix = normalMatrix(modelViewMatrix, true);
|
||||
|
||||
//send back to shaders
|
||||
//Send light position back to shaders
|
||||
gl.uniform4fv(lightPositionLoc, flatten(lightPosition));
|
||||
//***NOTE: No false on light unlike other sends***
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
shadedSphere3();
|
||||
Reference in new issue
Block a user