Browse Source

Add Vec2 adapter class, Vec3 still to finish

master
Krisjanis Rijnieks 8 years ago
parent
commit
5e23b21eb2
  1. 4
      src/Application/Application.cpp
  2. 5
      src/Application/Application.h
  3. 26
      src/Application/Modes/ProjectionMappingMode.cpp
  4. 3
      src/Application/Modes/ProjectionMappingMode.h
  5. 19
      src/Application/Modes/TextureMappingMode.cpp
  6. 5
      src/Application/Modes/TextureMappingMode.h
  7. 56
      src/Application/SettingsLoader.cpp
  8. 1
      src/Application/SettingsLoader.h
  9. 4
      src/Commands/AddGridColCmd.cpp
  10. 3
      src/Commands/AddGridColCmd.h
  11. 4
      src/Commands/AddGridRowCmd.cpp
  12. 3
      src/Commands/AddGridRowCmd.h
  13. 2
      src/Commands/DuplicateSurfaceCmd.cpp
  14. 1
      src/Commands/DuplicateSurfaceCmd.h
  15. 2
      src/Commands/MvAllTexCoordsCmd.cpp
  16. 3
      src/Commands/MvAllTexCoordsCmd.h
  17. 2
      src/Commands/MvSelectionCmd.cpp
  18. 5
      src/Commands/MvSelectionCmd.h
  19. 3
      src/Commands/MvSurfaceVertCmd.h
  20. 2
      src/Commands/MvTexCoordCmd.cpp
  21. 7
      src/Commands/MvTexCoordCmd.h
  22. 4
      src/Commands/RmGridColCmd.cpp
  23. 3
      src/Commands/RmGridColCmd.h
  24. 4
      src/Commands/RmGridRowCmd.cpp
  25. 3
      src/Commands/RmGridRowCmd.h
  26. 2
      src/Commands/SaveTexCoordPosCmd.cpp
  27. 5
      src/Commands/SaveTexCoordPosCmd.h
  28. 3
      src/Commands/StartDragSurfaceCmd.cpp
  29. 1
      src/Commands/StartDragSurfaceCmd.h
  30. 19
      src/Gui/Widgets/ProjectionEditorWidget.cpp
  31. 7
      src/Gui/Widgets/ProjectionEditorWidget.h
  32. 54
      src/Gui/Widgets/TextureEditorWidget.cpp
  33. 11
      src/Gui/Widgets/TextureEditorWidget.h
  34. 30
      src/Surfaces/BaseSurface.cpp
  35. 21
      src/Surfaces/BaseSurface.h
  36. 44
      src/Surfaces/CircleSurface.cpp
  37. 4
      src/Surfaces/CircleSurface.h
  38. 54
      src/Surfaces/GridWarpSurface.cpp
  39. 19
      src/Surfaces/GridWarpSurface.h
  40. 92
      src/Surfaces/HexagonSurface.cpp
  41. 25
      src/Surfaces/HexagonSurface.h
  42. 653
      src/Surfaces/QuadSurface.cpp
  43. 111
      src/Surfaces/QuadSurface.h
  44. 36
      src/Surfaces/SurfaceFactory.cpp
  45. 6
      src/Surfaces/SurfaceManager.cpp
  46. 7
      src/Surfaces/SurfaceManager.h
  47. 77
      src/Surfaces/TriangleSurface.cpp
  48. 25
      src/Surfaces/TriangleSurface.h
  49. 95
      src/Types/Vec2.cpp
  50. 49
      src/Types/Vec2.h
  51. 82
      src/Types/Vec3.cpp
  52. 43
      src/Types/Vec3.h
  53. 15
      src/UserInterface/BaseJoint.cpp
  54. 9
      src/UserInterface/BaseJoint.h
  55. 2
      src/UserInterface/CircleJoint.cpp
  56. 5
      src/UserInterface/CircleJoint.h
  57. 2
      src/ofxPiMapper.cpp
  58. 3
      src/ofxPiMapper.h

4
src/Application/Application.cpp

@ -356,7 +356,7 @@ void Application::selectPrevTexCoord(){
}
}
void Application::moveSelection(ofVec2f by){
void Application::moveSelection(Vec2 by){
if(_state == ProjectionMappingMode::instance()){
getCmdManager()->exec(new MvSelectionCmd(getSurfaceManager(), by));
}else if(_state == TextureMappingMode::instance()){
@ -541,7 +541,7 @@ void Application::togglePause(){
}
}
void Application::moveTexCoord(int texCoordIndex, ofVec2f by){
void Application::moveTexCoord(int texCoordIndex, Vec2 by){
if(texCoordIndex >= 0){
getCmdManager()->exec(new MvTexCoordCmd(texCoordIndex, by));
}else{

5
src/Application/Application.h

@ -9,6 +9,7 @@
#include "Info.h"
#include "SurfaceStack.h"
#include "Gui.h"
#include "Vec2.h"
// Commands
#include "SetApplicationModeCmd.h"
@ -113,7 +114,7 @@ class Application {
Moves vertex when in projection mapping mode.
Moves texture coordinate when in texture mapping mode.
*/
void moveSelection(ofVec2f by);
void moveSelection(Vec2 by);
void setPresentationMode();
void setTextureMode();
@ -131,7 +132,7 @@ class Application {
void removeGridRow();
void removeGridColumn();
void togglePause();
void moveTexCoord(int texCoordIndex, ofVec2f by);
void moveTexCoord(int texCoordIndex, Vec2 by);
// TODO: Add moveVertex.
// Make it so that other parts of the application react to the change.
void undo();

26
src/Application/Modes/ProjectionMappingMode.cpp

@ -114,33 +114,33 @@ void ProjectionMappingMode::onKeyPressed(Application * app, ofKeyEventArgs & arg
case OF_KEY_UP:
if(app->isShiftKeyDown()){
app->moveSelection(ofVec2f(0.0f, -10.0f));
app->moveSelection(Vec2(0.0f, -10.0f));
}else{
app->moveSelection(ofVec2f(0.0f, -1.0f));
app->moveSelection(Vec2(0.0f, -1.0f));
}
break;
case OF_KEY_DOWN:
if(app->isShiftKeyDown()){
app->moveSelection(ofVec2f(0.0f, 10.0f));
app->moveSelection(Vec2(0.0f, 10.0f));
}else{
app->moveSelection(ofVec2f(0.0f, 1.0f));
app->moveSelection(Vec2(0.0f, 1.0f));
}
break;
case OF_KEY_LEFT:
if(app->isShiftKeyDown()){
app->moveSelection(ofVec2f(-10.0f, 0.0f));
app->moveSelection(Vec2(-10.0f, 0.0f));
}else{
app->moveSelection(ofVec2f(-1.0f, 0.0f));
app->moveSelection(Vec2(-1.0f, 0.0f));
}
break;
case OF_KEY_RIGHT:
if(app->isShiftKeyDown()){
app->moveSelection(ofVec2f(10.0f, 0.0f));
app->moveSelection(Vec2(10.0f, 0.0f));
}else{
app->moveSelection(ofVec2f(1.0f, 0.0f));
app->moveSelection(Vec2(1.0f, 0.0f));
}
break;
@ -194,7 +194,7 @@ void ProjectionMappingMode::onMousePressed(Application * app, ofMouseEventArgs &
int hitJointIndex = -1;
BaseSurface * hitSurface = 0;
hitJoint = Gui::instance()->getProjectionEditorWidget().hitTestJoints(ofVec2f(args.x, args.y));
hitJoint = Gui::instance()->getProjectionEditorWidget().hitTestJoints(Vec2(args.x, args.y));
if(hitJoint){
for(int i = Gui::instance()->getProjectionEditorWidget().getJoints()->size() - 1; i >= 0 ; --i){
@ -205,7 +205,7 @@ void ProjectionMappingMode::onMousePressed(Application * app, ofMouseEventArgs &
}
}else{
for(int i = app->getSurfaceManager()->size() - 1; i >= 0; --i){
if(app->getSurfaceManager()->getSurface(i)->hitTest(ofVec2f(args.x, args.y))){
if(app->getSurfaceManager()->getSurface(i)->hitTest(Vec2(args.x, args.y))){
hitSurface = app->getSurfaceManager()->getSurface(i);
break;
}
@ -219,7 +219,7 @@ void ProjectionMappingMode::onMousePressed(Application * app, ofMouseEventArgs &
hitJoint->startDrag();
Gui::instance()->notifyJointPressed(args, hitJointIndex);
}else if(hitSurface){
_clickPosition = ofVec2f(args.x, args.y); // TODO: redesign this so we can use a kind of
_clickPosition = Vec2(args.x, args.y); // TODO: redesign this so we can use a kind of
// display stack.
_bSurfaceDrag = true; // TODO: Should be something like `hitSurface->startDrag()`
Gui::instance()->notifySurfacePressed(args, hitSurface);
@ -240,8 +240,8 @@ void ProjectionMappingMode::onMouseDragged(Application * app, ofMouseEventArgs &
// TODO: Handle app->getGui()->clickPosition and app->getGui()->bDrag locally.
if(_bSurfaceDrag){
ofVec2f mousePosition = ofVec2f(args.x, args.y);
ofVec2f distance = mousePosition - _clickPosition;
Vec2 mousePosition = Vec2(args.x, args.y);
Vec2 distance = mousePosition - _clickPosition;
Gui::instance()->getProjectionEditorWidget().moveSelectedSurface(distance);
_clickPosition = mousePosition;
}

3
src/Application/Modes/ProjectionMappingMode.h

@ -9,6 +9,7 @@
#include "SurfaceType.h"
#include "Gui.h"
#include "ScaleWidget.h"
#include "Vec2.h"
namespace ofx {
namespace piMapper {
@ -38,7 +39,7 @@ class ProjectionMappingMode : public ApplicationBaseMode {
float _surfaceScaleBeforeTransform;
ofVec2f _clickPosition;
Vec2 _clickPosition;
bool _bSurfaceDrag;
bool _bDrawLayerPanel;

19
src/Application/Modes/TextureMappingMode.cpp

@ -85,19 +85,19 @@ void TextureMappingMode::onKeyPressed(Application * app, ofKeyEventArgs & args){
switch(args.key){
case OF_KEY_LEFT:
moveSelectedTexCoord(app, ofVec2f(-moveStep, 0.0f));
moveSelectedTexCoord(app, Vec2(-moveStep, 0.0f));
break;
case OF_KEY_RIGHT:
moveSelectedTexCoord(app, ofVec2f(moveStep, 0.0f));
moveSelectedTexCoord(app, Vec2(moveStep, 0.0f));
break;
case OF_KEY_UP:
moveSelectedTexCoord(app, ofVec2f(0.0f, -moveStep));
moveSelectedTexCoord(app, Vec2(0.0f, -moveStep));
break;
case OF_KEY_DOWN:
moveSelectedTexCoord(app, ofVec2f(0.0f, moveStep));
moveSelectedTexCoord(app, Vec2(0.0f, moveStep));
break;
case '>':
@ -159,7 +159,7 @@ void TextureMappingMode::onMousePressed(Application * app, ofMouseEventArgs & ar
CircleJoint * hitJoint =
Gui::instance()->getTextureEditorWidget().hitTestJoints(
ofVec2f(args.x, args.y));
Vec2(args.x, args.y));
if(hitJoint != 0){
hitJoint->mousePressed(args);
@ -220,7 +220,7 @@ void TextureMappingMode::onMouseReleased(Application * app, ofMouseEventArgs & a
// create an undoable move tex coord command.
int selectedTexCoord = Gui::instance()->getTextureEditorWidget().getSelectedTexCoord();
if(selectedTexCoord >= 0){
ofVec2f texCoordCurrent =
Vec2 texCoordCurrent =
app->getSurfaceManager()->getSelectedSurface()->getTexCoords()[selectedTexCoord];
if(texCoordCurrent != _texCoordOnClick){
@ -247,7 +247,8 @@ void TextureMappingMode::onMouseDragged(Application * app, ofMouseEventArgs & ar
if(_bCropAreaDrag){
ofPoint mousePosition = ofPoint(args.x, args.y);
ofPoint distance = mousePosition - _clickPosition;
Gui::instance()->getTextureEditorWidget().moveTexCoords(distance);
Vec2 d = Vec2(distance.x, distance.y);
Gui::instance()->getTextureEditorWidget().moveTexCoords(d);
_clickPosition = mousePosition;
}
}else{
@ -263,7 +264,7 @@ void TextureMappingMode::drawTexture(Application * app){
ofEnableNormalizedTexCoords();
ofSetColor(255, 255, 255, 255);
app->getSurfaceManager()->getSelectedSurface()->drawTexture(ofVec2f(0, 0));
app->getSurfaceManager()->getSelectedSurface()->drawTexture(Vec2(0, 0));
if(!normalizedTexCoords){
ofDisableNormalizedTexCoords();
@ -271,7 +272,7 @@ void TextureMappingMode::drawTexture(Application * app){
}
}
void TextureMappingMode::moveSelectedTexCoord(Application * app, ofVec2f by){
void TextureMappingMode::moveSelectedTexCoord(Application * app, Vec2 by){
if(app->getSurfaceManager()->getSelectedSurface() != 0){
int selectedTexCoord = Gui::instance()->getTextureEditorWidget().getSelectedTexCoord();
app->moveTexCoord(selectedTexCoord, by);

5
src/Application/Modes/TextureMappingMode.h

@ -13,6 +13,7 @@
#include "SaveTexCoordPosCmd.h"
#include "SelTexCoordCmd.h"
#include "Gui.h"
#include "Vec2.h"
namespace ofx {
namespace piMapper {
@ -34,7 +35,7 @@ class TextureMappingMode : public ApplicationBaseMode {
void onGuiEvent(Application * app, GuiEvent & e){}
void drawTexture(Application * app);
void moveSelectedTexCoord(Application * app, ofVec2f by);
void moveSelectedTexCoord(Application * app, Vec2 by);
void selectNextVertex(Application * app);
void selectPrevVertex(Application * app);
@ -62,7 +63,7 @@ class TextureMappingMode : public ApplicationBaseMode {
ofPoint _prevCanvasTranslate;
ofPoint _clickCanvasTranslate;
ofVec2f _texCoordOnClick;
Vec2 _texCoordOnClick;
};

56
src/Application/SettingsLoader.cpp

@ -194,11 +194,11 @@ bool SettingsLoader::save(SurfaceManager & surfaceManager, string fileName){
xmlSettings->addTag("texCoords");
xmlSettings->pushTag("texCoords");
vector <ofVec2f> * texCoords = &surface->getTexCoords();
vector <Vec2> * texCoords = &surface->getTexCoords();
for(int j = 0; j < texCoords->size(); j++){
xmlSettings->addTag("texCoord");
xmlSettings->pushTag("texCoord", j);
ofVec2f * texCoord = &(*texCoords)[j];
Vec2 * texCoord = &(*texCoords)[j];
xmlSettings->addValue("x", texCoord->x);
xmlSettings->addValue("y", texCoord->y);
xmlSettings->popTag(); // texCoord
@ -252,28 +252,28 @@ bool SettingsLoader::create(string fileName){
}
BaseSurface * SettingsLoader::getTriangleSurface(ofxXmlSettings * xmlSettings){
vector <ofVec2f> vertices;
vector <Vec2> 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),
vertices.push_back(Vec2(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),
vertices.push_back(Vec2(xmlSettings->getValue("x", 100.0f),
xmlSettings->getValue("y", 0.0f)));
xmlSettings->popTag();
}
if(xmlSettings->tagExists("vertex", 2)){
xmlSettings->pushTag("vertex", 2);
vertices.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f),
vertices.push_back(Vec2(xmlSettings->getValue("x", 0.0f),
xmlSettings->getValue("y", 100.0f)));
xmlSettings->popTag();
}
@ -281,28 +281,28 @@ BaseSurface * SettingsLoader::getTriangleSurface(ofxXmlSettings * xmlSettings){
xmlSettings->popTag(); // vertices
}
vector <ofVec2f> texCoords;
vector <Vec2> texCoords;
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),
texCoords.push_back(Vec2(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),
texCoords.push_back(Vec2(xmlSettings->getValue("x", 1.0f),
xmlSettings->getValue("y", 0.0f)));
xmlSettings->popTag();
}
if(xmlSettings->tagExists("texCoord", 2)){
xmlSettings->pushTag("texCoord", 2);
texCoords.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f),
texCoords.push_back(Vec2(xmlSettings->getValue("x", 0.0f),
xmlSettings->getValue("y", 1.0f)));
xmlSettings->popTag();
}
@ -321,35 +321,35 @@ BaseSurface * SettingsLoader::getTriangleSurface(ofxXmlSettings * xmlSettings){
}
BaseSurface * SettingsLoader::getQuadSurface(ofxXmlSettings * xmlSettings){
vector <ofVec2f> vertices;
vector <Vec2> 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),
vertices.push_back(Vec2(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),
vertices.push_back(Vec2(xmlSettings->getValue("x", 100.0f),
xmlSettings->getValue("y", 0.0f)));
xmlSettings->popTag();
}
if(xmlSettings->tagExists("vertex", 2)){
xmlSettings->pushTag("vertex", 2);
vertices.push_back(ofVec2f(xmlSettings->getValue("x", 100.0f),
vertices.push_back(Vec2(xmlSettings->getValue("x", 100.0f),
xmlSettings->getValue("y", 100.0f)));
xmlSettings->popTag();
}
if(xmlSettings->tagExists("vertex", 3)){
xmlSettings->pushTag("vertex", 3);
vertices.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f),
vertices.push_back(Vec2(xmlSettings->getValue("x", 0.0f),
xmlSettings->getValue("y", 100.0f)));
xmlSettings->popTag();
}
@ -357,35 +357,35 @@ BaseSurface * SettingsLoader::getQuadSurface(ofxXmlSettings * xmlSettings){
xmlSettings->popTag(); // vertices
}
vector <ofVec2f> texCoords;
vector <Vec2> texCoords;
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),
texCoords.push_back(Vec2(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),
texCoords.push_back(Vec2(xmlSettings->getValue("x", 1.0f),
xmlSettings->getValue("y", 0.0f)));
xmlSettings->popTag();
}
if(xmlSettings->tagExists("texCoord", 2)){
xmlSettings->pushTag("texCoord", 2);
texCoords.push_back(ofVec2f(xmlSettings->getValue("x", 1.0f),
texCoords.push_back(Vec2(xmlSettings->getValue("x", 1.0f),
xmlSettings->getValue("y", 1.0f)));
xmlSettings->popTag();
}
if(xmlSettings->tagExists("texCoord", 3)){
xmlSettings->pushTag("texCoord", 3);
texCoords.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f),
texCoords.push_back(Vec2(xmlSettings->getValue("x", 0.0f),
xmlSettings->getValue("y", 1.0f)));
xmlSettings->popTag();
}
@ -415,7 +415,7 @@ BaseSurface * SettingsLoader::getQuadSurface(ofxXmlSettings * xmlSettings){
}
BaseSurface * SettingsLoader::getGridWarpSurface(ofxXmlSettings * xmlSettings){
vector <ofVec2f> vertices;
vector <Vec2> vertices;
if(xmlSettings->tagExists("vertices")){
xmlSettings->pushTag("vertices");
@ -424,7 +424,7 @@ BaseSurface * SettingsLoader::getGridWarpSurface(ofxXmlSettings * xmlSettings){
while(xmlSettings->tagExists("vertex", iv)){
xmlSettings->pushTag("vertex", iv);
vertices.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f),
vertices.push_back(Vec2(xmlSettings->getValue("x", 0.0f),
xmlSettings->getValue("y", 0.0f)));
xmlSettings->popTag();
++iv;
@ -433,7 +433,7 @@ BaseSurface * SettingsLoader::getGridWarpSurface(ofxXmlSettings * xmlSettings){
xmlSettings->popTag(); // vertices
}
vector <ofVec2f> texCoords;
vector <Vec2> texCoords;
if(xmlSettings->tagExists("texCoords")){
xmlSettings->pushTag("texCoords");
@ -442,7 +442,7 @@ BaseSurface * SettingsLoader::getGridWarpSurface(ofxXmlSettings * xmlSettings){
while(xmlSettings->tagExists("texCoord", it)){
xmlSettings->pushTag("texCoord", it);
texCoords.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f),
texCoords.push_back(Vec2(xmlSettings->getValue("x", 0.0f),
xmlSettings->getValue("y", 0.0f)));
xmlSettings->popTag();
++it;
@ -476,7 +476,7 @@ BaseSurface * SettingsLoader::getGridWarpSurface(ofxXmlSettings * xmlSettings){
}
BaseSurface * SettingsLoader::getHexagonSurface(ofxXmlSettings * xmlSettings){
vector <ofVec2f> vertices;
vector <Vec2> vertices;
if(xmlSettings->tagExists("vertices")){
xmlSettings->pushTag("vertices");
@ -484,7 +484,7 @@ BaseSurface * SettingsLoader::getHexagonSurface(ofxXmlSettings * xmlSettings){
unsigned int v = 0;
while(xmlSettings->tagExists("vertex", v)){
xmlSettings->pushTag("vertex", v);
vertices.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f),
vertices.push_back(Vec2(xmlSettings->getValue("x", 0.0f),
xmlSettings->getValue("y", 0.0f)));
xmlSettings->popTag(); // vertex
v += 1;
@ -493,7 +493,7 @@ BaseSurface * SettingsLoader::getHexagonSurface(ofxXmlSettings * xmlSettings){
xmlSettings->popTag(); // vertices
}
vector <ofVec2f> texCoords;
vector <Vec2> texCoords;
if(xmlSettings->tagExists("texCoords")){
xmlSettings->pushTag("texCoords");
@ -501,7 +501,7 @@ BaseSurface * SettingsLoader::getHexagonSurface(ofxXmlSettings * xmlSettings){
unsigned int t = 0;
while(xmlSettings->tagExists("texCoord", t)){
xmlSettings->pushTag("texCoord", t);
texCoords.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f),
texCoords.push_back(Vec2(xmlSettings->getValue("x", 0.0f),
xmlSettings->getValue("y", 0.0f)));
xmlSettings->popTag(); // texCoord
t += 1;

1
src/Application/SettingsLoader.h

@ -8,6 +8,7 @@
#include "SurfaceFactory.h"
#include "SurfaceType.h"
#include "SourceTypeHelper.h"
#include "Vec2.h"
namespace ofx {
namespace piMapper {

4
src/Commands/AddGridColCmd.cpp

@ -18,9 +18,9 @@ void AddGridColCmd::exec(){
void AddGridColCmd::undo(){
ofLogNotice("AddGridColCmd", "undo");
_surface->setGridCols(_surface->getGridCols() - 1);
vector <ofVec2f> v;
vector <Vec2> v;
for(int i = 0; i < _vertices.size(); ++i){
v.push_back( ofVec2f(_vertices[i].x, _vertices[i].y) );
v.push_back( Vec2(_vertices[i].x, _vertices[i].y) );
}
_surface->setVertices(v);
_surface->setTexCoords(_texCoords);

3
src/Commands/AddGridColCmd.h

@ -4,6 +4,7 @@
#include "BaseCmd.h"
#include "GridWarpSurface.h"
#include "ProjectionEditorWidget.h"
#include "Vec2.h"
class ofxPiMapper;
@ -19,7 +20,7 @@ class AddGridColCmd : public BaseUndoCmd {
private:
vector <ofVec3f> _vertices;
vector <ofVec2f> _texCoords;
vector <Vec2> _texCoords;
GridWarpSurface * _surface;
};

4
src/Commands/AddGridRowCmd.cpp

@ -18,9 +18,9 @@ void AddGridRowCmd::exec(){
void AddGridRowCmd::undo(){
ofLogNotice("AddGridRowCmd", "undo");
_surface->setGridRows(_surface->getGridRows() - 1);
vector <ofVec2f> v;
vector <Vec2> v;
for(int i = 0; i < _vertices.size(); ++i){
v.push_back( ofVec2f(_vertices[i].x, _vertices[i].y) );
v.push_back( Vec2(_vertices[i].x, _vertices[i].y) );
}
_surface->setVertices(v);
_surface->setTexCoords(_texCoords);

3
src/Commands/AddGridRowCmd.h

@ -4,6 +4,7 @@
#include "BaseCmd.h"
#include "GridWarpSurface.h"
#include "ProjectionEditorWidget.h"
#include "Vec2.h"
class ofxPiMapper;
@ -19,7 +20,7 @@ class AddGridRowCmd : public BaseUndoCmd {
private:
vector <ofVec3f> _vertices;
vector <ofVec2f> _texCoords;
vector <Vec2> _texCoords;
GridWarpSurface * _surface;
};

2
src/Commands/DuplicateSurfaceCmd.cpp

@ -12,7 +12,7 @@ void DuplicateSurfaceCmd::exec(){
ofLogNotice("DuplicateSurfaceCmd", "exec");
_duplicate = _surface->clone();
_surfaceManager->addSurface(_duplicate);
_duplicate->moveBy(ofVec2f(10.0f, 10.0f));
_duplicate->moveBy(Vec2(10.0f, 10.0f));
_surfaceManager->selectSurface(_duplicate);
}

1
src/Commands/DuplicateSurfaceCmd.h

@ -7,6 +7,7 @@
#include "BaseCmd.h"
#include "BaseSurface.h"
#include "SurfaceManager.h"
#include "Vec2.h"
namespace ofx {
namespace piMapper {

2
src/Commands/MvAllTexCoordsCmd.cpp

@ -15,7 +15,7 @@ void MvAllTexCoordsCmd::exec(){
void MvAllTexCoordsCmd::undo(){
ofLogNotice("MvAllTexCoordsCmd", "undo");
ofVec2f dist = _texCoords[0] - _surface->getTexCoords()[0];
Vec2 dist = _texCoords[0] - _surface->getTexCoords()[0];
dist.x = _surface->getSource()->getTexture()->getWidth() * dist.x;
dist.y = _surface->getSource()->getTexture()->getHeight() * dist.y;
_texEditor->moveTexCoords(dist);

3
src/Commands/MvAllTexCoordsCmd.h

@ -7,6 +7,7 @@
#include "BaseCmd.h"
#include "BaseSurface.h"
#include "TextureEditorWidget.h"
#include "Vec2.h"
namespace ofx {
namespace piMapper {
@ -19,7 +20,7 @@ class MvAllTexCoordsCmd : public BaseUndoCmd {
void undo();
private:
vector <ofVec2f> _texCoords;
vector <Vec2> _texCoords;
BaseSurface * _surface;
TextureEditorWidget * _texEditor;

2
src/Commands/MvSelectionCmd.cpp

@ -3,7 +3,7 @@
namespace ofx {
namespace piMapper {
MvSelectionCmd::MvSelectionCmd(SurfaceManager * sm, ofVec2f moveBy){
MvSelectionCmd::MvSelectionCmd(SurfaceManager * sm, Vec2 moveBy){
_surfaceManager = sm;
_movedBy = moveBy;
}

5
src/Commands/MvSelectionCmd.h

@ -2,6 +2,7 @@
#include "BaseCmd.h"
#include "SurfaceManager.h"
#include "Vec2.h"
namespace ofx {
namespace piMapper {
@ -9,13 +10,13 @@ namespace piMapper {
class MvSelectionCmd : public BaseUndoCmd {
public:
MvSelectionCmd(SurfaceManager * sm, ofVec2f moveBy);
MvSelectionCmd(SurfaceManager * sm, Vec2 moveBy);
void exec();
void undo();
private:
SurfaceManager * _surfaceManager;
ofVec2f _movedBy;
Vec2 _movedBy;
};

3
src/Commands/MvSurfaceVertCmd.h

@ -8,6 +8,7 @@
#include "BaseSurface.h"
#include "ProjectionEditorWidget.h"
#include "BaseJoint.h"
#include "Vec2.h"
namespace ofx {
namespace piMapper {
@ -21,7 +22,7 @@ class MvSurfaceVertCmd : public BaseUndoCmd {
private:
int _vertIndex;
ofVec2f _prevVertPos;
Vec2 _prevVertPos;
BaseSurface * _surface;
};

2
src/Commands/MvTexCoordCmd.cpp

@ -3,7 +3,7 @@
namespace ofx {
namespace piMapper {
MvTexCoordCmd::MvTexCoordCmd(int texCoordIndex, ofVec2f by){
MvTexCoordCmd::MvTexCoordCmd(int texCoordIndex, Vec2 by){
_texCoordIndex = texCoordIndex;
_moveBy = by;
}

7
src/Commands/MvTexCoordCmd.h

@ -7,6 +7,7 @@
#include "BaseCmd.h"
#include "CircleJoint.h"
#include "Gui.h"
#include "Vec2.h"
namespace ofx {
namespace piMapper {
@ -14,14 +15,14 @@ namespace piMapper {
class MvTexCoordCmd : public BaseUndoCmd {
public:
MvTexCoordCmd(int texCoordIndex, ofVec2f by);
MvTexCoordCmd(int texCoordIndex, Vec2 by);
void exec();
void undo();
private:
int _texCoordIndex;
ofVec2f _moveBy;
ofVec2f _positionBefore;
Vec2 _moveBy;
Vec2 _positionBefore;
};

4
src/Commands/RmGridColCmd.cpp

@ -30,10 +30,10 @@ void RmGridColCmd::undo(){
}
_surface->setGridCols(_surface->getGridCols() + 1);
vector <ofVec2f> v;
vector <Vec2> v;
for(int i = 0; i < _vertices.size(); ++i){
v.push_back( ofVec2f(_vertices[i].x, _vertices[i].y) );
v.push_back( Vec2(_vertices[i].x, _vertices[i].y) );
}
_surface->setVertices(v);

3
src/Commands/RmGridColCmd.h

@ -4,6 +4,7 @@
#include "BaseCmd.h"
#include "GridWarpSurface.h"
#include "ProjectionEditorWidget.h"
#include "Vec2.h"
class ofxPiMapper;
@ -19,7 +20,7 @@ class RmGridColCmd : public BaseUndoCmd {
private:
vector <ofVec3f> _vertices;
vector <ofVec2f> _texCoords;
vector <Vec2> _texCoords;
GridWarpSurface * _surface;
bool _doNotUndo;

4
src/Commands/RmGridRowCmd.cpp

@ -30,10 +30,10 @@ void RmGridRowCmd::undo(){
}
_surface->setGridRows(_surface->getGridRows() + 1);
vector <ofVec2f> v;
vector <Vec2> v;
for(int i = 0; i < _vertices.size(); ++i){
v.push_back( ofVec2f(_vertices[i].x, _vertices[i].y) );
v.push_back( Vec2(_vertices[i].x, _vertices[i].y) );
}
_surface->setVertices(v);

3
src/Commands/RmGridRowCmd.h

@ -4,6 +4,7 @@
#include "BaseCmd.h"
#include "GridWarpSurface.h"
#include "ProjectionEditorWidget.h"
#include "Vec2.h"
class ofxPiMapper;
@ -19,7 +20,7 @@ class RmGridRowCmd : public BaseUndoCmd {
private:
vector <ofVec3f> _vertices;
vector <ofVec2f> _texCoords;
vector <Vec2> _texCoords;
GridWarpSurface * _surface;
bool _doNotUndo;

2
src/Commands/SaveTexCoordPosCmd.cpp

@ -3,7 +3,7 @@
namespace ofx {
namespace piMapper {
SaveTexCoordPosCmd::SaveTexCoordPosCmd(int texCoordIndex, ofVec2f position){
SaveTexCoordPosCmd::SaveTexCoordPosCmd(int texCoordIndex, Vec2 position){
_texCoordIndex = texCoordIndex;
_position = position;
}

5
src/Commands/SaveTexCoordPosCmd.h

@ -7,6 +7,7 @@
#include "BaseCmd.h"
#include "CircleJoint.h"
#include "Gui.h"
#include "Vec2.h"
namespace ofx {
namespace piMapper {
@ -14,13 +15,13 @@ namespace piMapper {
class SaveTexCoordPosCmd : public BaseUndoCmd {
public:
SaveTexCoordPosCmd(int texCoordIndex, ofVec2f position);
SaveTexCoordPosCmd(int texCoordIndex, Vec2 position);
void exec();
void undo();
private:
int _texCoordIndex;
ofVec2f _position;
Vec2 _position;
};

3
src/Commands/StartDragSurfaceCmd.cpp

@ -15,7 +15,8 @@ void StartDragSurfaceCmd::exec(){
void StartDragSurfaceCmd::undo(){
ofLogNotice("StartDragSurfaceCmd", "undo");
_surface->moveBy(_previousVertices[0] - _surface->getVertices()[0]);
ofVec3f step = _previousVertices[0] - _surface->getVertices()[0];
_surface->moveBy(Vec2(step));
}
} // namespace piMapper

1
src/Commands/StartDragSurfaceCmd.h

@ -2,6 +2,7 @@
#include "BaseCmd.h"
#include "BaseSurface.h"
#include "Vec2.h"
namespace ofx {
namespace piMapper {

19
src/Gui/Widgets/ProjectionEditorWidget.cpp

@ -47,7 +47,7 @@ void ProjectionEditorWidget::mouseDragged(ofMouseEventArgs & args){
joints[i]->mouseDragged(args);
}
ofVec2f mousePosition = ofVec2f(args.x, args.y);
Vec2 mousePosition = Vec2(args.x, args.y);
// Collect all vertices of the projection surfaces
vector <ofVec3f *> allVertices;
@ -65,10 +65,13 @@ void ProjectionEditorWidget::mouseDragged(ofMouseEventArgs & args){
for(int i = 0; i < joints.size(); i++){
if(joints[i]->isDragged()){
for(int j = 0; j < allVertices.size(); j++){
float distance = mousePosition.distance(*allVertices[j]);
Vec2 v(*allVertices[j]);
float distance = mousePosition.distance(v);
if(distance < fSnapDistance){
joints[i]->position = *allVertices[j];
ofVec2f clickDistance = joints[i]->position - ofVec2f(args.x, args.y);
Vec2 jointPosition(joints[i]->position);
Vec2 clickPosition(args.x, args.y);
Vec2 clickDistance = jointPosition - clickPosition;
joints[i]->setClickDistance(clickDistance);
break;
}
@ -135,7 +138,7 @@ void ProjectionEditorWidget::createJoints(){
for(int i = 0; i < vertices.size(); i++){
joints.push_back(new CircleJoint());
joints.back()->position = ofVec2f(vertices[i].x, vertices[i].y);
joints.back()->position = Vec2(vertices[i].x, vertices[i].y);
}
}
@ -144,7 +147,7 @@ void ProjectionEditorWidget::updateJoints(){
vector <ofVec3f> & vertices =
surfaceManager->getSelectedSurface()->getVertices();
for(int i = 0; i < vertices.size(); i++){
joints[i]->position = ofVec2f(vertices[i].x, vertices[i].y);
joints[i]->position = Vec2(vertices[i].x, vertices[i].y);
}
}
@ -156,7 +159,7 @@ void ProjectionEditorWidget::unselectAllJoints(){
}
}
void ProjectionEditorWidget::moveSelectedSurface(ofVec2f by){
void ProjectionEditorWidget::moveSelectedSurface(Vec2 by){
if(surfaceManager == 0){
return;
}
@ -177,7 +180,7 @@ void ProjectionEditorWidget::setSnapDistance(float newSnapDistance){
fSnapDistance = newSnapDistance;
}
CircleJoint * ProjectionEditorWidget::hitTestJoints(ofVec2f pos){
CircleJoint * ProjectionEditorWidget::hitTestJoints(Vec2 pos){
if(surfaceManager->getSelectedSurface() == 0){
return 0;
}
@ -236,4 +239,4 @@ void ProjectionEditorWidget::drawJoints(){
}
} // namespace piMapper
} // namespace ofx
} // namespace ofx

7
src/Gui/Widgets/ProjectionEditorWidget.h

@ -2,6 +2,7 @@
#include "SurfaceManager.h"
#include "CircleJoint.h"
#include "Vec2.h"
namespace ofx {
namespace piMapper {
@ -22,11 +23,11 @@ class ProjectionEditorWidget {
void createJoints();
void updateJoints();
void unselectAllJoints();
void moveSelectedSurface(ofVec2f by);
void moveSelectedSurface(Vec2 by);
void stopDragJoints();
void updateVertices();
void setSnapDistance(float newSnapDistance);
CircleJoint * hitTestJoints(ofVec2f pos);
CircleJoint * hitTestJoints(Vec2 pos);
vector <CircleJoint *> * getJoints();
void onVertexChanged(int & i);
@ -46,4 +47,4 @@ class ProjectionEditorWidget {
};
} // namespace piMapper
} // namespace ofx
} // namespace ofx

54
src/Gui/Widgets/TextureEditorWidget.cpp

@ -23,7 +23,7 @@ void TextureEditorWidget::update(){
}
// update surface if one of the joints is being dragged
ofVec2f textureSize = ofVec2f(surface->getSource()->getTexture()->getWidth(),
Vec2 textureSize = Vec2(surface->getSource()->getTexture()->getWidth(),
surface->getSource()->getTexture()->getHeight());
// Get selected joint index
@ -44,8 +44,8 @@ void TextureEditorWidget::update(){
if(surface->getType() == SurfaceType::GRID_WARP_SURFACE){
GridWarpSurface * s = (GridWarpSurface *)surface;
vector <ofVec2f> & texCoords = surface->getTexCoords();
ofVec2f textureSize = ofVec2f(surface->getSource()->getTexture()->getWidth(),
vector <Vec2> & texCoords = surface->getTexCoords();
Vec2 textureSize = Vec2(surface->getSource()->getTexture()->getWidth(),
surface->getSource()->getTexture()->getHeight());
int rows = s->getGridRows();
@ -65,7 +65,7 @@ void TextureEditorWidget::update(){
int i = 0;
for(int iy = 0; iy <= rows; ++iy){
for(int ix = 0; ix <= cols; ++ix){
ofVec2f t;
Vec2 t;
t.x = sx + dx * ix;
t.y = sy + dy * iy;
surface->setTexCoord(i, t);
@ -132,7 +132,7 @@ void TextureEditorWidget::createJoints(){
return;
}
clearJoints();
vector <ofVec2f> & texCoords = surface->getTexCoords();
vector <Vec2> & texCoords = surface->getTexCoords();
if(surface->getSource()->getTexture()->isAllocated()){
_pollCreateJoints = false;
@ -141,11 +141,11 @@ void TextureEditorWidget::createJoints(){
return;
}
ofVec2f textureSize = ofVec2f(surface->getSource()->getTexture()->getWidth(),
Vec2 textureSize = Vec2(surface->getSource()->getTexture()->getWidth(),
surface->getSource()->getTexture()->getHeight());
// Select joints depending on the surface type
vector <ofVec2f> tc;
vector <Vec2> tc;
if(surface->getType() == SurfaceType::TRIANGLE_SURFACE){
tc = texCoords;
@ -274,13 +274,13 @@ void TextureEditorWidget::selectPrevTexCoord(){
selectTexCoord(joints.size() - 1);
}
void TextureEditorWidget::moveTexCoords(ofVec2f by){
void TextureEditorWidget::moveTexCoords(Vec2 by){
if(surface == 0){
return;
}
vector <ofVec2f> & texCoords = surface->getTexCoords();
ofVec2f textureSize = ofVec2f(surface->getSource()->getTexture()->getWidth(),
vector <Vec2> & texCoords = surface->getTexCoords();
Vec2 textureSize = Vec2(surface->getSource()->getTexture()->getWidth(),
surface->getSource()->getTexture()->getHeight());
for(int i = 0; i < joints.size(); i++){
@ -313,7 +313,7 @@ void TextureEditorWidget::moveTexCoords(ofVec2f by){
int i = 0;
for(int iy = 0; iy <= rows; ++iy){
for(int ix = 0; ix <= cols; ++ix){
ofVec2f t;
Vec2 t;
t.x = sx + dx * ix;
t.y = sy + dy * iy;
surface->setTexCoord(i, t);
@ -327,7 +327,7 @@ void TextureEditorWidget::moveTexCoords(ofVec2f by){
}
}
void TextureEditorWidget::moveTexCoordTo(int texCoordIndex, ofVec2f position){
void TextureEditorWidget::moveTexCoordTo(int texCoordIndex, Vec2 position){
if(surface == 0){
return;
}
@ -335,7 +335,7 @@ void TextureEditorWidget::moveTexCoordTo(int texCoordIndex, ofVec2f position){
ofLogNotice("TextureEditorWidget::moveTexCoordTo") << texCoordIndex << ", " << position.x << ", " << position.y;
surface->setTexCoord(texCoordIndex, position);
ofVec2f textureSize = ofVec2f(
Vec2 textureSize = Vec2(
surface->getSource()->getTexture()->getWidth(),
surface->getSource()->getTexture()->getHeight());
joints[texCoordIndex]->position = position * textureSize;
@ -347,7 +347,7 @@ void TextureEditorWidget::stopDragJoints(){
}
}
void TextureEditorWidget::moveSelection(ofVec2f by){
void TextureEditorWidget::moveSelection(Vec2 by){
// check if joints selected
bool bJointSelected = false;
BaseJoint * selectedJoint;
@ -369,32 +369,32 @@ void TextureEditorWidget::moveSelection(ofVec2f by){
void TextureEditorWidget::constrainJointsToQuad(int selectedJointIndex){
switch(selectedJointIndex){
case 0:
joints[1]->position = ofVec2f(joints[1]->position.x, joints[0]->position.y);
joints[2]->position = ofVec2f(joints[1]->position.x, joints[3]->position.y);
joints[3]->position = ofVec2f(joints[0]->position.x, joints[3]->position.y);
joints[1]->position = Vec2(joints[1]->position.x, joints[0]->position.y);
joints[2]->position = Vec2(joints[1]->position.x, joints[3]->position.y);
joints[3]->position = Vec2(joints[0]->position.x, joints[3]->position.y);
break;
case 1:
joints[0]->position = ofVec2f(joints[0]->position.x, joints[1]->position.y);
joints[2]->position = ofVec2f(joints[1]->position.x, joints[2]->position.y);
joints[3]->position = ofVec2f(joints[0]->position.x, joints[2]->position.y);
joints[0]->position = Vec2(joints[0]->position.x, joints[1]->position.y);
joints[2]->position = Vec2(joints[1]->position.x, joints[2]->position.y);
joints[3]->position = Vec2(joints[0]->position.x, joints[2]->position.y);
break;
case 2:
joints[1]->position = ofVec2f(joints[2]->position.x, joints[1]->position.y);
joints[3]->position = ofVec2f(joints[3]->position.x, joints[2]->position.y);
joints[0]->position = ofVec2f(joints[3]->position.x, joints[1]->position.y);
joints[1]->position = Vec2(joints[2]->position.x, joints[1]->position.y);
joints[3]->position = Vec2(joints[3]->position.x, joints[2]->position.y);
joints[0]->position = Vec2(joints[3]->position.x, joints[1]->position.y);
break;
case 3:
joints[0]->position = ofVec2f(joints[3]->position.x, joints[0]->position.y);
joints[2]->position = ofVec2f(joints[2]->position.x, joints[3]->position.y);
joints[1]->position = ofVec2f(joints[2]->position.x, joints[0]->position.y);
joints[0]->position = Vec2(joints[3]->position.x, joints[0]->position.y);
joints[2]->position = Vec2(joints[2]->position.x, joints[3]->position.y);
joints[1]->position = Vec2(joints[2]->position.x, joints[0]->position.y);
break;
} // switch
}
CircleJoint * TextureEditorWidget::hitTestJoints(ofVec2f pos){
CircleJoint * TextureEditorWidget::hitTestJoints(Vec2 pos){
for(int i = 0; i < joints.size(); i++){
if(joints[i]->hitTest(pos)){
return joints[i];

11
src/Gui/Widgets/TextureEditorWidget.h

@ -7,6 +7,7 @@
#include "CircleJoint.h"
#include "SurfaceType.h"
#include "GuiBaseWidget.h"
#include "Vec2.h"
namespace ofx {
namespace piMapper {
@ -38,13 +39,13 @@ class TextureEditorWidget : public GuiBaseWidget {
void selectNextTexCoord();
void selectPrevTexCoord();
void moveTexCoords(ofVec2f by);
void moveTexCoordTo(int texCoordIndex, ofVec2f position);
void moveTexCoords(Vec2 by);
void moveTexCoordTo(int texCoordIndex, Vec2 position);
void stopDragJoints();
void moveSelection(ofVec2f by);
void moveSelection(Vec2 by);
void constrainJointsToQuad(int selectedJointIndex);
CircleJoint * hitTestJoints(ofVec2f pos);
CircleJoint * hitTestJoints(Vec2 pos);
vector <CircleJoint *> & getJoints();
private:
@ -57,4 +58,4 @@ class TextureEditorWidget : public GuiBaseWidget {
};
} // namespace piMapper
} // namespace ofx
} // namespace ofx

30
src/Surfaces/BaseSurface.cpp

@ -51,24 +51,32 @@ void BaseSurface::createDefaultTexture(){
source = defaultSource;
}
void BaseSurface::drawTexture(ofVec2f position){
void BaseSurface::drawTexture(Vec2 position){
if(source->getTexture() == 0){
ofLogWarning("BaseSurface") << "Source texture empty. Not drawing.";
return;
}
ofMesh texMesh;
texMesh.addVertex(position);
texMesh.addVertex(position + ofVec2f(source->getTexture()->getWidth(), 0.0f));
texMesh.addVertex(position
+ ofVec2f(source->getTexture()->getWidth(), source->getTexture()->getHeight()));
texMesh.addVertex(position + ofVec2f(0.0f, source->getTexture()->getHeight()));
texMesh.addVertex(position.toOf());
Vec2 topRight(source->getTexture()->getWidth(), 0.0f);
texMesh.addVertex((position + topRight).toOf());
Vec2 bottomRight(source->getTexture()->getWidth(), source->getTexture()->getHeight());
texMesh.addVertex((position + bottomRight).toOf());
Vec2 bottomLeft(0.0f, source->getTexture()->getHeight());
texMesh.addVertex((position + bottomLeft).toOf());
texMesh.addTriangle(0, 2, 3);
texMesh.addTriangle(0, 1, 2);
texMesh.addTexCoord(ofVec2f(0.0f, 0.0f));
texMesh.addTexCoord(ofVec2f(1.0f, 0.0f));
texMesh.addTexCoord(ofVec2f(1.0f, 1.0f));
texMesh.addTexCoord(ofVec2f(0.0f, 1.0f));
texMesh.addTexCoord(Vec2(0.0f, 0.0f).toOf());
texMesh.addTexCoord(Vec2(1.0f, 0.0f).toOf());
texMesh.addTexCoord(Vec2(1.0f, 1.0f).toOf());
texMesh.addTexCoord(Vec2(0.0f, 1.0f).toOf());
source->getTexture()->bind();
texMesh.draw();
source->getTexture()->unbind();
@ -150,4 +158,4 @@ ofRectangle & BaseSurface::getBoundingBox(){
}
} // namespace piMapper
} // namespace ofx
} // namespace ofx

21
src/Surfaces/BaseSurface.h

@ -3,6 +3,7 @@
#include "ofMain.h"
#include <string>
#include "BaseSource.h"
#include "Vec2.h"
using namespace std;
@ -17,25 +18,25 @@ class BaseSurface {
virtual void setup() = 0;
virtual void draw() = 0;
virtual void setVertex(int index, ofVec2f p) = 0;
virtual void setVertices(vector<ofVec2f> v) = 0;
virtual void setTexCoord(int index, ofVec2f t) = 0;
virtual void setTexCoords(vector<ofVec2f> t) = 0;
virtual void moveBy(ofVec2f v) = 0;
virtual void setVertex(int index, Vec2 p) = 0;
virtual void setVertices(vector<Vec2> v) = 0;
virtual void setTexCoord(int index, Vec2 t) = 0;
virtual void setTexCoords(vector<Vec2> t) = 0;
virtual void moveBy(Vec2 v) = 0;
virtual int getType() = 0;
virtual bool hitTest(ofVec2f p) = 0;
virtual bool hitTest(Vec2 p) = 0;
virtual ofPolyline getHitArea() = 0;
virtual ofPolyline getTextureHitArea() = 0;
virtual vector <ofVec3f> & getVertices() = 0;
virtual vector <ofVec2f> & getTexCoords() = 0;
virtual vector <Vec2> & getTexCoords() = 0;
virtual BaseSurface * clone() = 0;
void drawTexture(ofVec2f position);
void drawTexture(Vec2 position);
void setSource(BaseSource * newSource);
void setMoved(bool moved);
void scaleTo(float scale);
@ -67,7 +68,9 @@ class BaseSurface {
bool _moved;
float _scale;
vector<Vec2> _texCoords;
};
} // namespace piMapper
} // namespace ofx
} // namespace ofx

44
src/Surfaces/CircleSurface.cpp

@ -104,10 +104,10 @@ void CircleSurface::setup() {
// }
//#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));
Vec2 t1 = Vec2(0.0f, 0.0f);
Vec2 t2 = Vec2(1.0f, 0.0f);
Vec2 t3 = Vec2(1.0f, 1.0f);
Vec2 t4 = Vec2(0.0f, 1.0f);
defaultTexCoords.push_back(t1);
defaultTexCoords.push_back(t2);
@ -162,7 +162,7 @@ void CircleSurface::draw() {
source->getTexture()->getTextureData().textureID = outputFbo.getTexture().getTextureData().textureID;
auto texCoords = getMesh().getTexCoords();
getMesh().clearTexCoords();
getMesh().addTexCoords(defaultTexCoords);
getMesh().addTexCoords(Vec2::toOf(defaultTexCoords));
// Draw the Quad:
QuadSurface::draw();
@ -264,35 +264,35 @@ void CircleSurface::setupTextures() {
// 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);
Vec3 p1 = Vec3(0, 0, 0);
Vec3 p2 = Vec3(0, h, 0);
Vec3 p3 = Vec3(w, h, 0);
Vec3 p4 = Vec3(w, 0, 0);
// Create 4 point for the texture coordinates
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));
Vec2 t1 = Vec2(Vec2(0.0f, 1.0f));
Vec2 t2 = Vec2(Vec2(1.0f, 1.0f));
Vec2 t3 = Vec2(Vec2(1.0f, 0.0f));
Vec2 t4 = Vec2(Vec2(0.0f, 0.0f));
// Clear maskMesh
maskMesh.clear();
// Create a surface with the points
maskMesh.addVertex(p1);
maskMesh.addVertex(p2);
maskMesh.addVertex(p3);
maskMesh.addVertex(p4);
maskMesh.addVertex(p1.toOf());
maskMesh.addVertex(p2.toOf());
maskMesh.addVertex(p3.toOf());
maskMesh.addVertex(p4.toOf());
// 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);
maskMesh.addTexCoord(t1.toOf());
maskMesh.addTexCoord(t2.toOf());
maskMesh.addTexCoord(t3.toOf());
maskMesh.addTexCoord(t4.toOf());
}
@ -302,4 +302,4 @@ int CircleSurface::getType() {
}
}
}
}

4
src/Surfaces/CircleSurface.h

@ -7,6 +7,8 @@
#define OFXPIMAPPER_CIRCLESURFACE_H
#include "QuadSurface.h"
#include "Vec2.h"
#include "Vec3.h"
#define CIRCLE_SURFACE_STRINGIFY(A) #A
@ -49,7 +51,7 @@ class CircleSurface : public QuadSurface {
// string gl3FragmentShader;
private:
std::vector<ofVec2f> defaultTexCoords;
std::vector<Vec2> 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.

54
src/Surfaces/GridWarpSurface.cpp

@ -34,11 +34,11 @@ void GridWarpSurface::draw(){
}
}
void GridWarpSurface::moveBy(ofVec2f v){
void GridWarpSurface::moveBy(Vec2 v){
vector <ofVec3f> & vertices = getVertices();
for(int i = 0; i < vertices.size(); i++){
vertices[i] += v;
vertices[i] += v.toOf();
}
setMoved(true);
@ -67,7 +67,7 @@ int GridWarpSurface::setGridCols(int c){
createGridMesh();
}
bool GridWarpSurface::hitTest(ofVec2f p){
bool GridWarpSurface::hitTest(Vec2 p){
ofPolyline pl;
int vertsPerRow = _gridCols + 1;
@ -85,7 +85,7 @@ bool GridWarpSurface::hitTest(ofVec2f p){
pl.addVertex(mesh.getVertex(d));
pl.close();
if(pl.inside(p)){
if(pl.inside(p.toOf())){
return true;
}
}
@ -127,8 +127,7 @@ ofPolyline GridWarpSurface::getHitArea(){
ofPolyline GridWarpSurface::getTextureHitArea(){
ofPolyline line;
vector <ofVec2f> & texCoords = mesh.getTexCoords();
ofVec2f textureSize = ofVec2f(source->getTexture()->getWidth(), source->getTexture()->getHeight());
Vec2 textureSize = Vec2(source->getTexture()->getWidth(), source->getTexture()->getHeight());
int vertsPerRow = _gridCols + 1;
int vertsPerCol = _gridRows + 1;
@ -138,32 +137,32 @@ ofPolyline GridWarpSurface::getTextureHitArea(){
int c = (_gridRows * vertsPerRow) + (vertsPerRow - 1);
int d = (_gridRows * vertsPerRow);
line.addVertex(ofPoint(texCoords[a] * textureSize));
line.addVertex(ofPoint(texCoords[b] * textureSize));
line.addVertex(ofPoint(texCoords[c] * textureSize));
line.addVertex(ofPoint(texCoords[d] * textureSize));
line.addVertex(ofPoint(mesh.getTexCoords()[a] * textureSize.toOf()));
line.addVertex(ofPoint(mesh.getTexCoords()[b] * textureSize.toOf()));
line.addVertex(ofPoint(mesh.getTexCoords()[c] * textureSize.toOf()));
line.addVertex(ofPoint(mesh.getTexCoords()[d] * textureSize.toOf()));
line.close();
return line;
}
void GridWarpSurface::setVertex(int index, ofVec2f p){
void GridWarpSurface::setVertex(int index, Vec2 p){
if(index >= mesh.getVertices().size()){
throw runtime_error("Vertex with provided index does not exist");
}
mesh.setVertex(index, p);
mesh.setVertex(index, p.toOf());
ofVec3f v = mesh.getVertex(index);
ofNotifyEvent(vertexChangedEvent, index, this);
}
void GridWarpSurface::setVertices(vector<ofVec2f> v){
void GridWarpSurface::setVertices(vector<Vec2> 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]);
mesh.setVertex(i, v[i].toOf());
}
ofNotifyEvent(verticesChangedEvent, mesh.getVertices(), this);
@ -181,19 +180,19 @@ void GridWarpSurface::setVertices(vector<ofVec3f> v){
ofNotifyEvent(verticesChangedEvent, mesh.getVertices(), this);
}
void GridWarpSurface::setTexCoord(int index, ofVec2f t){
void GridWarpSurface::setTexCoord(int index, Vec2 t){
if(index >= mesh.getVertices().size()){
throw runtime_error("Texture coordinate with provided index does not exist");
}
mesh.setTexCoord(index, t);
mesh.setTexCoord(index, t.toOf());
}
void GridWarpSurface::setTexCoords(vector<ofVec2f> t){
void GridWarpSurface::setTexCoords(vector<Vec2> t){
if(t.size() != mesh.getVertices().size()){
throw runtime_error("Wrong number of texture coordinates");
}
for(int i = 0; i < t.size(); ++i){
mesh.setTexCoord(i, t[i]);
mesh.setTexCoord(i, t[i].toOf());
}
}
@ -202,8 +201,12 @@ vector <ofVec3f> & GridWarpSurface::getVertices(){
return mesh.getVertices();
}
vector <ofVec2f> & GridWarpSurface::getTexCoords(){
return mesh.getTexCoords();
vector <Vec2> & GridWarpSurface::getTexCoords(){
_texCoords.clear();
for(auto c : mesh.getTexCoords()){
_texCoords.push_back(Vec2(c));
}
return _texCoords;
}
void GridWarpSurface::createGridMesh(){
@ -218,10 +221,9 @@ void GridWarpSurface::createGridMesh(){
// Add vertices for each col and row
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) ));
mesh.addVertex(Vec2(
margin + (vertexDistanceX * (float)ix),
margin + (vertexDistanceY * (float)iy)).toOf());
}
}
@ -245,7 +247,7 @@ void GridWarpSurface::createGridMesh(){
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));
mesh.addTexCoord(Vec2(xc, yc).toOf());
}
}
@ -263,4 +265,4 @@ BaseSurface * GridWarpSurface::clone(){
}
} // namespace piMapper
} // namespace ofx
} // namespace ofx

19
src/Surfaces/GridWarpSurface.h

@ -4,6 +4,7 @@
#include "BaseSurface.h"
#include "SurfaceType.h"
#include "HomographyHelper.h"
#include "Vec2.h"
namespace ofx {
namespace piMapper {
@ -14,7 +15,7 @@ class GridWarpSurface : public BaseSurface {
void setup();
void draw();
void moveBy(ofVec2f v);
void moveBy(Vec2 v);
int getType();
int getGridRows();
@ -22,18 +23,18 @@ class GridWarpSurface : public BaseSurface {
int setGridRows(int r);
int setGridCols(int c);
bool hitTest(ofVec2f p);
bool hitTest(Vec2 p);
ofPolyline getHitArea();
ofPolyline getTextureHitArea();
void setVertex(int index, ofVec2f p);
void setVertices(vector<ofVec2f> v);
void setVertex(int index, Vec2 p);
void setVertices(vector<Vec2> v);
void setVertices(vector<ofVec3f> v);
void setTexCoord(int index, ofVec2f t);
void setTexCoords(vector<ofVec2f> t);
void setTexCoord(int index, Vec2 t);
void setTexCoords(vector<Vec2> t);
vector <ofVec3f> & getVertices();
vector <ofVec2f> & getTexCoords();
vector <Vec2> & getTexCoords();
void createGridMesh();
@ -42,7 +43,9 @@ class GridWarpSurface : public BaseSurface {
private:
int _gridCols;
int _gridRows;
vector<Vec2> _texCoords;
};
} // namespace piMapper
} // namespace ofx
} // namespace ofx

92
src/Surfaces/HexagonSurface.cpp

@ -11,42 +11,42 @@ void HexagonSurface::setup(){
// Create 6 + 1 points for the hexagon surface.
vector <ofVec2f> verts;
vector <Vec2> verts;
verts.resize(7);
// Start with the center.
verts[0] = ofVec2f((float)ofGetWidth() / 2.0f, (float)ofGetHeight() / 2.0f);
verts[0] = Vec2((float)ofGetWidth() / 2.0f, (float)ofGetHeight() / 2.0f);
// Then from top left clockwise.
verts[1] = ofVec2f((float)ofGetWidth() / 3.0f, 0);
verts[2] = ofVec2f((float)ofGetWidth() / 3.0f * 2.0f, 0);
verts[3] = ofVec2f(ofGetWidth(), (float)ofGetHeight() / 2.0f);
verts[4] = ofVec2f((float)ofGetWidth() / 3.0f * 2.0f, ofGetHeight());
verts[5] = ofVec2f((float)ofGetWidth() / 3.0f, ofGetHeight());
verts[6] = ofVec2f(0, (float)ofGetHeight() / 2.0f);
verts[1] = Vec2((float)ofGetWidth() / 3.0f, 0);
verts[2] = Vec2((float)ofGetWidth() / 3.0f * 2.0f, 0);
verts[3] = Vec2(ofGetWidth(), (float)ofGetHeight() / 2.0f);
verts[4] = Vec2((float)ofGetWidth() / 3.0f * 2.0f, ofGetHeight());
verts[5] = Vec2((float)ofGetWidth() / 3.0f, ofGetHeight());
verts[6] = Vec2(0, (float)ofGetHeight() / 2.0f);
// No create the texture coordinates.
vector <ofVec2f> coords;
vector <Vec2> coords;
coords.resize(7);
// Start with center.
coords[0] = ofVec2f(0.5f, 0.5f);
coords[0] = Vec2(0.5f, 0.5f);
// Then from top left and go clockwise.
coords[1] = ofVec2f(1.0f / 3.0f, 0.0f);
coords[2] = ofVec2f(1.0f / 3.0f * 2.0f, 0.0f);
coords[3] = ofVec2f(1.0f, 0.5f);
coords[4] = ofVec2f(1.0f / 3.0f * 2.0f, 1.0f);
coords[5] = ofVec2f(1.0f / 3.0f, 1.0f);
coords[6] = ofVec2f(0.0f, 0.5f);
coords[1] = Vec2(1.0f / 3.0f, 0.0f);
coords[2] = Vec2(1.0f / 3.0f * 2.0f, 0.0f);
coords[3] = Vec2(1.0f, 0.5f);
coords[4] = Vec2(1.0f / 3.0f * 2.0f, 1.0f);
coords[5] = Vec2(1.0f / 3.0f, 1.0f);
coords[6] = Vec2(0.0f, 0.5f);
// And finally setup
setup(verts, coords, source);
}
void HexagonSurface::setup(
vector <ofVec2f> & verts,
vector <ofVec2f> & coords,
vector <Vec2> & verts,
vector <Vec2> & coords,
BaseSource * newSource){
// Assign texture
@ -57,7 +57,7 @@ void HexagonSurface::setup(
// Add vertices to the mesh
for(unsigned int i = 0; i < verts.size(); ++i){
mesh.addVertex(verts[i]);
mesh.addVertex(verts[i].toOf());
}
// Form triangles
@ -78,7 +78,7 @@ void HexagonSurface::setup(
// Add texture coords
for(unsigned int i = 0; i < coords.size(); ++i){
mesh.addTexCoord(coords[i]);
mesh.addTexCoord(coords[i].toOf());
}
}
@ -103,24 +103,24 @@ void HexagonSurface::draw(){
}
}
void HexagonSurface::setVertex(int index, ofVec2f p){
void HexagonSurface::setVertex(int index, Vec2 p){
if(index >= mesh.getVertices().size()){
ofLog() << "Vertex with this index does not exist: " << index << endl;
return;
}
mesh.setVertex(index, p);
mesh.setVertex(index, p.toOf());
ofVec3f v = mesh.getVertex(index);
ofNotifyEvent(vertexChangedEvent, index, this);
}
void HexagonSurface::setVertices(vector<ofVec2f> v){
void HexagonSurface::setVertices(vector<Vec2> 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]);
mesh.setVertex(i, v[i].toOf());
}
ofNotifyEvent(verticesChangedEvent, mesh.getVertices(), this);
@ -138,30 +138,30 @@ void HexagonSurface::setVertices(vector<ofVec3f> v){
ofNotifyEvent(verticesChangedEvent, mesh.getVertices(), this);
}
void HexagonSurface::setTexCoord(int index, ofVec2f t){
void HexagonSurface::setTexCoord(int index, Vec2 t){
if(index >= mesh.getTexCoords().size()){
ofLog() << "Texture coordinate with this index does not exist: " << index
<< endl;
return;
}
mesh.setTexCoord(index, t);
mesh.setTexCoord(index, t.toOf());
}
void HexagonSurface::setTexCoords(vector<ofVec2f> t){
void HexagonSurface::setTexCoords(vector<Vec2> t){
if(t.size() != mesh.getTexCoords().size()){
throw runtime_error("Wrong number of texture coordinates");
}
for(int i = 0; i < t.size(); ++i){
mesh.setTexCoord(i, t[i]);
mesh.setTexCoord(i, t[i].toOf());
}
}
void HexagonSurface::moveBy(ofVec2f v){
void HexagonSurface::moveBy(Vec2 v){
vector <ofVec3f> & vertices = getVertices();
for(int i = 0; i < vertices.size(); i++){
vertices[i] += v;
vertices[i] += v.toOf();
}
setMoved(true);
@ -172,7 +172,7 @@ int HexagonSurface::getType(){
return SurfaceType::HEXAGON_SURFACE;
}
bool HexagonSurface::hitTest(ofVec2f p){
bool HexagonSurface::hitTest(Vec2 p){
// Construct ofPolyline from vertices
ofPolyline line = getHitArea();
@ -183,22 +183,24 @@ bool HexagonSurface::hitTest(ofVec2f p){
}
}
ofVec2f HexagonSurface::getVertex(int index){
Vec2 HexagonSurface::getVertex(int index){
if(index > 2){
ofLog() << "Vertex with this index does not exist: " << index << endl;
throw runtime_error("Vertex index out of bounds.");
}
ofVec3f vert = mesh.getVertex(index);
return ofVec2f(vert.x, vert.y);
return Vec2(vert.x, vert.y);
}
ofVec2f HexagonSurface::getTexCoord(int index){
Vec2 HexagonSurface::getTexCoord(int index){
if(index > 2){
throw runtime_error("Texture coordinate index out of bounds.");
}
return mesh.getTexCoord(index);
return Vec2(
mesh.getTexCoord(index).x,
mesh.getTexCoord(index).y);
}
ofPolyline HexagonSurface::getHitArea(){
@ -215,17 +217,15 @@ ofPolyline HexagonSurface::getHitArea(){
ofPolyline HexagonSurface::getTextureHitArea(){
ofPolyline line;
vector <ofVec2f> & texCoords = mesh.getTexCoords();
ofVec2f textureSize = ofVec2f(
Vec2 textureSize = Vec2(
source->getTexture()->getWidth(),
source->getTexture()->getHeight());
for(int i = 1; i < texCoords.size(); i++){
line.addVertex(ofPoint(texCoords[i] * textureSize));
for(int i = 1; i < mesh.getTexCoords().size(); i++){
line.addVertex(ofPoint(mesh.getTexCoords()[i] * textureSize.toOf()));
}
line.close();
return line;
}
@ -234,8 +234,12 @@ vector <ofVec3f> & HexagonSurface::getVertices(){
return mesh.getVertices();
}
vector <ofVec2f> & HexagonSurface::getTexCoords(){
return mesh.getTexCoords();
vector <Vec2> & HexagonSurface::getTexCoords(){
_texCoords.clear();
for(auto tc : mesh.getTexCoords()){
_texCoords.push_back(tc);
}
return _texCoords;
}
BaseSurface * HexagonSurface::clone(){
@ -249,4 +253,4 @@ BaseSurface * HexagonSurface::clone(){
}
} // namespace piMapper
} // namespace ofx
} // namespace ofx

25
src/Surfaces/HexagonSurface.h

@ -3,6 +3,7 @@
#include "ofMain.h"
#include "BaseSurface.h"
#include "SurfaceType.h"
#include "Vec2.h"
namespace ofx {
namespace piMapper {
@ -15,31 +16,31 @@ class HexagonSurface : public BaseSurface {
void setup();
void setup(
vector <ofVec2f> & verts,
vector <ofVec2f> & coords,
vector <Vec2> & verts,
vector <Vec2> & coords,
BaseSource * newSource);
void draw();
void setVertex(int index, ofVec2f p);
void setVertices(vector<ofVec2f> v);
void setVertex(int index, Vec2 p);
void setVertices(vector<Vec2> v);
void setVertices(vector<ofVec3f> v);
void setTexCoord(int index, ofVec2f t);
void setTexCoords(vector<ofVec2f> t);
void setTexCoord(int index, Vec2 t);
void setTexCoords(vector<Vec2> t);
void moveBy(ofVec2f v);
void moveBy(Vec2 v);
int getType();
bool hitTest(ofVec2f p);
ofVec2f getVertex(int index);
ofVec2f getTexCoord(int index);
bool hitTest(Vec2 p);
Vec2 getVertex(int index);
Vec2 getTexCoord(int index);
ofPolyline getHitArea();
ofPolyline getTextureHitArea();
vector <ofVec3f> & getVertices();
vector <ofVec2f> & getTexCoords();
vector <Vec2> & getTexCoords();
BaseSurface * clone();
};
} // namespace piMapper
} // namespace ofx
} // namespace ofx

653
src/Surfaces/QuadSurface.cpp

@ -1,323 +1,330 @@
#include "QuadSurface.h"
namespace ofx {
namespace piMapper {
QuadSurface::QuadSurface(){
_perspectiveWarping = false;
setup();
}
QuadSurface::~QuadSurface(){
cout << "QuadSurface destructor." << endl;
}
void QuadSurface::setup(){
// Create 4 points for the 2 triangles
ofVec2f p1 = ofVec2f(0, 0);
ofVec2f p2 = ofVec2f(0, ofGetHeight());
ofVec2f p3 = ofVec2f(ofGetWidth(), ofGetHeight());
ofVec2f p4 = ofVec2f(ofGetWidth(), 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));
setup(p1, p2, p3, p4, t1, t2, t3, t4, source);
}
void QuadSurface::setup(ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f p4,
ofVec2f t1, ofVec2f t2, ofVec2f t3, ofVec2f t4,
BaseSource * newSource){
// Assign texture
source = newSource;
// Clear mesh
mesh.clear();
// Create a surface with the points
mesh.addVertex(p1);
mesh.addVertex(p2);
mesh.addVertex(p3);
mesh.addVertex(p4);
// Add 2 triangles
mesh.addTriangle(0, 2, 3);
mesh.addTriangle(0, 1, 2);
// Add texture coordinates
mesh.addTexCoord(t1);
mesh.addTexCoord(t2);
mesh.addTexCoord(t3);
mesh.addTexCoord(t4);
}
void QuadSurface::draw(){
if(source->getTexture() == 0){
return;
}
if(!source->getTexture()->isAllocated()){
return;
}
if(_perspectiveWarping){
if(mesh.haveVertsChanged() || mesh.haveTexCoordsChanged()){
calculateHomography();
}
ofRectangle box = getMeshBoundingBox();
ofMesh m = mesh;
m.setVertex(0, ofVec3f(0, 0, 0));
m.setVertex(1, ofVec3f(box.width, 0, 0));
m.setVertex(2, ofVec3f(box.width, box.height, 0));
m.setVertex(3, ofVec3f(0, box.height, 0));
ofPushMatrix();
if(true){
bool normalizedTexCoords = ofGetUsingNormalizedTexCoords();
ofEnableNormalizedTexCoords();
glMultMatrixf(_matrix);
source->getTexture()->bind();
m.draw();
source->getTexture()->unbind();
if(!normalizedTexCoords){
ofDisableNormalizedTexCoords();
}
}
ofPopMatrix();
}else{
bool normalizedTexCoords = ofGetUsingNormalizedTexCoords();
ofEnableNormalizedTexCoords();
ofPushStyle();
ofSetColor(255, 255, 255);
source->getTexture()->bind();
mesh.draw();
source->getTexture()->unbind();
ofPopStyle();
if(!normalizedTexCoords){
ofDisableNormalizedTexCoords();
}
}
}
void QuadSurface::setVertex(int index, ofVec2f p){
if(index > 3){
ofLog() << "Vertex with this index does not exist: " << index << endl;
return;
}
mesh.setVertex(index, p);
ofVec3f v = mesh.getVertex(index);
ofNotifyEvent(vertexChangedEvent, index, this);
}
void QuadSurface::setVertices(vector<ofVec2f> v){
if(v.size() != 4){
throw runtime_error("Wrong number of vertices");
}
for(int i = 0; i < 4; ++i){
mesh.setVertex(i, v[i]);
}
ofNotifyEvent(verticesChangedEvent, mesh.getVertices(), this);
}
void QuadSurface::setVertices(vector<ofVec3f> v){
if(v.size() != 4){
throw runtime_error("Wrong number of vertices");
}
for(int i = 0; i < 4; ++i){
mesh.setVertex(i, v[i]);
}
ofNotifyEvent(verticesChangedEvent, mesh.getVertices(), this);
}
void QuadSurface::setTexCoord(int index, ofVec2f t){
if(index > 3){
ofLog() << "Texture coordinate with this index does not exist: " << index
<< endl;
return;
}
mesh.setTexCoord(index, t);
}
void QuadSurface::setTexCoords(vector<ofVec2f> t){
if(t.size() != 4){
throw runtime_error("Wrong number of vertices");
}
for(int i = 0; i < 4; ++i){
mesh.setTexCoord(i, t[i]);
}
}
void QuadSurface::moveBy(ofVec2f v){
vector <ofVec3f> & vertices = getVertices();
for(int i = 0; i < vertices.size(); i++){
vertices[i] += v;
}
setMoved(true);
ofNotifyEvent(verticesChangedEvent, mesh.getVertices(), this);
}
int QuadSurface::getType(){
return SurfaceType::QUAD_SURFACE;
}
bool QuadSurface::hitTest(ofVec2f p){
// Construct ofPolyline from vertices
ofPolyline line = getHitArea();
if(line.inside(p.x, p.y)){
return true;
}else{
return false;
}
}
ofVec2f QuadSurface::getVertex(int index){
if(index > 3){
ofLog() << "Vertex with this index does not exist: " << index << endl;
throw runtime_error("Vertex index out of bounds.");
}
ofVec3f vert = mesh.getVertex(index);
return ofVec2f(vert.x, vert.y);
}
ofVec2f QuadSurface::getTexCoord(int index){
if(index > 3){
throw runtime_error("Texture coordinate index out of bounds.");
}
return mesh.getTexCoord(index);
}
ofPolyline QuadSurface::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 QuadSurface::getTextureHitArea(){
ofPolyline line;
vector <ofVec2f> & 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;
}
vector <ofVec3f> & QuadSurface::getVertices(){
// return only joint vertices
return mesh.getVertices();
}
vector <ofVec2f> & QuadSurface::getTexCoords(){
return mesh.getTexCoords();
}
void QuadSurface::calculateHomography(){
float src[4][2];
float dst[4][2];
ofRectangle box = getMeshBoundingBox();
src[0][0] = 0;
src[0][1] = 0;
src[1][0] = box.width;
src[1][1] = 0;
src[2][0] = box.width;
src[2][1] = box.height;
src[3][0] = 0;
src[3][1] = box.height;
ofVec3f p0 = mesh.getVertex(0);
ofVec3f p1 = mesh.getVertex(1);
ofVec3f p2 = mesh.getVertex(2);
ofVec3f p3 = mesh.getVertex(3);
dst[0][0] = p0.x;
dst[0][1] = p0.y;
dst[1][0] = p1.x;
dst[1][1] = p1.y;
dst[2][0] = p2.x;
dst[2][1] = p2.y;
dst[3][0] = p3.x;
dst[3][1] = p3.y;
HomographyHelper::findHomography(src, dst, _matrix);
}
void QuadSurface::setPerspectiveWarping(bool b){
_perspectiveWarping = b;
}
bool QuadSurface::getPerspectiveWarping(){
return _perspectiveWarping;
}
ofRectangle QuadSurface::getMeshBoundingBox(){
float minX = 10000.0f;
float minY = 10000.0f;
float maxX = 0.0f;
float maxY = 0.0f;
for(int i = 0; i < mesh.getVertices().size(); ++i){
if(mesh.getVertices()[i].x < minX){
minX = mesh.getVertices()[i].x;
}
if(mesh.getVertices()[i].y < minY){
minY = mesh.getVertices()[i].y;
}
if(mesh.getVertices()[i].x > maxX){
maxX = mesh.getVertices()[i].x;
}
if(mesh.getVertices()[i].y > maxY){
maxY = mesh.getVertices()[i].y;
}
}
ofRectangle boundingBox = ofRectangle(ofPoint(minX, minY), ofPoint(maxX, maxY));
return boundingBox;
}
BaseSurface * QuadSurface::clone(){
QuadSurface * s = new QuadSurface();
s->setVertices(getVertices());
s->setTexCoords(getTexCoords());
s->setPerspectiveWarping(getPerspectiveWarping());
BaseSource * src = getSource();
src->referenceCount++;
s->setSource(src);
return s;
}
} // namespace piMapper
} // namespace ofx
#include "QuadSurface.h"
namespace ofx {
namespace piMapper {
QuadSurface::QuadSurface(){
_perspectiveWarping = false;
setup();
}
QuadSurface::~QuadSurface(){
cout << "QuadSurface destructor." << endl;
}
void QuadSurface::setup(){
// Create 4 points for the 2 triangles
Vec2 p1 = Vec2(0, 0);
Vec2 p2 = Vec2(0, ofGetHeight());
Vec2 p3 = Vec2(ofGetWidth(), ofGetHeight());
Vec2 p4 = Vec2(ofGetWidth(), 0);
// Create 4 point for the texture coordinates
Vec2 t1 = Vec2(Vec2(0.0f, 0.0f));
Vec2 t2 = Vec2(Vec2(1.0f, 0.0f));
Vec2 t3 = Vec2(Vec2(1.0f, 1.0f));
Vec2 t4 = Vec2(Vec2(0.0f, 1.0f));
setup(p1, p2, p3, p4, t1, t2, t3, t4, source);
}
void QuadSurface::setup(Vec2 p1, Vec2 p2, Vec2 p3, Vec2 p4,
Vec2 t1, Vec2 t2, Vec2 t3, Vec2 t4,
BaseSource * newSource){
// Assign texture
source = newSource;
// Clear mesh
mesh.clear();
// Create a surface with the points
mesh.addVertex(p1.toOf());
mesh.addVertex(p2.toOf());
mesh.addVertex(p3.toOf());
mesh.addVertex(p4.toOf());
// Add 2 triangles
mesh.addTriangle(0, 2, 3);
mesh.addTriangle(0, 1, 2);
// Add texture coordinates
mesh.addTexCoord(t1.toOf());
mesh.addTexCoord(t2.toOf());
mesh.addTexCoord(t3.toOf());
mesh.addTexCoord(t4.toOf());
}
void QuadSurface::draw(){
if(source->getTexture() == 0){
return;
}
if(!source->getTexture()->isAllocated()){
return;
}
if(_perspectiveWarping){
if(mesh.haveVertsChanged() || mesh.haveTexCoordsChanged()){
calculateHomography();
}
ofRectangle box = getMeshBoundingBox();
ofMesh m = mesh;
m.setVertex(0, ofVec3f(0, 0, 0));
m.setVertex(1, ofVec3f(box.width, 0, 0));
m.setVertex(2, ofVec3f(box.width, box.height, 0));
m.setVertex(3, ofVec3f(0, box.height, 0));
ofPushMatrix();
if(true){
bool normalizedTexCoords = ofGetUsingNormalizedTexCoords();
ofEnableNormalizedTexCoords();
glMultMatrixf(_matrix);
source->getTexture()->bind();
m.draw();
source->getTexture()->unbind();
if(!normalizedTexCoords){
ofDisableNormalizedTexCoords();
}
}
ofPopMatrix();
}else{
bool normalizedTexCoords = ofGetUsingNormalizedTexCoords();
ofEnableNormalizedTexCoords();
ofPushStyle();
ofSetColor(255, 255, 255);
source->getTexture()->bind();
mesh.draw();
source->getTexture()->unbind();
ofPopStyle();
if(!normalizedTexCoords){
ofDisableNormalizedTexCoords();
}
}
}
void QuadSurface::setVertex(int index, Vec2 p){
if(index > 3){
ofLog() << "Vertex with this index does not exist: " << index << endl;
return;
}
mesh.setVertex(index, p.toOf());
ofVec3f v = mesh.getVertex(index);
ofNotifyEvent(vertexChangedEvent, index, this);
}
void QuadSurface::setVertices(vector<Vec2> v){
if(v.size() != 4){
throw runtime_error("Wrong number of vertices");
}
for(int i = 0; i < 4; ++i){
mesh.setVertex(i, v[i].toOf());
}
ofNotifyEvent(verticesChangedEvent, mesh.getVertices(), this);
}
void QuadSurface::setVertices(vector<ofVec3f> v){
if(v.size() != 4){
throw runtime_error("Wrong number of vertices");
}
for(int i = 0; i < 4; ++i){
mesh.setVertex(i, v[i]);
}
ofNotifyEvent(verticesChangedEvent, mesh.getVertices(), this);
}
void QuadSurface::setTexCoord(int index, Vec2 t){
if(index > 3){
ofLog() << "Texture coordinate with this index does not exist: " << index
<< endl;
return;
}
mesh.setTexCoord(index, t.toOf());
}
void QuadSurface::setTexCoords(vector<Vec2> t){
if(t.size() != 4){
throw runtime_error("Wrong number of vertices");
}
for(int i = 0; i < 4; ++i){
mesh.setTexCoord(i, t[i].toOf());
}
}
void QuadSurface::moveBy(Vec2 v){
vector <ofVec3f> & vertices = getVertices();
for(int i = 0; i < vertices.size(); i++){
vertices[i] += v.toOf();
}
setMoved(true);
ofNotifyEvent(verticesChangedEvent, mesh.getVertices(), this);
}
int QuadSurface::getType(){
return SurfaceType::QUAD_SURFACE;
}
bool QuadSurface::hitTest(Vec2 p){
// Construct ofPolyline from vertices
ofPolyline line = getHitArea();
if(line.inside(p.x, p.y)){
return true;
}else{
return false;
}
}
Vec2 QuadSurface::getVertex(int index){
if(index > 3){
ofLog() << "Vertex with this index does not exist: " << index << endl;
throw runtime_error("Vertex index out of bounds.");
}
ofVec3f vert = mesh.getVertex(index);
return Vec2(vert.x, vert.y);
}
Vec2 QuadSurface::getTexCoord(int index){
if(index > 3){
throw runtime_error("Texture coordinate index out of bounds.");
}
return Vec2(
mesh.getTexCoord(index).x,
mesh.getTexCoord(index).y);
}
ofPolyline QuadSurface::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 QuadSurface::getTextureHitArea(){
ofPolyline line;
Vec2 textureSize =
Vec2(source->getTexture()->getWidth(),
source->getTexture()->getHeight());
for(int i = 0; i < mesh.getTexCoords().size(); i++){
line.addVertex(ofPoint(mesh.getTexCoords()[i] * textureSize.toOf()));
}
line.close();
return line;
}
vector <ofVec3f> & QuadSurface::getVertices(){
// return only joint vertices
return mesh.getVertices();
}
vector <Vec2> & QuadSurface::getTexCoords(){
_texCoords.clear();
for(auto tc : mesh.getTexCoords()){
_texCoords.push_back(tc);
}
return _texCoords;
}
void QuadSurface::calculateHomography(){
float src[4][2];
float dst[4][2];
ofRectangle box = getMeshBoundingBox();
src[0][0] = 0;
src[0][1] = 0;
src[1][0] = box.width;
src[1][1] = 0;
src[2][0] = box.width;
src[2][1] = box.height;
src[3][0] = 0;
src[3][1] = box.height;
ofVec3f p0 = mesh.getVertex(0);
ofVec3f p1 = mesh.getVertex(1);
ofVec3f p2 = mesh.getVertex(2);
ofVec3f p3 = mesh.getVertex(3);
dst[0][0] = p0.x;
dst[0][1] = p0.y;
dst[1][0] = p1.x;
dst[1][1] = p1.y;
dst[2][0] = p2.x;
dst[2][1] = p2.y;
dst[3][0] = p3.x;
dst[3][1] = p3.y;
HomographyHelper::findHomography(src, dst, _matrix);
}
void QuadSurface::setPerspectiveWarping(bool b){
_perspectiveWarping = b;
}
bool QuadSurface::getPerspectiveWarping(){
return _perspectiveWarping;
}
ofRectangle QuadSurface::getMeshBoundingBox(){
float minX = 10000.0f;
float minY = 10000.0f;
float maxX = 0.0f;
float maxY = 0.0f;
for(int i = 0; i < mesh.getVertices().size(); ++i){
if(mesh.getVertices()[i].x < minX){
minX = mesh.getVertices()[i].x;
}
if(mesh.getVertices()[i].y < minY){
minY = mesh.getVertices()[i].y;
}
if(mesh.getVertices()[i].x > maxX){
maxX = mesh.getVertices()[i].x;
}
if(mesh.getVertices()[i].y > maxY){
maxY = mesh.getVertices()[i].y;
}
}
ofRectangle boundingBox = ofRectangle(ofPoint(minX, minY), ofPoint(maxX, maxY));
return boundingBox;
}
BaseSurface * QuadSurface::clone(){
QuadSurface * s = new QuadSurface();
s->setVertices(getVertices());
s->setTexCoords(getTexCoords());
s->setPerspectiveWarping(getPerspectiveWarping());
BaseSource * src = getSource();
src->referenceCount++;
s->setSource(src);
return s;
}
} // namespace piMapper
} // namespace ofx

111
src/Surfaces/QuadSurface.h

@ -1,55 +1,56 @@
#pragma once
#include "ofMain.h"
#include "BaseSurface.h"
#include "SurfaceType.h"
#include "HomographyHelper.h"
namespace ofx {
namespace piMapper {
class QuadSurface : public BaseSurface {
public:
QuadSurface();
~QuadSurface();
void setup();
void setup(ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f p4, ofVec2f t1,
ofVec2f t2, ofVec2f t3, ofVec2f t4, BaseSource * newSource);
void draw();
void setVertex(int index, ofVec2f p);
void setVertices(vector<ofVec2f> v);
void setVertices(vector<ofVec3f> v);
void setTexCoord(int index, ofVec2f t);
void setTexCoords(vector<ofVec2f> t);
void moveBy(ofVec2f v);
int getType();
bool hitTest(ofVec2f p);
ofVec2f getVertex(int index);
ofVec2f getTexCoord(int index);
ofPolyline getHitArea();
ofPolyline getTextureHitArea();
vector <ofVec3f> & getVertices();
vector <ofVec2f> & getTexCoords();
void setPerspectiveWarping(bool b);
bool getPerspectiveWarping();
ofRectangle getMeshBoundingBox();
BaseSurface * clone();
private:
void calculateHomography();
float _matrix[16];
bool _perspectiveWarping;
};
} // namespace piMapper
} // namespace ofx
#pragma once
#include "ofMain.h"
#include "BaseSurface.h"
#include "SurfaceType.h"
#include "HomographyHelper.h"
#include "Vec2.h"
namespace ofx {
namespace piMapper {
class QuadSurface : public BaseSurface {
public:
QuadSurface();
~QuadSurface();
void setup();
void setup(Vec2 p1, Vec2 p2, Vec2 p3, Vec2 p4, Vec2 t1,
Vec2 t2, Vec2 t3, Vec2 t4, BaseSource * newSource);
void draw();
void setVertex(int index, Vec2 p);
void setVertices(vector<Vec2> v);
void setVertices(vector<ofVec3f> v);
void setTexCoord(int index, Vec2 t);
void setTexCoords(vector<Vec2> t);
void moveBy(Vec2 v);
int getType();
bool hitTest(Vec2 p);
Vec2 getVertex(int index);
Vec2 getTexCoord(int index);
ofPolyline getHitArea();
ofPolyline getTextureHitArea();
vector <ofVec3f> & getVertices();
vector <Vec2> & getTexCoords();
void setPerspectiveWarping(bool b);
bool getPerspectiveWarping();
ofRectangle getMeshBoundingBox();
BaseSurface * clone();
private:
void calculateHomography();
float _matrix[16];
bool _perspectiveWarping;
};
} // namespace piMapper
} // namespace ofx

36
src/Surfaces/SurfaceFactory.cpp

@ -29,16 +29,16 @@ BaseSurface * SurfaceFactory::createSurface(SurfaceType type){
}
TriangleSurface * SurfaceFactory::createTriangleSurface(){
vector <ofVec2f> vertices;
vector <Vec2> vertices;
float margin = 50.0f;
vertices.push_back(ofVec2f((float)ofGetWidth() / 2.0f, margin));
vertices.push_back(ofVec2f((float)ofGetWidth() - margin, (float)ofGetHeight() - margin));
vertices.push_back(ofVec2f(margin, (float)ofGetHeight() - margin));
vertices.push_back(Vec2((float)ofGetWidth() / 2.0f, margin));
vertices.push_back(Vec2((float)ofGetWidth() - margin, (float)ofGetHeight() - margin));
vertices.push_back(Vec2(margin, (float)ofGetHeight() - margin));
vector <ofVec2f> texCoords;
texCoords.push_back(ofVec2f(0.5f, 0.0f));
texCoords.push_back(ofVec2f(1.0f, 1.0f));
texCoords.push_back(ofVec2f(0.0f, 1.0f));
vector <Vec2> texCoords;
texCoords.push_back(Vec2(0.5f, 0.0f));
texCoords.push_back(Vec2(1.0f, 1.0f));
texCoords.push_back(Vec2(0.0f, 1.0f));
TriangleSurface * triangleSurface = new TriangleSurface();
@ -51,18 +51,18 @@ TriangleSurface * SurfaceFactory::createTriangleSurface(){
}
QuadSurface * SurfaceFactory::createQuadSurface(){
vector <ofVec2f> vertices;
vector <Vec2> vertices;
float margin = 50.0f;
vertices.push_back(ofVec2f(margin, margin));
vertices.push_back(ofVec2f((float)ofGetWidth() - margin, margin));
vertices.push_back(ofVec2f((float)ofGetWidth() - margin, (float)ofGetHeight() - margin));
vertices.push_back(ofVec2f(margin, (float)ofGetHeight() - margin));
vertices.push_back(Vec2(margin, margin));
vertices.push_back(Vec2((float)ofGetWidth() - margin, margin));
vertices.push_back(Vec2((float)ofGetWidth() - margin, (float)ofGetHeight() - margin));
vertices.push_back(Vec2(margin, (float)ofGetHeight() - margin));
vector <ofVec2f> 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)));
vector <Vec2> texCoords;
texCoords.push_back(Vec2(Vec2(0.0f, 0.0f)));
texCoords.push_back(Vec2(Vec2(1.0f, 0.0f)));
texCoords.push_back(Vec2(Vec2(1.0f, 1.0f)));
texCoords.push_back(Vec2(Vec2(0.0f, 1.0f)));
QuadSurface * quadSurface = new QuadSurface();
quadSurface->setPerspectiveWarping(true);

6
src/Surfaces/SurfaceManager.cpp

@ -303,14 +303,14 @@ void SurfaceManager::selectVertex(int i){
ofNotifyEvent(vertexSelectedEvent, _selectedVertexIndex, this);
}
void SurfaceManager::moveSelectionBy(ofVec2f v){
void SurfaceManager::moveSelectionBy(Vec2 v){
if(selectedSurface == 0){
moveAllSurfacesBy(v);
return;
}
if(_selectedVertexIndex != -1){
selectedSurface->getVertices()[_selectedVertexIndex] += v;
selectedSurface->getVertices()[_selectedVertexIndex] += v.toOf();
ofNotifyEvent(vertexChangedEvent, _selectedVertexIndex, this);
}else{
selectedSurface->moveBy(v);
@ -321,7 +321,7 @@ void SurfaceManager::moveSelectionBy(ofVec2f v){
// it could be implemented as vector here.
}
void SurfaceManager::moveAllSurfacesBy(ofVec2f v){
void SurfaceManager::moveAllSurfacesBy(Vec2 v){
if(_activePresetIndex < 0){
ofLogWarning(
"SurfaceManager::moveAllSurfacesBy",

7
src/Surfaces/SurfaceManager.h

@ -9,6 +9,7 @@
#include "SourceType.h"
#include "SurfaceStack.h"
#include "SurfaceFactory.h"
#include "Vec2.h"
#include "ofEvents.h"
#include "ofxXmlSettings.h"
@ -50,8 +51,8 @@ class SurfaceManager {
void selectPrevVertex();
void selectVertex(int i);
void moveSelectionBy(ofVec2f v);
void moveAllSurfacesBy(ofVec2f v);
void moveSelectionBy(Vec2 v);
void moveAllSurfacesBy(Vec2 v);
int size();
int getSelectedVertexIndex();
@ -90,4 +91,4 @@ class SurfaceManager {
};
} // namespace piMapper
} // namespace ofx
} // namespace ofx

77
src/Surfaces/TriangleSurface.cpp

@ -11,20 +11,20 @@ TriangleSurface::~TriangleSurface(){}
void TriangleSurface::setup(){
// Create 3 points for the triangle
ofVec2f p1 = ofVec2f(ofGetWidth() / 2.0f, 0);
ofVec2f p2 = ofVec2f(ofVec2f(0, ofGetHeight()));
ofVec2f p3 = ofVec2f(ofGetWidth(), ofGetHeight());
Vec2 p1 = Vec2(ofGetWidth() / 2.0f, 0);
Vec2 p2 = Vec2(Vec2(0, ofGetHeight()));
Vec2 p3 = Vec2(ofGetWidth(), ofGetHeight());
// Create 3 point for the texture coordinates
ofVec2f t1 = ofVec2f(0.5f, 0);
ofVec2f t2 = ofVec2f(0, 1.0f);
ofVec2f t3 = ofVec2f(1, 1.0f);
Vec2 t1 = Vec2(0.5f, 0);
Vec2 t2 = Vec2(0, 1.0f);
Vec2 t3 = Vec2(1, 1.0f);
setup(p1, p2, p3, t1, t2, t3, source);
}
void TriangleSurface::setup(ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f t1,
ofVec2f t2, ofVec2f t3, BaseSource * newSource){
void TriangleSurface::setup(Vec2 p1, Vec2 p2, Vec2 p3, Vec2 t1,
Vec2 t2, Vec2 t3, BaseSource * newSource){
// Assign texture
source = newSource;
@ -32,14 +32,14 @@ void TriangleSurface::setup(ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f t1,
mesh.clear();
// Create a surface with the points
mesh.addVertex(p1);
mesh.addVertex(p2);
mesh.addVertex(p3);
mesh.addVertex(p1.toOf());
mesh.addVertex(p2.toOf());
mesh.addVertex(p3.toOf());
// Add texture coordinates
mesh.addTexCoord(t1);
mesh.addTexCoord(t2);
mesh.addTexCoord(t3);
mesh.addTexCoord(t1.toOf());
mesh.addTexCoord(t2.toOf());
mesh.addTexCoord(t3.toOf());
}
void TriangleSurface::draw(){
@ -63,24 +63,24 @@ void TriangleSurface::draw(){
}
}
void TriangleSurface::setVertex(int index, ofVec2f p){
void TriangleSurface::setVertex(int index, Vec2 p){
if(index > 2){
ofLog() << "Vertex with this index does not exist: " << index << endl;
return;
}
mesh.setVertex(index, p);
mesh.setVertex(index, p.toOf());
ofVec3f v = mesh.getVertex(index);
ofNotifyEvent(vertexChangedEvent, index, this);
}
void TriangleSurface::setVertices(vector<ofVec2f> v){
void TriangleSurface::setVertices(vector<Vec2> v){
if(v.size() != 3){
throw runtime_error("Wrong number of vertices");
}
for(int i = 0; i < 3; ++i){
mesh.setVertex(i, v[i]);
mesh.setVertex(i, v[i].toOf());
}
ofNotifyEvent(verticesChangedEvent, mesh.getVertices(), this);
@ -98,30 +98,30 @@ void TriangleSurface::setVertices(vector<ofVec3f> v){
ofNotifyEvent(verticesChangedEvent, mesh.getVertices(), this);
}
void TriangleSurface::setTexCoord(int index, ofVec2f t){
void TriangleSurface::setTexCoord(int index, Vec2 t){
if(index > 2){
ofLog() << "Texture coordinate with this index does not exist: " << index
<< endl;
return;
}
mesh.setTexCoord(index, t);
mesh.setTexCoord(index, t.toOf());
}
void TriangleSurface::setTexCoords(vector<ofVec2f> t){
void TriangleSurface::setTexCoords(vector<Vec2> t){
if(t.size() != 3){
throw runtime_error("Wrong number of texture coordinates");
}
for(int i = 0; i < 3; ++i){
mesh.setTexCoord(i, t[i]);
mesh.setTexCoord(i, t[i].toOf());
}
}
void TriangleSurface::moveBy(ofVec2f v){
void TriangleSurface::moveBy(Vec2 v){
vector <ofVec3f> & vertices = getVertices();
for(int i = 0; i < vertices.size(); i++){
vertices[i] += v;
vertices[i] += v.toOf();
}
setMoved(true);
@ -132,7 +132,7 @@ int TriangleSurface::getType(){
return SurfaceType::TRIANGLE_SURFACE;
}
bool TriangleSurface::hitTest(ofVec2f p){
bool TriangleSurface::hitTest(Vec2 p){
// Construct ofPolyline from vertices
ofPolyline line = getHitArea();
@ -143,22 +143,24 @@ bool TriangleSurface::hitTest(ofVec2f p){
}
}
ofVec2f TriangleSurface::getVertex(int index){
Vec2 TriangleSurface::getVertex(int index){
if(index > 2){
ofLog() << "Vertex with this index does not exist: " << index << endl;
throw runtime_error("Vertex index out of bounds.");
}
ofVec3f vert = mesh.getVertex(index);
return ofVec2f(vert.x, vert.y);
return Vec2(vert.x, vert.y);
}
ofVec2f TriangleSurface::getTexCoord(int index){
Vec2 TriangleSurface::getTexCoord(int index){
if(index > 2){
throw runtime_error("Texture coordinate index out of bounds.");
}
return mesh.getTexCoord(index);
return Vec2(
mesh.getTexCoord(index).x,
mesh.getTexCoord(index).y);
}
ofPolyline TriangleSurface::getHitArea(){
@ -173,10 +175,9 @@ ofPolyline TriangleSurface::getHitArea(){
ofPolyline TriangleSurface::getTextureHitArea(){
ofPolyline line;
vector <ofVec2f> & 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));
Vec2 textureSize = Vec2(source->getTexture()->getWidth(), source->getTexture()->getHeight());
for(int i = 0; i < mesh.getTexCoords().size(); i++){
line.addVertex(ofPoint(mesh.getTexCoords()[i] * textureSize.toOf()));
}
line.close();
@ -188,8 +189,12 @@ vector <ofVec3f> & TriangleSurface::getVertices(){
return mesh.getVertices();
}
vector <ofVec2f> & TriangleSurface::getTexCoords(){
return mesh.getTexCoords();
vector <Vec2> & TriangleSurface::getTexCoords(){
_texCoords.clear();
for(auto tc : mesh.getTexCoords()){
_texCoords.push_back(tc);
}
return _texCoords;
}
BaseSurface * TriangleSurface::clone(){
@ -203,4 +208,4 @@ BaseSurface * TriangleSurface::clone(){
}
} // namespace piMapper
} // namespace ofx
} // namespace ofx

25
src/Surfaces/TriangleSurface.h

@ -3,6 +3,7 @@
#include "ofMain.h"
#include "BaseSurface.h"
#include "SurfaceType.h"
#include "Vec2.h"
namespace ofx {
namespace piMapper {
@ -13,30 +14,30 @@ class TriangleSurface : public BaseSurface {
~TriangleSurface();
void setup();
void setup(ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f t1, ofVec2f t2,
ofVec2f t3, BaseSource * newSource);
void setup(Vec2 p1, Vec2 p2, Vec2 p3, Vec2 t1, Vec2 t2,
Vec2 t3, BaseSource * newSource);
void draw();
void setVertex(int index, ofVec2f p);
void setVertices(vector<ofVec2f> v);
void setVertex(int index, Vec2 p);
void setVertices(vector<Vec2> v);
void setVertices(vector<ofVec3f> v);
void setTexCoord(int index, ofVec2f t);
void setTexCoords(vector<ofVec2f> t);
void setTexCoord(int index, Vec2 t);
void setTexCoords(vector<Vec2> t);
void moveBy(ofVec2f v);
void moveBy(Vec2 v);
int getType();
bool hitTest(ofVec2f p);
ofVec2f getVertex(int index);
ofVec2f getTexCoord(int index);
bool hitTest(Vec2 p);
Vec2 getVertex(int index);
Vec2 getTexCoord(int index);
ofPolyline getHitArea();
ofPolyline getTextureHitArea();
vector <ofVec3f> & getVertices();
vector <ofVec2f> & getTexCoords();
vector <Vec2> & getTexCoords();
BaseSurface * clone();
};
} // namespace piMapper
} // namespace ofx
} // namespace ofx

95
src/Types/Vec2.cpp

@ -0,0 +1,95 @@
#include "Vec2.h"
namespace ofx {
namespace piMapper {
Vec2::Vec2(){
x = 0.0f;
y = 0.0f;
}
Vec2::Vec2(float $x, float $y){
x = $x;
y = $y;
}
#if OF_VERSION_MAJOR == 0 && OF_VERSION_MINOR <= 9
Vec2::Vec2(ofVec2f & src){
x = src.x;
y = src.y;
}
Vec2::Vec2(ofVec3f & src){
x = src.x;
y = src.y;
}
ofVec2f Vec2::toOf(){
return ofVec2f(x, y);
}
ofVec2f Vec2::toOf(Vec2 & src){
return ofVec2f(src.x, src.y);
}
vector<ofVec2f> Vec2::toOf(vector<Vec2> & src){
vector<ofVec2f> dst;
for(auto v : src){
dst.push_back(ofVec2f(v.x, v.y));
}
return dst;
}
float Vec2::distance(Vec2 & other){
ofVec2f v1(x, y);
ofVec2f v2(other.x, other.y);
return v1.distance(v2);
}
#else
// TODO: The same for glm::vec2
#endif
void Vec2::operator=(const Vec2 & other){
x = other.x;
y = other.y;
}
void Vec2::operator=(const ofVec3f & other){
x = other.x;
y = other.y;
}
void Vec2::operator+=(Vec2 & other){
x += other.x;
y += other.y;
}
Vec2 Vec2::operator+(Vec2 & other){
return Vec2(x + other.x, y + other.y);
}
Vec2 Vec2::operator/(Vec2 & other){
return Vec2(x / other.x, y / other.y);
}
Vec2 Vec2::operator*(Vec2 & other){
return Vec2(x * other.x, y * other.y);
}
Vec2 Vec2::operator-(){
return Vec2(-x, -y);
}
Vec2 Vec2::operator-(Vec2 & other){
return Vec2(x - other.x, y - other.y);
}
bool Vec2::operator!=(Vec2 & other){
if(x == other.x && y == other.y){
return false;
}
return true;
}
} // namespace piMapper
} // namespace ofx

49
src/Types/Vec2.h

@ -0,0 +1,49 @@
#pragma once
#include "ofMain.h"
#if OF_VERSION_MAJOR == 0 && OF_VERSION_MINOR <= 9
// ...
#else
// TODO: include glm
#endif
namespace ofx {
namespace piMapper {
//#if OF_VERSION_MAJOR == 0 && OF_VERSION_MINOR <= 9
class Vec2{
public:
Vec2();
Vec2(float $x, float $y);
#if OF_VERSION_MAJOR == 0 && OF_VERSION_MINOR <= 9
Vec2(ofVec2f & src);
Vec2(ofVec3f & src);
ofVec2f toOf();
static ofVec2f toOf(Vec2 & src);
static vector<ofVec2f> toOf(vector<Vec2> & src);
float distance(Vec2 & other);
#else
// TODO: The same for glm::vec2
// static vector<glm::vec2> toOf(vector<Vec2> & src);
#endif
void operator=(const Vec2 & other);
void operator=(const ofVec3f & other);
void operator+=(Vec2 & other);
Vec2 operator+(Vec2 & other);
Vec2 operator/(Vec2 & other);
Vec2 operator*(Vec2 & other);
Vec2 operator-();
Vec2 operator-(Vec2 & other);
bool operator!=(Vec2 & other);
float x;
float y;
};
} // namespace piMapper
} // namespace ofx

82
src/Types/Vec3.cpp

@ -0,0 +1,82 @@
#include "Vec3.h"
namespace ofx {
namespace piMapper {
Vec3::Vec3(){
x = 0.0f;
y = 0.0f;
z = 0.0f;
}
Vec3::Vec3(float $x, float $y, float $z){
x = $x;
y = $y;
z = $z;
}
#if OF_VERSION_MAJOR == 0 && OF_VERSION_MINOR <= 9
Vec3::Vec3(ofVec3f & src){
x = src.x;
y = src.y;
z = src.z;
}
ofVec3f Vec3::toOf(){
ofVec3f(x, y, z);
}
ofVec3f toOf(Vec3 & src){
return ofVec3f(src.x, src.y, src.z);
}
vector<ofVec3f> toOf(vector<Vec3> & src){
vector<ofVec3f> dst;
for(auto v : src){
dst.push_back(ofVec3f(v.x, v.y, v.z));
}
return dst;
}
#else
// TODO: The same for glm::vec2
#endif
void Vec3::operator=(const Vec3 & other){
x = other.x;
y = other.y;
z = other.z;
}
void Vec3::operator=(const ofVec3f & other){
x = other.x;
y = other.y;
z = other.z;
}
Vec3 Vec3::operator+(Vec3 & other){
return Vec3(
x + other.x,
y + other.y,
z + other.z);
}
Vec3 Vec3::operator-(){
return Vec3(-x, -y, -z);
}
Vec3 Vec3::operator-(Vec3 & other){
return Vec3(
x - other.x,
y - other.y,
z - other.z);
}
bool Vec3::operator!=(Vec3 & other){
if(x == other.x && y == other.y && z == other.z){
return false;
}
return true;
}
} // namespace piMapper
} // namespace ofx

43
src/Types/Vec3.h

@ -0,0 +1,43 @@
#pragma once
#if OF_VERSION_MAJOR == 0 && OF_VERSION_MINOR <= 9
#include "ofMain.h"
#else
// TODO: include glm
#endif
namespace ofx {
namespace piMapper {
class Vec3{
public:
Vec3();
Vec3(float $x, float $y, float $z);
#if OF_VERSION_MAJOR == 0 && OF_VERSION_MINOR <= 9
Vec3(ofVec3f & src);
ofVec3f toOf();
static ofVec3f toOf(Vec3 & src);
static vector<ofVec3f> toOf(vector<Vec3> & src);
#else
// TODO: The same for glm::vec2
// static vector<glm::vec2> toOf(vector<Vec2> & src);
#endif
void operator=(const Vec3 & other);
void operator=(const ofVec3f & other);
Vec3 operator+(Vec3 & other);
Vec3 operator-();
Vec3 operator-(Vec3 & other);
bool operator!=(Vec3 & other);
float x;
float y;
float z;
};
} // namespace piMapper
} // namespace ofx

15
src/UserInterface/BaseJoint.cpp

@ -9,8 +9,9 @@ BaseJoint::BaseJoint(){
}
void BaseJoint::mousePressed(ofMouseEventArgs & args){
if(hitTest(ofVec2f(args.x, args.y))){
clickDistance = position - ofVec2f(args.x, args.y);
if(hitTest(Vec2(args.x, args.y))){
Vec2 clickPosition(args.x, args.y);
clickDistance = position - clickPosition;
}
}
@ -22,7 +23,7 @@ void BaseJoint::mouseDragged(ofMouseEventArgs & args){
if(!bDrag){
return;
}
position = ofVec2f(args.x, args.y) + clickDistance;
position = Vec2(args.x, args.y) + clickDistance;
}
void BaseJoint::startDrag(){
@ -41,7 +42,7 @@ void BaseJoint::unselect(){
selected = false;
}
void BaseJoint::setClickDistance(ofVec2f newClickDistance){
void BaseJoint::setClickDistance(Vec2 newClickDistance){
clickDistance = newClickDistance;
}
@ -63,12 +64,12 @@ void BaseJoint::setDefaultColors(){
void BaseJoint::setDefaultProperties(){
enabled = true;
visible = true;
position = ofVec2f(20.0f, 20.0f);
clickDistance = ofVec2f(0.0f, 0.0f);
position = Vec2(20.0f, 20.0f);
clickDistance = Vec2(0.0f, 0.0f);
bDrag = false;
selected = false;
strokeWidth = 1.5f;
}
} // namespace piMapper
} // namespace ofx
} // namespace ofx

9
src/UserInterface/BaseJoint.h

@ -1,6 +1,7 @@
#pragma once
#include "ofMain.h"
#include "Vec2.h"
namespace ofx {
namespace piMapper {
@ -10,7 +11,7 @@ class BaseJoint {
public:
BaseJoint();
ofVec2f position;
Vec2 position;
bool enabled;
bool visible;
bool selected;
@ -22,13 +23,13 @@ class BaseJoint {
void stopDrag();
void select();
void unselect();
void setClickDistance(ofVec2f newClickDistance);
void setClickDistance(Vec2 newClickDistance);
bool isDragged();
bool isSelected();
virtual void update(){}
virtual void draw(){}
virtual bool hitTest(ofVec2f position){ return false; }
virtual bool hitTest(Vec2 position){ return false; }
protected:
ofColor fillColor;
@ -36,7 +37,7 @@ class BaseJoint {
ofColor fillColorSelected;
ofColor strokeColorSelected;
float strokeWidth;
ofVec2f clickDistance; // How far from the center of the joint the user has
Vec2 clickDistance; // How far from the center of the joint the user has
// clicked?
bool bDrag;

2
src/UserInterface/CircleJoint.cpp

@ -59,7 +59,7 @@ void CircleJoint::setDefaultProperties(){
radius = 10.0f;
}
bool CircleJoint::hitTest(ofVec2f pos){
bool CircleJoint::hitTest(Vec2 pos){
float distance = position.distance(pos);
if(distance < radius){
return true;

5
src/UserInterface/CircleJoint.h

@ -2,6 +2,7 @@
#include "ofMain.h"
#include "BaseJoint.h"
#include "Vec2.h"
namespace ofx {
namespace piMapper {
@ -12,7 +13,7 @@ class CircleJoint : public BaseJoint {
void update();
void draw();
bool hitTest(ofVec2f position);
bool hitTest(Vec2 position);
private:
float radius;
@ -22,4 +23,4 @@ class CircleJoint : public BaseJoint {
};
} // namespace piMapper
} // namespace ofx
} // namespace ofx

2
src/ofxPiMapper.cpp

@ -147,7 +147,7 @@ void ofxPiMapper::togglePause(){
_application.togglePause();
}
void ofxPiMapper::moveSelection(ofVec2f by){
void ofxPiMapper::moveSelection(ofx::piMapper::Vec2 by){
_application.moveSelection(by);
}

3
src/ofxPiMapper.h

@ -6,6 +6,7 @@
#include "Application.h"
#include "SurfaceType.h"
#include "Mode.h"
#include "Vec2.h"
class ofxPiMapper {
public:
@ -66,7 +67,7 @@ class ofxPiMapper {
void scaleDown();
void togglePauseForSurface(unsigned int i);
void togglePause();
void moveSelection(ofVec2f by);
void moveSelection(ofx::piMapper::Vec2 by);
void createSurface(ofx::piMapper::SurfaceType type);
void eraseSurface(int i);

Loading…
Cancel
Save