summaryrefslogtreecommitdiff
path: root/gaunt01/src/morphmesh.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'gaunt01/src/morphmesh.cpp')
-rw-r--r--gaunt01/src/morphmesh.cpp133
1 files changed, 133 insertions, 0 deletions
diff --git a/gaunt01/src/morphmesh.cpp b/gaunt01/src/morphmesh.cpp
new file mode 100644
index 0000000..64bc24e
--- /dev/null
+++ b/gaunt01/src/morphmesh.cpp
@@ -0,0 +1,133 @@
+#include "morphmesh.h"
+
+morphmesh::morphmesh()
+{
+ loaded=false;
+}
+
+morphmesh::morphmesh(string filename)
+{
+ morphmesh();
+ loadfile(filename);
+}
+
+morphmesh::~morphmesh()
+{
+ //dtor
+}
+
+int morphmesh::getNumTargets(){
+ return morphs.size();
+}
+void morphmesh::draw() {
+ draw(0);
+}
+void morphmesh::draw(int target){
+ clearVertices();
+ addVertices(morphs[target]);
+ ofMesh::draw();
+}
+void morphmesh::draw(const vector<int>& targets, const vector<float>& weights){
+ clearVertices();
+ //normalise weights
+ int targetsNum=min(targets.size(),morphs.size());
+ float totalWeights=0;
+ for (int i=0;i<targetsNum;i++) totalWeights+=weights[i];
+ float weightFactor=1.0/totalWeights;
+ float bx,by,bz;
+ for (int j=0;j<morphs[0].size();j++) {
+ bx=by=bz=0;
+ for (int i=0;i<targetsNum;i++) {
+ bx+=morphs[targets[i]][j].x*weights[i];
+ by+=morphs[targets[i]][j].y*weights[i];
+ bz+=morphs[targets[i]][j].z*weights[i];
+ }
+ addVertex(ofVec3f(bx*weightFactor,by*weightFactor,bz*weightFactor));
+ }
+ ofMesh::draw();
+}
+
+bool morphmesh::loadfile(string filename){
+ loaded=false;
+ ofxXmlSettings XML;
+ if( !XML.loadFile(filename) ){
+ printf("unable to load %s check data/ folder\n",filename.c_str());
+ }else{
+ 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])));
+ }
+ morphs.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;
+}
+
+bool morphmesh::isLoaded()
+{
+ return loaded;
+} \ No newline at end of file