Skip to content

Commit

Permalink
finished patch.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andor Salga committed Jun 26, 2011
1 parent b23f4cb commit 0d9f8f0
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 55 deletions.
Binary file added clouds/tiny.psi
Binary file not shown.
85 changes: 48 additions & 37 deletions parsers/psi.js
Expand Up @@ -31,7 +31,9 @@
@author: Mickael Medel
asydik.wordpress.com
Created: February 2011
Updated: April 2011
Updated: April 2011
Maintained by: Andor Salga
*/
var PSIParser = (function() {

Expand Down Expand Up @@ -429,6 +431,29 @@ var PSIParser = (function() {
nfactor = -0.5 + Math.pow(2.0, 10.0);
start(AJAX.parser);
};

/*
*/
AJAX.parseVertsCols = function(chunk, numBytes, byteIdx, verts, cols){
var diffX = xMax - xMin;
var diffY = yMax - yMin;
var diffZ = zMax - zMin;

var scaleX = sfactor + xMin;
var scaleY = sfactor + yMin;
var scaleZ = sfactor + zMin;

for(var point = 0; point < numTotalPoints; byteIdx += 12, point++){
verts[point*3 + 0] = (diffX * getXYZ(chunk, byteIdx )) / scaleX;
verts[point*3 + 1] = (diffY * getXYZ(chunk, byteIdx + 3)) / scaleY;
verts[point*3 + 2] = (diffZ * getXYZ(chunk, byteIdx + 6)) / scaleZ;

cols[point*3 + 0] = getRGB(chunk, byteIdx + 9 ) / 255;
cols[point*3 + 1] = getRGB(chunk, byteIdx + 10) / 255;
cols[point*3 + 2] = getRGB(chunk, byteIdx + 11) / 255;
}
};

/*
Occurs exactly once, when the file is done being downloaded.
Expand All @@ -445,6 +470,7 @@ var PSIParser = (function() {
AJAX.firstLoad(textData);
}

// !!! need to fix this
// If we downloaded the file in one go
if(firstRun && evt.lengthComputable && evt.loaded/evt.total === 1){

Expand Down Expand Up @@ -517,27 +543,10 @@ var PSIParser = (function() {
var norms;

var numBytes = chunk.length;

// Define these here so we don't have to keep calculating
// them in the loop.
var diffX = xMax - xMin;
var diffY = yMax - yMin;
var diffZ = zMax - zMin;

var scaleX = sfactor + xMin;
var scaleY = sfactor + yMin;
var scaleZ = sfactor + zMin;


var byteIdx = 0;
for(var point = 0; point < numTotalPoints; byteIdx += 12, point++){
verts[point*3 + 0] = (diffX * getXYZ(chunk, byteIdx )) / scaleX;
verts[point*3 + 1] = (diffY * getXYZ(chunk, byteIdx + 3)) / scaleY;
verts[point*3 + 2] = (diffZ * getXYZ(chunk, byteIdx + 6)) / scaleZ;

cols[point*3 + 0] = getRGB(chunk, byteIdx + 9 ) / 255;
cols[point*3 + 1] = getRGB(chunk, byteIdx + 10) / 255;
cols[point*3 + 2] = getRGB(chunk, byteIdx + 11) / 255;
}
AJAX.parseVertsCols(chunk, numBytes, byteIdx, verts, cols);


// Parse the normals if we have them.
if(formatID == 2){
Expand Down Expand Up @@ -755,17 +764,8 @@ var PSIParser = (function() {
// 3 bytes are used for each x, y, z values
// each of the last 3 bytes of the 12 correspond to an rgb value
else{
for(var i = 0, j = 0; i < numBytes; i+=12, j += 3){
verts[j] = ((xMax - xMin) * getXYZ(chunk, i )) / sfactor + xMin;
verts[j+1] = ((yMax - yMin) * getXYZ(chunk, i+3)) / sfactor + yMin;
verts[j+2] = ((zMax - zMin) * getXYZ(chunk, i+6)) / sfactor + zMin;

if(cols){
cols[j] = getRGB(chunk, i+9 ) / 255;
cols[j+1] = getRGB(chunk, i+10) / 255;
cols[j+2] = getRGB(chunk, i+11) / 255;
}
}
var byteIdx = 0;
AJAX.parseVertsCols(chunk, numBytes, byteIdx, verts, cols);
}
}
// if the file was obtained in one go
Expand Down Expand Up @@ -857,6 +857,8 @@ var PSIParser = (function() {
*/
AJAX.firstLoad = function(textData){
var temp;

firstRun = false;

// numPtStr - number of points in the file
tagExists = textData.indexOf(numPtStr);
Expand All @@ -874,12 +876,11 @@ var PSIParser = (function() {
}
}

//
// sptSzStr - (spot Size) needs work
// sptSzStr - (spot Size)

// posMinStr - lowest value in the file (used for decompression)
//
tagExists = textData.indexOf(posMinStr);

if(tagExists !== -1){
endTagExists = textData.indexOf(endXMLStr, tagExists);
temp = textData.substring((tagExists + posMinStr.length), endTagExists);
Expand All @@ -893,6 +894,7 @@ var PSIParser = (function() {

// posMaxStr - highest value in the file (used for decompression)
tagExists = textData.indexOf(posMaxStr);

if(tagExists !== -1){
endTagExists = textData.indexOf(endXMLStr, tagExists);
temp = textData.substring((tagExists + posMaxStr.length), endTagExists);
Expand All @@ -905,6 +907,11 @@ var PSIParser = (function() {

bgnTag = textData.substring(tagExists, (endTagExists + 1));
}

///!!!
else{
firstRun = true;
}
}

/**
Expand All @@ -930,6 +937,10 @@ var PSIParser = (function() {
AJAX.firstLoad(textData);
}

if(firstRun){
return;
}

// checks if begin or end tags can be found using rgx
endTag = endLvlStr;
tagExists = textData.indexOf(bgnTag);
Expand Down Expand Up @@ -982,8 +993,8 @@ var PSIParser = (function() {
}
// parse position and colors
else{
if(firstRun){
firstRun = false;
if(firstRun){
//firstRun = false;
}
var chunk = textData.substring(AJAX.startOfNextChunk, AJAX.last12Index);

Expand Down
4 changes: 1 addition & 3 deletions tests/psi_parser/index.html
Expand Up @@ -3,7 +3,6 @@
<head>
<link rel="stylesheet" type="text/css" href="../style.css" />
<script src="../../xbps.js"></script>
<script src="../../shaders/fixed_function.js"></script>
<script src="test.js"></script>
</head>

Expand All @@ -12,8 +11,7 @@
<h1><a href="http://zenit.senecac.on.ca/wiki/index.php/XB_PointStream">XB PointStream</a> PSI Parser</h1>

<p>
This test uses the PSI Parser along with Mickey_Mouse.psi to render the point cloud.
<br />
Test the PSI Parser<br />
<br />
<canvas id="canvas" width="500" height="500"></canvas><br />
</p>
Expand Down
41 changes: 26 additions & 15 deletions tests/psi_parser/test.js
@@ -1,41 +1,52 @@
var ps, mickey;
var ps, pointCloud;
var yRot = 0.0;
var zoomed = -50;
const KEY_ESC = 27;
var zoomed = -60;

function zoom(amt){
zoomed += amt * 2 * 1;
}

function keyDown(){
if(ps.key == KEY_ESC){
ps.println("Streaming aborted by user");
}
function pointLight(light){
var lightName = "lights" + light.id;
ps.uniformi( lightName + ".isOn", true);
ps.uniformf( lightName + ".position", light.position);
ps.uniformi( lightName + ".type", 2);
ps.uniformf( lightName + ".attenuation", light.attenuation);

if(light.ambient){ps.uniformf( lightName + ".ambient", light.ambient);}
if(light.diffuse){ps.uniformf( lightName + ".diffuse", light.diffuse);}
if(light.specular){ps.uniformf( lightName + ".specular", light.specular);}
}


function render() {
ps.translate(0, 0, zoomed);
ps.rotateY(yRot += 0.001);

var c = mickey.getCenter();
var c = pointCloud.getCenter();
ps.translate(-c[0], -c[1], -c[2]);

ps.clear();
ps.render(mickey);
ps.render(pointCloud);
}

function start(){
ps = new PointStream();
ps.setup(document.getElementById('canvas'));
ps.background([0.25, 0.25, 0.25, 1]);
ps.background([1, 1, 0.7, 1]);

var vertShader = ps.getShaderStr("../../shaders/fixed_function.vs");
var fragShader = ps.getShaderStr("../../shaders/fixed_function.fs");

var prog = ps.createProgram(vertShader, fragShader);
ps.useProgram(prog);

pointLight({id:0, ambient:[.2,.2,.2], diffuse:[.7,.7,.7], attenuation:[1,0,0], position: [0,0,1]});

ps.onRender = render;
ps.onMouseScroll = zoom;
ps.onKeyDown = keyDown;

var progObj = ps.createProgram(fixedFunctionVert, fixedFunctionFrag);
ps.useProgram(progObj);
ps.pointSize(5);
ps.pointSize(8);

mickey = ps.load("../../clouds/Mickey_Mouse.psi");
pointCloud = ps.load("../../clouds/mickey_156K_n.psi");
}

0 comments on commit 0d9f8f0

Please sign in to comment.