diff --git a/README.md b/README.md index 71668dc..9ec3065 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ Version history ### Version 0.1.2: TODO: -/+ Possibility to add multiple surfaces with a kind of layer management utility. - - Add GUI for surface manager instead per surface + + Add GUI for surface manager instead per surface + Ability to select and drag whole surfaces + Select / deselect surfaces - Example with video source. Maybe one example with different surfaces and sources. diff --git a/example/src/ofApp.cpp b/example/src/ofApp.cpp index 017e866..2ef4120 100644 --- a/example/src/ofApp.cpp +++ b/example/src/ofApp.cpp @@ -63,6 +63,7 @@ void ofApp::keyPressed(int key) case '2': gui.setMode(ofxGuiMode::TEXTURE_MAPPING); break; case '3': gui.setMode(ofxGuiMode::PROJECTION_MAPPING); break; case 'i': bShowInfo = !bShowInfo; break; + case 'r': addRandomSurface(); break; default: break; } } @@ -83,4 +84,18 @@ void ofApp::mouseDragged(int x, int y, int button) { // //surfaceManager.mouseDragged(x, y, button); +} + +void ofApp::addRandomSurface() +{ + int surfaceType = ofxSurfaceType::TRIANGLE_SURFACE; + vector vertices; + vertices.push_back( ofVec2f( ofRandomWidth(), ofRandomHeight() ) ); + vertices.push_back( ofVec2f( ofRandomWidth(), ofRandomHeight() ) ); + vertices.push_back( ofVec2f( ofRandomWidth(), ofRandomHeight() ) ); + vector texCoords; + texCoords.push_back( ofVec2f( ofRandomuf(), ofRandomuf() ) ); + texCoords.push_back( ofVec2f( ofRandomuf(), ofRandomuf() ) ); + texCoords.push_back( ofVec2f( ofRandomuf(), ofRandomuf() ) ); + surfaceManager.addSurface(surfaceType, vertices, texCoords); } \ No newline at end of file diff --git a/example/src/ofApp.h b/example/src/ofApp.h index d79bd08..6851978 100644 --- a/example/src/ofApp.h +++ b/example/src/ofApp.h @@ -16,6 +16,8 @@ public: void mouseReleased(int x, int y, int button); void mouseDragged(int x, int y, int button); + void addRandomSurface(); + ofImage image; ofxSurfaceManager surfaceManager; ofxSurfaceManagerGui gui; diff --git a/src/ofxSurfaceManager.cpp b/src/ofxSurfaceManager.cpp index beb087b..85dd28b 100644 --- a/src/ofxSurfaceManager.cpp +++ b/src/ofxSurfaceManager.cpp @@ -30,6 +30,62 @@ void ofxSurfaceManager::addSurface(int surfaceType) } } +void ofxSurfaceManager::addSurface(int surfaceType, ofTexture* texturePtr) +{ + if ( surfaceType == ofxSurfaceType::TRIANGLE_SURFACE ) { + surfaces.push_back( new ofxTriangleSurface() ); + surfaces.back()->setTexture(texturePtr); + } else { + throw std::runtime_error("Attempt to add non-existing surface type."); + } +} + +void ofxSurfaceManager::addSurface(int surfaceType, vector vertices, vector texCoords) +{ + if ( surfaceType == ofxSurfaceType::TRIANGLE_SURFACE ) { + + 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."); + } + + surfaces.push_back( new ofxTriangleSurface() ); + + for ( int i=0; i<3; 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."); + } + +} + +void ofxSurfaceManager::addSurface(int surfaceType, ofTexture* texturePtr, vector vertices, vector texCoords) +{ + if ( surfaceType == ofxSurfaceType::TRIANGLE_SURFACE ) { + + 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."); + } + + surfaces.push_back( new ofxTriangleSurface() ); + surfaces.back()->setTexture(texturePtr); + + for ( int i=0; i<3; 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."); + } +} + ofxBaseSurface* ofxSurfaceManager::selectSurface(int index) { if ( index >= surfaces.size() ) { diff --git a/src/ofxSurfaceManager.h b/src/ofxSurfaceManager.h index cf43b1f..5f14354 100644 --- a/src/ofxSurfaceManager.h +++ b/src/ofxSurfaceManager.h @@ -15,6 +15,9 @@ public: void draw(); void addSurface(int surfaceType); + void addSurface(int surfaceType, ofTexture* texturePtr); + void addSurface(int surfaceType, vector vertices, vector texCoords); + void addSurface(int surfaceType, ofTexture* texturePtr, vector vertices, vector texCoords); ofxBaseSurface* getSurface(int index); int size(); ofxBaseSurface* selectSurface(int index);