diff options
| author | Tim Redfern <tim@eclectronics.org> | 2012-03-28 16:28:45 +0100 |
|---|---|---|
| committer | Tim Redfern <tim@eclectronics.org> | 2012-03-28 16:28:45 +0100 |
| commit | 623e1924aeea83ea70c8ae7f645b067f17a293ea (patch) | |
| tree | 6016c770098ef923e6fac0be12040703d99fcbb2 /morpher/src/morphmesh.cpp | |
| parent | a47b39541a7f60dfdda921c9598abe947e1e6ad9 (diff) | |
mesh loader and morpher working
Diffstat (limited to 'morpher/src/morphmesh.cpp')
| -rw-r--r-- | morpher/src/morphmesh.cpp | 104 |
1 files changed, 99 insertions, 5 deletions
diff --git a/morpher/src/morphmesh.cpp b/morpher/src/morphmesh.cpp index c8826f3..c617f6f 100644 --- a/morpher/src/morphmesh.cpp +++ b/morpher/src/morphmesh.cpp @@ -10,17 +10,111 @@ morphmesh::~morphmesh() //dtor } -bool morphmesh::loadmesh(string filename){ +int morphmesh::getNumTargets(){ + return targets.size(); +} + +void morphmesh::draw(int target){ + clearVertices(); + addVertices(targets[target]); + ofMesh::draw(); +} +void morphmesh::draw(const vector<float>& targetBlend){ + clearVertices(); + //normalise weights + int weightsNum=min(targetBlend.size(),targets.size()); + float totalWeights=0; + for (int i=0;i<weightsNum;i++) totalWeights+=targetBlend[i]; + float weightFactor=1.0/totalWeights; + float bx,by,bz; + for (int j=0;j<targets[0].size();j++) { + bx=by=bz=0; + for (int i=0;i<weightsNum;i++) { + bx+=targets[i][j].x*targetBlend[i]; + by+=targets[i][j].y*targetBlend[i]; + bz+=targets[i][j].z*targetBlend[i]; + } + addVertex(ofVec3f(bx*weightFactor,by*weightFactor,bz*weightFactor)); + } + ofMesh::draw(); +} + +bool morphmesh::loadfile(string filename){ bool loaded=false; ofxXmlSettings XML; if( !XML.loadFile(filename) ){ printf("unable to load %s check data/ folder\n",filename.c_str()); }else{ - if(XML.pushTag("MeshList")) { - int verts=ofToInt(XML.getAttribute("mesh","VertexCount","none",0)); - int numMeshes=XML.getNumTags("Mesh"); - + if(XML.pushTag("Oak3DModelDocument")) { + if(XML.pushTag("MeshList")) { + int numMeshes=XML.getNumTags("Mesh"); + for (int i=0;i<numMeshes;i++) { + XML.pushTag("Mesh",i); + if (XML.pushTag("AttributeList")) { + vector<ofVec3f> verts; + + string vertstring=XML.getAttribute("Attribute","Data","none",0); + stringstream ss(vertstring); + istream_iterator<string> begin(ss); + istream_iterator<string> end; + vector<string> vstrings(begin, end); + for (int j=0;j<vstrings.size();j+=3) { + verts.push_back(ofVec3f(ofToFloat(vstrings[j]),ofToFloat(vstrings[j+1]),ofToFloat(vstrings[j+2]))); + } + targets.push_back(verts); + + if (i==0) { + addVertices(verts); + + vector<ofVec3f> norms; + string normstring=XML.getAttribute("Attribute","Data","none",1); + stringstream ns(normstring); + istream_iterator<string> nbegin(ns); + istream_iterator<string> nend; + vector<string> nstrings(nbegin, nend); + for (int j=0;j<nstrings.size();j+=3) { + norms.push_back(ofVec3f(ofToFloat(nstrings[j]),ofToFloat(nstrings[j+1]),ofToFloat(nstrings[j+2]))); + } + addNormals(norms); + + vector<ofVec2f> texcords; + string texstring=XML.getAttribute("Attribute","Data","none",2); + stringstream ts(texstring); + istream_iterator<string> tbegin(ts); + istream_iterator<string> tend; + vector<string> tstrings(tbegin, tend); + for (int j=0;j<tstrings.size();j+=2) { + texcords.push_back(ofVec2f(ofToFloat(tstrings[j]),ofToFloat(tstrings[j+1]))); + } + addTexCoords(texcords); + + XML.popTag(); + + if (XML.pushTag("IndexList")) { + vector<ofIndexType> faces; + string facestring=XML.getAttribute("Index","Data","none",0); + stringstream fs(facestring); + istream_iterator<string> fbegin(fs); + istream_iterator<string> fend; + vector<string> fstrings(fbegin, fend); + for (int j=0;j<fstrings.size();j+=3) { + faces.push_back(ofIndexType(ofToInt(fstrings[j]))); + faces.push_back(ofIndexType(ofToInt(fstrings[j+1]))); + faces.push_back(ofIndexType(ofToInt(fstrings[j+2]))); + } + addIndices(faces); + loaded=true; + XML.popTag(); + } + } + else XML.popTag(); + } + XML.popTag(); + } + } + XML.popTag(); } + XML.popTag(); } return loaded; }
\ No newline at end of file |
