96 lines
2.4 KiB
JavaScript
96 lines
2.4 KiB
JavaScript
"use strict";
|
|
|
|
var canvas;
|
|
var gl;
|
|
|
|
var xAxis = 0;
|
|
var yAxis = 1;
|
|
var zAxis = 2;
|
|
|
|
var axis = 0;
|
|
var theta = [ 0, 0, 0 ];
|
|
|
|
var thetaLoc;
|
|
|
|
var flag = false;
|
|
|
|
var points = [];
|
|
var colors = [];
|
|
|
|
window.onload = function init()
|
|
{
|
|
canvas = document.getElementById( "gl-canvas" );
|
|
|
|
gl = canvas.getContext('webgl2');
|
|
if (!gl) { alert( "WebGL 2.0 isn't available" ); }
|
|
|
|
var myCube = cube();
|
|
var myCube2 = cube(1.0);
|
|
|
|
myCube.scale(0.5, 0.5, 0.5);
|
|
myCube2.scale(0.5, 0.5, 0.5);
|
|
myCube.rotate(45, [1, 1, 1]);
|
|
myCube.translate(0.5, 0.5, 0.0);
|
|
myCube2.translate(-0.5, -0.5, 0.0);
|
|
|
|
|
|
colors = myCube.TriangleVertexColors;
|
|
points = myCube.TriangleVertices;
|
|
points = points.concat(myCube2.TriangleVertices);
|
|
colors = colors.concat(myCube2.TriangleFaceColors);
|
|
|
|
gl.viewport( 0, 0, canvas.width, canvas.height );
|
|
gl.clearColor( 1.0, 1.0, 1.0, 1.0 );
|
|
|
|
gl.enable(gl.DEPTH_TEST);
|
|
//gl.enable(gl.CULL);
|
|
|
|
//
|
|
// 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(points), gl.STATIC_DRAW );
|
|
|
|
|
|
var positionLoc = gl.getAttribLocation( program, "aPosition" );
|
|
gl.vertexAttribPointer( positionLoc, 4, gl.FLOAT, false, 0, 0 );
|
|
gl.enableVertexAttribArray( positionLoc );
|
|
|
|
thetaLoc = gl.getUniformLocation(program, "theta");
|
|
|
|
//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("ButtonT").onclick = function(){flag = !flag;};
|
|
render();
|
|
}
|
|
|
|
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, points.length);
|
|
requestAnimationFrame( render );
|
|
}
|