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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
#include "morphmesh.h"
sequence::sequence()
{
}
track::track()
{
}
/*
void track::addKey(float time,float weight)
{
keys.push_back(key(time,weight));
}
*/
float track::evaluate(float time)
{
//interpolate key track
if (time<=keys.begin()->first) return keys.begin()->second;
if (time>keys.rbegin()->first) return keys.rbegin()->second;
multimap< float,float >::iterator key2=keys.upper_bound( time );
multimap< float,float >::iterator key1=key2--;
float interval=key2->first-key1->first;
return (((1.0-((key2->first-time)/interval))*key2->second)+((1.0-((time-key1->first)/interval))*key1->second));
}
morphmesh::morphmesh()
{
loaded=false;
/*
//testing track function
track testrack;
testrack.keys.insert( pair<float,float>(0.0,2.0) );
testrack.keys.insert( pair<float,float>(1.0,4.0) );
testrack.keys.insert( pair<float,float>(0.5,10.0) );
for (float f=-0.4;f<1.4;f+=0.1) {
printf("%f : %f\n",f,testrack.evaluate(f));
}
*/
}
morphmesh::morphmesh(string filename)
{
morphmesh();
loadMesh(filename);
}
int morphmesh::getNumTargets(){
return morphs.size();
}
void morphmesh::draw() {
draw(0);
}
void morphmesh::draw(int target){
map<string,vector<ofVec3f> >::iterator it=morphs.begin();
for ( int i=0;i<target;i++ ) it++;
draw(it->first);
}
void morphmesh::draw(string target){
clearVertices();
addVertices(morphs[target]);
ofMesh::draw();
}
void morphmesh::draw(const vector<string>& 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[targets[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::loadMesh(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");
vector<string> meshnames;
for (int i=0;i<numMeshes;i++) meshnames.push_back(XML.getAttribute("Mesh","Name","none",i));
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[meshnames[i]]=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;
}
|