...

View Full Version : Parsing text not working in 3D projection script.



alex@sadler.tk
07-31-2011, 08:35 PM
Well hello everyone, I have a small problem here. The problem is I have a script that draws 3d models that are in the OBJ Wavefront format onto the canvas. I have narrowed the problem I have down to a small section of the code, the code creates an array within an array to store the X,Y and Z co-ordinates, there is also some code that stores the linking of the vertices.
The arrays are as arranged as follows:

coordinate

0


X


Y


Z

1

2

3 etc.

the vertices linking array is as above but with point 1 and point 2 as opposed to X,Y and Z. Within the array X,Y and Z (and also points 1 and 2 in their array) are numbered 0,1 and 2.

the code is this (I'll show it in context):

function drawmodel() {
var coordinate = new Array();
var edges = new Array();
textbox = document.getElementById('textbox');
fpos = textbox.value.search('f ') - 1;
coor = textbox.value.substr(0, fpos);
link = textbox.value.substr(fpos, textbox.value.length);
canvasthing = document.getElementById('main');
halfCW = canvasthing.width / 2;
halfCH = canvasthing.height / 2;
camZ = 0;
camX = halfCW;
camY = halfCH;
Xrot = 0;
Yrot = 0;
Zrot = 0;
FL = 10;
ctx = canvasthing.getContext('2d');
//Below (the two for loops) is where it's going wrong (well, one of the places anyway :)
for (i=0;i<coor.split('\n').length;i++) {
var coordinate [i] = new Array(3);
coordinate[i] = coor.split('\n')[i].substr(2).split(' ');
}
for (i=0;i<link.split('\n').length;i++) {
var edges [i] = new Array(2);
edges[i] = link.split('\n')[i].substr(2).split(' ');
}
j = 0;
ctx.beginPath();
while (j<edges.length) {
XYprem = Math.sin(Yrot) * edges[j] [2];
XY = Math.cos(Yrot) * edges[j] [1] - XYprem;
XZprem = Math.cos(Yrot) * edges[j] [2];
XZ = Math.sin(Yrot) * edges[j] [1] - XZprem;
YXprem = Math.sin(Xrot) * edges[j] [0];
YX = Math.cos(Xrot) * XZ - YXprem;
YZprem = Math.cos(Xrot) * edges[j] [0];
YZ = Math.sin(Xrot) * XZ - YZprem;
ZXprem = Math.sin(Zrot) * XY;
ZX = Math.cos(Zrot) * YX - ZXprem;
ZYprem = Math.cos(Zrot) * XY;
ZY = Math.sin(Zrot) * YX - ZYprem;
FLZ = FL + YZ + edges[j] [2] - camZ;
SF = FL / FLZ;
XcX = ZX + edges[j] [0] - camX;
YcY = ZY + edges[j] [1] - camY;
X = XcX * SF + halfCW;
Y = YcY * SF + halfCH;
ctx.moveTo(X, Y);
j++;
XYprem = Math.sin(Yrot) * edges[j] [2];
XY = Math.cos(Yrot) * edges[j] [1] - XYprem;
XZprem = Math.cos(Yrot) * edges[j] [2];
XZ = Math.sin(Yrot) * edges[j] [1] - XZprem;
YXprem = Math.sin(Xrot) * edges[j] [0];
YX = Math.cos(Xrot) * XZ - YXprem;
YZprem = Math.cos(Xrot) * edges[j] [0];
YZ = Math.sin(Xrot) * XZ - YZprem;
ZXprem = Math.sin(Zrot) * XY;
ZX = Math.cos(Zrot) * YX - ZXprem;
ZYprem = Math.cos(Zrot) * XY;
ZY = Math.sin(Zrot) * YX - ZYprem;
FLZ = FL + YZ + edges[j] [2] - camZ;
SF = FL / FLZ;
XcX = ZX + edges[j] [0] - camX;
YcY = ZY + edges[j] [1] - camY;
X = XcX * SF + halfCW;
Y = YcY * SF + halfCH;
ctx.lineTo(X, Y);
j++;
}
ctx.stroke();
}

I hope you understand my query and can solve my problem!

Thanks in advanced,
Alex.

alex@sadler.tk
08-01-2011, 05:22 PM
*Bump*



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum