From e98cae857d1d6dd0ef904933687cb27c5117a0fc Mon Sep 17 00:00:00 2001 From: c-mendoza Date: Thu, 14 Dec 2017 13:35:04 -0500 Subject: [PATCH 01/11] CircleSurface + changes in various files to support the new surface --- src/Application/SettingsLoader.cpp | 85 +++---- src/Gui/Widgets/TextureEditorWidget.cpp | 3 +- src/Surfaces/CircleSurface.cpp | 299 ++++++++++++++++++++++++ src/Surfaces/CircleSurface.h | 63 +++++ src/Surfaces/SurfaceFactory.cpp | 7 + src/Surfaces/SurfaceFactory.h | 2 + src/Surfaces/SurfaceType.h | 3 +- 7 files changed, 421 insertions(+), 41 deletions(-) create mode 100644 src/Surfaces/CircleSurface.cpp create mode 100644 src/Surfaces/CircleSurface.h diff --git a/src/Application/SettingsLoader.cpp b/src/Application/SettingsLoader.cpp index 9f8d5fd..8f294e6 100644 --- a/src/Application/SettingsLoader.cpp +++ b/src/Application/SettingsLoader.cpp @@ -20,46 +20,46 @@ bool SettingsLoader::load( SurfaceManager & surfaceManager, MediaServer & mediaServer, string fileName){ - + ofxXmlSettings * xmlSettings = new ofxXmlSettings(); string sourceType = ""; string sourceName = ""; - + BaseSource * source = 0; - + if(!xmlSettings->loadFile(fileName)){ ofLogWarning("SettingsLoader::load()") << "Could not load XML settings"; return false; } - + if(!xmlSettings->tagExists("surfaces")){ xmlSettings->addTag("surfaces"); } - + // Count tags. unsigned int numPresets = xmlSettings->getNumTags("surfaces"); cout << "numPresets: " << numPresets << endl; - + // Clear previous presets and surfaces first. surfaceManager.clearPresets(); - + // Loop through tags in the XML. for(unsigned int i = 0; i < numPresets; ++i){ - + xmlSettings->pushTag("surfaces", i); - + SurfaceStack * surfaces = surfaceManager.createPreset(); int numSurfaces = xmlSettings->getNumTags("surface"); for(int i = 0; i < numSurfaces; i++){ if(xmlSettings->tagExists("surface", i)){ - + SurfaceType type = SurfaceType::NONE; if(xmlSettings->attributeExists("surface", "type")){ type = static_cast( xmlSettings->getAttribute("surface", "type", 0, i)); } - + xmlSettings->pushTag("surface", i); // attempt to load surface source @@ -122,6 +122,13 @@ bool SettingsLoader::load( quadSurface->setSource(source); } surfaces->push_back(quadSurface); + }else if(type == SurfaceType::CIRCLE_SURFACE){ + QuadSurface * base = (QuadSurface*)getQuadSurface(xmlSettings); + CircleSurface * circleSurface = new CircleSurface(*base); + if(sourceName != "none" && source != 0){ + circleSurface->setSource(source); + } + surfaces->push_back(circleSurface); }else if(type == SurfaceType::GRID_WARP_SURFACE){ BaseSurface * gridWarpSurface = getGridWarpSurface(xmlSettings); if(sourceName != "none" && source != 0){ @@ -227,13 +234,13 @@ bool SettingsLoader::save(SurfaceManager & surfaceManager, string fileName){ xmlSettings->addValue("gridRows", gws->getGridRows()); xmlSettings->popTag(); } - + xmlSettings->popTag(); // surface } xmlSettings->popTag(); // surfaces - + } // for - + xmlSettings->save(fileName); } @@ -245,17 +252,17 @@ bool SettingsLoader::create(string fileName){ BaseSurface * SettingsLoader::getTriangleSurface(ofxXmlSettings * xmlSettings){ vector vertices; - + if(xmlSettings->tagExists("vertices")){ xmlSettings->pushTag("vertices"); - + if(xmlSettings->tagExists("vertex", 0)){ xmlSettings->pushTag("vertex", 0); vertices.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f), xmlSettings->getValue("y", 0.0f))); xmlSettings->popTag(); } - + if(xmlSettings->tagExists("vertex", 1)){ xmlSettings->pushTag("vertex", 1); vertices.push_back(ofVec2f(xmlSettings->getValue("x", 100.0f), @@ -277,14 +284,14 @@ BaseSurface * SettingsLoader::getTriangleSurface(ofxXmlSettings * xmlSettings){ if(xmlSettings->tagExists("texCoords")){ xmlSettings->pushTag("texCoords"); - + if(xmlSettings->tagExists("texCoord", 0)){ xmlSettings->pushTag("texCoord", 0); texCoords.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f), xmlSettings->getValue("y", 0.0f))); xmlSettings->popTag(); } - + if(xmlSettings->tagExists("texCoord", 1)){ xmlSettings->pushTag("texCoord", 1); texCoords.push_back(ofVec2f(xmlSettings->getValue("x", 1.0f), @@ -308,16 +315,16 @@ BaseSurface * SettingsLoader::getTriangleSurface(ofxXmlSettings * xmlSettings){ SurfaceType::TRIANGLE_SURFACE); triangleSurface->setVertices(vertices); triangleSurface->setTexCoords(texCoords); - + return triangleSurface; } BaseSurface * SettingsLoader::getQuadSurface(ofxXmlSettings * xmlSettings){ vector vertices; - + if(xmlSettings->tagExists("vertices")){ xmlSettings->pushTag("vertices"); - + if(xmlSettings->tagExists("vertex", 0)){ xmlSettings->pushTag("vertex", 0); vertices.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f), @@ -348,7 +355,7 @@ BaseSurface * SettingsLoader::getQuadSurface(ofxXmlSettings * xmlSettings){ xmlSettings->popTag(); // vertices } - + vector texCoords; if(xmlSettings->tagExists("texCoords")){ @@ -360,7 +367,7 @@ BaseSurface * SettingsLoader::getQuadSurface(ofxXmlSettings * xmlSettings){ xmlSettings->getValue("y", 0.0f))); xmlSettings->popTag(); } - + if(xmlSettings->tagExists("texCoord", 1)){ xmlSettings->pushTag("texCoord", 1); texCoords.push_back(ofVec2f(xmlSettings->getValue("x", 1.0f), @@ -384,14 +391,14 @@ BaseSurface * SettingsLoader::getQuadSurface(ofxXmlSettings * xmlSettings){ xmlSettings->popTag(); // texCoords } - + // Create and add quad surface BaseSurface * quadSurface = SurfaceFactory::instance()->createSurface( SurfaceType::QUAD_SURFACE); quadSurface->setVertices(vertices); quadSurface->setTexCoords(texCoords); - + // Read properties // Only perspective warping for now bool perspectiveWarping = false; @@ -402,18 +409,18 @@ BaseSurface * SettingsLoader::getQuadSurface(ofxXmlSettings * xmlSettings){ } QuadSurface * qs = (QuadSurface *)quadSurface; qs->setPerspectiveWarping(perspectiveWarping); - + return quadSurface; } BaseSurface * SettingsLoader::getGridWarpSurface(ofxXmlSettings * xmlSettings){ vector vertices; - + if(xmlSettings->tagExists("vertices")){ xmlSettings->pushTag("vertices"); - + int iv = 0; - + while(xmlSettings->tagExists("vertex", iv)){ xmlSettings->pushTag("vertex", iv); vertices.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f), @@ -424,14 +431,14 @@ BaseSurface * SettingsLoader::getGridWarpSurface(ofxXmlSettings * xmlSettings){ xmlSettings->popTag(); // vertices } - + vector texCoords; if(xmlSettings->tagExists("texCoords")){ xmlSettings->pushTag("texCoords"); int it = 0; - + while(xmlSettings->tagExists("texCoord", it)){ xmlSettings->pushTag("texCoord", it); texCoords.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f), @@ -442,7 +449,7 @@ BaseSurface * SettingsLoader::getGridWarpSurface(ofxXmlSettings * xmlSettings){ xmlSettings->popTag(); // texCoords } - + // Read properties // Only perspective warping for now int gridCols = 0; @@ -453,7 +460,7 @@ BaseSurface * SettingsLoader::getGridWarpSurface(ofxXmlSettings * xmlSettings){ gridRows = xmlSettings->getValue("gridRows", 0); xmlSettings->popTag(); // properties } - + // Create and add quad surface BaseSurface * gridWarpSurface = SurfaceFactory::instance()->createSurface( @@ -463,16 +470,16 @@ BaseSurface * SettingsLoader::getGridWarpSurface(ofxXmlSettings * xmlSettings){ ((GridWarpSurface *)gridWarpSurface)->createGridMesh(); gridWarpSurface->setVertices(vertices); gridWarpSurface->setTexCoords(texCoords); - + return gridWarpSurface; } BaseSurface * SettingsLoader::getHexagonSurface(ofxXmlSettings * xmlSettings){ vector vertices; - + if(xmlSettings->tagExists("vertices")){ xmlSettings->pushTag("vertices"); - + unsigned int v = 0; while(xmlSettings->tagExists("vertex", v)){ xmlSettings->pushTag("vertex", v); @@ -489,7 +496,7 @@ BaseSurface * SettingsLoader::getHexagonSurface(ofxXmlSettings * xmlSettings){ if(xmlSettings->tagExists("texCoords")){ xmlSettings->pushTag("texCoords"); - + unsigned int t = 0; while(xmlSettings->tagExists("texCoord", t)){ xmlSettings->pushTag("texCoord", t); @@ -508,7 +515,7 @@ BaseSurface * SettingsLoader::getHexagonSurface(ofxXmlSettings * xmlSettings){ SurfaceType::HEXAGON_SURFACE); hexagonSurface->setVertices(vertices); hexagonSurface->setTexCoords(texCoords); - + return hexagonSurface; } diff --git a/src/Gui/Widgets/TextureEditorWidget.cpp b/src/Gui/Widgets/TextureEditorWidget.cpp index 6ce0a88..617c0a6 100644 --- a/src/Gui/Widgets/TextureEditorWidget.cpp +++ b/src/Gui/Widgets/TextureEditorWidget.cpp @@ -149,7 +149,8 @@ void TextureEditorWidget::createJoints(){ if(surface->getType() == SurfaceType::TRIANGLE_SURFACE){ tc = texCoords; - }else if(surface->getType() == SurfaceType::QUAD_SURFACE){ + }else if(surface->getType() == SurfaceType::QUAD_SURFACE || + surface->getType() == SurfaceType::CIRCLE_SURFACE){ tc = texCoords; }else if(surface->getType() == SurfaceType::HEXAGON_SURFACE){ tc = texCoords; diff --git a/src/Surfaces/CircleSurface.cpp b/src/Surfaces/CircleSurface.cpp new file mode 100644 index 0000000..95af0b2 --- /dev/null +++ b/src/Surfaces/CircleSurface.cpp @@ -0,0 +1,299 @@ +// +// CircleSurface.cpp +// Copyright (c) 2017 Cristobal Mendoza +// http://cuppetellimendoza.com + +#include "CircleSurface.h" + +namespace ofx { +namespace piMapper { + +CircleSurface::CircleSurface() : QuadSurface() { + setup(); +} + +CircleSurface::CircleSurface(QuadSurface &surface) { + setup(); + setVertices(surface.getVertices()); + setTexCoords(surface.getTexCoords()); + setPerspectiveWarping(surface.getPerspectiveWarping()); +} + +CircleSurface::~CircleSurface() {} + +void CircleSurface::setup() { + + setPerspectiveWarping(true); + QuadSurface::setup(); + + updateMask = true; +// maskIsReady = false; + + glESVertexShader = STRINGIFY( + attribute vec4 position; + attribute vec4 color; + attribute vec4 normal; + attribute vec2 texcoord; + + uniform mat4 modelViewMatrix; + uniform mat4 projectionMatrix; + uniform sampler2D maskTex; + + varying vec4 colorVarying; + varying vec2 texCoordVarying; + + void main() { + + //get our current vertex position so we can modify it + vec4 pos = projectionMatrix*modelViewMatrix*position; + + gl_Position = pos; + colorVarying = color; + texCoordVarying = texcoord; + } + ); + + glESFragmentShader = STRINGIFY( +//#ifdef GL_ES +// define default precision for float, vec, mat. + precision highp float; +//#endif + + uniform sampler2D tex0; + uniform sampler2D maskTex; + uniform vec4 globalColor; + + varying vec2 texCoordVarying; + + void main (void) + { + vec2 pos = texCoordVarying; + vec3 src = texture2D(tex0, pos).rgb; + float mask = texture2D(maskTex, pos).r; + gl_FragColor = vec4( src , mask); + } + ); + + gl2FragmentShader = "#version 120\n #extension GL_ARB_texture_rectangle : enable\n"; + gl2FragmentShader += STRINGIFY( + uniform sampler2DRect tex0; + uniform sampler2DRect maskTex; + + void main (void) { + vec2 pos = gl_TexCoord[0].st; + + vec3 src = texture2DRect(tex0, pos).rgb; + float mask = texture2DRect(maskTex, pos).r; + + gl_FragColor = vec4(src, mask); + } + ); + +#ifdef TARGET_OPENGLES + maskShader.setupShaderFromSource(GL_VERTEX_SHADER, glESVertexShader); + maskShader.setupShaderFromSource(GL_FRAGMENT_SHADER, glESFragmentShader); + maskShader.bindDefaults(); + maskShader.linkProgram(); +#else + if (ofIsGLProgrammableRenderer()) { + + } else { + maskShader.setupShaderFromSource(GL_FRAGMENT_SHADER, gl2FragmentShader); + maskShader.linkProgram(); + } +#endif + + ofVec2f t1 = ofVec2f(ofVec2f(0.0f, 0.0f)); + ofVec2f t2 = ofVec2f(ofVec2f(1.0f, 0.0f)); + ofVec2f t3 = ofVec2f(ofVec2f(1.0f, 1.0f)); + ofVec2f t4 = ofVec2f(ofVec2f(0.0f, 1.0f)); + + defaultTexCoords.push_back(t1); + defaultTexCoords.push_back(t2); + defaultTexCoords.push_back(t3); + defaultTexCoords.push_back(t4); +} + +void CircleSurface::draw() { + + ofEnableAlphaBlending(); + if (source->getTexture() == 0) + { + return; + } + if (!source->getTexture()->isAllocated()){ + return; + } + + if (source != currentSource) { // Pointer comparison + // Create the mask here + setupTextures(); + currentSource = source; + } + + + // Save Normie state: + auto isNorm = ofGetUsingNormalizedTexCoords(); + + // If we get to this part of the function, the mask fbo + // should already be allocated and the mask texture created. + + ofEnableNormalizedTexCoords(); + // Get the texture from the source an store a copy + // of the source texture's id: + auto sourceTex = ofTexture(*(source->getTexture())); + auto sourceTexId = sourceTex.getTextureData().textureID; + + // Get the quad surface's mesh texture coordinates: + auto quadTexCoords = getMesh().getTexCoords(); + + maskMesh.clearTexCoords(); + + // Set the mesh's texture coords to the quads. + // This gets us the coordinates set in the TextureEditor + maskMesh.addTexCoords(quadTexCoords); + + float w = outputFbo.getWidth(); + float h = outputFbo.getHeight(); + + // Draw the scaled texture into an FBO + scaledSourceFbo.begin(true); + { + ofClear(0, 0, 0, 255); + ofSetupScreenOrtho(w, h, -1, 1); + ofEnableNormalizedTexCoords(); + ofFill(); + ofSetColor(255); + sourceTex.bind(); + maskMesh.draw(); + sourceTex.unbind(); + } + scaledSourceFbo.end(); + + // Now draw the texture, masked, into the outputFbo. + // This enables us to use the TextureEditor to determine + // what part of the source will be masked: + maskMesh.clearTexCoords(); + + // Set default normalized texCoords + maskMesh.addTexCoords(defaultTexCoords); + + outputFbo.begin(true); + { + ofClear(0, 0, 0, 0); + ofSetupScreenOrtho(w, h, -1, 1); + ofEnableNormalizedTexCoords(); + maskShader.begin(); + maskShader.setUniformTexture("maskTex", maskFbo.getTexture(), 1); + ofSetColor(255); + ofFill(); + ofSetRectMode(OF_RECTMODE_CORNER); + scaledSourceFbo.getTexture().bind(); + maskMesh.draw(); + scaledSourceFbo.getTexture().unbind(); + maskShader.end(); + } + outputFbo.end(); + + getMesh().clearTexCoords(); + getMesh().addTexCoords(defaultTexCoords); + + // Swap the texture id of the source with the one of our + // newly drawn outputFbo: + source->getTexture()->getTextureData().textureID = outputFbo.getTexture().getTextureData().textureID; + + // Draw the Quad: + QuadSurface::draw(); + + // Reset the texture id of the source + source->getTexture()->getTextureData().textureID = sourceTexId; + + // Reset the texture coords of the QuadSurface mesh: + getMesh().clearTexCoords(); + getMesh().addTexCoords(quadTexCoords); + + if (!isNorm) ofDisableNormalizedTexCoords(); +} + +void CircleSurface::setFeathering(float f) { + feathering = f; + updateMask = true; +} + +void CircleSurface::setupTextures() { + float w = source->getTexture()->getWidth(); + float h = source->getTexture()->getHeight(); + float dia = 0; + if (w > h) { + dia = h; + } else { + dia = w; + } + + float padding = 10; + + maskFbo.allocate(w, h); + maskFbo.begin(false); + ofPushStyle(); + ofSetupScreenOrtho(w, h, -1, 1); + ofClear(0, 0, 0, 255); + ofFill(); + ofSetColor(255); + ofSetCircleResolution(300); + ofDrawEllipse(w/2, h/2, w-padding, h-padding); + ofPopStyle(); + maskFbo.end(); + + outputFbo.allocate(w, h); + outputFbo.begin(); + ofClear(0, 0, 0, 255); + outputFbo.end(); + + scaledSourceFbo.allocate(w, h); + scaledSourceFbo.begin(); + ofClear(0, 0, 0, 255); + scaledSourceFbo.end(); + + // This is lifted from QuadSurface::setup to ensure that the two + // meshes are similar: + + // Create 4 points for the 2 triangles + ofVec2f p1 = ofVec2f(0, 0); + ofVec2f p2 = ofVec2f(0, h); + ofVec2f p3 = ofVec2f(w, h); + ofVec2f p4 = ofVec2f(w, 0); + + // Create 4 point for the texture coordinates + ofVec2f t1 = ofVec2f(ofVec2f(0.0f, 0.0f)); + ofVec2f t2 = ofVec2f(ofVec2f(1.0f, 0.0f)); + ofVec2f t3 = ofVec2f(ofVec2f(1.0f, 1.0f)); + ofVec2f t4 = ofVec2f(ofVec2f(0.0f, 1.0f)); + + // Clear maskMesh + maskMesh.clear(); + + // Create a surface with the points + maskMesh.addVertex(p1); + maskMesh.addVertex(p2); + maskMesh.addVertex(p3); + maskMesh.addVertex(p4); + + // Add 2 triangles + maskMesh.addTriangle(0, 2, 3); + maskMesh.addTriangle(0, 1, 2); + + // Add texture coordinates + maskMesh.addTexCoord(t1); + maskMesh.addTexCoord(t2); + maskMesh.addTexCoord(t3); + maskMesh.addTexCoord(t4); +} + + + +int CircleSurface::getType() { + return SurfaceType::CIRCLE_SURFACE; +} + +} +} \ No newline at end of file diff --git a/src/Surfaces/CircleSurface.h b/src/Surfaces/CircleSurface.h new file mode 100644 index 0000000..262736d --- /dev/null +++ b/src/Surfaces/CircleSurface.h @@ -0,0 +1,63 @@ +// +// CircleSurface.h +// Copyright (c) 2017 Cristobal Mendoza +// http://cuppetellimendoza.com + +#ifndef OFXPIMAPPER_CIRCLESURFACE_H +#define OFXPIMAPPER_CIRCLESURFACE_H + +#include "QuadSurface.h" + +#define STRINGIFY(A) #A + +namespace ofx { +namespace piMapper { + +class CircleSurface : public QuadSurface { + public: + CircleSurface(); + CircleSurface(QuadSurface &surface); + int getType() override; + ~CircleSurface(); + void draw() override; + void setup() override; + + // TODO: Feathering + void setFeathering(float f); + + protected: + void setupTextures(); + ofFbo maskFbo; + ofFbo scaledSourceFbo; + ofFbo outputFbo; + ofShader maskShader; + ofShader gradientShader; + float feathering = 0.0f; + bool updateMask; + bool maskIsReady; + + string glESFragmentShader; + string glESVertexShader; + + string gl2FragmentShader; + string gl2VertexShader; + + ofMesh maskMesh; + + // TODO: gl3 Shaders +// string gl3VertexShader; +// string gl3FragmentShader; + + private: + std::vector defaultTexCoords; + // We will use this pointer to determine if the source has changed. + // This is a total kludge, but it keeps me from messing with the + // upstream source. + BaseSource* currentSource = 0; +}; + +} +} + + +#endif //OFXPIMAPPER_CIRCLESURFACE_H diff --git a/src/Surfaces/SurfaceFactory.cpp b/src/Surfaces/SurfaceFactory.cpp index 1adbba2..b85ae01 100644 --- a/src/Surfaces/SurfaceFactory.cpp +++ b/src/Surfaces/SurfaceFactory.cpp @@ -21,6 +21,8 @@ BaseSurface * SurfaceFactory::createSurface(SurfaceType type){ return createGridWarpSurface(); }else if(type == SurfaceType::HEXAGON_SURFACE){ return createHexagonSurface(); + }else if(type == SurfaceType::CIRCLE_SURFACE){ + return createCircleSurface(); }else{ throw runtime_error("Undefined surface type"); } @@ -83,5 +85,10 @@ HexagonSurface * SurfaceFactory::createHexagonSurface(){ return hexagonSurface; } +CircleSurface * SurfaceFactory::createCircleSurface() { + CircleSurface * circleSurface = new CircleSurface(); + return circleSurface; +} + } // namespace piMapper } // namespace ofx diff --git a/src/Surfaces/SurfaceFactory.h b/src/Surfaces/SurfaceFactory.h index 90b2827..14cd012 100644 --- a/src/Surfaces/SurfaceFactory.h +++ b/src/Surfaces/SurfaceFactory.h @@ -7,6 +7,7 @@ #include "QuadSurface.h" #include "GridWarpSurface.h" #include "HexagonSurface.h" +#include "CircleSurface.h" namespace ofx { namespace piMapper { @@ -26,6 +27,7 @@ class SurfaceFactory { QuadSurface * createQuadSurface(); GridWarpSurface * createGridWarpSurface(); HexagonSurface * createHexagonSurface(); + CircleSurface* createCircleSurface(); }; } // namespace piMapper diff --git a/src/Surfaces/SurfaceType.h b/src/Surfaces/SurfaceType.h index ae52270..b039427 100644 --- a/src/Surfaces/SurfaceType.h +++ b/src/Surfaces/SurfaceType.h @@ -8,7 +8,8 @@ enum SurfaceType{ QUAD_SURFACE, GRID_WARP_SURFACE, HEXAGON_SURFACE, - NONE + NONE, + CIRCLE_SURFACE }; } // namespace piMapper From b3d9fb3a3177818003012aaf8651477e435e97c4 Mon Sep 17 00:00:00 2001 From: c-mendoza Date: Fri, 15 Dec 2017 18:24:37 -0500 Subject: [PATCH 02/11] Update example xml file to display a CircleSurface --- example/bin/data/magslideshow_settings.xml | 4 +- example/bin/data/ofxpimapper.xml | 81 +++++++++++++++++----- 2 files changed, 65 insertions(+), 20 deletions(-) diff --git a/example/bin/data/magslideshow_settings.xml b/example/bin/data/magslideshow_settings.xml index 58c00da..42b8b02 100644 --- a/example/bin/data/magslideshow_settings.xml +++ b/example/bin/data/magslideshow_settings.xml @@ -1,6 +1,6 @@ - 1920 - 1080 + 1280 + 720 2 PING-PONG diff --git a/example/bin/data/ofxpimapper.xml b/example/bin/data/ofxpimapper.xml index b9b1861..2390ae8 100644 --- a/example/bin/data/ofxpimapper.xml +++ b/example/bin/data/ofxpimapper.xml @@ -2,8 +2,8 @@ - 244.000000000 - 151.187255859 + 140.000000000 + 65.000000000 397.625549316 @@ -33,46 +33,91 @@ image4.jpg + + + + 183.000000000 + 70.000000000 + + + 862.290893555 + 79.031463623 + + + 830.286376953 + 652.968627930 + + + 195.000000000 + 711.000000000 + + + + + 0.224322945 + 0.023645690 + + + 0.774270892 + 0.023645690 + + + 0.774270892 + 0.973813117 + + + 0.224322945 + 0.973813117 + + + + fbo + Slide Show Source + + + 1 + + - 305.332824707 - 158.923416138 + 219.956512451 + 481.153137207 - 823.889770508 - 135.275909424 + 742.043579102 + 481.153137207 - 883.853393555 - 670.724243164 + 742.043579102 + 792.846923828 - 241.146575928 - 656.366699219 + 219.956512451 + 792.846923828 - 0.000000000 + 0.106250003 0.000000000 - 1.000000000 + 0.918749988 0.000000000 - 1.000000000 - 1.000000000 + 0.918749988 + 0.994444430 - 0.000000000 - 1.000000000 + 0.106250003 + 0.994444430 - fbo - Slide Show Source + video + earth-and-technicians.mp4 1 From a8821686cc9c318710096c823f7c450bbfc9b2e2 Mon Sep 17 00:00:00 2001 From: c-mendoza Date: Fri, 15 Dec 2017 18:40:23 -0500 Subject: [PATCH 03/11] Update Key Bindings to support CircleSurface Bound to 'r' Update Info.cpp to show the new key binding in the help screen. --- src/Application/Modes/ProjectionMappingMode.cpp | 8 ++++++-- src/Info/Info.cpp | 11 ++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/Application/Modes/ProjectionMappingMode.cpp b/src/Application/Modes/ProjectionMappingMode.cpp index 9495743..9344fa3 100644 --- a/src/Application/Modes/ProjectionMappingMode.cpp +++ b/src/Application/Modes/ProjectionMappingMode.cpp @@ -58,8 +58,12 @@ void ProjectionMappingMode::onKeyPressed(Application * app, ofKeyEventArgs & arg case 'q': app->createSurface(SurfaceType::QUAD_SURFACE); - break; - + break; + + case 'r': + app->createSurface(SurfaceType::CIRCLE_SURFACE); + break; + case 'g': app->createSurface(SurfaceType::GRID_WARP_SURFACE); break; diff --git a/src/Info/Info.cpp b/src/Info/Info.cpp index 9d9f489..7823ee0 100644 --- a/src/Info/Info.cpp +++ b/src/Info/Info.cpp @@ -13,15 +13,16 @@ Info::Info(){ " 3. Projection mapping mode\n" " - Press <,> and <.> to select previous or next surface\n" " - Press \"<\" and \">\" to select previous or next vertex\n" - " - Press to add new triangle surface\n" - " - Press to add new quad surface\n" - " - Press

to toggle perspective warping while quad surface selected\n" - " - Press to add new grid surface\n" + " - Press to add new Triangle surface\n" + " - Press to add new Quad surface\n" + " - Press to add a new ciRcle surface\n" + " - Press

to toggle Perspective warping while quad surface selected\n" + " - Press to add new Grid surface\n" " - Press <[> and <]> to remove or add columns to selected grid surface\n" " - Press <{> and <}> to remove or add rows to selected grid surface\n" " - Press <+> and <-> to scale surface up and down\n" " - Press <9> and <0> to move selected surface one layer up or down\n" - " - Press to hide/show layer panel\n" + " - Press to hide/show Layer panel\n" " - Press to delete selection\n" " - Press to play/pause the video\n" " - Type to clear composition\n" From 2a9e451e12d3edf660754baf94719934daafa52d Mon Sep 17 00:00:00 2001 From: c-mendoza Date: Fri, 15 Dec 2017 21:17:25 -0500 Subject: [PATCH 04/11] Fix for SettingsLoader to support CircleSurface --- src/Application/SettingsLoader.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Application/SettingsLoader.cpp b/src/Application/SettingsLoader.cpp index 8f294e6..9582c4c 100644 --- a/src/Application/SettingsLoader.cpp +++ b/src/Application/SettingsLoader.cpp @@ -216,7 +216,8 @@ bool SettingsLoader::save(SurfaceManager & surfaceManager, string fileName){ // Save surface options // For now only if quad surface - if(surface->getType() == SurfaceType::QUAD_SURFACE){ + if (surface->getType() == SurfaceType::QUAD_SURFACE || + surface->getType() == SurfaceType::CIRCLE_SURFACE) { QuadSurface * qs = (QuadSurface *)surface; if(!xmlSettings->tagExists("properties")){ xmlSettings->addTag("properties"); From 7f29c755ab1eacc081f5373e64b3ee70b4c47439 Mon Sep 17 00:00:00 2001 From: c-mendoza Date: Fri, 15 Dec 2017 21:17:55 -0500 Subject: [PATCH 05/11] Small fix to CircleSurface --- src/Surfaces/CircleSurface.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Surfaces/CircleSurface.cpp b/src/Surfaces/CircleSurface.cpp index 95af0b2..6183084 100644 --- a/src/Surfaces/CircleSurface.cpp +++ b/src/Surfaces/CircleSurface.cpp @@ -23,8 +23,8 @@ CircleSurface::~CircleSurface() {} void CircleSurface::setup() { - setPerspectiveWarping(true); QuadSurface::setup(); + setPerspectiveWarping(true); updateMask = true; // maskIsReady = false; From 6f298161c6046ac41378ab67e7b57d2a8b4e965f Mon Sep 17 00:00:00 2001 From: c-mendoza Date: Fri, 15 Dec 2017 22:58:09 -0500 Subject: [PATCH 06/11] Example update for RPi Example update for RPi --- example/bin/data/ofxpimapper.xml | 97 +++++++++++++++++++++++--------- example/src/main.cpp | 24 ++++++-- 2 files changed, 91 insertions(+), 30 deletions(-) diff --git a/example/bin/data/ofxpimapper.xml b/example/bin/data/ofxpimapper.xml index 2390ae8..99a1ecc 100644 --- a/example/bin/data/ofxpimapper.xml +++ b/example/bin/data/ofxpimapper.xml @@ -2,16 +2,16 @@ - 140.000000000 - 65.000000000 + 119.000000000 + 379.000000000 - 397.625549316 - 304.812774658 + 387.000000000 + 96.000000000 - 90.374511719 - 304.812774658 + 405.000000000 + 657.000000000 @@ -36,8 +36,8 @@ - 183.000000000 - 70.000000000 + 387.000000000 + 96.000000000 862.290893555 @@ -48,8 +48,8 @@ 652.968627930 - 195.000000000 - 711.000000000 + 405.000000000 + 657.000000000 @@ -78,46 +78,91 @@ 1 - + + + + 860.523620605 + 46.077301025 + + + 860.523620605 + 247.922729492 + + + 1379.000000000 + 533.000000000 + + + 1183.476318359 + 46.077301025 + + + + + 0.000000000 + 0.000000000 + + + 1.000000000 + 0.000000000 + + + 1.000000000 + 1.000000000 + + + 0.000000000 + 1.000000000 + + + + image + image2.jpg + + + 1 + + + - 219.956512451 - 481.153137207 + 1276.000000000 + 204.000000000 - 742.043579102 - 481.153137207 + 724.000000000 + 387.000000000 - 742.043579102 - 792.846923828 + 707.000000000 + 809.000000000 - 219.956512451 - 792.846923828 + 1284.000000000 + 708.000000000 - 0.106250003 + 0.101562500 0.000000000 - 0.918749988 + 0.884374976 0.000000000 - 0.918749988 - 0.994444430 + 0.884374976 + 1.002777815 - 0.106250003 - 0.994444430 + 0.101562500 + 1.002777815 video - earth-and-technicians.mp4 + control-panel-and-operation.mp4 1 diff --git a/example/src/main.cpp b/example/src/main.cpp index d743ec0..9bdae84 100644 --- a/example/src/main.cpp +++ b/example/src/main.cpp @@ -14,9 +14,25 @@ int main(int argc, char * argv[]){ break; } } - - Settings::instance()->setFullscreen(fullscreen); - ofSetupOpenGL(1024, 768, OF_WINDOW); - ofRunApp(new ofApp()); + shared_ptr window; +#ifdef TARGET_OPENGLES + ofGLESWindowSettings esSettings; + esSettings.glesVersion = 2; + esSettings.width = 1440; + esSettings.height = 900; + window = ofCreateWindow(esSettings); +#else + ofGLWindowSettings glSettings; + glSettings.glVersionMajor = 2; + glSettings.glVersionMinor = 1; + glSettings.width = 1440; + glSettings.height = 900; + window = ofCreateWindow(glSettings); +#endif + + auto app = std::make_shared(); + ofRunApp(window, app); + Settings::instance()->setFullscreen(fullscreen); + ofRunMainLoop(); } From 345b1b5be10fa993bb4d1362ba4bb670b9b745b0 Mon Sep 17 00:00:00 2001 From: magdesign Date: Sat, 16 Dec 2017 14:04:48 +0100 Subject: [PATCH 07/11] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 115133f..d848bda 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,8 @@ check out this http://www.hv-a.com/lpmt/sssm.pdf on page: 10 https://forum.openframeworks.cc/t/grid-mesh-warping/12883 ### 7. SoftEdge: Image overlay to making adges transparent, could be achieved with overlaing a png with a black gradient. - +### 7. Color Adjustment: +Adjust brightness and contrast. ## Targeting OF_0.9.x on RaspberryPi. From 71dfeae8bd92958add53d6eca47155c2e9fd7f6a Mon Sep 17 00:00:00 2001 From: c-mendoza Date: Sat, 16 Dec 2017 10:14:25 -0500 Subject: [PATCH 08/11] Fix for full screen --- example/src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/src/main.cpp b/example/src/main.cpp index 9bdae84..ad3c2af 100644 --- a/example/src/main.cpp +++ b/example/src/main.cpp @@ -32,7 +32,7 @@ int main(int argc, char * argv[]){ #endif auto app = std::make_shared(); - ofRunApp(window, app); Settings::instance()->setFullscreen(fullscreen); + ofRunApp(window, app); ofRunMainLoop(); } From d3270674d2b6d4ec8908e033d0520e0f0b76abfc Mon Sep 17 00:00:00 2001 From: c-mendoza Date: Sat, 16 Dec 2017 10:15:06 -0500 Subject: [PATCH 09/11] Renamed stringify macro to avoid a naming conflict --- src/Surfaces/CircleSurface.cpp | 6 +++--- src/Surfaces/CircleSurface.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Surfaces/CircleSurface.cpp b/src/Surfaces/CircleSurface.cpp index 6183084..a636dc8 100644 --- a/src/Surfaces/CircleSurface.cpp +++ b/src/Surfaces/CircleSurface.cpp @@ -29,7 +29,7 @@ void CircleSurface::setup() { updateMask = true; // maskIsReady = false; - glESVertexShader = STRINGIFY( + glESVertexShader = CIRCLE_SURFACE_STRINGIFY( attribute vec4 position; attribute vec4 color; attribute vec4 normal; @@ -53,7 +53,7 @@ void CircleSurface::setup() { } ); - glESFragmentShader = STRINGIFY( + glESFragmentShader = CIRCLE_SURFACE_STRINGIFY( //#ifdef GL_ES // define default precision for float, vec, mat. precision highp float; @@ -75,7 +75,7 @@ void CircleSurface::setup() { ); gl2FragmentShader = "#version 120\n #extension GL_ARB_texture_rectangle : enable\n"; - gl2FragmentShader += STRINGIFY( + gl2FragmentShader += CIRCLE_SURFACE_STRINGIFY( uniform sampler2DRect tex0; uniform sampler2DRect maskTex; diff --git a/src/Surfaces/CircleSurface.h b/src/Surfaces/CircleSurface.h index 262736d..6a21020 100644 --- a/src/Surfaces/CircleSurface.h +++ b/src/Surfaces/CircleSurface.h @@ -8,7 +8,7 @@ #include "QuadSurface.h" -#define STRINGIFY(A) #A +#define CIRCLE_SURFACE_STRINGIFY(A) #A namespace ofx { namespace piMapper { From e73f8624d0c0345506d2836c0fc3a1829a1a68ef Mon Sep 17 00:00:00 2001 From: c-mendoza Date: Sat, 16 Dec 2017 15:21:56 -0500 Subject: [PATCH 10/11] Alpha masking without shaders for RPi --- example/src/main.cpp | 22 +++------------------- src/Surfaces/CircleSurface.cpp | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/example/src/main.cpp b/example/src/main.cpp index ad3c2af..2c8ec5b 100644 --- a/example/src/main.cpp +++ b/example/src/main.cpp @@ -15,24 +15,8 @@ int main(int argc, char * argv[]){ } } - shared_ptr window; -#ifdef TARGET_OPENGLES - ofGLESWindowSettings esSettings; - esSettings.glesVersion = 2; - esSettings.width = 1440; - esSettings.height = 900; - window = ofCreateWindow(esSettings); -#else - ofGLWindowSettings glSettings; - glSettings.glVersionMajor = 2; - glSettings.glVersionMinor = 1; - glSettings.width = 1440; - glSettings.height = 900; - window = ofCreateWindow(glSettings); -#endif - - auto app = std::make_shared(); Settings::instance()->setFullscreen(fullscreen); - ofRunApp(window, app); - ofRunMainLoop(); + + ofSetupOpenGL(1024, 768, OF_WINDOW); + ofRunApp(new ofApp()); } diff --git a/src/Surfaces/CircleSurface.cpp b/src/Surfaces/CircleSurface.cpp index a636dc8..4d7f9ec 100644 --- a/src/Surfaces/CircleSurface.cpp +++ b/src/Surfaces/CircleSurface.cpp @@ -183,6 +183,23 @@ void CircleSurface::draw() { ofClear(0, 0, 0, 0); ofSetupScreenOrtho(w, h, -1, 1); ofEnableNormalizedTexCoords(); + ofSetColor(255); + ofFill(); + ofSetRectMode(OF_RECTMODE_CORNER); +#ifdef TARGET_RASPBERRY_PI + scaledSourceFbo.getTexture().bind(); + maskMesh.draw(); + scaledSourceFbo.getTexture().unbind(); + + // Masking without shaders... + ofPushStyle(); + ofEnableBlendMode(OF_BLENDMODE_MULTIPLY); + ofSetColor(255); + ofFill(); + ofDisableNormalizedTexCoords(); + maskFbo.draw(0, 0); + ofPopStyle(); +#else maskShader.begin(); maskShader.setUniformTexture("maskTex", maskFbo.getTexture(), 1); ofSetColor(255); @@ -192,6 +209,8 @@ void CircleSurface::draw() { maskMesh.draw(); scaledSourceFbo.getTexture().unbind(); maskShader.end(); +#endif + } outputFbo.end(); From 2122f4bc8a7ceccb776be8785058176b464962b2 Mon Sep 17 00:00:00 2001 From: c-mendoza Date: Sun, 17 Dec 2017 19:28:04 -0500 Subject: [PATCH 11/11] CircleSurface: refactoring for performance, wip --- example/bin/data/magslideshow_settings.xml | 8 +- example/bin/data/ofxpimapper.xml | 89 +++++-- src/Surfaces/CircleSurface.cpp | 279 ++++++++++----------- src/Surfaces/CircleSurface.h | 17 +- 4 files changed, 213 insertions(+), 180 deletions(-) diff --git a/example/bin/data/magslideshow_settings.xml b/example/bin/data/magslideshow_settings.xml index 42b8b02..3e12c23 100644 --- a/example/bin/data/magslideshow_settings.xml +++ b/example/bin/data/magslideshow_settings.xml @@ -1,14 +1,14 @@ - 1280 - 720 + 800 + 600 2 - PING-PONG + NORMAL 0 Dissolve - 2 + 3 FitProportionally diff --git a/example/bin/data/ofxpimapper.xml b/example/bin/data/ofxpimapper.xml index 99a1ecc..73dc26f 100644 --- a/example/bin/data/ofxpimapper.xml +++ b/example/bin/data/ofxpimapper.xml @@ -36,38 +36,38 @@ - 387.000000000 + 162.000000000 96.000000000 - 862.290893555 + 637.290893555 79.031463623 - 830.286376953 + 605.286376953 652.968627930 - 405.000000000 + 180.000000000 657.000000000 - 0.224322945 - 0.023645690 + 0.159322947 + 0.055312362 - 0.774270892 - 0.023645690 + 0.830520868 + 0.055312362 - 0.774270892 - 0.973813117 + 0.830520868 + 0.938813090 - 0.224322945 - 0.973813117 + 0.159322947 + 0.938813090 @@ -85,8 +85,8 @@ 46.077301025 - 860.523620605 - 247.922729492 + 887.000000000 + 402.000000000 1379.000000000 @@ -126,20 +126,20 @@ - 1276.000000000 - 204.000000000 + 1364.000000000 + 252.000000000 - 724.000000000 - 387.000000000 + 742.000000000 + 392.000000000 - 707.000000000 - 809.000000000 + 795.000000000 + 857.000000000 - 1284.000000000 - 708.000000000 + 1372.000000000 + 756.000000000 @@ -168,4 +168,49 @@ 1 + + + + 341.000000000 + 222.000000000 + + + 341.000000000 + 990.000000000 + + + 1365.000000000 + 990.000000000 + + + 1365.000000000 + 222.000000000 + + + + + 0.016666668 + 0.006666641 + + + 0.536666691 + 0.006666641 + + + 0.536666691 + 0.526666641 + + + 0.016666668 + 0.526666641 + + + + image + image2.jpg + + + 1 + + diff --git a/src/Surfaces/CircleSurface.cpp b/src/Surfaces/CircleSurface.cpp index 4d7f9ec..2f73354 100644 --- a/src/Surfaces/CircleSurface.cpp +++ b/src/Surfaces/CircleSurface.cpp @@ -26,83 +26,84 @@ void CircleSurface::setup() { QuadSurface::setup(); setPerspectiveWarping(true); + lastSourceTextureId = UINT_MAX; updateMask = true; // maskIsReady = false; - glESVertexShader = CIRCLE_SURFACE_STRINGIFY( - attribute vec4 position; - attribute vec4 color; - attribute vec4 normal; - attribute vec2 texcoord; - - uniform mat4 modelViewMatrix; - uniform mat4 projectionMatrix; - uniform sampler2D maskTex; - - varying vec4 colorVarying; - varying vec2 texCoordVarying; - - void main() { - - //get our current vertex position so we can modify it - vec4 pos = projectionMatrix*modelViewMatrix*position; - - gl_Position = pos; - colorVarying = color; - texCoordVarying = texcoord; - } - ); - - glESFragmentShader = CIRCLE_SURFACE_STRINGIFY( -//#ifdef GL_ES -// define default precision for float, vec, mat. - precision highp float; +// glESVertexShader = CIRCLE_SURFACE_STRINGIFY( +// attribute vec4 position; +// attribute vec4 color; +// attribute vec4 normal; +// attribute vec2 texcoord; +// +// uniform mat4 modelViewMatrix; +// uniform mat4 projectionMatrix; +// uniform sampler2D maskTex; +// +// varying vec4 colorVarying; +// varying vec2 texCoordVarying; +// +// void main() { +// +// //get our current vertex position so we can modify it +// vec4 pos = projectionMatrix*modelViewMatrix*position; +// +// gl_Position = pos; +// colorVarying = color; +// texCoordVarying = texcoord; +// } +// ); +// +// glESFragmentShader = CIRCLE_SURFACE_STRINGIFY( +////#ifdef GL_ES +//// define default precision for float, vec, mat. +// precision highp float; +////#endif +// +// uniform sampler2D tex0; +// uniform sampler2D maskTex; +// uniform vec4 globalColor; +// +// varying vec2 texCoordVarying; +// +// void main (void) +// { +// vec2 pos = texCoordVarying; +// vec3 src = texture2D(tex0, pos).rgb; +// float mask = texture2D(maskTex, pos).r; +// gl_FragColor = vec4( src , mask); +// } +// ); +// +// gl2FragmentShader = "#version 120\n #extension GL_ARB_texture_rectangle : enable\n"; +// gl2FragmentShader += CIRCLE_SURFACE_STRINGIFY( +// uniform sampler2DRect tex0; +// uniform sampler2DRect maskTex; +// +// void main (void) { +// vec2 pos = gl_TexCoord[0].st; +// +// vec3 src = texture2DRect(tex0, pos).rgb; +// float mask = texture2DRect(maskTex, pos).r; +// +// gl_FragColor = vec4(src, mask); +// } +// ); +// +//#ifdef TARGET_OPENGLES +// maskShader.setupShaderFromSource(GL_VERTEX_SHADER, glESVertexShader); +// maskShader.setupShaderFromSource(GL_FRAGMENT_SHADER, glESFragmentShader); +// maskShader.bindDefaults(); +// maskShader.linkProgram(); +//#else +// if (ofIsGLProgrammableRenderer()) { +// +// } else { +// maskShader.setupShaderFromSource(GL_FRAGMENT_SHADER, gl2FragmentShader); +// maskShader.linkProgram(); +// } //#endif - uniform sampler2D tex0; - uniform sampler2D maskTex; - uniform vec4 globalColor; - - varying vec2 texCoordVarying; - - void main (void) - { - vec2 pos = texCoordVarying; - vec3 src = texture2D(tex0, pos).rgb; - float mask = texture2D(maskTex, pos).r; - gl_FragColor = vec4( src , mask); - } - ); - - gl2FragmentShader = "#version 120\n #extension GL_ARB_texture_rectangle : enable\n"; - gl2FragmentShader += CIRCLE_SURFACE_STRINGIFY( - uniform sampler2DRect tex0; - uniform sampler2DRect maskTex; - - void main (void) { - vec2 pos = gl_TexCoord[0].st; - - vec3 src = texture2DRect(tex0, pos).rgb; - float mask = texture2DRect(maskTex, pos).r; - - gl_FragColor = vec4(src, mask); - } - ); - -#ifdef TARGET_OPENGLES - maskShader.setupShaderFromSource(GL_VERTEX_SHADER, glESVertexShader); - maskShader.setupShaderFromSource(GL_FRAGMENT_SHADER, glESFragmentShader); - maskShader.bindDefaults(); - maskShader.linkProgram(); -#else - if (ofIsGLProgrammableRenderer()) { - - } else { - maskShader.setupShaderFromSource(GL_FRAGMENT_SHADER, gl2FragmentShader); - maskShader.linkProgram(); - } -#endif - ofVec2f t1 = ofVec2f(ofVec2f(0.0f, 0.0f)); ofVec2f t2 = ofVec2f(ofVec2f(1.0f, 0.0f)); ofVec2f t3 = ofVec2f(ofVec2f(1.0f, 1.0f)); @@ -128,6 +129,7 @@ void CircleSurface::draw() { if (source != currentSource) { // Pointer comparison // Create the mask here setupTextures(); + lastSourceTextureId = UINT_MAX; currentSource = source; } @@ -144,53 +146,61 @@ void CircleSurface::draw() { auto sourceTex = ofTexture(*(source->getTexture())); auto sourceTexId = sourceTex.getTextureData().textureID; - // Get the quad surface's mesh texture coordinates: - auto quadTexCoords = getMesh().getTexCoords(); + // Draw the mask only if the sources are FBO's, videos, + // or if the last texture id was UINT_MAX (which means that + // the mask has not yet been draw). +// if (source->getType() == SOURCE_TYPE_FBO || +// source->getType() == SOURCE_TYPE_VIDEO || +// lastSourceTextureId == UINT_MAX) { + if (true) { + lastSourceTextureId = sourceTexId; + drawMaskForSource(sourceTex); + } - maskMesh.clearTexCoords(); + // Swap the texture id of the source with the one of our + // newly drawn outputFbo: + source->getTexture()->getTextureData().textureID = outputFbo.getTexture().getTextureData().textureID; + auto texCoords = getMesh().getTexCoords(); + getMesh().clearTexCoords(); + getMesh().addTexCoords(defaultTexCoords); + // Draw the Quad: + QuadSurface::draw(); - // Set the mesh's texture coords to the quads. - // This gets us the coordinates set in the TextureEditor - maskMesh.addTexCoords(quadTexCoords); + // Reset the texture id of the source + source->getTexture()->getTextureData().textureID = lastSourceTextureId; - float w = outputFbo.getWidth(); - float h = outputFbo.getHeight(); + // Reset the texture coords of the QuadSurface mesh: + getMesh().clearTexCoords(); + getMesh().addTexCoords(texCoords); - // Draw the scaled texture into an FBO - scaledSourceFbo.begin(true); - { - ofClear(0, 0, 0, 255); - ofSetupScreenOrtho(w, h, -1, 1); - ofEnableNormalizedTexCoords(); - ofFill(); - ofSetColor(255); - sourceTex.bind(); - maskMesh.draw(); - sourceTex.unbind(); - } - scaledSourceFbo.end(); + if (!isNorm) ofDisableNormalizedTexCoords(); +} - // Now draw the texture, masked, into the outputFbo. - // This enables us to use the TextureEditor to determine - // what part of the source will be masked: - maskMesh.clearTexCoords(); +void CircleSurface::setFeathering(float f) { + feathering = f; + updateMask = true; +} + + +void CircleSurface::drawMaskForSource(ofTexture &sourceTex) { + auto quadTexCoords = getMesh().getTexCoords(); - // Set default normalized texCoords - maskMesh.addTexCoords(defaultTexCoords); + maskMesh.clearTexCoords(); + // Set the mesh's texture coords to the quads. + // This gets us the coordinates set in the TextureEditor + maskMesh.addTexCoords(quadTexCoords); outputFbo.begin(true); { ofClear(0, 0, 0, 0); - ofSetupScreenOrtho(w, h, -1, 1); ofEnableNormalizedTexCoords(); ofSetColor(255); ofFill(); ofSetRectMode(OF_RECTMODE_CORNER); -#ifdef TARGET_RASPBERRY_PI - scaledSourceFbo.getTexture().bind(); +//#ifdef TARGET_RASPBERRY_PI + sourceTex.bind(); maskMesh.draw(); - scaledSourceFbo.getTexture().unbind(); - + sourceTex.unbind(); // Masking without shaders... ofPushStyle(); ofEnableBlendMode(OF_BLENDMODE_MULTIPLY); @@ -199,44 +209,21 @@ void CircleSurface::draw() { ofDisableNormalizedTexCoords(); maskFbo.draw(0, 0); ofPopStyle(); -#else - maskShader.begin(); - maskShader.setUniformTexture("maskTex", maskFbo.getTexture(), 1); - ofSetColor(255); - ofFill(); - ofSetRectMode(OF_RECTMODE_CORNER); - scaledSourceFbo.getTexture().bind(); - maskMesh.draw(); - scaledSourceFbo.getTexture().unbind(); - maskShader.end(); -#endif +//#else +// maskShader.begin(); +// maskShader.setUniformTexture("maskTex", maskFbo.getTexture(), 1); +// ofSetColor(255); +// ofFill(); +// ofSetRectMode(OF_RECTMODE_CORNER); +// scaledSourceFbo.getTexture().bind(); +// maskMesh.draw(); +// scaledSourceFbo.getTexture().unbind(); +// maskShader.end(); +//#endif } outputFbo.end(); - getMesh().clearTexCoords(); - getMesh().addTexCoords(defaultTexCoords); - - // Swap the texture id of the source with the one of our - // newly drawn outputFbo: - source->getTexture()->getTextureData().textureID = outputFbo.getTexture().getTextureData().textureID; - - // Draw the Quad: - QuadSurface::draw(); - - // Reset the texture id of the source - source->getTexture()->getTextureData().textureID = sourceTexId; - - // Reset the texture coords of the QuadSurface mesh: - getMesh().clearTexCoords(); - getMesh().addTexCoords(quadTexCoords); - - if (!isNorm) ofDisableNormalizedTexCoords(); -} - -void CircleSurface::setFeathering(float f) { - feathering = f; - updateMask = true; } void CircleSurface::setupTextures() { @@ -268,10 +255,10 @@ void CircleSurface::setupTextures() { ofClear(0, 0, 0, 255); outputFbo.end(); - scaledSourceFbo.allocate(w, h); - scaledSourceFbo.begin(); - ofClear(0, 0, 0, 255); - scaledSourceFbo.end(); +// scaledSourceFbo.allocate(w, h); +// scaledSourceFbo.begin(); +// ofClear(0, 0, 0, 255); +// scaledSourceFbo.end(); // This is lifted from QuadSurface::setup to ensure that the two // meshes are similar: @@ -283,10 +270,10 @@ void CircleSurface::setupTextures() { ofVec2f p4 = ofVec2f(w, 0); // Create 4 point for the texture coordinates - ofVec2f t1 = ofVec2f(ofVec2f(0.0f, 0.0f)); - ofVec2f t2 = ofVec2f(ofVec2f(1.0f, 0.0f)); - ofVec2f t3 = ofVec2f(ofVec2f(1.0f, 1.0f)); - ofVec2f t4 = ofVec2f(ofVec2f(0.0f, 1.0f)); + ofVec2f t1 = ofVec2f(ofVec2f(0.0f, 1.0f)); + ofVec2f t2 = ofVec2f(ofVec2f(1.0f, 1.0f)); + ofVec2f t3 = ofVec2f(ofVec2f(1.0f, 0.0f)); + ofVec2f t4 = ofVec2f(ofVec2f(0.0f, 0.0f)); // Clear maskMesh maskMesh.clear(); diff --git a/src/Surfaces/CircleSurface.h b/src/Surfaces/CircleSurface.h index 6a21020..ddbfe33 100644 --- a/src/Surfaces/CircleSurface.h +++ b/src/Surfaces/CircleSurface.h @@ -27,20 +27,20 @@ class CircleSurface : public QuadSurface { protected: void setupTextures(); + void drawMaskForSource(ofTexture &sourceTexture); ofFbo maskFbo; - ofFbo scaledSourceFbo; +// ofFbo scaledSourceFbo; ofFbo outputFbo; - ofShader maskShader; - ofShader gradientShader; +// ofShader maskShader; float feathering = 0.0f; bool updateMask; bool maskIsReady; - string glESFragmentShader; - string glESVertexShader; - - string gl2FragmentShader; - string gl2VertexShader; +// string glESFragmentShader; +// string glESVertexShader; +// +// string gl2FragmentShader; +// string gl2VertexShader; ofMesh maskMesh; @@ -54,6 +54,7 @@ class CircleSurface : public QuadSurface { // This is a total kludge, but it keeps me from messing with the // upstream source. BaseSource* currentSource = 0; + unsigned int lastSourceTextureId; }; }