view Vacuum/resources/Image_xml.js @ 0:718974a1a32b

Vacuum
author <e085737>
date Tue, 07 Dec 2010 17:31:15 +0900
parents
children
line wrap: on
line source

function createFromXMLfile(gl,url){
    if(gl.obj == undefined) gl.obj = new Object();
    var xmlChara = new Object();
    xmlChara = parsePanel(url);
    for (var name in xmlChara)
        {
            gl.obj[name] = makeXmlObj(gl, xmlChara[name]);
            gl.obj[name].texture = loadImageTexture(gl, xmlChara[name].image);
            gl.obj[name].name = name;
 	}
}

/*
function createFromXMLfile(gl, url)
{
    xml = new Object();
    var xmlChara = new Object();
    xmlChara = parsePanel(url);
    for (var name in xmlChara)
        {
            xml[name] = makeXmlObj(gl, xmlChara[name]);
            xml[name].texture = loadImageTexture(gl, xmlChara[name].image);
        }
    return xml[name]   
}
*/


function parsePanel(url) {
    var http = new JKL.ParseXML(url);
    var data = http.parse()["OBJECT-3D"];
    var xmlObj = new Object();

    if (data["surface"][0]) {
                for (var i in data["surface"]) {
                        var obj = xmlObj[data["surface"][i]["name"]] = new Object();
                        obj.vertex = splitVector(data["surface"][i]["coordinate"]);
                        obj.normal = splitVector(data["surface"][i]["normal"]);
                        obj.texCoords = splitVector(data["surface"][i]["texture"]);
                        obj.model = splitVector(data["surface"][i]["model"]);
                        var xmlObjImage = data["surface"][i]["image"];
                        obj.image = 'data:image/png;base64,'+xmlObjImage["#text"];
                }
    } else {
        var obj = xmlObj[data["surface"]["name"]] = new Object();
        obj.vertex =  splitVector(data["surface"]["coordinate"]);
        obj.normal =  splitVector(data["surface"]["normal"]);
        obj.texCoords = splitVector(data["surface"]["texture"]);
        obj.model = splitVector(data["surface"]["model"]);
        var xmlObjImage = data["surface"]["image"];
        obj.image = 'data:image/png;base64,'+xmlObjImage["#text"];
    }
    return xmlObj;
}

function splitVector(str) {
    //    return str.replace(/^\s+/g, "").replace(/\s+/g, " ").split(" ");
    return str.replace(/^\s+/g, "").replace(/\s+/g, " ").replace(/\s$/g, "").split(" ");
}

function makeXmlObj(ctx, xmlObj)
{

    var model = new Object();
    model.x = parseFloat(xmlObj.model[0]);
    model.y = parseFloat(xmlObj.model[1]);
    model.z = parseFloat(xmlObj.model[2]);
  
    var geometryData = [ ];
    var normalData   = [ ];
    var texCoordData = [ ];
    var indexData    = [ ];
    var index = 0;
    var xmax = 0;
    var xmin = 0;
    var ymax = 0;
    var ymin = 0;    
   
    
    for(var i=0; xmlObj.vertex[i]; i+=3){
		geometryData.push(parseFloat(xmlObj.vertex[i]) - model.x);
		geometryData.push(parseFloat(xmlObj.vertex[i+1]) - model.y);
		geometryData.push(parseFloat(xmlObj.vertex[i+2]) - model.z);
		if ( (i % 3) == 0) {
			indexData.push(index);
			index++;
		}
		//オブジェクトの最大・最小のx,y座標を求める
		if ( i == 0) {
		    xmax = geometryData[0];
		    xmin = geometryData[0];
		    ymax = geometryData[1];
		    ymin = geometryData[1];
		}
		if ( i > 0 ){
		    xmax = Math.max( xmax, geometryData[i]);
		    xmin = Math.min( xmin, geometryData[i]);
		    ymax = Math.max( ymax, geometryData[i+1]);
		    ymin = Math.min( ymin, geometryData[i+1]);
		}
		
    }
    for(i=0; xmlObj.texCoords[i]; i++){
		texCoordData.push(parseFloat(xmlObj.texCoords[i]));

    }
    for(i=0; xmlObj.normal[i]; i+=3){
		normalData.push(parseFloat(xmlObj.normal[i]));
		normalData.push(parseFloat(xmlObj.normal[i+1]));
		normalData.push(parseFloat(xmlObj.normal[i+2]));
    }

    var retval = { };
    //オブジェクトの横幅,縦幅を求める
    retval.w = xmax - xmin;
    retval.h = ymax - ymin;

    retval.normalObject = ctx.createBuffer();
    ctx.bindBuffer(ctx.ARRAY_BUFFER, retval.normalObject);
    ctx.bufferData(ctx.ARRAY_BUFFER, new Float32Array(normalData), ctx.STATIC_DRAW);
	
    retval.texCoordObject = ctx.createBuffer();
    ctx.bindBuffer(ctx.ARRAY_BUFFER, retval.texCoordObject);
    ctx.bufferData(ctx.ARRAY_BUFFER, new Float32Array(texCoordData), ctx.STATIC_DRAW);
	
    retval.vertexObject = ctx.createBuffer();
    ctx.bindBuffer(ctx.ARRAY_BUFFER, retval.vertexObject);
    ctx.bufferData(ctx.ARRAY_BUFFER, new Float32Array(geometryData), ctx.STATIC_DRAW);
    
    retval.numIndices = indexData.length;
    retval.indexObject = ctx.createBuffer();
    ctx.bindBuffer(ctx.ELEMENT_ARRAY_BUFFER, retval.indexObject);
    ctx.bufferData(ctx.ELEMENT_ARRAY_BUFFER, new Uint16Array(indexData), ctx.STREAM_DRAW);
	
    return retval;
}