/*
The main line of the previous clip space vertex shader contained this code:
gl_Position = vec4(position, 1.0);
The position variable is what was passed in as an attribute to the shader and was defined in the draw method. This is a three dimensional point, but the gl_Position variable that ends up getting passed down through the pipeline is actually 4 dimensions. Instead of (x,y,z) it is (x,y,z,w). In this example the w coordinate is being set to 1.0. The obvious question is "why the extra dimension?" It turns out that this addition allows for lots of nice techniques for manipulating 3d data.
A three dimensional point is defined in a typical Cartesian coordinate system. The added 4th dimension changes this point into a homogeneous coordinate. It still represents a point in 3d space and it can easily be demonstrated how to construct this type of coordinate through a pair of simple functions.
*/
function cartesianToHomogeneous (point) {
var x = point[0];
var y = point[1];
var z = point[2];
return [x, y, z, 1];
}
function homogeneousToCartesian (point) {
var x = point[0];
var y = point[1];
var z = point[2];
var w = point[3];
return [x/w, y/w, z/w];
}
/*
As can be seen, the w component divides the x, y, and z components. When the w component is a non-zero real number then homogeneous coordinate easily translates back into a normal point in Cartesian space. Now what happens if the w component is zero? In JavaScript the value returned would be as follows.
*/
homogeneousToCartesian([10,4,5,0]);
// Evaluates to: [Infinity, Infinity, Infinity]
/*
This homogeneous coordinate represents some point at infinity. This is a handy way to represent a ray shooting off from the origin in a specific direction. In addition to a ray, it could also be thought of as a representation of a directional vector. If this homogeneous coordinate is multiplied against a matrix with a translation then the translation is effectively stripped out.
TODO...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!