From 623e1924aeea83ea70c8ae7f645b067f17a293ea Mon Sep 17 00:00:00 2001 From: Tim Redfern Date: Wed, 28 Mar 2012 16:28:45 +0100 Subject: mesh loader and morpher working --- morpher/bin/data/Bird-test1.xml | 39 --------------- morpher/config.make | 2 + morpher/morpher.cbp | 2 + morpher/src/main.cpp | 2 +- morpher/src/morphmesh.cpp | 104 ++++++++++++++++++++++++++++++++++++++-- morpher/src/morphmesh.h | 11 ++++- morpher/src/normBindTexture.cpp | 52 ++++++++++++++++++++ morpher/src/normBindTexture.h | 11 +++++ morpher/src/testApp.cpp | 47 +++++++++++++++--- morpher/src/testApp.h | 16 ++++++- 10 files changed, 232 insertions(+), 54 deletions(-) delete mode 100644 morpher/bin/data/Bird-test1.xml create mode 100644 morpher/src/normBindTexture.cpp create mode 100644 morpher/src/normBindTexture.h diff --git a/morpher/bin/data/Bird-test1.xml b/morpher/bin/data/Bird-test1.xml deleted file mode 100644 index 6f58854..0000000 --- a/morpher/bin/data/Bird-test1.xml +++ /dev/null @@ -1,39 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/morpher/config.make b/morpher/config.make index 9ce3e2f..2fdc0ee 100644 --- a/morpher/config.make +++ b/morpher/config.make @@ -19,6 +19,8 @@ USER_CFLAGS = USER_LDFLAGS = +#/usr/lib/libavformat.a /usr/lib/libavcodec.a /usr/lib/libavutil.a /usr/lib/libswscale.a + # use this to add system libraries for example: # USER_LIBS = -lpango diff --git a/morpher/morpher.cbp b/morpher/morpher.cbp index 920b41e..7b45ba2 100644 --- a/morpher/morpher.cbp +++ b/morpher/morpher.cbp @@ -45,6 +45,8 @@ + + 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& targetBlend){ + clearVertices(); + //normalise weights + int weightsNum=min(targetBlend.size(),targets.size()); + float totalWeights=0; + for (int i=0;i verts; + + string vertstring=XML.getAttribute("Attribute","Data","none",0); + stringstream ss(vertstring); + istream_iterator begin(ss); + istream_iterator end; + vector vstrings(begin, end); + for (int j=0;j norms; + string normstring=XML.getAttribute("Attribute","Data","none",1); + stringstream ns(normstring); + istream_iterator nbegin(ns); + istream_iterator nend; + vector nstrings(nbegin, nend); + for (int j=0;j texcords; + string texstring=XML.getAttribute("Attribute","Data","none",2); + stringstream ts(texstring); + istream_iterator tbegin(ts); + istream_iterator tend; + vector tstrings(tbegin, tend); + for (int j=0;j faces; + string facestring=XML.getAttribute("Index","Data","none",0); + stringstream fs(facestring); + istream_iterator fbegin(fs); + istream_iterator fend; + vector fstrings(fbegin, fend); + for (int j=0;j +#include + #include #include @@ -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& targetBlend); + int getNumTargets(); protected: private: - vector < >targets; + //vector < >targets; + vector< vector > 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 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; }; -- cgit v1.2.3