From 7e955b712865a97bd98bd023ced64b7afb325515 Mon Sep 17 00:00:00 2001 From: Krisjanis Rijnieks Date: Wed, 27 Jan 2016 17:36:29 +0100 Subject: [PATCH] Add `GridWarkSurface` initial source files --- src/Surfaces/GridWarpSurface.cpp | 126 +++++++++++++++++++++++++++++++ src/Surfaces/GridWarpSurface.h | 33 ++++++++ 2 files changed, 159 insertions(+) create mode 100644 src/Surfaces/GridWarpSurface.cpp create mode 100644 src/Surfaces/GridWarpSurface.h diff --git a/src/Surfaces/GridWarpSurface.cpp b/src/Surfaces/GridWarpSurface.cpp new file mode 100644 index 0000000..a95b237 --- /dev/null +++ b/src/Surfaces/GridWarpSurface.cpp @@ -0,0 +1,126 @@ +#include "GridWarpSurface.h" + +namespace ofx { +namespace piMapper { + +GridWarpSurface::GridWarpSurface(){ + _gridCols = 2; + _gridRows = 3; + createGridMesh(); +} + +void GridWarpSurface::draw(){ + if(source->getTexture() == 0){ + return; + } + + if(!source->getTexture()->isAllocated()){ + return; + } + + source->getTexture()->bind(); + mesh.draw(); + source->getTexture()->unbind(); +} + +void GridWarpSurface::moveBy(ofVec2f v){ + vector & vertices = getVertices(); + for(int i = 0; i < vertices.size(); i++){ + vertices[i] += v; + } + setMoved(true); +} + +int GridWarpSurface::getType(){ + return SurfaceType::GRID_WARP_SURFACE; +} + +bool GridWarpSurface::hitTest(ofVec2f p){ + ofPolyline line = getHitArea(); + + if(line.inside(p.x, p.y)){ + return true; + }else{ + return false; + } +} + +ofPolyline GridWarpSurface::getHitArea(){ + ofPolyline line; + line.addVertex(ofPoint(mesh.getVertex(0).x, mesh.getVertex(0).y)); + line.addVertex(ofPoint(mesh.getVertex(1).x, mesh.getVertex(1).y)); + line.addVertex(ofPoint(mesh.getVertex(2).x, mesh.getVertex(2).y)); + line.addVertex(ofPoint(mesh.getVertex(3).x, mesh.getVertex(3).y)); + line.close(); + + return line; +} + +ofPolyline GridWarpSurface::getTextureHitArea(){ + ofPolyline line; + vector & texCoords = mesh.getTexCoords(); + ofVec2f textureSize = ofVec2f(source->getTexture()->getWidth(), source->getTexture()->getHeight()); + for(int i = 0; i < texCoords.size(); i++){ + line.addVertex(ofPoint(texCoords[i] * textureSize)); + } + line.close(); + + return line; +} + +void GridWarpSurface::createGridMesh(){ + // Assign texture + source = getDefaultSource(); + + // Clear mesh + mesh.clear(); + + float margin = 100.0f; + float surfaceWidth = (float)ofGetWidth() - margin * 2.0f; + float surfaceHeight = (float)ofGetHeight() - margin * 2.0f; + float vertexDistanceX = surfaceWidth / (float)_gridCols; + 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){ + mesh.addVertex( + ofVec2f( + margin + vertexDistanceX * (float)ix, + margin + vertexDistanceY * (float)iy)); + } + } + + int vertsPerCol = _gridCols + 1; + int vertsPerRow = _gridRows + 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); + } + } + + // Add texture coordinates for each of the vertices + for(int ix = 0; ix <= _gridCols; ++ix){ + for(int iy = 0; iy <= _gridRows; ++iy){ + 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)); + } + } +} + + + +} // namespace piMapper +} // namespace ofx \ No newline at end of file diff --git a/src/Surfaces/GridWarpSurface.h b/src/Surfaces/GridWarpSurface.h new file mode 100644 index 0000000..a584ca1 --- /dev/null +++ b/src/Surfaces/GridWarpSurface.h @@ -0,0 +1,33 @@ +#pragma once + +#include "ofMain.h" +#include "BaseSurface.h" +#include "SurfaceType.h" +#include "HomographyHelper.h" + +namespace ofx { +namespace piMapper { + +class GridWarpSurface : public BaseSurface { + public: + GridWarpSurface(); + + void draw(); + void moveBy(ofVec2f v); + + int getType(); + + bool hitTest(ofVec2f p); + + ofPolyline getHitArea(); + ofPolyline getTextureHitArea(); + + private: + int _gridCols; + int _gridRows; + + void createGridMesh(); +}; + +} // namespace piMapper +} // namespace ofx \ No newline at end of file