1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
#include "morphmesh.h"
morphmesh::morphmesh()
{}
morphmesh::morphmesh(string filename)
{
loadfile(filename);
}
morphmesh::~morphmesh()
{
//dtor
}
int morphmesh::getNumTargets(){
return morphs.size();
}
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){
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("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;
}
|