Mercurial > hg > Members > e105711
view webGL/src/f4xml.js @ 10:98a90bc478b3 draft
Change function name.upd->update
author | e105711 <yomitan.ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 17 May 2012 16:04:02 +0900 |
parents | c5455ae6cd70 |
children | 74dc1d5f2e47 |
line wrap: on
line source
function parseXml(gl){ imgs = []; loadObjXml(gl,"./xml/title.xml"); //0 loadObjXml(gl,"./xml/press.xml"); loadObjXml(gl,"./xml/test.xml"); loadObjXml(gl,"./xml/line.xml"); loadObjXml(gl,"./xml/blue_notes.xml"); loadObjXml(gl,"./xml/pink_notes.xml"); //5 loadObjXml(gl,"./xml/back.xml"); imgs.push(gl.obj["title"]); imgs.push(gl.obj["press"]); imgs.push(gl.obj["test"]); imgs.push(gl.obj["line"]); imgs.push(gl.obj["blue_notes"]); imgs.push(gl.obj["pink_notes"]); imgs.push(gl.obj["back"]); } function loadObjXml(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); } } function PutSpriteF(ctx,x,y,scale,glObj){ ctx.enableVertexAttribArray(0); ctx.enableVertexAttribArray(1); ctx.enableVertexAttribArray(2); ctx.bindBuffer(ctx.ARRAY_BUFFER, glObj.vertexObject); ctx.vertexAttribPointer(2, 3, ctx.FLOAT, false, 0, 0); ctx.bindBuffer(ctx.ARRAY_BUFFER, glObj.normalObject); ctx.vertexAttribPointer(0, 3, ctx.FLOAT, false, 0, 0); ctx.bindBuffer(ctx.ARRAY_BUFFER, glObj.texCoordObject); ctx.vertexAttribPointer(1, 2, ctx.FLOAT, false, 0, 0); ctx.bindBuffer(ctx.ELEMENT_ARRAY_BUFFER, glObj.indexObject); // generate the model-view matrix var mvMatrix = new J3DIMatrix4(); mvMatrix.scale(scale, scale, scale); mvMatrix.translate( x, y, 10); // construct the normal matrix from the model-view matrix var normalMatrix = new J3DIMatrix4(mvMatrix); normalMatrix.invert(); normalMatrix.transpose(); normalMatrix.setUniform(ctx, ctx.getUniformLocation(ctx.program, "u_normalMatrix"), false); // construct the model-view * projection matrix var mvpMatrix = new J3DIMatrix4(ctx.perspectiveMatrix); mvpMatrix.multiply(mvMatrix); mvpMatrix.setUniform(ctx, ctx.getUniformLocation(ctx.program, "u_modelViewProjMatrix"), false); ctx.bindTexture(ctx.TEXTURE_2D, glObj.texture); ctx.drawElements(ctx.TRIANGLES, glObj.numIndices, ctx.UNSIGNED_SHORT, 0); } 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++; } // Get object's maximum and minimum 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 = { }; // Get ogject's width and height. 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; }