Using comments within the code itself, can you provide an line by line explanation of the below JavaScript file? The file itself deals with WebGl and if that helps you. Please and thank you JavaScript file: function MVbuffer(size) { var b = {}; b.buf = new Float32Array(size); b.index = 0; b.push = function(x) { for(var i=0; i
Using comments within the code itself, can you provide an line by line explanation of the below JavaScript file? The file itself deals with WebGl and if that helps you.
Please and thank you
JavaScript file:
function MVbuffer(size) {
var b = {};
b.buf = new Float32Array(size);
b.index = 0;
b.push = function(x) {
for(var i=0; i<x.length; i++) {
b.buf[b.index+i] = x[i];
}
b.index += x.length;
b.type = '';
}
return b;
}
function isVector(v) {
if(v.type == "vec2" || v.type == "vec3" || v.type == "vec4") return true;
return false;
}
function isMatrix(v) {
if(v.type == "mat2" || v.type == "mat3" || v.type == "mat4") return true;
return false;
}
function radians( degrees ) {
return degrees * Math.PI / 180.0;
}
//----------------------------------------------------------------------------
function patch() {
var out = new Array(4);
for(var i = 0; i< 4; i++) out[i] = new Array(4);
out.type = "patch";
return out;
}
function curve() {
var out = new Array(4);
out.type = "curve";
return out;
}
//
//
//
function vec2()
{
var out = new Array(2);
out.type = 'vec2';
switch ( arguments.length ) {
case 0:
out[0] = 0.0;
out[1] = 0.0;
break;
case 1:
if(isVector(arguments[0] && (arguments[0].type != 'vec2'))) {
out[0] = arguments[0][0];
out[1] = arguments[0][1];
}
break;
case 2:
out[0] = arguments[0];
out[1] = arguments[1];
break;
}
return out;
}
function vec3()
{
//var result = _argumentsToArray( arguments );
var out = new Array(3);
out.type = 'vec3';
switch ( arguments.length ) {
case 0:
out[0] = 0.0;
out[1] = 0.0;
out[2] = 0.0;
return out;
case 1:
if(isVector(arguments[0]) && (arguments[0].type == "vec3")) {
out[0] = arguments[0][0];
out[1] = arguments[0][1];
out[2] = arguments[0][2];
return out;
}
case 3:
out[0] = arguments[0];
out[1] = arguments[1];
out[2] = arguments[2];
return out;
default:
throw "vec3: wrong arguments";
}
return out;
}
function vec4()
{
var out = new Array(4);
out.type = 'vec4';
switch ( arguments.length ) {
case 0:
out[0] = 0.0;
out[1] = 0.0;
out[2] = 0.0;
out[3] = 0.0;
return out;
case 1:
if(isVector(arguments[0])) {
if(arguments[0].type == "vec4") {
out[0] = arguments[0][0];
out[1] = arguments[0][1];
out[2] = arguments[0][2];
out[3] = arguments[0][3];
return out;
}
}
else if(arguments[0].type == "vec3") {
out[0] = arguments[0][0];
out[1] = arguments[0][1];
out[2] = arguments[0][2];
out[3] = 1.0;
return out;
}
else {
out[0] = arguments[0][0];
out[1] = arguments[0][1];
out[2] = arguments[0][2];
out[3] = arguments[0][3];
return out;
}
case 2:
if(typeof(arguments[0])=='number'&&arguments[1].type == 'vec3') {
out[0] = arguments[0];
out[1] = arguments[1][0];
out[2] = arguments[1][1];
out[3] = arguments[1][2];
return out;
}
return out;
case 4:
if(isVector(arguments[0])) {
out[0] = arguments[0][0];
out[1] = arguments[0][1];
out[2] = arguments[0][2];
out[3] = arguments[0][3];
return out;
}
out[0] = arguments[0];
out[1] = arguments[1];
out[2] = arguments[2];
out[3] = arguments[3];
return out;
case 3:
out[0] = arguments[0][0];
out[1] = arguments[0][1];
out[2] = arguments[0][2];
out[3] = 1.0;
return out;
default:
throw "vec4: wrong arguments";
}
}

Step by step
Solved in 3 steps









