diff options
Diffstat (limited to 'morpher/src')
| -rw-r--r-- | morpher/src/main.cpp | 2 | ||||
| -rw-r--r-- | morpher/src/morphmesh.cpp | 104 | ||||
| -rw-r--r-- | morpher/src/morphmesh.h | 11 | ||||
| -rw-r--r-- | morpher/src/normBindTexture.cpp | 52 | ||||
| -rw-r--r-- | morpher/src/normBindTexture.h | 11 | ||||
| -rw-r--r-- | morpher/src/testApp.cpp | 47 | ||||
| -rw-r--r-- | morpher/src/testApp.h | 16 |
7 files changed, 228 insertions, 15 deletions
diff --git a/morpher/src/main.cpp b/morpher/src/main.cpp index 6a32c6a..e502a4d 100644 --- a/morpher/src/main.cpp +++ b/morpher/src/main.cpp @@ -6,7 +6,7 @@ int main( ){ ofAppGlutWindow window; - ofSetupOpenGL(&window, 1024,768, OF_WINDOW); // <-------- setup the GL context + ofSetupOpenGL(&window, 640,480, OF_WINDOW); // <-------- setup the GL context // this kicks off the running of my app // can be OF_WINDOW or OF_FULLSCREEN 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 diff --git a/morpher/src/morphmesh.h b/morpher/src/morphmesh.h index 3f401c6..303533c 100644 --- a/morpher/src/morphmesh.h +++ b/morpher/src/morphmesh.h @@ -1,6 +1,9 @@ #ifndef MORPHMESH_H #define MORPHMESH_H +#include <iostream> +#include <iterator> + #include <ofMesh.h> #include <ofxXmlSettings.h> @@ -10,10 +13,14 @@ class morphmesh : public ofMesh public: morphmesh(); virtual ~morphmesh(); - bool loadmesh(string filename); + bool loadfile(string filename); + void draw(int target); + void draw(const vector<float>& targetBlend); + int getNumTargets(); protected: private: - vector < <vector <ofVec3f> >targets; + //vector < <vector <ofVec3f> >targets; + vector< vector<ofVec3f> > targets; }; #endif // MORPHMESH_H diff --git a/morpher/src/normBindTexture.cpp b/morpher/src/normBindTexture.cpp new file mode 100644 index 0000000..68c17c9 --- /dev/null +++ b/morpher/src/normBindTexture.cpp @@ -0,0 +1,52 @@ +#include "normBindTexture.h" + +#include "ofMain.h" + +//texture binding with normalised coords +void bindTexture(ofBaseHasTexture &t) { + ofTexture &tex = t.getTextureReference(); + tex.bind(); + + glMatrixMode(GL_TEXTURE); + glPushMatrix(); + glLoadIdentity(); + + ofTextureData texData = tex.getTextureData(); + if(texData.textureTarget == GL_TEXTURE_RECTANGLE_ARB) { + glScalef(tex.getWidth(), tex.getHeight(), 1.0f); + } else { + glScalef(tex.getWidth() / texData.tex_w, tex.getHeight() / texData.tex_h, 1.0f); + } + + glMatrixMode(GL_MODELVIEW); +} +void unbindTexture(ofBaseHasTexture &t) { + t.getTextureReference().unbind(); + + glMatrixMode(GL_TEXTURE); + glPopMatrix(); + glMatrixMode(GL_MODELVIEW); +} +void bindTex(ofTexture &tex) { + tex.bind(); + + glMatrixMode(GL_TEXTURE); + glPushMatrix(); + glLoadIdentity(); + + ofTextureData texData = tex.getTextureData(); + if(texData.textureTarget == GL_TEXTURE_RECTANGLE_ARB) { + glScalef(tex.getWidth(), tex.getHeight(), 1.0f); + } else { + glScalef(tex.getWidth() / texData.tex_w, tex.getHeight() / texData.tex_h, 1.0f); + } + + glMatrixMode(GL_MODELVIEW); +} +void unbindTex(ofTexture &tex) { + tex.unbind(); + + glMatrixMode(GL_TEXTURE); + glPopMatrix(); + glMatrixMode(GL_MODELVIEW); +} diff --git a/morpher/src/normBindTexture.h b/morpher/src/normBindTexture.h new file mode 100644 index 0000000..f2bcb71 --- /dev/null +++ b/morpher/src/normBindTexture.h @@ -0,0 +1,11 @@ +#ifndef NORMBINDTEXTURE_H +#define NORMBINDTEXTURE_H + +#include "ofMain.h" + +void bindTexture(ofBaseHasTexture &t); +void unbindTexture(ofBaseHasTexture &t); +void bindTex(ofTexture &tex); +void unbindTex(ofTexture &tex); + +#endif // NORMBINDTEXTURE_H diff --git a/morpher/src/testApp.cpp b/morpher/src/testApp.cpp index aac34cc..41b04eb 100644 --- a/morpher/src/testApp.cpp +++ b/morpher/src/testApp.cpp @@ -1,10 +1,20 @@ #include "testApp.h" -#include "morphmesh.h" - //-------------------------------------------------------------- void testApp::setup(){ - + mesh=morphmesh(); + //mesh.loadfile("Bird-test1.xml"); + if (mesh.loadfile("Bird-test.xml")) printf("mesh loaded with %i vertices, %i face indices, %i targets\n",mesh.getNumVertices(),mesh.getNumIndices(),mesh.getNumTargets()); + else printf("XML not parsed\n"); + + texture.loadImage("texture2.jpg"); + + xr=yr=0; + xo=yo=0; + + glEnable(GL_DEPTH_TEST); + + //movieExporter.setup(); } //-------------------------------------------------------------- @@ -16,8 +26,27 @@ void testApp::update(){ //-------------------------------------------------------------- void testApp::draw(){ + //calculate morph targets + float segment=(sin(ofGetElapsedTimef())*0.5)+0.5; + vector<float> weights; + weights.push_back(segment); + weights.push_back(1.0-segment); + //printf("drawing %f %f\n",segment,1.0-segment); + ofBackground(0,0,0); - + bindTexture(texture); + ofPushMatrix(); + ofTranslate(ofGetWidth()/2,ofGetHeight()/2); + ofRotate(xr,0,1,0); + ofRotate(yr,1,0,0); + ofRotate(180,1,0,0); + ofScale(2.0,2.0,2.0); + mesh.draw(weights); + ofPopMatrix(); + unbindTexture(texture); + + ofSetHexColor(0xFFFFFF); + ofDrawBitmapString("fps: "+ofToString(ofGetFrameRate(), 2), 10, 15); } //-------------------------------------------------------------- @@ -25,6 +54,8 @@ void testApp::keyPressed(int key){ switch (key){ case 'a': + //if (movieExporter.isRecording()) movieExporter.stop(); + //else movieExporter.record(); break; case 'z': break; @@ -43,12 +74,16 @@ void testApp::mouseMoved(int x, int y ){ //-------------------------------------------------------------- void testApp::mouseDragged(int x, int y, int button){ - + xr+=(x-xo); + yr+=(y-yo); + xo=x; + yo=y; } //-------------------------------------------------------------- void testApp::mousePressed(int x, int y, int button){ - + xo=x; + yo=y; } void testApp::mouseReleased(int x, int y, int button){ diff --git a/morpher/src/testApp.h b/morpher/src/testApp.h index 58fb4ce..c0b665e 100644 --- a/morpher/src/testApp.h +++ b/morpher/src/testApp.h @@ -2,6 +2,12 @@ #include "ofMain.h" +#include "morphmesh.h" +#include "normBindTexture.h" + + +//#include "ofxMovieExporter.h" + class testApp : public ofBaseApp{ public: @@ -18,6 +24,14 @@ class testApp : public ofBaseApp{ void windowResized(int w, int h); void dragEvent(ofDragInfo dragInfo); void gotMessage(ofMessage msg); - + + + morphmesh mesh; + ofImage texture; + + float xr,yr; + int yo,xo; + + //Apex::ofxMovieExporter movieExporter; }; |
