1
edit
Changes
no edit summary
===Parser Interface===
==Intro==
Currently XBPS only supports reading .ASC file types. If you require the library to render other files, you will need to write a custom parser and register it with the library. This should not be difficult, there are only a few things your parser must implement.
===Parser Interface=Example==Here is an example which you can use to help understand the process: [link here]
The library would then take care following bit of the rest by creating code is a skeleton which you can use as an instanceof their aid to help you write your own parser, call its methods and return a point cloud objectfor XB PointStream.
<pre>
var Your_Parser_Name = (function() { /* The constructor XBPS will create an instance of your parser and pass in an object with three named properties: start - must occur exactly once. When you call this function, pass in a reference to your parser end - must occur exactly once. When you call this function, pass in a reference to your parser parse - may occur one or many times. When you call this function, pass in a reference to your parser as the first argument and an object as the second argument. This second object must have variables referencing typed single-dimensional arrays which contain the parsed values. For example, if vertsArray and colsArray were Float32Array arrays, you would call the parse function like this: var attributes = {}; attributes["ps_Vertex"] = vertsArray; attributes["ps_Color"] = colsArray; parse(thisParser, attributes); PointStream will create buffers using these values and start rendering them using the built-in shaders. Notice the variable names have been qualified with "ps_". If you are using the XB PointStream built-in shaders, you will need to use these exact variable names. These are the only two variables the built-in shaders read. If your parser reads in vertex normal data, you will need to write your own shaders to handle lighting. */ function Your_Parser_Name(config) { /*Returns the version of this parser.*/ this.__defineGetter__("version", function(){return /*!!*/;}); /*Get the number of parsed points so far.*/ this.__defineGetter__("numParsedPoints", function(){return /*!!*/;}); /*Get the total number of points in the point cloud.*/ this.__defineGetter__("numTotalPoints", function(){return /*!!*/;}); /*Returns the progress of downloading the point cloud between zero and one.*/ this.__defineGetter__("progress", function(){return /*!!*/;}); /*Returns the file size of the resource in bytes.*/ this.__defineGetter__("fileSize", function(){return /*!!*/;}); /*Path = path to the resource */ this.load = function(path){/*!!*/}; } return Your_Parser_Name;}());</pre>
function FOO_Parser(config) { var start - must occur exactly once= config.start || function(){}; var parse = config.parse || function(){}; var end = config.end || function(){}; var fileSizeInBytes = 0; var numParsedPoints = 0; var numTotalPoints = 0; var progress = 0; // keep track if onprogress event handler was called to // handle Chrome/WebKit vs. Minefield differences. Has // Minefield will call onprogress zero or many times // Chrome/WebKit will call onprogress one argumentor many times var onProgressCalled = false; var AJAX = null; /* Returns the version of this parser. */ this.__defineGetter__("version", function(){return 0.1;}); /* Get the parser itselfnumber of parsed points so far. */ end - must occur exactly once this. Has one argument__defineGetter__("numParsedPoints", function(){return numParsedPoints;}); /* Get the parser itselftotal number of points in the point cloud. */ this.__defineGetter__("numTotalPoints", function(){ return numTotalPoints;}); parse - may occur /* Get the progress of downloading the point cloud (zero to one or many times-1 if unknown) */ this. Has two arguments__defineGetter__("progress", function(){ return progress;}); /* Returns the parser itself and afile size of the resource in bytes. */ named collection of value types this.__defineGetter__("fileSize", function(){return fileSizeInBytes;});
/*occurs exactly once, when the resource begins to be downloaded */ Begins to load AJAX.onloadstart = function(evt){ start(AJAX.parser); }; /*occurs exactly once, when the resourcefile is done being downloaded */ AJAX.onload = function(evt){ var ascData = AJAX.responseText; var chunk = null;
AJAX.parseChunk(chunk);
AJAX.parseChunk = function(chunk){ /*/ this occurs over network connections, but not locally. if(chunk !== ""){ // trim leading and trailing spaces chunk = chunk.replace(/\s+$/,""); Get the version of this parser chunk = chunk.replace(/^\s+/,""); // split on white space chunk = chunk.split(/\s+/); @returns {String} parser version * var numVerts = chunk.length/3; numParsedPoints += numVerts; version var verts = new Float32Array(numVerts * 3);
for(var i = 0, j = 0, len = chunk.length; i < len; i += 3, j += 3){ verts[j] = parseFloat(chunk[i]); verts[j+1] = parseFloat(chunk[i+1]); verts[j+2] = parseFloat(chunk[i+2]); } // XB PointStream expects an object with named/value pairs // which contain the attribute arrays. These must match attribute // names found in the shader parse(AJAX.parser, {"ps_Vertex":verts}); } }; /*On Minefield, this will occur zero or many times On Chrome/WebKit this will occur one or many times */ AJAX.onprogress = function(evt){ if(evt.lengthComputable){ fileSizeInBytes = evt.total; Get the number of points which have been parsed progress = evt.loaded/evt.total; }
/*/ if we have something to actually parse Get the total number of points in the point cloud, including points if(AJAX.responseText){ which have not yet been parsed var ascData = AJAX.responseText;
<pre>function start(){ var ps = new PointStream(); ps.setup(document.getElementById('canvas')); ps.registerParser("foo", FOO_Parser); @returns ps.onRender = function render() {Number} the number of bytes in the resource or ps.translate(0, 0, -1 if unknown20); ps.render(acorn); };*/ var acorn = ps.load("pointCloud.foo");fileSize}
</pre>
<pre>
/*
The following example demonstrates how XB PointStream XBPS might use
a particular parser.
*/
// create a hypothetical parser and set the callbacks
parser = new XYZParserYour_Parser_Name({ start: startCallback, parse: parseCallback, end: finishCallback});
// load some resource
parser.load("pointcloud.xyz");
</pre>