diff --git a/src/Application/ProjectionMappingState.cpp b/src/Application/ProjectionMappingState.cpp index 907036d..5917413 100644 --- a/src/Application/ProjectionMappingState.cpp +++ b/src/Application/ProjectionMappingState.cpp @@ -34,8 +34,19 @@ void ProjectionMappingState::onKeyPressed(Application * app, ofKeyEventArgs & ar SurfaceType::QUAD_SURFACE) ); break; + + case 'g': + app->getCmdManager()->exec( + new AddSurfaceCmd( + app->getSurfaceManager(), + SurfaceType::GRID_WARP_SURFACE) + ); + break; case OF_KEY_BACKSPACE: + if(app->getSurfaceManager()->getSelectedSurface() == 0){ + break; + } app->getCmdManager()->exec( new RmSurfaceCmd(app->getSurfaceManager())); break; diff --git a/src/Surfaces/GridWarpSurface.cpp b/src/Surfaces/GridWarpSurface.cpp index a95b237..174d159 100644 --- a/src/Surfaces/GridWarpSurface.cpp +++ b/src/Surfaces/GridWarpSurface.cpp @@ -21,14 +21,20 @@ void GridWarpSurface::draw(){ source->getTexture()->bind(); mesh.draw(); source->getTexture()->unbind(); + + // debug vertices + mesh.drawWireframe(); + //mesh.drawVertices(); } void GridWarpSurface::moveBy(ofVec2f v){ + /* vector & vertices = getVertices(); for(int i = 0; i < vertices.size(); i++){ vertices[i] += v; } setMoved(true); + */ } int GridWarpSurface::getType(){ @@ -36,6 +42,8 @@ int GridWarpSurface::getType(){ } bool GridWarpSurface::hitTest(ofVec2f p){ + return false; + /* ofPolyline line = getHitArea(); if(line.inside(p.x, p.y)){ @@ -43,9 +51,13 @@ bool GridWarpSurface::hitTest(ofVec2f p){ }else{ return false; } + */ } ofPolyline GridWarpSurface::getHitArea(){ + ofPolyline line; + return line; + /* ofPolyline line; line.addVertex(ofPoint(mesh.getVertex(0).x, mesh.getVertex(0).y)); line.addVertex(ofPoint(mesh.getVertex(1).x, mesh.getVertex(1).y)); @@ -54,6 +66,7 @@ ofPolyline GridWarpSurface::getHitArea(){ line.close(); return line; + */ } ofPolyline GridWarpSurface::getTextureHitArea(){ @@ -68,11 +81,29 @@ ofPolyline GridWarpSurface::getTextureHitArea(){ return line; } -void GridWarpSurface::createGridMesh(){ - // Assign texture - source = getDefaultSource(); +void GridWarpSurface::setVertex(int index, ofVec2f p){ + if(index >= mesh.getVertices().size()){ + ofLog() << "Vertex with this index does not exist: " << index << endl; + return; + } + mesh.setVertex(index, p); +} - // Clear mesh +void GridWarpSurface::setVertices(vector v){ + if(v.size() != mesh.getVertices().size()){ + throw runtime_error("Wrong number of vertices"); + } + + for(int i = 0; i < v.size(); ++i){ + mesh.setVertex(i, v[i]); + } +} + +vector & GridWarpSurface::getVertices(){ + return mesh.getVertices(); +} + +void GridWarpSurface::createGridMesh(){ mesh.clear(); float margin = 100.0f; @@ -82,37 +113,33 @@ void GridWarpSurface::createGridMesh(){ float vertexDistanceY = surfaceHeight / (float)_gridRows; // Add vertices for each col and row - for(int ix = 0; ix <= _gridCols; ++ix){ - for(int iy = 0; iy <= _gridRows; ++iy){ + for(int iy = 0; iy <= _gridRows; ++iy){ + for(int ix = 0; ix <= _gridCols; ++ix){ mesh.addVertex( ofVec2f( - margin + vertexDistanceX * (float)ix, - margin + vertexDistanceY * (float)iy)); + margin + (vertexDistanceX * (float)ix), + margin + (vertexDistanceY * (float)iy) )); } } - int vertsPerCol = _gridCols + 1; - int vertsPerRow = _gridRows + 1; + int vertsPerCol = _gridRows + 1; + int vertsPerRow = _gridCols + 1; // Form triangles for all grid cols and rows - for(int ix = 0; ix < _gridCols; ++ix){ - for(int iy = 0; iy < _gridRows; ++iy){ - - mesh.addTriangle( - (iy * vertsPerRow) + ix, - ((iy + 1) * vertsPerRow) + ix, - ((iy + 1) * vertsPerRow) + (ix + 1)); - - mesh.addTriangle( - (iy * vertsPerRow) + ix, - (iy * vertsPerRow) + (ix + 1), - ((iy + 1) * vertsPerRow) + ix); + for(int iy = 0; iy < _gridRows; ++iy){ + for(int ix = 0; ix < _gridCols; ++ix){ + int a = (iy * vertsPerRow) + ix; + int b = (iy * vertsPerRow) + ix + 1; + int c = ((iy + 1) * vertsPerRow) + ix + 1; + int d = ((iy + 1) * vertsPerRow) + ix; + mesh.addTriangle(a, b, c); + mesh.addTriangle(a, c, d); } } // Add texture coordinates for each of the vertices - for(int ix = 0; ix <= _gridCols; ++ix){ - for(int iy = 0; iy <= _gridRows; ++iy){ + for(int iy = 0; iy <= _gridRows; ++iy){ + for(int ix = 0; ix <= _gridCols; ++ix){ float xc = (ix == 0) ? 0.0f : (float)ix / (float)_gridCols; float yc = (iy == 0) ? 0.0f : (float)iy / (float)_gridRows; mesh.addTexCoord(ofVec2f(xc, yc)); diff --git a/src/Surfaces/GridWarpSurface.h b/src/Surfaces/GridWarpSurface.h index a584ca1..15db21b 100644 --- a/src/Surfaces/GridWarpSurface.h +++ b/src/Surfaces/GridWarpSurface.h @@ -22,6 +22,11 @@ class GridWarpSurface : public BaseSurface { ofPolyline getHitArea(); ofPolyline getTextureHitArea(); + void setVertex(int index, ofVec2f p); + void setVertices(vector v); + vector & getVertices(); + + private: int _gridCols; int _gridRows;