summaryrefslogtreecommitdiff
path: root/gaunt01/src/morphmesh.cpp
blob: 6dc333825d88ac2a3d40a7a4c392b496b0059abd (plain)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include "morphmesh.h"

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;
    map< float,float >::iterator key2=keys.upper_bound( time );
    map< 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));
}

sequence::sequence(string _name,float _length,float _fadeinTime,float _fadeoutTime)
{
    name=_name;
    length=_length;
    startTime=0;
    stopTime=0;
    fadeinTime=_fadeinTime;
    fadeoutTime=_fadeoutTime;
    active=false;
}

//-----------------------------------------------------
// stopTime & startTime are absolute
//
// all other time markers are relative to the sequence
//
//
void sequence::reset(){
	active=false;
	fadeinTime=-1;
	fadeoutTime=-1;
	startTime=0;
	stopTime=0;
}
void sequence::start(){
	reset();
	active=true;
	startTime=ofGetElapsedTimef();
}
void sequence::startAt(float time){
	reset();
	active=true;
	startTime=ofGetElapsedTimef()+time;
}
void sequence::stopAt(float time){
	reset();
	active=true;
	stopTime=ofGetElapsedTimef()+time;
}
void sequence::fadeout(float time){
	if(active==true) {
		reset();
		active=true;
		startTime=ofGetElapsedTimef();
		fadeoutTime=0;
		stopTime=ofGetElapsedTimef()+time;
	}
}
void sequence::fadein(float time){
	reset();
	active=true;
	fadeinTime=time;
	startTime=ofGetElapsedTimef();
}
void sequence::stop(){
	reset();
}

vector<morphWeight> sequence::evaluate(float time){
	float now=ofGetElapsedTimef();
	float seqtime=now-startTime; 
	float animtime=time-startTime;
	//for this to be correct time has to begin at actual startTime and proceed relatively
	//stopTime has to be based on absolute time
	//fadeweight algorithm has to be absolute
	float looptime=fmod(animtime,length); 
	float fadeWeight=(seqtime<fadeinTime?seqtime/fadeinTime:seqtime>fadeoutTime&&fadeoutTime>-1?1.0-(seqtime/(stopTime-startTime)):1.0);
	vector<morphWeight> weights;
	map<string,track>::iterator i;
	for( i = tracks.begin(); i != tracks.end(); i++ ) {
		weights.push_back(morphWeight(i->first,(i->second.evaluate(looptime))*fadeWeight));
	}
	return weights;
}

morphmesh::morphmesh()
{
	loaded=false;
	speed=1.0f;

    /*
	//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();
}
int morphmesh::getNumSequences(){
	return sequences.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<morphWeight>& weights){
	clearVertices();
	//normalise weights
	float totalWeights=0;
	for (int i=0;i<weights.size();i++) totalWeights+=weights[i].weight;
	float weightFactor=1.0/totalWeights;
	float bx,by,bz;
	for (int j=0;j<morphs.begin()->second.size();j++) {
		bx=by=bz=0;
		for (int i=0;i<weights.size();i++) {
			bx+=morphs[weights[i].name][j].x*weights[i].weight;
			by+=morphs[weights[i].name][j].y*weights[i].weight;
			bz+=morphs[weights[i].name][j].z*weights[i].weight;
		}
		addVertex(ofVec3f(bx*weightFactor,by*weightFactor,bz*weightFactor));
	}
	ofMesh::draw();
}

void morphmesh::drawAnimated(){
	//compute time increment
	time+=(ofGetElapsedTimef()-lastTime)*speed;
	lastTime=ofGetElapsedTimef();
	vector<morphWeight> weights;
	map<string,sequence>::iterator i;
	for( i = sequences.begin(); i != sequences.end(); i++ ) {
		if (i->second.active) {
			vector<morphWeight> newWeights=i->second.evaluate(time);
			weights.insert(weights.end(),newWeights.begin(),newWeights.end());
			if (i->second.stopTime>0&&ofGetElapsedTimef()>i->second.stopTime) i->second.active=false;
		}
	}
	draw(weights);
}


bool morphmesh::loadSeqs(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("Gauntletanim")) {
		    int numSeqs=XML.getNumTags("Sequence");
		    vector<string> seqnames;
		    for (int i=0;i<numSeqs;i++) seqnames.push_back(XML.getAttribute("Sequence","Name","none",i));
		    for (int i=0;i<numSeqs;i++) {
                float length=ofToFloat(XML.getAttribute("Sequence","Length","0",i));
                float fadeinTime=ofToFloat(XML.getAttribute("Sequence","Fadein","0",i));
                float fadeoutTime=ofToFloat(XML.getAttribute("Sequence","Fadeout","0",i));
                sequence seq=sequence(seqnames[i],length,fadeinTime,fadeoutTime);
			  
                XML.pushTag("Sequence",i);
                    int numTargs=XML.getNumTags("Target");
                    vector<string> targnames;
                    for (int j=0;j<numTargs;j++) targnames.push_back(XML.getAttribute("Target","Name","none",j));
                    for (int j=0;j<numTargs;j++) {
                        XML.pushTag("Target",j);

                            vector<float> times;
                            string keystring=XML.getAttribute("Keys","Times","none",0);
                            stringstream ks(keystring);
                            istream_iterator<string> kbegin(ks);
                            istream_iterator<string> kend;
                            vector<string> kstrings(kbegin, kend);

                            vector<float> values;
                            string valstring=XML.getAttribute("Keys","Values","none",0);
                            stringstream vs(valstring);
                            istream_iterator<string> vbegin(vs);
                            istream_iterator<string> vend;
                            vector<string> vstrings(vbegin, vend);

                            track tr;

                            for (int k=0;k<min(kstrings.size(),vstrings.size());k++) tr.keys[ofToFloat(kstrings[k])]=ofToFloat(vstrings[k]);

                            seq.tracks[targnames[j]]=tr;

                        XML.popTag();
                    }
		    sequences[seqnames[i]]=seq;
		    /*
		    printf("testing sequence: %s\n",seqnames[i].c_str());
		    for (int k=0;k<targnames.size();k++) {
			    printf("track: %s\n",targnames[k].c_str());
			    for (float l=-.2;l<=2.2;l+=0.1) {
				    printf("%f : %f\n",l,sequences[seqnames[i]].tracks[targnames[k]].evaluate(l));
			    }
		    }
		    */
		    loaded=true;
                XML.popTag();
            }
            XML.popTag();
		}
	}
	return loaded;
}



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;
}