Browse Source

Add surface selection in projection mapping edit mode

master
Krisjanis Rijnieks 11 years ago
parent
commit
7c002c73b6
  1. 15
      example/src/ofApp.cpp
  2. 1
      example/src/ofApp.h
  3. 2
      src/ofxBaseSurface.h
  4. 1
      src/ofxPiMapper.h
  5. 17
      src/ofxProjectionEditor.cpp
  6. 2
      src/ofxProjectionEditor.h
  7. 103
      src/ofxSurfaceManagerGui.cpp
  8. 13
      src/ofxSurfaceManagerGui.h
  9. 34
      src/ofxTextureEditor.cpp
  10. 11
      src/ofxTextureEditor.h
  11. 23
      src/ofxTriangleSurface.cpp
  12. 3
      src/ofxTriangleSurface.h

15
example/src/ofApp.cpp

@ -11,6 +11,9 @@ void ofApp::setup()
surfaceManager.getSurface(1)->setVertex(0, ofVec2f(10, 10));
surfaceManager.getSurface(1)->setVertex(1, ofVec2f(400, 20));
surfaceManager.getSurface(1)->setVertex(2, ofVec2f(300, 400));
gui.setSurfaceManager( &surfaceManager );
gui.selectSurface(1);
}
void ofApp::update()
@ -20,11 +23,13 @@ void ofApp::update()
ofVec2f p;
p.x = ofRandomWidth();
p.y = ofRandomHeight();
//surfaceManager.getSurface(1)->setVertex(0, p);
//triangleSurface.setVertex(0, p);
ofVec2f t;
t.x = ofRandomuf();
t.y = ofRandomuf();
//surfaceManager.getSurface(1)->setTexCoord(0, t);
//triangleSurface.setTexCoord(0, t);
//surfaceManager.update();
@ -32,7 +37,9 @@ void ofApp::update()
void ofApp::draw()
{
surfaceManager.draw();
//surfaceManager.draw();
// if using gui - use ofxSurfaceManagerGui::draw() instead of surfaceManager::draw()
gui.draw();
if ( bShowInfo ) {
// Draw instructions
@ -53,9 +60,9 @@ void ofApp::keyPressed(int key)
cout << "Key pressed: " << static_cast<char>(key) << endl;
switch (key) {
//case '1': surfaceManager.setGuiMode(ofxSurfaceGui::NONE); break;
//case '2': surfaceManager.setGuiMode(ofxSurfaceGui::TEXTURE_MAPPING); break;
//case '3': surfaceManager.setGuiMode(ofxSurfaceGui::PROJECTION_MAPPING); break;
case '1': gui.setMode(ofxGuiMode::NONE); break;
case '2': gui.setMode(ofxGuiMode::TEXTURE_MAPPING); break;
case '3': gui.setMode(ofxGuiMode::PROJECTION_MAPPING); break;
case 'i': bShowInfo = !bShowInfo; break;
default: break;
}

1
example/src/ofApp.h

@ -18,6 +18,7 @@ public:
ofImage image;
ofxSurfaceManager surfaceManager;
ofxSurfaceManagerGui gui;
bool bShowInfo;
};

2
src/ofxBaseSurface.h

@ -15,6 +15,8 @@ public:
virtual void setVertex(int index, ofVec2f p){};
virtual void setTexCoord(int index, ofVec2f t){};
virtual int getType(){};
virtual bool hitTest(ofVec2f p){};
virtual ofPolyline getHitArea(){};
// Draws a texture using ofMesh
void drawTexture(ofVec2f position);

1
src/ofxPiMapper.h

@ -2,5 +2,6 @@
#define H_OFX_PI_MAPPER
#include "ofxSurfaceManager.h"
#include "ofxSurfaceManagerGui.h"
#endif

17
src/ofxProjectionEditor.cpp

@ -1 +1,16 @@
#include "ofxProjectionEditor.h"
#include "ofxProjectionEditor.h"
ofxProjectionEditor::ofxProjectionEditor()
{
}
ofxProjectionEditor::~ofxProjectionEditor()
{
}
void ofxProjectionEditor::draw()
{
}

2
src/ofxProjectionEditor.h

@ -6,6 +6,8 @@ class ofxProjectionEditor
public:
ofxProjectionEditor();
~ofxProjectionEditor();
void draw();
};
#endif

103
src/ofxSurfaceManagerGui.cpp

@ -4,16 +4,24 @@ ofxSurfaceManagerGui::ofxSurfaceManagerGui()
{
surfaceManager = NULL;
guiMode = ofxGuiMode::NONE;
selectedSurface = NULL;
registerMouseEvents();
}
ofxSurfaceManagerGui::ofxSurfaceManagerGui(ofxSurfaceManager* newSurfaceManager)
ofxSurfaceManagerGui::~ofxSurfaceManagerGui()
{
surfaceManager = newSurfaceManager;
unregisterMouseEvents();
surfaceManager = NULL;
}
ofxSurfaceManagerGui::~ofxSurfaceManagerGui()
void ofxSurfaceManagerGui::registerMouseEvents()
{
surfaceManager = NULL;
ofAddListener(ofEvents().mousePressed, this, &ofxSurfaceManagerGui::mousePressed);
}
void ofxSurfaceManagerGui::unregisterMouseEvents()
{
ofRemoveListener(ofEvents().mousePressed, this, &ofxSurfaceManagerGui::mousePressed);
}
void ofxSurfaceManagerGui::draw()
@ -21,15 +29,96 @@ void ofxSurfaceManagerGui::draw()
if ( surfaceManager == NULL ) return;
if ( guiMode == ofxGuiMode::NONE ) {
// Do nothing
surfaceManager->draw();
} else if ( guiMode == ofxGuiMode::TEXTURE_MAPPING ) {
// Draw texture mapping GUI
// draw the texture of the selected surface
if ( selectedSurface != NULL ) {
selectedSurface->drawTexture( ofVec2f(0,0) );
}
// draw surfaces with opacity
ofPushStyle();
ofSetColor(255, 255, 255, 200);
surfaceManager->draw();
ofPopStyle();
// hilight selected surface
drawSelectedSurfaceHighlight();
// draw texture editing GUI on top
textureEditor.draw();
} else if ( guiMode == ofxGuiMode::PROJECTION_MAPPING ) {
// Draw projection mapping GUI
// draw projection surfaces first
surfaceManager->draw();
// highlight selected surface
drawSelectedSurfaceHighlight();
// draw projection mapping editing gui
projectionEditor.draw();
}
}
void ofxSurfaceManagerGui::mousePressed(ofMouseEventArgs &args)
{
if ( guiMode == ofxGuiMode::NONE ) {
return;
} else if ( guiMode == ofxGuiMode::TEXTURE_MAPPING ) {
} else if ( guiMode == ofxGuiMode::PROJECTION_MAPPING ) {
// attempt to select surface, loop from end to beginning
for ( int i=surfaceManager->size()-1; i>=0; i-- ) {
if ( surfaceManager->getSurface(i)->hitTest( ofVec2f(args.x, args.y) ) ) {
selectSurface(i);
break;
}
}
}
}
void ofxSurfaceManagerGui::setSurfaceManager(ofxSurfaceManager* newSurfaceManager)
{
surfaceManager = newSurfaceManager;
}
void ofxSurfaceManagerGui::setMode(int newGuiMode)
{
if (newGuiMode != ofxGuiMode::NONE &&
newGuiMode != ofxGuiMode::TEXTURE_MAPPING &&
newGuiMode != ofxGuiMode::PROJECTION_MAPPING) {
throw std::runtime_error("Trying to set invalid mode.");
}
guiMode = newGuiMode;
}
ofxBaseSurface* ofxSurfaceManagerGui::selectSurface(int index)
{
if ( index >= surfaceManager->size() ) {
throw std::runtime_error("Surface index out of bounds.");
}
selectedSurface = surfaceManager->getSurface(index);
}
void ofxSurfaceManagerGui::deselectSurface()
{
selectedSurface = NULL;
}
void ofxSurfaceManagerGui::drawSelectedSurfaceHighlight()
{
if ( selectedSurface == NULL ) return;
ofPolyline line = selectedSurface->getHitArea();
ofPushStyle();
ofSetLineWidth(1);
ofSetColor(255, 255, 255, 255);
line.draw();
ofPopStyle();
}

13
src/ofxSurfaceManagerGui.h

@ -1,7 +1,7 @@
#ifndef H_OFX_SURFACE_MANAGER_GUI
#define H_OFX_SURFACE_MANAGER_GUI
#include "ofMain.h"
#include "ofEvents.h"
#include "ofxSurfaceManager.h"
#include "ofxTextureEditor.h"
#include "ofxProjectionEditor.h"
@ -11,18 +11,25 @@ class ofxSurfaceManagerGui
{
public:
ofxSurfaceManagerGui();
ofxSurfaceManagerGui(ofxSurfaceManager* newSurfaceManager);
~ofxSurfaceManagerGui();
void draw();
void registerMouseEvents();
void unregisterMouseEvents();
void draw();
void mousePressed(ofMouseEventArgs& args);
void setSurfaceManager(ofxSurfaceManager* newSurfaceManager);
void setMode(int newGuiMode);
ofxBaseSurface* selectSurface(int index);
void deselectSurface();
void drawSelectedSurfaceHighlight();
private:
ofxSurfaceManager* surfaceManager;
ofxTextureEditor textureEditor;
ofxProjectionEditor projectionEditor;
int guiMode;
ofxBaseSurface* selectedSurface;
};
#endif

34
src/ofxTextureEditor.cpp

@ -1 +1,33 @@
#include "ofxTextureEditor.h"
#include "ofxTextureEditor.h"
ofxTextureEditor::ofxTextureEditor()
{
clear();
}
ofxTextureEditor::ofxTextureEditor(ofxBaseSurface* newSurface)
{
setSurface(newSurface);
}
ofxTextureEditor::~ofxTextureEditor()
{
clear();
}
void ofxTextureEditor::draw()
{
if ( surface != NULL ) {
surface->drawTexture(ofVec2f(0.0f,0.0f));
}
}
void ofxTextureEditor::setSurface(ofxBaseSurface* newSurface)
{
surface = newSurface;
}
void ofxTextureEditor::clear()
{
surface = NULL;
}

11
src/ofxTextureEditor.h

@ -1,11 +1,22 @@
#ifndef H_OFX_TEXTURE_EDITOR
#define H_OFX_TEXTURE_EDITOR
#include "ofxBaseSurface.h"
class ofxTextureEditor
{
public:
ofxTextureEditor();
ofxTextureEditor(ofxBaseSurface* newSurface);
~ofxTextureEditor();
void draw();
void setSurface(ofxBaseSurface* newSurface);
void clear();
private:
ofxBaseSurface* surface;
};
#endif

23
src/ofxTriangleSurface.cpp

@ -77,6 +77,18 @@ int ofxTriangleSurface::getType()
return ofxSurfaceType::TRIANGLE_SURFACE;
}
bool ofxTriangleSurface::hitTest(ofVec2f p)
{
// Construct ofPolyline from vertices
ofPolyline line = getHitArea();
if ( line.inside(p.x, p.y) ) {
return true;
} else {
return false;
}
}
ofVec2f ofxTriangleSurface::getVertex(int index)
{
if ( index > 2 ) {
@ -95,4 +107,15 @@ ofVec2f ofxTriangleSurface::getTexCoord(int index)
}
return mesh.getTexCoord(index);
}
ofPolyline ofxTriangleSurface::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.close();
return line;
}

3
src/ofxTriangleSurface.h

@ -18,9 +18,10 @@ public:
void setTexCoord( int index, ofVec2f t );
int getType();
bool hitTest(ofVec2f p);
ofVec2f getVertex(int index);
ofVec2f getTexCoord(int index);
ofPolyline getHitArea();
};
#endif
Loading…
Cancel
Save