annotate webGL/test/resources/makeXmlObj.js @ 2:be36da713ffd

update makeXmlObj.js
author NOBUYASU Oshiro
date Mon, 31 May 2010 03:25:15 +0900
parents b67b790dcf4f
children 10344afb38a6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
1 function parseXml( url ) {
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
2 var http = new JKL.ParseXML( url );
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
3 var data = http.parse()["OBJECT-3D"];
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
4 var xmlObj = new Object();
2
be36da713ffd update makeXmlObj.js
NOBUYASU Oshiro
parents: 0
diff changeset
5
0
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
6 if (data["surface"][0]) {
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
7 for (var i in data["surface"]) {
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
8 var obj = xmlObj[data["surface"][i]["name"]] = new Object();
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
9 obj.vertex = splitVector(data["surface"][i]["coordinate"]);
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
10 obj.normal = splitVector(data["surface"][i]["normal"]);
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
11 obj.texCoords = splitVector(data["surface"][i]["texture"]);
2
be36da713ffd update makeXmlObj.js
NOBUYASU Oshiro
parents: 0
diff changeset
12 var xmlObjImage = data["surface"][i]["image"];
be36da713ffd update makeXmlObj.js
NOBUYASU Oshiro
parents: 0
diff changeset
13 obj.image = 'data:image/png;base64,'+xmlObjImage["#text"];
0
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
14 }
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
15 } else {
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
16 var obj = xmlObj[data["surface"]["name"]] = new Object();
2
be36da713ffd update makeXmlObj.js
NOBUYASU Oshiro
parents: 0
diff changeset
17 obj.vertex = splitVector(data["surface"]["coordinate"]);
be36da713ffd update makeXmlObj.js
NOBUYASU Oshiro
parents: 0
diff changeset
18 obj.normal = splitVector(data["surface"]["normal"]);
be36da713ffd update makeXmlObj.js
NOBUYASU Oshiro
parents: 0
diff changeset
19 obj.texCoords = splitVector(data["surface"]["texture"]);
be36da713ffd update makeXmlObj.js
NOBUYASU Oshiro
parents: 0
diff changeset
20 var xmlObjImage = data["surface"]["image"];
be36da713ffd update makeXmlObj.js
NOBUYASU Oshiro
parents: 0
diff changeset
21 obj.image = 'data:image/png;base64,'+xmlObjImage["#text"];
0
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
22 }
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
23 return xmlObj;
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
24 }
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
25
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
26 function splitVector(str) {
2
be36da713ffd update makeXmlObj.js
NOBUYASU Oshiro
parents: 0
diff changeset
27 // return str.replace(/^\s+/g, "").replace(/\s+/g, " ").split(" ");
be36da713ffd update makeXmlObj.js
NOBUYASU Oshiro
parents: 0
diff changeset
28 return str.replace(/^\s+/g, "").replace(/\s+/g, " ").replace(/\s$/g, "").split(" ");
0
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
29 }
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
30
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
31 function makeXmlObj(ctx, xmlObj)
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
32 {
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
33 var geometryData = [ ];
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
34 var normalData = [ ];
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
35 var texCoordData = [ ];
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
36 var indexData = [ ];
2
be36da713ffd update makeXmlObj.js
NOBUYASU Oshiro
parents: 0
diff changeset
37 var index = 0;
be36da713ffd update makeXmlObj.js
NOBUYASU Oshiro
parents: 0
diff changeset
38 for(var i=0; xmlObj.vertex[i]; i++){
0
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
39 geometryData.push(parseFloat(xmlObj.vertex[i]));
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
40 if ( (i % 3) == 0) {
2
be36da713ffd update makeXmlObj.js
NOBUYASU Oshiro
parents: 0
diff changeset
41 indexData.push(index);
be36da713ffd update makeXmlObj.js
NOBUYASU Oshiro
parents: 0
diff changeset
42 index++;
0
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
43 }
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
44 }
2
be36da713ffd update makeXmlObj.js
NOBUYASU Oshiro
parents: 0
diff changeset
45 for(i=0; xmlObj.texCoords[i]; i++){
be36da713ffd update makeXmlObj.js
NOBUYASU Oshiro
parents: 0
diff changeset
46 texCoordData.push(parseFloat(xmlObj.texCoords[i]));
0
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
47 }
2
be36da713ffd update makeXmlObj.js
NOBUYASU Oshiro
parents: 0
diff changeset
48 for(i=0; xmlObj.normal[i]; i++){
be36da713ffd update makeXmlObj.js
NOBUYASU Oshiro
parents: 0
diff changeset
49 normalData.push(parseFloat(xmlObj.normal[i]));
0
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
50 }
2
be36da713ffd update makeXmlObj.js
NOBUYASU Oshiro
parents: 0
diff changeset
51
0
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
52 var retval = { };
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
53
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
54 retval.normalObject = ctx.createBuffer();
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
55 ctx.bindBuffer(ctx.ARRAY_BUFFER, retval.normalObject);
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
56 ctx.bufferData(ctx.ARRAY_BUFFER, new WebGLFloatArray(normalData), ctx.STATIC_DRAW);
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
57
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
58 retval.texCoordObject = ctx.createBuffer();
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
59 ctx.bindBuffer(ctx.ARRAY_BUFFER, retval.texCoordObject);
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
60 ctx.bufferData(ctx.ARRAY_BUFFER, new WebGLFloatArray(texCoordData), ctx.STATIC_DRAW);
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
61
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
62 retval.vertexObject = ctx.createBuffer();
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
63 ctx.bindBuffer(ctx.ARRAY_BUFFER, retval.vertexObject);
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
64 ctx.bufferData(ctx.ARRAY_BUFFER, new WebGLFloatArray(geometryData), ctx.STATIC_DRAW);
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
65
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
66 retval.numIndices = indexData.length;
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
67 retval.indexObject = ctx.createBuffer();
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
68 ctx.bindBuffer(ctx.ELEMENT_ARRAY_BUFFER, retval.indexObject);
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
69 ctx.bufferData(ctx.ELEMENT_ARRAY_BUFFER, new WebGLUnsignedShortArray(indexData), ctx.STREAM_DRAW);
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
70
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
71 return retval;
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
72 }
b67b790dcf4f upload All File
e085711@nobuyasuoshiro.local
parents:
diff changeset
73