# Visible Surface Ray Tracing Algorithm

## In this example below you will see how to do a Visible Surface Ray Tracing Algorithm with some HTML / CSS and JavascriptSec 4-13, PECG, D.F.RogersRay Tracing algorithm used as hidden surface removal algorithm Viewpoint is located at infinity on the positive z axis, rays are parallel to the z axis, scene has been transformed to image space

This awesome code was written by arcollector, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
• HTML
• CSS
• JavaScript
``````<!DOCTYPE html>
<html lang="en" >

<meta charset="UTF-8">
<title>Visible Surface Ray Tracing Algorithm</title>

<body>

<html>
<body>
<table class="framebuffer"></table>
</body>
</html>

<script  src="js/index.js"></script>

</body>

</html>
``````
``````
.framebuffer {
border-collapse: collapse;
border: 1px solid #000;
table-layout: fixed;
margin: 0 auto;
}

.framebuffer td {
border: 1px solid #000;
width: 10px;
height: 10px;
}
``````
``````
// --------------------------------------------
//
// --------------------------------------------
var Vector = function( x,y,z,w ) {
this.x = x;
this.y = y;
this.z = z;
this.w = typeof w === 'undefined' ? 1 : w;
return this;
};

Vector.prototype.length = function() {
return Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z);
};

Vector.prototype.normalize = function() {
var l = this.length();
this.x /= l;
this.y /= l;
this.z /= l;
return this;
};

Vector.prototype.copy = function() {
return new Vector(this.x,this.y,this.z);
};

Vector.prototype.toString = function() {
console.log( "Purple,Point[" +
"{" + ((Math.abs(this.x)<=1e-6)?0:this.x) + "," + ((Math.abs(this.y)<=1e-6)?0:this.y) + "," + ((Math.abs(this.z)<=1e-6)?0:this.z) + "}" +
"],"
);
};

Vector.prototype.transform = function( m ) {
var x = this.x*m._00 + this.y*m._01 + this.z*m._02 + this.w*m._03;
var y = this.x*m._10 + this.y*m._11 + this.z*m._12 + this.w*m._13;
var z = this.x*m._20 + this.y*m._21 + this.z*m._22 + this.w*m._23;
var w = this.x*m._30 + this.y*m._31 + this.z*m._32 + this.w*m._33;

this.x = x/w;
this.y = y/w;
this.z = z/w;
this.w = 1;
};

// --------------------------------------------
//
// --------------------------------------------
var vectorToRGB = function( v ) {
return "rgb(" + v.x + "," + v.y + "," + v.z + ")";
};

// --------------------------------------------
//
// --------------------------------------------
var frameBuffer = [];
var WIDTH = 32;
var HEIGHT = 32;
var BACKGROUND_COLOR = new Vector(0,0,0); // black
var \$table = document.querySelector('.framebuffer');
for( var y = 0; y < HEIGHT; y++ ) {
var \$tr = document.createElement( 'tr' )
for( var x = 0; x < WIDTH; x++ ) {
var \$td = document.createElement( 'td');
\$td.style.backgroundColor = vectorToRGB(BACKGROUND_COLOR);
\$td.setAttribute( 'title', '('+x+','+(-y+HEIGHT-1)+')');
\$tr.appendChild(\$td);
}
\$table.appendChild(\$tr);
}

// --------------------------------------------
//
// --------------------------------------------
var Matrix4x4 = function( v1,v2,v3,v4 ) {
v1 = v1 || new Vector(1,0,0,0);
v2 = v2 || new Vector(0,1,0,0);
v3 = v3 || new Vector(0,0,1,0);
v4 = v4 || new Vector(0,0,0,1);

this._00 = v1.x; this._10 = v2.x; this._20 = v3.x; this._30 = v4.x;
this._01 = v1.y; this._11 = v2.y; this._21 = v3.y; this._31 = v4.y;
this._02 = v1.z; this._12 = v2.z; this._22 = v3.z; this._32 = v4.z;
this._03 = v1.w; this._13 = v2.w; this._23 = v3.w; this._33 = v4.w;
return this;
};

Matrix4x4.prototype.multiply = function( m ) {
var _00 = this._00*m._00 + this._10*m._01 + this._20*m._02 + this._30*m._03;
var _10 = this._00*m._10 + this._10*m._11 + this._20*m._12 + this._30*m._13;
var _20 = this._00*m._20 + this._10*m._21 + this._20*m._22 + this._30*m._23;
var _30 = this._00*m._30 + this._10*m._31 + this._20*m._32 + this._30*m._33;

var _01 = this._01*m._00 + this._11*m._01 + this._21*m._02 + this._31*m._03;
var _11 = this._01*m._10 + this._11*m._11 + this._21*m._12 + this._31*m._13;
var _21 = this._01*m._20 + this._11*m._21 + this._21*m._22 + this._31*m._23;
var _31 = this._01*m._30 + this._11*m._31 + this._21*m._32 + this._31*m._33;

var _02 = this._02*m._00 + this._12*m._01 + this._22*m._02 + this._32*m._03;
var _12 = this._02*m._10 + this._12*m._11 + this._22*m._12 + this._32*m._13;
var _22 = this._02*m._20 + this._12*m._21 + this._22*m._22 + this._32*m._23;
var _32 = this._02*m._30 + this._12*m._31 + this._22*m._32 + this._32*m._33;

var _03 = this._03*m._00 + this._13*m._01 + this._23*m._02 + this._33*m._03;
var _13 = this._03*m._10 + this._13*m._11 + this._23*m._12 + this._33*m._13;
var _23 = this._03*m._20 + this._13*m._21 + this._23*m._22 + this._33*m._23;
var _33 = this._03*m._30 + this._13*m._31 + this._23*m._32 + this._33*m._33;

this._00 = _00; this._10 = _10; this._20 = _20; this._30 = _30;
this._01 = _01; this._11 = _11; this._21 = _21; this._31 = _31;
this._02 = _02; this._12 = _12; this._22 = _22; this._32 = _32;
this._03 = _03; this._13 = _13; this._23 = _23; this._33 = _33;

return this;
};

Matrix4x4.prototype.transpose = function() {
return new Matrix4x4(
new Vector( this._00,this._10,this._20,this._30 ),
new Vector( this._01,this._11,this._21,this._31 ),
new Vector( this._02,this._12,this._22,this._32 ),
new Vector( this._03,this._13,this._23,this._33 )
);
};

Matrix4x4.prototype.toString = function() {
console.log(
"mat = {\n" +
"{" + this._00 + "," + this._10 + "," + this._20 + "," + this._30 + "},\n" +
"{" + this._01 + "," + this._11 + "," + this._21 + "," + this._31 + "},\n" +
"{" + this._02 + "," + this._12 + "," + this._22 + "," + this._32 + "},\n" +
"{" + this._03 + "," + this._13 + "," + this._23 + "," + this._33 + "},\n" +
"}"
);
};

// --------------------------------------------
//
// --------------------------------------------
var VectorLine = function( point, direction ) {
this.point = point;
this.direction = direction;
};

VectorLine.prototype.getMatrixOntoZAxis = function() {
var point = this.point.copy();
//point.normalize();

var matrix = new Matrix4x4(
new Vector(1,0,0,-point.x),
new Vector(0,1,0,-point.y),
new Vector(0,0,1,0),
new Vector(0,0,0,1)
);

var inverseMatrix = new Matrix4x4(
new Vector(1,0,0,point.x),
new Vector(0,1,0,point.y),
new Vector(0,0,1,0),
new Vector(0,0,0,1)
);

/*var l = point.length();
var l_yz = new Vector(0,point.y,point.z).length();
var rotX = new Matrix4x4(
new Vector(1,0,0,0),
new Vector(0,point.z/l_yz,-point.y/l_yz,0),
new Vector(0,point.y/l_yz,point.z/l_yz,0),
new Vector(0,0,0,1)
);
var rotY = new Matrix4x4(
new Vector(l_yz/l,0,-point.x/l,0),
new Vector(0,1,0,0),
new Vector(point.x/l,0,l_yz/l,0),
new Vector(0,0,0,1)
);

var matrix = new Matrix4x4();
matrix.multiply( rotX );
matrix.multiply( rotY );

var inverseMatrix = new Matrix4x4();
inverseMatrix.multiply( rotY.transpose() );
inverseMatrix.multiply( rotX.transpose() );*/

/*var test = new Matrix4x4();
test.multiply( matrix );
test.multiply( inverseMatrix );
test.toString();*/

return  [ matrix, inverseMatrix ];
};

// --------------------------------------------
//
// --------------------------------------------
var Polygon = function() {
this.vertices = [];
for( var i = 0, l = arguments.length-1; i < l; i++ ) {
this.vertices[i] = arguments[i];
}
this.color = arguments[arguments.length-1];
this.calcPlaneEquation();
this.calcBoundingSphere();
this.calcBoundingBox();
};

Polygon.prototype.calcPlaneEquation = function() {
this.normal = new Vector(0,0,0);
for( var i = 0, l = this.vertices.length-1; i < l; i++ ) {
var vi = this.vertices[i];
var vj = this.vertices[i+1];
this.normal.x += (vi.y-vj.y)*(vi.z+vj.z);
this.normal.y += (vi.z-vj.z)*(vi.x+vj.x);
this.normal.z += (vi.x-vj.x)*(vi.y+vj.y);
}
var vi = this.vertices[this.vertices.length-1];
var vj = this.vertices[0];
this.normal.x += (vi.y-vj.y)*(vi.z+vj.z);
this.normal.y += (vi.z-vj.z)*(vi.x+vj.x);
this.normal.z += (vi.x-vj.x)*(vi.y+vj.y);
//this.normal.normalize();
this.A = this.normal.x;
this.B = this.normal.y;
this.C = this.normal.z;
var v = this.vertices[0];
this.D = -(this.A*v.x + this.B*v.y + this.C*v.z);
};

Polygon.prototype.displayPlaneEquation = function() {
console.log( this.A+"x" + (this.B<0?"":"+")  + this.B+"y" + (this.C<0?"":"+") + this.C+"z" + (this.D<0?"":"+") + this.D + "=0" );
};

Polygon.prototype.transform = function( m ) {
for( var i = 0, l = this.vertices.length; i < l; i++ ) {
this.vertices[i].transform( m );
}
this.calcPlaneEquation();
};

Polygon.prototype.calcCentroid = function() {
this.center = new Vector(0,0,0);
for( var i = 0, l = this.vertices.length; i < l; i++ ) {
var v = this.vertices[i];
this.center.x += v.x;
this.center.y += v.y;
this.center.z += v.z;
}
this.center.x /= this.vertices.length;
this.center.y /= this.vertices.length;
this.center.z /= this.vertices.length;
};

Polygon.prototype.calcBoundingSphere = function() {
this.calcCentroid();
var v = this.vertices[0];
this.radius2 = Math.pow(this.center.x - v.x, 2) + Math.pow(this.center.y - v.y, 2) + Math.pow(this.center.z - v.z, 2);
};

Polygon.prototype.displayBoundingSphere = function() {
console.log( "Sphere center: " + "(" + this.center.x + "," + this.center.y + "," + this.center.z + ")" + "\n" +
);
};

Polygon.prototype.calcBoundingBox = function() {
var v = this.vertices[0];
this.min = new Vector(v.x,v.y,v.z);
this.max = new Vector(v.x,v.y,v.z);
for( var i = 1, l = this.vertices.length; i < l; i++ ) {
var v = this.vertices[i];
if( this.min.x > v.x ) {
this.min.x = v.x;
}
if( this.max.x < v.x ) {
this.max.x = v.x;
}
if( this.min.y > v.y ) {
this.min.y = v.y;
}
if( this.max.y < v.y ) {
this.max.y = v.y;
}
if( this.min.z > v.z ) {
this.min.z = v.z;
}
if( this.max.z < v.z ) {
this.max.z = v.z;
}
}
};

Polygon.prototype.displayBoundingBox = function() {
console.log( this.min.x + "," + this.max.x + ", " +
this.min.y + "," + this.max.y + ", " +
this.min.z + "," + this.max.z
);
};

Polygon.prototype.transformBoundingBox = function( m ) {
this.min.transform( m );
this.max.transform( m );
};

// --------------------------------------------
//
// --------------------------------------------
var VisibleSurfaceRayTracing = {

objectsList: [],
activeObjectList: [],
intersectionList: [],

calcDistanceSphereRay: function( poly, ray ) {
return Math.pow(poly.center.x - ray.x, 2) + Math.pow(poly.center.y - ray.y, 2);
},

rayIntersectSphere: function( poly, ray ) {
if( this.calcDistanceSphereRay(poly,ray) < poly.radius2 ) {
return true;
}
return false;
},

this.objectsList.push( poly );
},

render: function() {
var direction = new Vector(0,0,1);
for( var y = 0; y < HEIGHT; y++ ) {
for( var x = 0; x < WIDTH; x++ ) {
var ray = new Vector(x+.5,y+.5,0);
//console.log( "Evaluating at point (" + ray.x + "," + ray.y + ")" );
var line = new VectorLine(ray, direction);
for( var i = 0, l = this.objectsList.length; i < l; i++ ) {
var poly = this.objectsList[i];
if( this.rayIntersectSphere(poly , ray) ) {
this.activeObjectList.push( poly );
}
}
if( this.activeObjectList.length ) {
var m = line.getMatrixOntoZAxis();
var transformation = m[0];
var inverse = m[1];
for( var i = 0, l = this.activeObjectList.length; i < l; i++ ) {
var poly = this.activeObjectList[i];
//console.log( "Testing poly with color (" + poly.color.x + "," + poly.color.y + "," + poly.color.z + ")" );
poly.transformBoundingBox( transformation );
if( Math.sign(poly.min.x) != Math.sign(poly.max.x) &&
Math.sign(poly.min.y) != Math.sign(poly.max.y) ) {
//console.log( "\tIntersection with bounding box" );
if( this.pointInsidePoly( ray, poly ) ) {
poly.transform( transformation );
poly.calcPlaneEquation();
var z = this.calcDepthValue( poly );
//console.log( "\tIntersection depth at " + z );
this.intersectionList.push( { poly: poly, z: z } );
poly.transform( inverse );
} else {
//console.log( "\tPoint is outside" );
}
}
poly.transformBoundingBox( inverse );
}
if( this.intersectionList.length ) {
this.intersectionList.sort( this.sortFunc );
} else {
}
} else {
//console.log( "No polys intersect at this point" );
}
this.intersectionList = [];
this.activeObjectList = [];
//console.log( "-----------" );
}
}

\$tds = \$table.querySelectorAll( 'td' );
var index = 0;
for( var y = 0; y < HEIGHT; y++ ) {
for( var x = 0; x < WIDTH; x++ ) {
index++;
}
}

},

pointInsidePoly: function( ray, poly ) {
var x = ray.x, y = ray.y;
var vi = poly.vertices[0];
var vj = poly.vertices[1];
var x1 = vi.x, y1 = vi.y;
var x2 = vj.x, y2 = vj.y;
var prevSign = (x2-x1)*(y-y1)-(x-x1)*(y2-y1);
prevSign = Math.abs(prevSign) <= 1e-6 ? 0 : Math.sign(prevSign);
for( var i = 1, l = poly.vertices.length-1; i < l; i++ ) {
vi = poly.vertices[i];
vj = poly.vertices[i+1];
x1 = vi.x, y1 = vi.y;
x2 = vj.x, y2 = vj.y;
var curSign = (x2-x1)*(y-y1)-(x-x1)*(y2-y1);
curSign = Math.abs(curSign) <= 1e-6 ? 0 : Math.sign(curSign);
if( prevSign != 0 && curSign != 0 && curSign != prevSign )  {
return false;
}
prevSign = curSign;
}
vi = poly.vertices[poly.vertices.length-1];
vj = poly.vertices[0];
x1 = vi.x, y1 = vi.y;
x2 = vj.x, y2 = vj.y;
var curSign = (x2-x1)*(y-y1)-(x-x1)*(y2-y1);
curSign = Math.abs(curSign) <= 1e-6 ? 0 : Math.sign(curSign);
if( curSign != 0 && prevSign != 0 && prevSign != curSign ) {
return false;
}
return true;
},

calcDepthValue: function( poly ) {
return -poly.D/poly.C;
},

sortFunc: function( a, b ) {
return b.z - a.z;
},

};

// --------------------------------------------
//
// --------------------------------------------
var triangle = new Polygon(
new Vector(15,15,15),
new Vector(25,25,5),
new Vector(30,10,5),
new Vector(0,255,0) // green
);
var rectangle = new Polygon(
new Vector(10,5,10),
new Vector(10,25,10),
new Vector(25,25,10),
new Vector(25,5,10),
new Vector(255,0,0) // red
);