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,35 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<script id="vertex-shader" type="x-shader/x-vertex">
|
||||
#version 300 es
|
||||
in vec4 aPosition;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = aPosition;
|
||||
}
|
||||
</script>
|
||||
|
||||
<script id="fragment-shader" type="x-shader/x-fragment">
|
||||
#version 300 es
|
||||
precision mediump float;
|
||||
|
||||
out vec4 fColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
fColor = vec4( 1.0, 1.0, 0.0, 1.0 );
|
||||
}
|
||||
</script>
|
||||
|
||||
<script type="text/javascript" src="../Common/initShaders.js"></script>
|
||||
<script type="text/javascript" src="../Common/MV.js"></script>
|
||||
<script type="text/javascript" src="Exam0_03.js"></script>
|
||||
|
||||
<body>
|
||||
<canvas id="gl-canvas" width="512" height="512">>
|
||||
Oops ... your browser doesn't support the HTML5 canvas element
|
||||
</canvas>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,44 @@
|
||||
var canvas;
|
||||
var gl;
|
||||
|
||||
|
||||
window.onload = function init() {
|
||||
canvas = document.getElementById( "gl-canvas" );
|
||||
|
||||
gl = gl = canvas.getContext('webgl2');
|
||||
if ( !gl ) { alert( "WebGL 2.0 isn't available" ); }
|
||||
|
||||
// Four Vertices
|
||||
var vertices = [
|
||||
vec2( -0.5, -0.5 ),
|
||||
vec2( -0.5, 0.5 ),
|
||||
vec2( 0.5, 0.5 ),
|
||||
vec2( 0.5, -0.5 )
|
||||
];
|
||||
|
||||
// Configure WebGL
|
||||
gl.viewport( 0, 0, canvas.width, canvas.height );
|
||||
gl.clearColor( 0.0, 0.0, 0.0, 1.0 );
|
||||
|
||||
// Load shaders and initialize attribute buffers
|
||||
var program = initShaders( gl, "vertex-shader", "fragment-shader" );
|
||||
gl.useProgram( program );
|
||||
|
||||
// Load the data into the GPU
|
||||
var bufferId = gl.createBuffer();
|
||||
gl.bindBuffer( gl.ARRAY_BUFFER, bufferId );
|
||||
gl.bufferData( gl.ARRAY_BUFFER, flatten(vertices), gl.STATIC_DRAW );
|
||||
|
||||
// Associate out shader variable with our data buffer
|
||||
var aPosition = gl.getAttribLocation( program, "aPosition" );
|
||||
gl.vertexAttribPointer( aPosition, 2, gl.FLOAT, false, 0, 0 );
|
||||
gl.enableVertexAttribArray(aPosition);
|
||||
|
||||
render();
|
||||
};
|
||||
|
||||
function render() {
|
||||
gl.clear( gl.COLOR_BUFFER_BIT );
|
||||
gl.drawArrays( gl.TRIANGLE_FAN, 0, 4 );
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<script id="vertex-shader" type="x-shader/x-vertex">
|
||||
#version 300 es
|
||||
|
||||
in vec4 aPosition;
|
||||
uniform float uTheta;
|
||||
in vec3 aColor;
|
||||
out vec4 vColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
float s = sin(uTheta);
|
||||
float c = cos(uTheta);
|
||||
|
||||
gl_Position.x = -s*aPosition.y + c*aPosition.x;
|
||||
gl_Position.y = s*aPosition.x + c*aPosition.y;
|
||||
gl_Position.z = 0.0;
|
||||
gl_Position.w = 1.0;
|
||||
|
||||
vColor = vec4(aColor, 1.0);
|
||||
}
|
||||
</script>
|
||||
|
||||
<script id="fragment-shader" type="x-shader/x-fragment">
|
||||
#version 300 es
|
||||
|
||||
precision mediump float;
|
||||
in vec4 vColor;
|
||||
out vec4 fColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
fColor = vColor;
|
||||
}
|
||||
</script>
|
||||
|
||||
<script type="text/javascript" src="../Common/initShaders.js"></script>
|
||||
<script type="text/javascript" src="../Common/MVnew.js"></script>
|
||||
<script type="text/javascript" src="Exam1_03.js"></script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<select id="Controls" size="4">
|
||||
<option value="0">Pause</option>
|
||||
<option value="1">Direction</option>
|
||||
<option value="2">Faster</option>
|
||||
<option value="3">Slower</option>
|
||||
</select>
|
||||
|
||||
<button id="Color">Color change</button>
|
||||
|
||||
<canvas id="gl-canvas" width="512" height="512">
|
||||
Oops ... your browser doesn't support the HTML5 canvas element
|
||||
</canvas>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -0,0 +1,137 @@
|
||||
var gl;
|
||||
|
||||
var theta = 0.0;
|
||||
var thetaLoc;
|
||||
|
||||
var pause = true;
|
||||
var direction = true;
|
||||
var speed = 0.05;
|
||||
|
||||
var colors = [];
|
||||
var flip = false;
|
||||
|
||||
|
||||
window.onload = function init() {
|
||||
var canvas = document.getElementById("gl-canvas");
|
||||
|
||||
gl = canvas.getContext('webgl2');
|
||||
if (!gl) alert("WebGL 2.0 isn't available");
|
||||
|
||||
// Configure WebGL
|
||||
gl.viewport(0, 0, canvas.width, canvas.height);
|
||||
gl.clearColor(0.8, 0.8, 0.8, 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 vertices = [
|
||||
//x , y , z
|
||||
//triangle 1
|
||||
vec3(-1.0, 0.25, 0.0),
|
||||
vec3(0.0, 1.0, 0.0),
|
||||
vec3(1.0, 0.25, 0.0),
|
||||
|
||||
//triangle 2
|
||||
vec3(-1.0, -0.25, 0.0),
|
||||
vec3(1.0, -0.25, 0.0),
|
||||
vec3(0.0, -1.0, 0.0),
|
||||
|
||||
];
|
||||
|
||||
var baseColors = [
|
||||
vec3(1.0, 0.0, 0.0), //Red
|
||||
vec3(0.0, 1.0, 0.0), //Green
|
||||
vec3(0.0, 0.0, 1.0), //Blue
|
||||
vec3(1.0, 0.0, 1.0), //Magenta
|
||||
vec3(1.0, 1.0, 0.0), //Yellow
|
||||
vec3(0.0, 1.0, 1.0), //Cyan
|
||||
vec3(0.0, 0.0, 0.0), //Black
|
||||
vec3(1.0, 1.0, 1.0) //White
|
||||
];
|
||||
|
||||
colors = [baseColors[0], baseColors[1], baseColors[2], baseColors[5], baseColors[4], baseColors[3]];
|
||||
|
||||
|
||||
//Set color buffer (Next 6 lines are required for color)
|
||||
var cBuffer = gl.createBuffer();
|
||||
gl.bindBuffer(gl.ARRAY_BUFFER, cBuffer);
|
||||
gl.bufferData(gl.ARRAY_BUFFER, flatten(colors), gl.STATIC_DRAW);
|
||||
|
||||
var colorLoc = gl.getAttribLocation(program, "aColor");
|
||||
gl.vertexAttribPointer(colorLoc, 3, gl.FLOAT, false, 0, 0);
|
||||
gl.enableVertexAttribArray(colorLoc);
|
||||
|
||||
// Create a buffer object, initialize it, and associate it with the
|
||||
// associated attribute variable in our vertex shader
|
||||
var vBuffer = gl.createBuffer();
|
||||
gl.bindBuffer(gl.ARRAY_BUFFER, vBuffer);
|
||||
gl.bufferData(gl.ARRAY_BUFFER, flatten(vertices), gl.STATIC_DRAW);
|
||||
|
||||
var positionLoc = gl.getAttribLocation(program, "aPosition");
|
||||
gl.vertexAttribPointer(positionLoc, 3, gl.FLOAT, false, 0, 0);
|
||||
gl.enableVertexAttribArray(positionLoc);
|
||||
|
||||
thetaLoc = gl.getUniformLocation(program, "uTheta");
|
||||
|
||||
// Initialize event handlers
|
||||
document.getElementById("Color").onclick = function () {
|
||||
//change colors
|
||||
flip = !flip;
|
||||
if (flip) {
|
||||
//Flip colors
|
||||
colors = [baseColors[3], baseColors[4], baseColors[5], baseColors[2], baseColors[1], baseColors[0]];
|
||||
}
|
||||
if (!flip) {
|
||||
//top triangle R, G, B Bottom triangle Cy, Ye, Mg
|
||||
colors = [baseColors[0], baseColors[1], baseColors[2], baseColors[5], baseColors[4], baseColors[3]];
|
||||
}
|
||||
//Update the color buffer
|
||||
gl.bindBuffer(gl.ARRAY_BUFFER, cBuffer);
|
||||
gl.bufferData(gl.ARRAY_BUFFER, flatten(colors), gl.STATIC_DRAW);
|
||||
};
|
||||
|
||||
document.getElementById("Controls").onclick = function (event) {
|
||||
switch (event.target.index) {
|
||||
case 0:
|
||||
//pause
|
||||
pause = !pause;
|
||||
break;
|
||||
case 1:
|
||||
//direction
|
||||
direction = !direction;
|
||||
break;
|
||||
case 2:
|
||||
//speed up
|
||||
speed = speed * 2.0;
|
||||
break;
|
||||
case 3:
|
||||
//slow down
|
||||
speed = speed / 2.0;
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
render();
|
||||
};
|
||||
|
||||
function render() {
|
||||
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
|
||||
if (!pause) {
|
||||
if (direction) {
|
||||
theta += speed;
|
||||
}
|
||||
if (!direction) {
|
||||
theta -= speed;
|
||||
}
|
||||
}
|
||||
gl.uniform1f(thetaLoc, theta);
|
||||
|
||||
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 3);
|
||||
gl.drawArrays(gl.TRIANGLE_STRIP, 3, 3);
|
||||
|
||||
requestAnimationFrame(render);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<button id="xButton">Rotate X</button>
|
||||
<button id="yButton">Rotate Y</button>
|
||||
<button id="zButton">Rotate Z</button>
|
||||
<button id="directionButton">Direction</button>
|
||||
<button id="pauseButton">Pause</button>
|
||||
<button id="colorButton">Color</button>
|
||||
<button id="sceneButton">Scene</button>
|
||||
<br />
|
||||
|
||||
<script id="vertex-shader" type="x-shader/x-vertex">
|
||||
#version 300 es
|
||||
|
||||
in vec4 aPosition;
|
||||
in vec4 aColor;
|
||||
out vec4 vColor;
|
||||
|
||||
uniform vec3 uTheta;
|
||||
uniform vec3 uScale;
|
||||
//uniform vec3 uTranslate;
|
||||
|
||||
void main()
|
||||
{
|
||||
// Compute the sines and cosines of theta for each of
|
||||
// the three axes in one computation.
|
||||
vec3 angles = radians(uTheta);
|
||||
vec3 c = cos(angles);
|
||||
vec3 s = sin(angles);
|
||||
|
||||
// Remember: these matrices are column-major
|
||||
|
||||
mat4 rx = mat4( 1.0, 0.0, 0.0, 0.0,
|
||||
0.0, c.x, s.x, 0.0,
|
||||
0.0, -s.x, c.x, 0.0,
|
||||
0.0, 0.0, 0.0, 1.0);
|
||||
|
||||
mat4 ry = mat4( c.y, 0.0, -s.y, 0.0,
|
||||
0.0, 1.0, 0.0, 0.0,
|
||||
s.y, 0.0, c.y, 0.0,
|
||||
0.0, 0.0, 0.0, 1.0);
|
||||
mat4 rz = mat4( c.z, s.z, 0.0, 0.0,
|
||||
-s.z, c.z, 0.0, 0.0,
|
||||
0.0, 0.0, 1.0, 0.0,
|
||||
0.0, 0.0, 0.0, 1.0);
|
||||
//Scale matrix
|
||||
mat4 S = mat4( uScale.x, 0.0, 0.0, 0.0,
|
||||
0.0, uScale.y, 0.0, 0.0,
|
||||
0.0, 0.0, uScale.z, 0.0,
|
||||
0.0, 0.0, 0.0, 1.0);
|
||||
|
||||
//translate matrix
|
||||
/*
|
||||
mat4 T = mat4( UTranslate.x, 0.0, 0.0, 0.0,
|
||||
0.0, UTranslate.y, 0.0, 0.0,
|
||||
0.0, 0.0, UTranslate.z, 0.0,
|
||||
0.0, 0.0, 0.0, 1.0);
|
||||
*/
|
||||
vColor = aColor;
|
||||
gl_Position = rz * ry * rx * S * aPosition;
|
||||
gl_Position.z = -gl_Position.z;
|
||||
}
|
||||
</script>
|
||||
|
||||
<script id="fragment-shader" type="x-shader/x-fragment">
|
||||
#version 300 es
|
||||
|
||||
precision mediump float;
|
||||
|
||||
in vec4 vColor;
|
||||
out vec4 fColor;
|
||||
|
||||
void
|
||||
main()
|
||||
{
|
||||
fColor = vColor;
|
||||
}
|
||||
</script>
|
||||
|
||||
<script type="text/javascript" src="../Common/initShaders.js"></script>
|
||||
<script type="text/javascript" src="../Common/MVnew.js"></script>
|
||||
<script type="text/javascript" src="Exam2_03.js"></script>
|
||||
|
||||
<body>
|
||||
<canvas id="gl-canvas" width="512"" height=" 512">
|
||||
Oops ... your browser doesn't support the HTML5 canvas element
|
||||
</canvas>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -0,0 +1,217 @@
|
||||
/*Save the Exam2_XX files with XX replaced by your student number. Modify the code to make the buttons work as demonstrated for the Exercises. The Scene button works as demonstrated in class.
|
||||
|
||||
Apply the transformations in the vertex shader by sending the angles, scaling, and translation factors as uniform variables.
|
||||
|
||||
Do not use the rotate, scale, or translate functions included in MV.js or MVnew.js.
|
||||
Do not name any variables “rotate”, “scale”, or “translate”.
|
||||
Do not modify the “vertices” variable.
|
||||
Do not add any vertices or buffers.
|
||||
Upload your files in the space below.
|
||||
|
||||
for scene make 3 copies at smaller scales, smallest is top left biggest bottome right
|
||||
send factors as uniform variables
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
var canvas;
|
||||
var gl;
|
||||
|
||||
var numPositions = 36;
|
||||
|
||||
var positions = [];
|
||||
var colors = [];
|
||||
|
||||
var xAxis = 0;
|
||||
var yAxis = 1;
|
||||
var zAxis = 2;
|
||||
|
||||
var axis = 0;
|
||||
var scaleAxis;
|
||||
var theta = [0, 0, 0];
|
||||
var scale = [1.0, 1.0, 1.0];
|
||||
var translateFac = [0, 0, 0];
|
||||
|
||||
var thetaLoc;
|
||||
var scaleLoc;
|
||||
var translateLoc;
|
||||
|
||||
var factor = 0.5;
|
||||
var pause = false;
|
||||
var direction = true;
|
||||
var solid = true;
|
||||
var scene = false;
|
||||
|
||||
var vertices = [
|
||||
vec4(-0.5, -0.5, 0.5, 1.0),
|
||||
vec4(-0.5, 0.5, 0.5, 1.0),
|
||||
vec4(0.5, 0.5, 0.5, 1.0),
|
||||
vec4(0.5, -0.5, 0.5, 1.0),
|
||||
vec4(-0.5, -0.5, -0.5, 1.0),
|
||||
vec4(-0.5, 0.5, -0.5, 1.0),
|
||||
vec4(0.5, 0.5, -0.5, 1.0),
|
||||
vec4(0.5, -0.5, -0.5, 1.0)
|
||||
];
|
||||
var vertexColors = [
|
||||
vec4(0.0, 0.0, 0.0, 1.0), // black
|
||||
vec4(1.0, 0.0, 0.0, 1.0), // red
|
||||
vec4(1.0, 1.0, 0.0, 1.0), // yellow
|
||||
vec4(0.0, 1.0, 0.0, 1.0), // green
|
||||
vec4(0.0, 0.0, 1.0, 1.0), // blue
|
||||
vec4(1.0, 0.0, 1.0, 1.0), // magenta
|
||||
vec4(0.0, 1.0, 1.0, 1.0), // cyan
|
||||
vec4(1.0, 1.0, 1.0, 1.0) // white
|
||||
];
|
||||
|
||||
window.onload = function init() {
|
||||
canvas = document.getElementById("gl-canvas");
|
||||
|
||||
gl = canvas.getContext('webgl2');
|
||||
if (!gl) alert("WebGL 2.0 isn't available");
|
||||
|
||||
colorCube();
|
||||
|
||||
gl.viewport(0, 0, canvas.width, canvas.height);
|
||||
gl.clearColor(0.7, 0.7, 0.7, 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 vBuffer = gl.createBuffer();
|
||||
gl.bindBuffer(gl.ARRAY_BUFFER, vBuffer);
|
||||
gl.bufferData(gl.ARRAY_BUFFER, flatten(positions), gl.STATIC_DRAW);
|
||||
|
||||
var positionLoc = gl.getAttribLocation(program, "aPosition");
|
||||
gl.vertexAttribPointer(positionLoc, 4, gl.FLOAT, false, 0, 0);
|
||||
gl.enableVertexAttribArray(positionLoc);
|
||||
|
||||
var cBuffer = gl.createBuffer();
|
||||
gl.bindBuffer(gl.ARRAY_BUFFER, cBuffer);
|
||||
gl.bufferData(gl.ARRAY_BUFFER, flatten(colors), gl.STATIC_DRAW);
|
||||
|
||||
var colorLoc = gl.getAttribLocation(program, "aColor");
|
||||
gl.vertexAttribPointer(colorLoc, 4, gl.FLOAT, false, 0, 0);
|
||||
gl.enableVertexAttribArray(colorLoc);
|
||||
|
||||
thetaLoc = gl.getUniformLocation(program, "uTheta");
|
||||
scaleLoc = gl.getUniformLocation(program, "uScale");
|
||||
//translateLoc = gl.getUniformLocation(program, "uTranslate");
|
||||
|
||||
|
||||
|
||||
//event listeners for buttons
|
||||
document.getElementById("xButton").onclick = function () {
|
||||
axis = xAxis;
|
||||
};
|
||||
document.getElementById("yButton").onclick = function () {
|
||||
axis = yAxis;
|
||||
};
|
||||
document.getElementById("zButton").onclick = function () {
|
||||
axis = zAxis;
|
||||
};
|
||||
document.getElementById("pauseButton").onclick = function () {
|
||||
pause = !pause
|
||||
if (pause == true) {
|
||||
theta = theta;
|
||||
}
|
||||
};
|
||||
document.getElementById("sceneButton").onclick = function () {
|
||||
scene = !scene
|
||||
};
|
||||
document.getElementById("directionButton").onclick = function () {
|
||||
direction = !direction;
|
||||
};
|
||||
document.getElementById("colorButton").onclick = function () {
|
||||
solid = !solid;
|
||||
//Reset color array
|
||||
colors = [];
|
||||
//call for new colors
|
||||
colorCube();
|
||||
//Update colors
|
||||
gl.bindBuffer(gl.ARRAY_BUFFER, cBuffer);
|
||||
gl.bufferData(gl.ARRAY_BUFFER, flatten(colors), gl.STATIC_DRAW);
|
||||
};
|
||||
render();
|
||||
}
|
||||
|
||||
function colorCube() {
|
||||
quad(1, 0, 3, 2);
|
||||
quad(2, 3, 7, 6);
|
||||
quad(3, 0, 4, 7);
|
||||
quad(6, 5, 1, 2);
|
||||
quad(4, 5, 6, 7);
|
||||
quad(5, 4, 0, 1);
|
||||
}
|
||||
|
||||
function quad(a, b, c, d) {
|
||||
// We need to parition the quad into two triangles in order for
|
||||
// WebGL to be able to render it. In this case, we create two
|
||||
// triangles from the quad indices
|
||||
|
||||
var indices = [a, b, c, a, c, d];
|
||||
if (solid) {
|
||||
for (var i = 0; i < indices.length; ++i) {
|
||||
positions.push(vertices[indices[i]]);
|
||||
// for solid colored faces use (only pushes 6 colors)
|
||||
colors.push(vertexColors[a]);
|
||||
}
|
||||
}
|
||||
if (!solid) {
|
||||
for (var i = 0; i < indices.length; ++i) {
|
||||
// for interpolated vertex colors use (pushes a color per vertex)
|
||||
positions.push(vertices[indices[i]]);
|
||||
colors.push(vertexColors[indices[i]]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function render() {
|
||||
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
|
||||
|
||||
if (direction == true && !pause) {
|
||||
theta[axis] += 2.0;
|
||||
}
|
||||
if (direction == false && !pause) {
|
||||
theta[axis] -= 2.0;
|
||||
}
|
||||
if (scene) {
|
||||
//Make 3 copies
|
||||
/*
|
||||
for (var i = 0; i < 3; ++i) {
|
||||
if (i == 0) {
|
||||
translateFac = [.4, -.4, 0]
|
||||
}
|
||||
if (i == 1) {
|
||||
translateFac = [0, 0, 0]
|
||||
}
|
||||
if (i == 2) {
|
||||
translateFac = [-.4, .4, 0]
|
||||
}
|
||||
|
||||
scaleFac = scaleFac * 1 / (i + 1);
|
||||
|
||||
gl.uniform3fv(thetaLoc, theta);
|
||||
gl.uniform3fv(scaleLoc, scaleFac);
|
||||
gl.uniform3fv(translateLoc, translateFac);
|
||||
|
||||
gl.drawArrays(gl.TRIANGLES, 0, numPositions);
|
||||
*/
|
||||
|
||||
|
||||
|
||||
}
|
||||
if (!scene) {
|
||||
gl.uniform3fv(thetaLoc, theta);
|
||||
gl.uniform3fv(scaleLoc, scale);
|
||||
|
||||
//gl.uniform3fv(translateLoc, translateFac);
|
||||
|
||||
gl.drawArrays(gl.TRIANGLES, 0, numPositions);
|
||||
}
|
||||
|
||||
|
||||
|
||||
requestAnimationFrame(render);
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<script id="vertex-shader" type="x-shader/x-vertex">
|
||||
#version 300 es
|
||||
|
||||
in vec4 aPosition;
|
||||
in vec4 aNormal;
|
||||
out vec4 vColor;
|
||||
|
||||
uniform vec4 uAmbientProduct, uDiffuseProduct, uSpecularProduct;
|
||||
uniform mat4 uModelViewMatrix;
|
||||
uniform mat4 uProjectionMatrix;
|
||||
uniform vec4 uLightPosition;
|
||||
uniform float uShininess;
|
||||
uniform mat3 uNormalMatrix;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec3 pos = (uModelViewMatrix * aPosition).xyz;
|
||||
vec3 light = uLightPosition.xyz;
|
||||
vec3 L;
|
||||
|
||||
// check for directional light
|
||||
if(uLightPosition.w == 0.0) L = normalize(uLightPosition.xyz);
|
||||
else L = normalize(uLightPosition.xyz - pos);
|
||||
|
||||
vec3 E = -normalize(pos);
|
||||
vec3 H = normalize(L + E);
|
||||
|
||||
// Transform vertex normal into eye coordinates
|
||||
vec3 N = normalize(uNormalMatrix*aNormal.xyz);
|
||||
|
||||
// Compute terms in the illumination equation
|
||||
vec4 ambient = uAmbientProduct;
|
||||
|
||||
float Kd = max( dot(L, N), 0.0 );
|
||||
vec4 diffuse = Kd*uDiffuseProduct;
|
||||
|
||||
float Ks = pow( max(dot(N, H), 0.0), uShininess );
|
||||
vec4 specular = Ks * uSpecularProduct;
|
||||
|
||||
if( dot(L, N) < 0.0 ) {
|
||||
specular = vec4(0.0, 0.0, 0.0, 1.0);
|
||||
}
|
||||
|
||||
gl_Position = uProjectionMatrix * uModelViewMatrix * aPosition;
|
||||
|
||||
vColor = ambient + diffuse + specular;
|
||||
vColor.a = 1.0;
|
||||
}
|
||||
</script>
|
||||
|
||||
<script id="fragment-shader" type="x-shader/x-fragment">
|
||||
#version 300 es
|
||||
|
||||
precision mediump float;
|
||||
|
||||
in vec4 vColor;
|
||||
out vec4 fColor;
|
||||
|
||||
void
|
||||
main()
|
||||
{
|
||||
fColor = vColor;
|
||||
}
|
||||
</script>
|
||||
|
||||
<p></p>
|
||||
<button id="Button0">Initial</button>
|
||||
<button id="Button1">Scan</button>
|
||||
<button id="Button2">Inspect</button>
|
||||
<button id="Button3">Stretch</button>
|
||||
<button id="Button4">Tarnish</button>
|
||||
<p></p>
|
||||
<script type="text/javascript" src="../Common/initShaders.js"></script>
|
||||
<script type="text/javascript" src="../Common/MVnew.js"></script>
|
||||
<script type="text/javascript" src="Exam3_03.js"></script>
|
||||
|
||||
<body>
|
||||
<canvas id="gl-canvas" width="512" height="512">
|
||||
Oops ... your browser doesn't support the HTML5 canvas element
|
||||
</canvas>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,282 @@
|
||||
/*
|
||||
initial - goes to initial state
|
||||
scan - camera look left / right, or back to *original state
|
||||
inspect - raise camera 0.5 units and rotate light around objects using polar coordinants
|
||||
stretch - makes object narrower and wider, *freezes in place and continues where left off
|
||||
Tarnish - changes the color (ambient values become compliment), *freezes color in place and continues where left off
|
||||
|
||||
* no rotate, scale or translate functions allowed
|
||||
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
var canvas;
|
||||
var gl;
|
||||
|
||||
var nRows = 50;
|
||||
var nColumns = 50;
|
||||
|
||||
// data for radial hat function: sin(Pi*r)/(Pi*r)
|
||||
var data = [];
|
||||
for(var i = 0; i < nRows; ++i) {
|
||||
data.push([]);
|
||||
var x = Math.PI*(4*i/nRows-2.0);
|
||||
for(var j = 0; j < nColumns; ++j) {
|
||||
var y = Math.PI*(4*j/nRows-2.0);
|
||||
var r = Math.sqrt(x*x+y*y);
|
||||
// take care of 0/0 for r = 0
|
||||
data[i][j] = r ? Math.sin(r) / r : 1.0;
|
||||
}
|
||||
}
|
||||
|
||||
var positionsArray = [];
|
||||
var normalsArray = [];
|
||||
|
||||
var eye = vec3(0.0, 0.0, 1.0);
|
||||
var at = vec3(0.0, 0.0, 0.0);
|
||||
var up = vec3(0.0, 1.0, 0.0);
|
||||
|
||||
var left = -1.5;
|
||||
var right = 1.5;
|
||||
var ytop = 1.5;
|
||||
var bottom = -0.75;
|
||||
var near = -10;
|
||||
var far = 10;
|
||||
|
||||
var radius = 2.0;
|
||||
var theta = 0.0;
|
||||
var phi = 0.0;
|
||||
|
||||
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 modelViewMatrix, projectionMatrix;
|
||||
var modelViewMatrixLoc, projectionMatrixLoc;
|
||||
var nMatrix, nMatrixLoc;
|
||||
|
||||
var ambientProduct, ambientProductLoc;
|
||||
var diffuseProduct, diffuseProductLoc;
|
||||
var specularProduct, specularProductLoc;
|
||||
var lightPositionLoc, shininessLoc;
|
||||
|
||||
var apt, bpt, cpt, dpt;
|
||||
|
||||
var initial = true;
|
||||
var scan = false;
|
||||
var inspect = false;
|
||||
var stretch = false;
|
||||
var tarnish = false;
|
||||
|
||||
window.onload = function init() {
|
||||
console.log('init function called');
|
||||
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(0.5, 0.5, 0.5, 1.0);
|
||||
|
||||
// enable depth testing and polygon offset
|
||||
gl.enable(gl.DEPTH_TEST);
|
||||
gl.depthFunc(gl.LEQUAL);
|
||||
gl.enable(gl.POLYGON_OFFSET_FILL);
|
||||
gl.polygonOffset(1.0, 2.0);
|
||||
|
||||
// vertex array of nRows*nColumns quadrilaterals
|
||||
// (two triangles/quad) from data
|
||||
for(var i=0; i<nRows-1; i++) {
|
||||
for(var j=0; j<nColumns-1;j++) {
|
||||
var apt = vec4(2*i/nRows-1, data[i][j], 2*j/nColumns-1, 1.0);
|
||||
var bpt = vec4(2*(i+1)/nRows-1, data[i+1][j], 2*j/nColumns-1, 1.0);
|
||||
var cpt = vec4(2*(i+1)/nRows-1, data[i+1][j+1], 2*(j+1)/nColumns-1, 1.0);
|
||||
var dpt = vec4(2*i/nRows-1, data[i][j+1], 2*(j+1)/nColumns-1, 1.0);
|
||||
positionsArray.push(apt);
|
||||
positionsArray.push(bpt);
|
||||
positionsArray.push(cpt);
|
||||
positionsArray.push(dpt);
|
||||
normalsArray.push(apt);
|
||||
normalsArray.push(bpt);
|
||||
normalsArray.push(cpt);
|
||||
normalsArray.push(dpt);
|
||||
}
|
||||
}
|
||||
|
||||
// Load shaders and initialize attribute buffers
|
||||
var program = initShaders(gl, "vertex-shader", "fragment-shader");
|
||||
gl.useProgram(program);
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
modelViewMatrixLoc = gl.getUniformLocation( program, "uModelViewMatrix" );
|
||||
projectionMatrixLoc = gl.getUniformLocation( program, "uProjectionMatrix" );
|
||||
nMatrixLoc = gl.getUniformLocation( program, "uNormalMatrix" );
|
||||
|
||||
ambientProduct = mult(lightAmbient, materialAmbient);
|
||||
diffuseProduct = mult(lightDiffuse, materialDiffuse);
|
||||
specularProduct = mult(lightSpecular, materialSpecular);
|
||||
|
||||
ambientProductLoc = gl.getUniformLocation(program,"uAmbientProduct");
|
||||
diffuseProductLoc = gl.getUniformLocation(program,"uDiffuseProduct");
|
||||
specularProductLoc = gl.getUniformLocation(program,"uSpecularProduct");
|
||||
lightPositionLoc = gl.getUniformLocation(program,"uLightPosition")
|
||||
shininessLoc = gl.getUniformLocation(program, "uShininess");
|
||||
|
||||
gl.uniform4fv( ambientProductLoc, ambientProduct );
|
||||
gl.uniform4fv( diffuseProductLoc, diffuseProduct );
|
||||
gl.uniform4fv( specularProductLoc, specularProduct );
|
||||
gl.uniform4fv( lightPositionLoc, lightPosition );
|
||||
gl.uniform1f( shininessLoc,materialShininess );
|
||||
|
||||
// Initialize event handlers
|
||||
document.getElementById("Button0").onclick = function () {
|
||||
//reset conditions to initial
|
||||
scan = false;
|
||||
inspect = false;
|
||||
stretch = false;
|
||||
tarnish = false;
|
||||
|
||||
lightPosition = vec4(1.0, 1.0, 1.0, 0.0);
|
||||
lightAmbient = vec4(0.2, 0.2, 0.2, 1.0);
|
||||
lightDiffuse = vec4(1.0, 1.0, 1.0, 1.0);
|
||||
lightSpecular = vec4(1.0, 1.0, 1.0, 1.0);
|
||||
|
||||
materialAmbient = vec4(1.0, 0.0, 1.0, 1.0);
|
||||
materialDiffuse = vec4(1.0, 0.8, 0.0, 1.0);
|
||||
materialSpecular = vec4(1.0, 1.0, 1.0, 1.0);
|
||||
materialShininess = 20.0;
|
||||
|
||||
};
|
||||
document.getElementById("Button1").onclick = function () {
|
||||
//scan
|
||||
scan = !scan;
|
||||
|
||||
|
||||
};
|
||||
document.getElementById("Button2").onclick = function () {
|
||||
//inspect
|
||||
inspect = !inspect;
|
||||
|
||||
|
||||
};
|
||||
document.getElementById("Button3").onclick = function () {
|
||||
//stretch
|
||||
stretch = !stretch;
|
||||
|
||||
};
|
||||
document.getElementById("Button4").onclick = function () {
|
||||
//tarnish
|
||||
tarnish = !tarnish;
|
||||
|
||||
};
|
||||
render();
|
||||
}
|
||||
|
||||
function render() {
|
||||
gl.clear( gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
|
||||
|
||||
//Logic
|
||||
if (scan){
|
||||
//move camera left / right
|
||||
var direction = true;
|
||||
if (direction){
|
||||
if (eye[0] >= 1.0){
|
||||
direction = !direction;
|
||||
}
|
||||
eye[0] += 0.01;
|
||||
}
|
||||
if (!direction){
|
||||
if (eye[0] <= -1.0){
|
||||
direction = !direction;
|
||||
}
|
||||
eye[0] -= 0.01;
|
||||
}
|
||||
}
|
||||
if (inspect){
|
||||
// move light around object using polar coords
|
||||
lightPosition[0] += 0.01;
|
||||
lightPosition[2] += 0.01;
|
||||
|
||||
}
|
||||
if (stretch){
|
||||
//stretch and shrink obj
|
||||
var direction = true;
|
||||
if (direction){
|
||||
//stretch
|
||||
}
|
||||
if (!direction){
|
||||
//shrink
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
if (tarnish){
|
||||
//change ambient color
|
||||
var green = false;
|
||||
if (green){
|
||||
if (lightAmbient[0] <= 0) {
|
||||
green = !green;
|
||||
}
|
||||
lightAmbient[0] -= 0.01;
|
||||
lightAmbient[1] += 0.01;
|
||||
lightAmbient[2] -= 0.01;
|
||||
|
||||
}
|
||||
if (!green){
|
||||
if (lightAmbient[0] >= 0.2){
|
||||
green = !green;
|
||||
}
|
||||
lightAmbient[0] += 0.01;
|
||||
lightAmbient[1] -= 0.01;
|
||||
lightAmbient[2] += 0.01;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
modelViewMatrix = lookAt(eye, at, up);
|
||||
projectionMatrix = ortho(left, right, bottom, ytop, near, far);
|
||||
nMatrix = normalMatrix(modelViewMatrix, true);
|
||||
|
||||
ambientProduct = mult(lightAmbient, materialAmbient);
|
||||
diffuseProduct = mult(lightDiffuse, materialDiffuse);
|
||||
specularProduct = mult(lightSpecular, materialSpecular);
|
||||
|
||||
gl.uniform4fv( ambientProductLoc, flatten(ambientProduct) );
|
||||
gl.uniform4fv( diffuseProductLoc, flatten(diffuseProduct) );
|
||||
gl.uniform4fv( specularProductLoc, flatten(specularProduct) );
|
||||
gl.uniform4fv( lightPositionLoc, flatten(lightPosition) );
|
||||
gl.uniform1f( shininessLoc, materialShininess );
|
||||
|
||||
gl.uniformMatrix4fv(modelViewMatrixLoc, false, flatten(modelViewMatrix));
|
||||
gl.uniformMatrix4fv(projectionMatrixLoc, false, flatten(projectionMatrix));
|
||||
gl.uniformMatrix3fv(nMatrixLoc, false, flatten(nMatrix) );
|
||||
|
||||
for(var i=0; i<positionsArray.length; i+=4) {
|
||||
gl.drawArrays( gl.TRIANGLE_FAN, i, 4 );
|
||||
}
|
||||
|
||||
requestAnimationFrame(render);
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<script id="vertex-shader" type="x-shader/x-vertex">
|
||||
#version 300 es
|
||||
|
||||
in vec4 aPosition;
|
||||
in vec3 aNormal;
|
||||
out vec4 vColor;
|
||||
|
||||
uniform vec4 uAmbientProduct, uDiffuseProduct, uSpecularProduct;
|
||||
uniform mat4 uModelViewMatrix;
|
||||
uniform mat4 uProjectionMatrix;
|
||||
uniform vec4 uLightPosition;
|
||||
uniform float uShininess;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec3 pos = -(uModelViewMatrix * aPosition).xyz;
|
||||
|
||||
//fixed light postion
|
||||
vec3 light = uLightPosition.xyz;
|
||||
vec3 L = normalize(light - pos);
|
||||
|
||||
vec3 E = normalize(-pos);
|
||||
vec3 H = normalize(L + E);
|
||||
|
||||
vec4 NN = vec4(aNormal,0);
|
||||
|
||||
// Transform vertex normal into eye coordinates
|
||||
vec3 N = normalize((uModelViewMatrix*NN).xyz);
|
||||
|
||||
// Compute terms in the illumination equation
|
||||
vec4 ambient = uAmbientProduct;
|
||||
|
||||
float Kd = max(dot(L, N), 0.0);
|
||||
vec4 diffuse = Kd*uDiffuseProduct;
|
||||
|
||||
float Ks = pow( max(dot(N, H), 0.0), uShininess );
|
||||
vec4 specular = Ks * uSpecularProduct;
|
||||
|
||||
if( dot(L, N) < 0.0 ) {
|
||||
specular = vec4(0.0, 0.0, 0.0, 1.0);
|
||||
}
|
||||
|
||||
gl_Position = uProjectionMatrix * uModelViewMatrix *aPosition;
|
||||
vColor = ambient + diffuse +specular;
|
||||
|
||||
vColor.a = 1.0;
|
||||
}
|
||||
</script>
|
||||
|
||||
<script id="fragment-shader" type="x-shader/x-fragment">
|
||||
#version 300 es
|
||||
|
||||
precision mediump float;
|
||||
|
||||
in vec4 vColor;
|
||||
out vec4 fColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
fColor = vColor;
|
||||
}
|
||||
</script>
|
||||
|
||||
<script type="text/javascript" src="../Common/initShaders.js"></script>
|
||||
<script type="text/javascript" src="../Common/MVnew.js"></script>
|
||||
<script type="text/javascript" src="Exam4_03.js"></script>
|
||||
|
||||
<button id = "Button1">Walk</button>
|
||||
<button id = "Button2">Run</button>
|
||||
<button id = "Button3">Agree</button>
|
||||
<button id = "Button4">Disagree</button>
|
||||
<button id = "Button9">Reset</button>
|
||||
|
||||
<div>
|
||||
torso angle -180 <input id="slider0" type="range"
|
||||
min="-180" max="180" step="10" value="140"
|
||||
/>
|
||||
180
|
||||
</div><br/>
|
||||
|
||||
<body>
|
||||
<canvas id="gl-canvas" width="512"" height="512">
|
||||
Oops ... your browser doesn't support the HTML5 canvas element
|
||||
</canvas>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,482 @@
|
||||
/*
|
||||
walk right arm with left leg return to standing
|
||||
run faster animation arms and legs at 45 stops at standing
|
||||
agree - like nod, stops in place +-45
|
||||
disagree shake head, stops in place +-45
|
||||
reset resets all to initial
|
||||
snaps between agree and disagree, cannot work at the same time but work independent
|
||||
from agree/ disagree
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
//Button Variables
|
||||
var initialPos = true;
|
||||
var walk = false;
|
||||
var run = false;
|
||||
var wave = false;
|
||||
var agree = false;
|
||||
var disagree = false;
|
||||
|
||||
//Movement variables
|
||||
var legFWD = true;
|
||||
var armFWD = true;
|
||||
var lookLft = true;
|
||||
var lookUp = true;
|
||||
var legStop = 45;
|
||||
var armStop = 45;
|
||||
var speed = 0;
|
||||
|
||||
var canvas;
|
||||
var gl;
|
||||
var program;
|
||||
|
||||
var projectionMatrix;
|
||||
var modelViewMatrix;
|
||||
var instanceMatrix;
|
||||
var modelViewMatrixLoc;
|
||||
|
||||
var positionsArray = [];
|
||||
var normalsArray = [];
|
||||
|
||||
var numVertices = 24;
|
||||
|
||||
var vertices = [
|
||||
vec4( -0.5, -0.5, 0.5, 1.0 ),
|
||||
vec4( -0.5, 0.5, 0.5, 1.0 ),
|
||||
vec4( 0.5, 0.5, 0.5, 1.0 ),
|
||||
vec4( 0.5, -0.5, 0.5, 1.0 ),
|
||||
vec4( -0.5, -0.5, -0.5, 1.0 ),
|
||||
vec4( -0.5, 0.5, -0.5, 1.0 ),
|
||||
vec4( 0.5, 0.5, -0.5, 1.0 ),
|
||||
vec4( 0.5, -0.5, -0.5, 1.0 )
|
||||
];
|
||||
|
||||
var lightPosition = vec4(0.0, 1.0, 2.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, 0.8, 0.0, 1.0);
|
||||
var materialShininess = 100.0;
|
||||
|
||||
var torsoId = 0;
|
||||
var headId = 1;
|
||||
var head1Id = 1;
|
||||
var leftUpperArmId = 2;
|
||||
var leftLowerArmId = 3;
|
||||
var rightUpperArmId = 4;
|
||||
var rightLowerArmId = 5;
|
||||
var leftUpperLegId = 6;
|
||||
var leftLowerLegId = 7;
|
||||
var rightUpperLegId = 8;
|
||||
var rightLowerLegId = 9;
|
||||
var head2Id = 10;
|
||||
|
||||
var torsoHeight = 5.0;
|
||||
var torsoWidth = 2.0;
|
||||
var upperArmHeight = 2.0;
|
||||
var lowerArmHeight = 2.0;
|
||||
var upperArmWidth = 0.7;
|
||||
var lowerArmWidth = 0.5;
|
||||
var upperLegWidth = 0.7;
|
||||
var lowerLegWidth = 0.5;
|
||||
var lowerLegHeight = 2.0;
|
||||
var upperLegHeight = 3.0;
|
||||
var headHeight = 1.5;
|
||||
var headWidth = 1.0;
|
||||
|
||||
var numNodes = 10;
|
||||
var numAngles = 11;
|
||||
|
||||
|
||||
var theta = [200, 0, 180, 0, 180, 0, 180, 0, 180, 0, 0];
|
||||
|
||||
|
||||
var stack = [];
|
||||
var figure = [];
|
||||
|
||||
for( var i=0; i<numNodes; i++) figure[i] = createNode(null, null, null, null);
|
||||
|
||||
var vBuffer;
|
||||
|
||||
function createNode(transform, render, sibling, child){
|
||||
var node = {
|
||||
transform: transform,
|
||||
render: render,
|
||||
sibling: sibling,
|
||||
child: child,
|
||||
}
|
||||
return node;
|
||||
}
|
||||
function initNodes(Id) {
|
||||
|
||||
var m = mat4();
|
||||
|
||||
switch(Id) {
|
||||
case torsoId:
|
||||
m = rotate(theta[torsoId], vec3(0, 1, 0) );
|
||||
figure[torsoId] = createNode( m, torso, null, headId );
|
||||
break;
|
||||
case headId:
|
||||
case head1Id:
|
||||
case head2Id:
|
||||
m = translate(0.0, torsoHeight+0.5*headHeight, 0.0);
|
||||
m = mult(m, rotate(theta[head1Id], vec3(1, 0, 0)))
|
||||
m = mult(m, rotate(theta[head2Id], vec3(0, 1, 0)));
|
||||
m = mult(m, translate(0.0, -0.5*headHeight, 0.0));
|
||||
figure[headId] = createNode( m, head, leftUpperArmId, null);
|
||||
break;
|
||||
case leftUpperArmId:
|
||||
m = translate(-(torsoWidth/1.75), 0.95*torsoHeight, 0.0);
|
||||
m = mult(m, rotate(theta[leftUpperArmId], vec3(1, 0, 0)));
|
||||
figure[leftUpperArmId] = createNode( m, leftUpperArm, rightUpperArmId, leftLowerArmId );
|
||||
break;
|
||||
case rightUpperArmId:
|
||||
m = translate(torsoWidth/1.75, 0.95*torsoHeight, 0.0);
|
||||
m = mult(m, rotate(theta[rightUpperArmId], vec3(1, 0, 0)));
|
||||
figure[rightUpperArmId] = createNode( m, rightUpperArm, leftUpperLegId, rightLowerArmId );
|
||||
break;
|
||||
case leftUpperLegId:
|
||||
m = translate(-(torsoWidth/2.0), 0.1*upperLegHeight, 0.0);
|
||||
m = mult(m , rotate(theta[leftUpperLegId], vec3(1, 0, 0)));
|
||||
figure[leftUpperLegId] = createNode( m, leftUpperLeg, rightUpperLegId, leftLowerLegId );
|
||||
break;
|
||||
case rightUpperLegId:
|
||||
m = translate(torsoWidth/2.0, 0.1*upperLegHeight, 0.0);
|
||||
m = mult(m, rotate(theta[rightUpperLegId], vec3(1, 0, 0)));
|
||||
figure[rightUpperLegId] = createNode( m, rightUpperLeg, null, rightLowerLegId );
|
||||
break;
|
||||
case leftLowerArmId:
|
||||
m = translate(0.0, upperArmHeight, 0.0);
|
||||
m = mult(m, rotate(theta[leftLowerArmId], vec3(1, 0, 0)));
|
||||
figure[leftLowerArmId] = createNode( m, leftLowerArm, null, null );
|
||||
break;
|
||||
case rightLowerArmId:
|
||||
m = translate(0.0, upperArmHeight, 0.0);
|
||||
m = mult(m, rotate(theta[rightLowerArmId], vec3(1, 0, 0)));
|
||||
figure[rightLowerArmId] = createNode( m, rightLowerArm, null, null );
|
||||
break;
|
||||
case leftLowerLegId:
|
||||
m = translate(0.0, upperLegHeight, 0.0);
|
||||
m = mult(m, rotate(theta[leftLowerLegId],vec3(1, 0, 0)));
|
||||
figure[leftLowerLegId] = createNode( m, leftLowerLeg, null, null );
|
||||
break;
|
||||
case rightLowerLegId:
|
||||
m = translate(0.0, upperLegHeight, 0.0);
|
||||
m = mult(m, rotate(theta[rightLowerLegId], vec3(1, 0, 0)));
|
||||
figure[rightLowerLegId] = createNode( m, rightLowerLeg, null, null );
|
||||
break;
|
||||
}
|
||||
}
|
||||
function traverse(Id) {
|
||||
if(Id == null) return;
|
||||
stack.push(modelViewMatrix);
|
||||
modelViewMatrix = mult(modelViewMatrix, figure[Id].transform);
|
||||
figure[Id].render();
|
||||
if(figure[Id].child != null) traverse(figure[Id].child);
|
||||
modelViewMatrix = stack.pop();
|
||||
if(figure[Id].sibling != null) traverse(figure[Id].sibling);
|
||||
}
|
||||
function torso() {
|
||||
instanceMatrix = mult(modelViewMatrix, translate(0.0, 0.5*torsoHeight, 0.0) );
|
||||
instanceMatrix = mult(instanceMatrix, scale( torsoWidth, torsoHeight, torsoWidth));
|
||||
gl.uniformMatrix4fv(modelViewMatrixLoc, false, flatten(instanceMatrix) );
|
||||
for(var i =0; i<6; i++) gl.drawArrays(gl.TRIANGLE_FAN, 4*i, 4);
|
||||
}
|
||||
function head() {
|
||||
instanceMatrix = mult(modelViewMatrix, translate(0.0, 0.5 * headHeight, 0.0 ));
|
||||
instanceMatrix = mult(instanceMatrix, scale(headWidth, headHeight, headWidth) );
|
||||
gl.uniformMatrix4fv(modelViewMatrixLoc, false, flatten(instanceMatrix) );
|
||||
for(var i =0; i<6; i++) gl.drawArrays(gl.TRIANGLE_FAN, 4*i, 4);
|
||||
}
|
||||
function leftUpperArm() {
|
||||
instanceMatrix = mult(modelViewMatrix, translate(0.0, 0.5 * upperArmHeight, 0.0) );
|
||||
instanceMatrix = mult(instanceMatrix, scale(upperArmWidth, upperArmHeight, upperArmWidth) );
|
||||
gl.uniformMatrix4fv(modelViewMatrixLoc, false, flatten(instanceMatrix) );
|
||||
for(var i =0; i<6; i++) gl.drawArrays(gl.TRIANGLE_FAN, 4*i, 4);
|
||||
}
|
||||
function leftLowerArm() {
|
||||
instanceMatrix = mult(modelViewMatrix, translate(0.0, 0.5 * lowerArmHeight, 0.0) );
|
||||
instanceMatrix = mult(instanceMatrix, scale(lowerArmWidth, lowerArmHeight, lowerArmWidth) );
|
||||
gl.uniformMatrix4fv(modelViewMatrixLoc, false, flatten(instanceMatrix) );
|
||||
for(var i =0; i<6; i++) gl.drawArrays(gl.TRIANGLE_FAN, 4*i, 4);
|
||||
}
|
||||
function rightUpperArm() {
|
||||
instanceMatrix = mult(modelViewMatrix, translate(0.0, 0.5 * upperArmHeight, 0.0) );
|
||||
instanceMatrix = mult(instanceMatrix, scale(upperArmWidth, upperArmHeight, upperArmWidth) );
|
||||
gl.uniformMatrix4fv(modelViewMatrixLoc, false, flatten(instanceMatrix) );
|
||||
for(var i =0; i<6; i++) gl.drawArrays(gl.TRIANGLE_FAN, 4*i, 4);
|
||||
}
|
||||
function rightLowerArm() {
|
||||
instanceMatrix = mult(modelViewMatrix, translate(0.0, 0.5 * lowerArmHeight, 0.0) );
|
||||
instanceMatrix = mult(instanceMatrix, scale(lowerArmWidth, lowerArmHeight, lowerArmWidth) );
|
||||
gl.uniformMatrix4fv(modelViewMatrixLoc, false, flatten(instanceMatrix) );
|
||||
for(var i =0; i<6; i++) gl.drawArrays(gl.TRIANGLE_FAN, 4*i, 4);
|
||||
}
|
||||
function leftUpperLeg() {
|
||||
instanceMatrix = mult(modelViewMatrix, translate(0.0, 0.5 * upperLegHeight, 0.0) );
|
||||
instanceMatrix = mult(instanceMatrix, scale(upperLegWidth, upperLegHeight, upperLegWidth) );
|
||||
gl.uniformMatrix4fv(modelViewMatrixLoc, false, flatten(instanceMatrix) );
|
||||
for(var i =0; i<6; i++) gl.drawArrays(gl.TRIANGLE_FAN, 4*i, 4);
|
||||
}
|
||||
function leftLowerLeg() {
|
||||
instanceMatrix = mult(modelViewMatrix, translate( 0.0, 0.5 * lowerLegHeight, 0.0) );
|
||||
instanceMatrix = mult(instanceMatrix, scale(lowerLegWidth, lowerLegHeight, lowerLegWidth) );
|
||||
gl.uniformMatrix4fv(modelViewMatrixLoc, false, flatten(instanceMatrix) );
|
||||
for(var i =0; i<6; i++) gl.drawArrays(gl.TRIANGLE_FAN, 4*i, 4);
|
||||
}
|
||||
function rightUpperLeg() {
|
||||
instanceMatrix = mult(modelViewMatrix, translate(0.0, 0.5 * upperLegHeight, 0.0) );
|
||||
instanceMatrix = mult(instanceMatrix, scale(upperLegWidth, upperLegHeight, upperLegWidth) );
|
||||
gl.uniformMatrix4fv(modelViewMatrixLoc, false, flatten(instanceMatrix) );
|
||||
for(var i =0; i<6; i++) gl.drawArrays(gl.TRIANGLE_FAN, 4*i, 4);
|
||||
}
|
||||
function rightLowerLeg() {
|
||||
instanceMatrix = mult(modelViewMatrix, translate(0.0, 0.5 * lowerLegHeight, 0.0) );
|
||||
instanceMatrix = mult(instanceMatrix, scale(lowerLegWidth, lowerLegHeight, lowerLegWidth) )
|
||||
gl.uniformMatrix4fv(modelViewMatrixLoc, false, flatten(instanceMatrix) );
|
||||
for(var i =0; i<6; i++) gl.drawArrays(gl.TRIANGLE_FAN, 4*i, 4);
|
||||
}
|
||||
|
||||
function quad(a, b, c, d) {
|
||||
var t1 = subtract(vertices[b], vertices[a]);
|
||||
var t2 = subtract(vertices[c], vertices[b]);
|
||||
var normal = cross(t1, t2);
|
||||
normal = vec3(normal);
|
||||
|
||||
positionsArray.push(vertices[a]);
|
||||
normalsArray.push(normal);
|
||||
positionsArray.push(vertices[b]);
|
||||
normalsArray.push(normal);
|
||||
positionsArray.push(vertices[c]);
|
||||
normalsArray.push(normal);
|
||||
positionsArray.push(vertices[d]);
|
||||
normalsArray.push(normal);
|
||||
}
|
||||
function cube(){
|
||||
quad( 1, 0, 3, 2 );
|
||||
quad( 2, 3, 7, 6 );
|
||||
quad( 3, 0, 4, 7 );
|
||||
quad( 6, 5, 1, 2 );
|
||||
quad( 4, 5, 6, 7 );
|
||||
quad( 5, 4, 0, 1 );
|
||||
}
|
||||
|
||||
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
|
||||
program = initShaders( gl, "vertex-shader", "fragment-shader");
|
||||
gl.useProgram( program);
|
||||
|
||||
instanceMatrix = mat4();
|
||||
projectionMatrix = ortho(-10.0,10.0,-10.0,10.0,-10.0,10.0);
|
||||
modelViewMatrix = mat4();
|
||||
|
||||
gl.uniformMatrix4fv(gl.getUniformLocation( program, "uModelViewMatrix"), false, flatten(modelViewMatrix) );
|
||||
gl.uniformMatrix4fv( gl.getUniformLocation( program, "uProjectionMatrix"), false, flatten(projectionMatrix) );
|
||||
|
||||
modelViewMatrixLoc = gl.getUniformLocation(program, "uModelViewMatrix")
|
||||
|
||||
cube();
|
||||
|
||||
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);
|
||||
|
||||
var ambientProduct = mult(lightAmbient, materialAmbient);
|
||||
var diffuseProduct = mult(lightDiffuse, materialDiffuse);
|
||||
var specularProduct = mult(lightSpecular, materialSpecular);
|
||||
|
||||
document.getElementById("slider0").onchange = function(event) {
|
||||
theta[torsoId ] = event.target.value;
|
||||
initNodes(torsoId);
|
||||
};
|
||||
|
||||
document.getElementById("Button1").onclick = function(){
|
||||
//walk
|
||||
walk = !walk;
|
||||
initialPos = false;
|
||||
run = false;
|
||||
};
|
||||
document.getElementById("Button2").onclick = function(){
|
||||
//run
|
||||
run = !run;
|
||||
initialPos = false;
|
||||
walk = false;
|
||||
};
|
||||
document.getElementById("Button3").onclick = function(){
|
||||
//agree
|
||||
agree = !agree;
|
||||
initialPos = false;
|
||||
|
||||
};
|
||||
document.getElementById("Button4").onclick = function(){
|
||||
//disagree
|
||||
disagree = !disagree
|
||||
initialPos = false;
|
||||
};
|
||||
document.getElementById("Button9").onclick = function(){
|
||||
//reset
|
||||
initialPos = true;
|
||||
walk = false;
|
||||
run = false;
|
||||
agree = false;
|
||||
disagree = false;
|
||||
|
||||
|
||||
};
|
||||
|
||||
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);
|
||||
|
||||
for(i=0; i<numNodes; i++) initNodes(i);
|
||||
|
||||
render();
|
||||
}
|
||||
|
||||
var render = function() {
|
||||
|
||||
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
|
||||
|
||||
if (initialPos){
|
||||
//Order: Torso, Head X, Upper LA, Lower LA, Upper RA, Lower LA, Upper LL, Lower LL, Upper RL, Lower LL, Head Y
|
||||
theta = [200, 0, 180, 0, 180, 0, 180, 0, 180, 0, 0];
|
||||
}
|
||||
if (!initialPos){
|
||||
|
||||
//Need to fix other buttons besides reset and walks
|
||||
|
||||
if (walk || run){
|
||||
if (walk){
|
||||
speed = 1;
|
||||
theta[leftLowerArmId] = 0;
|
||||
theta[rightLowerArmId] = 0;
|
||||
theta[leftLowerLegId] = 0;
|
||||
theta[rightLowerLegId] = 0;
|
||||
run = false;
|
||||
}
|
||||
if (run){
|
||||
speed = 3;
|
||||
theta[leftLowerArmId] = -45;
|
||||
theta[rightLowerArmId] = -45;
|
||||
theta[leftLowerLegId] = 45;
|
||||
theta[rightLowerLegId] = 45;
|
||||
walk = false;
|
||||
}
|
||||
//create stops for limbs to set direction
|
||||
//legs and arms move inversely so only need to check on of each limb
|
||||
if (legFWD){
|
||||
//Change direction if greater than stop value
|
||||
if (theta[leftUpperLegId] >= (180 + legStop)){
|
||||
legFWD = !legFWD;
|
||||
}
|
||||
//otherwise move leg
|
||||
theta[leftUpperLegId] += speed;
|
||||
theta[rightUpperLegId] -= speed;
|
||||
}
|
||||
if (!legFWD){
|
||||
//Change direction if less than stop value
|
||||
if (theta[leftUpperLegId] <= (180 - legStop)){
|
||||
legFWD = !legFWD;
|
||||
}
|
||||
//otherwise move leg
|
||||
theta[leftUpperLegId] -= speed;
|
||||
theta[rightUpperLegId] += speed;
|
||||
}
|
||||
|
||||
if (armFWD){
|
||||
if (theta[leftUpperArmId] >= (180 + armStop)){
|
||||
armFWD = !armFWD;
|
||||
}
|
||||
theta[leftUpperArmId] += speed;
|
||||
theta[rightUpperArmId] -= speed;
|
||||
|
||||
}
|
||||
if (!armFWD){
|
||||
if (theta[leftUpperArmId] <= (180 - armStop)){
|
||||
armFWD = !armFWD;
|
||||
}
|
||||
theta[leftUpperArmId] -= speed;
|
||||
theta[rightUpperArmId] += speed;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
if (agree){
|
||||
if (lookUp){
|
||||
//change x back to original
|
||||
theta[head2Id] = 0;
|
||||
disagree = false;
|
||||
//Y
|
||||
if(theta[head1Id] >= 45){
|
||||
lookUp = !lookUp;
|
||||
}
|
||||
theta[head1Id] += 1;
|
||||
}
|
||||
if (!lookUp){
|
||||
if(theta[head1Id] <= 0){
|
||||
lookUp = !lookUp;
|
||||
}
|
||||
theta[head1Id] -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (disagree){
|
||||
//Change y back to original
|
||||
theta[head1Id] = 0;
|
||||
agree = false;
|
||||
//X
|
||||
if (lookLft){
|
||||
if(theta[head2Id] >= (theta[torsoId] + 45)){
|
||||
lookLft = !lookLft;
|
||||
}
|
||||
theta[head2Id] += 1;
|
||||
|
||||
}
|
||||
if (!lookLft){
|
||||
if(theta[head2Id] <= (theta[torsoId] - 45)){
|
||||
lookLft = !lookLft;
|
||||
}
|
||||
theta[head2Id] -= 1;
|
||||
}
|
||||
}
|
||||
if ((!walk && !run) && (!agree && !disagree)){
|
||||
//move to standing
|
||||
//Order: Torso, Head X, Upper LA, Lower LA, Upper RA, Lower LA, Upper LL, Lower LL, Upper RL, Lower LL, Head Y
|
||||
theta = [200, 0, 180, 0, 180, 0, 180, 0, 180, 0, 0];
|
||||
}
|
||||
}
|
||||
//Redraw
|
||||
for(i=0; i<numNodes; i++) initNodes(i);
|
||||
|
||||
traverse(torsoId);
|
||||
requestAnimationFrame(render);
|
||||
}
|
||||
Reference in new issue
Block a user