diff --git a/example-fboTexture/bin/data/defaultSurfaces.xml b/example-fboTexture/bin/data/defaultSurfaces.xml old mode 100644 new mode 100755 diff --git a/example-fboTexture/src/ofApp.cpp b/example-fboTexture/src/ofApp.cpp old mode 100644 new mode 100755 index 0a66d68..7012632 --- a/example-fboTexture/src/ofApp.cpp +++ b/example-fboTexture/src/ofApp.cpp @@ -64,6 +64,7 @@ void ofApp::draw() ss << " 4. Source selection mode\n\n"; ss << "You can switch between the modes by using <1>, <2>, <3> and <4> keys on the keyboard.\n\n"; ss << "Press or to add random or normal surface.\n"; + ss << "Press to add a new quad surface.\n"; ss << "Press to save the composition.\n"; ss << "Press to toggle fullscreen.\n"; ss << "Press to reassign the fbo texture to the first surface\n"; @@ -90,6 +91,7 @@ void ofApp::keyPressed(int key) case '4': gui.setMode(ofxGuiMode::SOURCE_SELECTION); break; case 'i': bShowInfo = !bShowInfo; break; case 'r': addRandomSurface(); break; + case 'q': addQuadSurface(); break; case 'n': addSurface(); break; case 'f': ofToggleFullscreen(); break; case 's': surfaceManager.saveXmlSettings("surfaces.xml"); break; @@ -116,6 +118,29 @@ void ofApp::addRandomSurface() surfaceManager.selectSurface(surfaceManager.size()-1); } +void ofApp::addQuadSurface() +{ + int surfaceType = ofxSurfaceType::QUAD_SURFACE; + vector vertices; + + int border = 50; + vertices.push_back( ofVec2f(border, border) ); + vertices.push_back( ofVec2f(border, ofGetHeight() - border) ); + vertices.push_back( ofVec2f(ofGetWidth() - border, ofGetHeight() - border) ); + vertices.push_back( ofVec2f(ofGetWidth() - border, border) ); + + vector texCoords; + texCoords.push_back( ofVec2f(ofVec2f(0.0f, 0.0f)) ); + texCoords.push_back( ofVec2f(ofVec2f(1.0f, 0.0f)) ); + texCoords.push_back( ofVec2f(ofVec2f(1.0f, 1.0f)) ); + texCoords.push_back( ofVec2f(ofVec2f(0.0f, 1.0f)) ); + + surfaceManager.addSurface(surfaceType, vertices, texCoords); + + // select this surface right away + surfaceManager.selectSurface(surfaceManager.size()-1); +} + void ofApp::addSurface() { int surfaceType = ofxSurfaceType::TRIANGLE_SURFACE; diff --git a/example-fboTexture/src/ofApp.h b/example-fboTexture/src/ofApp.h old mode 100644 new mode 100755 index 39b01f8..05d7a8f --- a/example-fboTexture/src/ofApp.h +++ b/example-fboTexture/src/ofApp.h @@ -15,6 +15,7 @@ public: void keyPressed(int key); void addRandomSurface(); + void addQuadSurface(); void addSurface(); void setFboAsTexture(); diff --git a/src/ofxProjectionEditor.h b/src/ofxProjectionEditor.h old mode 100644 new mode 100755 diff --git a/src/ofxSurfaceManager.cpp b/src/ofxSurfaceManager.cpp old mode 100644 new mode 100755 index fe9faa1..e3e261b --- a/src/ofxSurfaceManager.cpp +++ b/src/ofxSurfaceManager.cpp @@ -21,7 +21,11 @@ void ofxSurfaceManager::addSurface(int surfaceType) { if ( surfaceType == ofxSurfaceType::TRIANGLE_SURFACE ) { surfaces.push_back( new ofxTriangleSurface() ); - } else { + } + else if (surfaceType == ofxSurfaceType::QUAD_SURFACE ) { + surfaces.push_back( new ofxQuadSurface() ); + } + else { throw std::runtime_error("Attempt to add non-existing surface type."); } } @@ -31,7 +35,12 @@ void ofxSurfaceManager::addSurface(int surfaceType, ofTexture* texturePtr) if ( surfaceType == ofxSurfaceType::TRIANGLE_SURFACE ) { surfaces.push_back( new ofxTriangleSurface() ); surfaces.back()->setTexture(texturePtr); - } else { + } + else if (surfaceType == ofxSurfaceType::QUAD_SURFACE ) { + surfaces.push_back( new ofxQuadSurface() ); + surfaces.back()->setTexture(texturePtr); + } + else { throw std::runtime_error("Attempt to add non-existing surface type."); } } @@ -43,7 +52,7 @@ void ofxSurfaceManager::addSurface(int surfaceType, vector vertices, ve if ( vertices.size() < 3 ) { throw std::runtime_error("There must be 3 vertices for a triangle surface."); } else if (texCoords.size() < 3) { - throw std::runtime_error("Thre must be 3 texture coordinates for a triangle surface."); + throw std::runtime_error("There must be 3 texture coordinates for a triangle surface."); } surfaces.push_back( new ofxTriangleSurface() ); @@ -53,7 +62,22 @@ void ofxSurfaceManager::addSurface(int surfaceType, vector vertices, ve surfaces.back()->setTexCoord(i, texCoords[i]); } - } else { + } + else if (surfaceType == ofxSurfaceType::QUAD_SURFACE ) { + if ( vertices.size() < 4 ) { + throw std::runtime_error("There must be 4 vertices for a quad surface."); + } else if (texCoords.size() < 4) { + throw std::runtime_error("There must be 4 texture coordinates for a quad surface."); + } + + surfaces.push_back( new ofxQuadSurface() ); + + for ( int i=0; i<4; i++ ) { + surfaces.back()->setVertex(i, vertices[i]); + surfaces.back()->setTexCoord(i, texCoords[i]); + } + } + else { throw std::runtime_error("Attempt to add non-existing surface type."); } @@ -77,7 +101,23 @@ void ofxSurfaceManager::addSurface(int surfaceType, ofTexture* texturePtr, vecto surfaces.back()->setTexCoord(i, texCoords[i]); } - } else { + } + else if (surfaceType == ofxSurfaceType::QUAD_SURFACE ) { + if ( vertices.size() < 4 ) { + throw std::runtime_error("There must be 4 vertices for a quad surface."); + } else if (texCoords.size() < 4) { + throw std::runtime_error("Thre must be 4 texture coordinates for a quad surface."); + } + + surfaces.push_back( new ofxQuadSurface() ); + surfaces.back()->setTexture(texturePtr); + + for ( int i=0; i<4; i++ ) { + surfaces.back()->setVertex(i, vertices[i]); + surfaces.back()->setTexCoord(i, texCoords[i]); + } + } + else { throw std::runtime_error("Attempt to add non-existing surface type."); } } @@ -138,6 +178,16 @@ void ofxSurfaceManager::clear() } } +// String getTypeString(ofxSurfaceType e) +// { +// switch e +// { +// case TRINAGLE_SURFACE: return "TRINAGLE_SURFACE"; +// case QUAD_SURFACE: return "QUAD_SURFACE"; +// default: throw Exception("Bad MyEnum"); +// } +// } + void ofxSurfaceManager::saveXmlSettings(string fileName) { xmlSettings.clear(); @@ -147,10 +197,9 @@ void ofxSurfaceManager::saveXmlSettings(string fileName) xmlSettings.pushTag("surfaces"); for ( int i=0; i* vertices = &surface->getVertices(); @@ -186,9 +235,16 @@ void ofxSurfaceManager::saveXmlSettings(string fileName) xmlSettings.addValue("source-type", "image"); xmlSettings.addValue("source-name", getSurfaceSourceName(surface)); //xmlSettings.addValue("source-path", "/root/etc/image.jpg"); - xmlSettings.popTag(); // source - + + + // xmlSettings.addTag("type"); + // xmlSettings.pushTag("type"); + // // surfaceType == ofxSurfaceType::TRIANGLE_SURFACE + // ofxSurfaceType surfaceType = &surface->getType(); + // xmlSettings.addValue("surface-type", surfaceType); + // xmlSettings.popTag(); // type + xmlSettings.popTag(); // surface } xmlSettings.popTag(); // surfaces @@ -198,6 +254,8 @@ void ofxSurfaceManager::saveXmlSettings(string fileName) void ofxSurfaceManager::loadXmlSettings(string fileName) { + ofLog(OF_LOG_WARNING, "A load XML settings..."); + if (!xmlSettings.loadFile(fileName)){ ofLog(OF_LOG_WARNING, "Could not load XML settings."); return; @@ -225,55 +283,125 @@ void ofxSurfaceManager::loadXmlSettings(string fileName) sourceTexture = loadImageSource(sourceName, ss.str()); } xmlSettings.popTag(); // source - + + // // attempt to load surface type + // ofLog(OF_LOG_WARNING, "Attempt to load surface type."); + // xmlSettings.pushTag("type"); + // string surfaceType = xmlSettings.getValue("surface-type", "TRIANGLE_SURFACE"); + // xmlSettings.popTag(); // type + // get vertices (only for triangle surface for now) xmlSettings.pushTag("vertices"); vector vertices; - - xmlSettings.pushTag("vertex", 0); - vertices.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 0.0f) ) ); - xmlSettings.popTag(); - - xmlSettings.pushTag("vertex", 1); - vertices.push_back( ofVec2f( xmlSettings.getValue("x", 100.0f), xmlSettings.getValue("y", 0.0f) ) ); - xmlSettings.popTag(); - - xmlSettings.pushTag("vertex", 2); - vertices.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 100.0f) ) ); - xmlSettings.popTag(); - - xmlSettings.popTag(); // vertices - - // get texture coordinates (only for triangle surfaces for now) - xmlSettings.pushTag("texCoords"); - - vector texCoords; - - xmlSettings.pushTag("texCoord", 0); - texCoords.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 0.0f) ) ); - xmlSettings.popTag(); - - xmlSettings.pushTag("texCoord", 1); - texCoords.push_back( ofVec2f( xmlSettings.getValue("x", 1.0f), xmlSettings.getValue("y", 0.0f) ) ); - xmlSettings.popTag(); - - xmlSettings.pushTag("texCoord", 2); - texCoords.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 1.0f) ) ); - xmlSettings.popTag(); - - xmlSettings.popTag(); // texCoords - - - // now we have variables sourceName and sourceTexture - // by checking those we can use one or another addSurface method - if ( sourceName != "none" && sourceTexture != NULL ) { - addSurface(ofxSurfaceType::TRIANGLE_SURFACE, sourceTexture, vertices, texCoords); - } else { - addSurface(ofxSurfaceType::TRIANGLE_SURFACE, vertices, texCoords); + + int vertexCount = vertices.size(); + // int vertexCount = vertices->size() + + //it's a triangle ? + if (vertexCount == 3) + // if (surfaceType == TRIANGLE_SURFACE) + { + xmlSettings.pushTag("vertex", 0); + vertices.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 0.0f) ) ); + xmlSettings.popTag(); + + xmlSettings.pushTag("vertex", 1); + vertices.push_back( ofVec2f( xmlSettings.getValue("x", 100.0f), xmlSettings.getValue("y", 0.0f) ) ); + xmlSettings.popTag(); + + xmlSettings.pushTag("vertex", 2); + vertices.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 100.0f) ) ); + xmlSettings.popTag(); + + xmlSettings.popTag(); // vertices + + xmlSettings.pushTag("texCoords"); + + vector texCoords; + + xmlSettings.pushTag("texCoord", 0); + texCoords.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 0.0f) ) ); + xmlSettings.popTag(); + + xmlSettings.pushTag("texCoord", 1); + texCoords.push_back( ofVec2f( xmlSettings.getValue("x", 1.0f), xmlSettings.getValue("y", 0.0f) ) ); + xmlSettings.popTag(); + + xmlSettings.pushTag("texCoord", 2); + texCoords.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 1.0f) ) ); + xmlSettings.popTag(); + + xmlSettings.popTag(); // texCoords + + + // now we have variables sourceName and sourceTexture + // by checking those we can use one or another addSurface method + if ( sourceName != "none" && sourceTexture != NULL ) { + addSurface(ofxSurfaceType::TRIANGLE_SURFACE, sourceTexture, vertices, texCoords); + } else { + addSurface(ofxSurfaceType::TRIANGLE_SURFACE, vertices, texCoords); + } + + xmlSettings.popTag(); // surface + } + // it's a quad ? + else if (vertexCount == 4) + // if (surface-type == QUAD_SURFACE) + { + ofSendMessage("create Quad"); + + xmlSettings.pushTag("vertex", 0); + vertices.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 0.0f) ) ); + xmlSettings.popTag(); + + xmlSettings.pushTag("vertex", 1); + vertices.push_back( ofVec2f( xmlSettings.getValue("x", 100.0f), xmlSettings.getValue("y", 0.0f) ) ); + xmlSettings.popTag(); + + xmlSettings.pushTag("vertex", 2); + vertices.push_back( ofVec2f( xmlSettings.getValue("x", 100.0f), xmlSettings.getValue("y", 100.0f) ) ); + xmlSettings.popTag(); + + xmlSettings.pushTag("vertex", 3); + vertices.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 100.0f) ) ); + xmlSettings.popTag(); + + xmlSettings.popTag(); // vertices + + xmlSettings.pushTag("texCoords"); + + vector texCoords; + + xmlSettings.pushTag("texCoord", 0); + texCoords.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 0.0f) ) ); + xmlSettings.popTag(); + + xmlSettings.pushTag("texCoord", 1); + texCoords.push_back( ofVec2f( xmlSettings.getValue("x", 1.0f), xmlSettings.getValue("y", 0.0f) ) ); + xmlSettings.popTag(); + + xmlSettings.pushTag("texCoord", 2); + texCoords.push_back( ofVec2f( xmlSettings.getValue("x", 1.0f), xmlSettings.getValue("y", 1.0f) ) ); + xmlSettings.popTag(); + + xmlSettings.pushTag("texCoord", 3); + texCoords.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 1.0f) ) ); + xmlSettings.popTag(); + + xmlSettings.popTag(); // texCoords + + + // now we have variables sourceName and sourceTexture + // by checking those we can use one or another addSurface method + if ( sourceName != "none" && sourceTexture != NULL ) { + addSurface(ofxSurfaceType::QUAD_SURFACE, sourceTexture, vertices, texCoords); + } else { + addSurface(ofxSurfaceType::QUAD_SURFACE, vertices, texCoords); + } + + xmlSettings.popTag(); // surface } - - xmlSettings.popTag(); // surface } xmlSettings.popTag(); // surfaces diff --git a/src/ofxSurfaceManager.h b/src/ofxSurfaceManager.h old mode 100644 new mode 100755 index 2dae777..90e8bb1 --- a/src/ofxSurfaceManager.h +++ b/src/ofxSurfaceManager.h @@ -3,6 +3,7 @@ #include "ofxBaseSurface.h" #include "ofxTriangleSurface.h" +#include "ofxQuadSurface.h" #include "ofxSurfaceType.h" #include "ofEvents.h" #include "ofxXmlSettings.h" diff --git a/src/ofxSurfaceType.h b/src/ofxSurfaceType.h old mode 100644 new mode 100755 index 5914130..cbda131 --- a/src/ofxSurfaceType.h +++ b/src/ofxSurfaceType.h @@ -4,7 +4,8 @@ struct ofxSurfaceType { enum { - TRIANGLE_SURFACE + TRIANGLE_SURFACE, + QUAD_SURFACE }; }; diff --git a/src/ofxTextureEditor.cpp b/src/ofxTextureEditor.cpp old mode 100644 new mode 100755