Files
CCUCoding/CSCI440/08/cubet.js
T

134 lines
3.5 KiB
JavaScript

"use strict";
var canvas;
var gl;
var numPositions = 36;
var positions = [];
var colors = [];
var xAxis = 0;
var yAxis = 1;
var zAxis = 2;
var axis = xAxis;
var flag = true;
var HSRflag = true;
var theta = vec3(45.0, 45.0, 45.0);
var thetaLoc;
window.onload = function ()
{
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.5,0.5, 0.5, 1.0);
gl.enable(gl.DEPTH_TEST);
gl.enable(gl.BLEND);
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
//
// Load shaders and initialize attribute buffers
//
var program = initShaders(gl, "vertex-shader", "fragment-shader");
gl.useProgram(program);
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);
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, 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(positionLoc);
thetaLoc = gl.getUniformLocation(program, "uTheta");
document.getElementById("ButtonX").onclick = function(){axis = xAxis;};
document.getElementById("ButtonY").onclick = function(){axis = yAxis;};
document.getElementById("ButtonZ").onclick = function(){axis = zAxis;};
document.getElementById("ButtonT").onclick = function(){flag = !flag;};
document.getElementById("ButtonH").onclick = function(){
if(HSRflag) gl.enable(gl.DEPTH_TEST);
else gl.disable(gl.DEPTH_TEST);
//if(HSRflag) gl.enable(gl.CULL_FACE);
//else gl.disable(gl.CULL_FACE);
HSRflag = !HSRflag;
};
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)
{
var vertices = [
vec3(-0.5, -0.5, 0.5),
vec3(-0.5, 0.5, 0.5),
vec3(0.5, 0.5, 0.5),
vec3(0.5, -0.5, 0.5),
vec3(-0.5, -0.5, -0.5),
vec3(-0.5, 0.5, -0.5),
vec3(0.5, 0.5, -0.5),
vec3(0.5, -0.5, -0.5)
];
var vertexColors = [
vec4(0.0, 0.0, 0.0, 0.5), // black
vec4(1.0, 0.0, 0.0, 0.5), // red
vec4(1.0, 1.0, 0.0, 0.5), // yellow
vec4(0.0, 1.0, 0.0, 0.5), // green
vec4(0.0, 0.0, 1.0, 0.5), // blue
vec4(1.0, 0.0, 1.0, 0.5), // magenta
vec4(0.0, 1.0, 1.0, 0.5), // cyan
vec4(1.0, 1.0, 1.0, 0.5) // white
];
// 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.
var indices = [a, b, c, a, c, d];
for ( var i = 0; i < indices.length; ++i ) {
positions.push(vertices[indices[i]]);
colors.push(vertexColors[a]);
}
}
function render()
{
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
if(flag) theta[axis] += 2.0;
gl.uniform3fv(thetaLoc, theta);
gl.drawArrays(gl.TRIANGLES, 0, numPositions);
requestAnimationFrame(render);
}