Browse Source

Add surface manager

master
Krisjanis Rijnieks 11 years ago
parent
commit
ea5c83dceb
  1. 3
      README.md
  2. 2
      example/src/main.cpp
  3. 46
      example/src/ofApp.cpp
  4. 6
      example/src/ofApp.h
  5. 2
      src/ofxPiMapper.h
  6. 74
      src/ofxSurfaceGui.cpp
  7. 6
      src/ofxSurfaceGui.h
  8. 142
      src/ofxSurfaceManager.cpp
  9. 42
      src/ofxSurfaceManager.h

3
README.md

@ -38,7 +38,8 @@ Version history
### Version 0.1.2:
TODO:
- Possibility to add multiple surfaces with a kind of layer management utility.
- Ability to select and drag whole surfaces
+ Ability to select and drag whole surfaces
- Select / deselect surfaces
- Example with video source. Maybe one example with different surfaces and sources.
- How to switch between sources?

2
example/src/main.cpp

@ -3,6 +3,6 @@
int main()
{
ofSetupOpenGL(1024, 768, OF_WINDOW);
ofSetupOpenGL(600, 500, OF_WINDOW);
ofRunApp(new ofApp());
}

46
example/src/ofApp.cpp

@ -3,14 +3,9 @@
void ofApp::setup()
{
image.loadImage("TestPatternInvert.jpg");
bShowInfo = false;
triangleSurface.setup(ofVec2f(30,40), ofVec2f(500,500), ofVec2f(0,600),
ofVec2f(0,0), ofVec2f(1,1), ofVec2f(0,1),
&image.getTextureReference());
gui.setup(triangleSurface);
bShowInfo = true;
surfaceManager.addSurface();
}
void ofApp::update()
@ -27,33 +22,12 @@ void ofApp::update()
t.y = ofRandomuf();
//triangleSurface.setTexCoord(0, t);
gui.update();
surfaceManager.update();
}
void ofApp::draw()
{
// Check GUI mode - we want to see the texture that we are editing
// together with the actual surface being projection mapped.
ofxSurfaceGui::editMode mode = gui.getMode();
if ( mode == ofxSurfaceGui::TEXTURE_MAPPING ) {
// Draw texture of the surface in the background
//triangleSurface.getTexture()->draw(ofPoint(0,0));
triangleSurface.drawTexture(ofVec2f(0, 0));
// Make the triangle surface transparent but still visible
// while we map the texture coordinates.
ofPushStyle();
ofSetColor(255, 255, 255, 200);
}
triangleSurface.draw();
if ( mode == ofxSurfaceGui::TEXTURE_MAPPING ) {
ofPopStyle();
}
gui.draw();
surfaceManager.draw();
if ( bShowInfo ) {
// Draw instructions
@ -74,9 +48,9 @@ void ofApp::keyPressed(int key)
cout << "Key pressed: " << static_cast<char>(key) << endl;
switch (key) {
case '1': gui.setMode(ofxSurfaceGui::NONE); break;
case '2': gui.setMode(ofxSurfaceGui::TEXTURE_MAPPING); break;
case '3': gui.setMode(ofxSurfaceGui::PROJECTION_MAPPING); break;
case '1': surfaceManager.setGuiMode(ofxSurfaceGui::NONE); break;
case '2': surfaceManager.setGuiMode(ofxSurfaceGui::TEXTURE_MAPPING); break;
case '3': surfaceManager.setGuiMode(ofxSurfaceGui::PROJECTION_MAPPING); break;
case 'i': bShowInfo = !bShowInfo; break;
default: break;
}
@ -85,17 +59,17 @@ void ofApp::keyPressed(int key)
void ofApp::mousePressed(int x, int y, int button)
{
//cout << "Mouse pressed." << endl;
gui.mousePressed(x, y, button);
surfaceManager.mousePressed(x, y, button);
}
void ofApp::mouseReleased(int x, int y, int button)
{
//cout << "Mouse released." << endl;
gui.mouseReleased(x, y, button);
surfaceManager.mouseReleased(x, y, button);
}
void ofApp::mouseDragged(int x, int y, int button)
{
//
gui.mouseDragged(x, y, button);
surfaceManager.mouseDragged(x, y, button);
}

6
example/src/ofApp.h

@ -4,8 +4,6 @@
#include "ofMain.h"
#include "ofxPiMapper.h"
#include "ofxSurfaceGui.h"
class ofApp : public ofBaseApp
{
public:
@ -18,10 +16,8 @@ public:
void mouseReleased(int x, int y, int button);
void mouseDragged(int x, int y, int button);
ofxTriangleSurface triangleSurface;
ofImage image;
ofxSurfaceGui gui;
ofxSurfaceManager surfaceManager;
bool bShowInfo;
};

2
src/ofxPiMapper.h

@ -1,6 +1,6 @@
#ifndef H_OFX_PI_MAPPER
#define H_OFX_PI_MAPPER
#include "ofxTriangleSurface.h"
#include "ofxSurfaceManager.h"
#endif

74
src/ofxSurfaceGui.cpp

@ -8,6 +8,7 @@ ofxSurfaceGui::ofxSurfaceGui()
bTextureMappingJointSelected = false;
bTextureDragging = false;
bProjectionDragging = false;
bSelected = false;
}
ofxSurfaceGui::~ofxSurfaceGui()
@ -43,20 +44,8 @@ void ofxSurfaceGui::draw()
if (surface == NULL) return;
if (mode == NONE) return;
if (mode == PROJECTION_MAPPING) {
ofPolyline line;
for ( int i=0; i<projectionMappingJoints.size(); i++ ) {
line.addVertex( ofPoint(projectionMappingJoints[i].position.x,
projectionMappingJoints[i].position.y) );
}
line.close();
line.draw();
for ( int i=0; i<projectionMappingJoints.size(); i++ ) {
projectionMappingJoints[i].draw();
}
} else if (mode == TEXTURE_MAPPING) {
// This has to be on bottom, so is drawn first
if (bSelected && mode == TEXTURE_MAPPING) {
ofPolyline line;
for ( int i=0; i<textureMappingJoints.size(); i++ ) {
line.addVertex( ofPoint(textureMappingJoints[i].position.x,
@ -69,12 +58,31 @@ void ofxSurfaceGui::draw()
textureMappingJoints[i].draw();
}
}
// Draw line around projection surface always when selected
if ( bSelected ) {
ofPolyline line;
for ( int i=0; i<projectionMappingJoints.size(); i++ ) {
line.addVertex( ofPoint(projectionMappingJoints[i].position.x,
projectionMappingJoints[i].position.y) );
}
line.close();
line.draw();
}
// Draw projection surface joints
if (bSelected && mode == PROJECTION_MAPPING) {
for ( int i=0; i<projectionMappingJoints.size(); i++ ) {
projectionMappingJoints[i].draw();
}
}
}
void ofxSurfaceGui::mousePressed(int x, int y, int button)
{
if (surface == NULL) return;
if (mode == NONE) return;
if (!bSelected) return;
if (mode == PROJECTION_MAPPING) {
bProjectionMappingJointSelected = false;
@ -140,6 +148,7 @@ void ofxSurfaceGui::mouseDragged(int x, int y, int button)
{
if (surface == NULL) return;
if (mode == NONE) return;
if (!bSelected) return;
if (mode == PROJECTION_MAPPING) {
if ( bProjectionDragging ) {
@ -194,6 +203,34 @@ void ofxSurfaceGui::setMode(ofxSurfaceGui::editMode newMode)
mode = newMode;
}
void ofxSurfaceGui::select()
{
bSelected = true;
}
void ofxSurfaceGui::unselect()
{
bSelected = false;
}
bool ofxSurfaceGui::hitTest(float x, float y)
{
if ( projectionAreaExists() ) {
if ( projectionHitarea.inside(x, y) ) {
return true;
} else {
return false;
}
} else {
return false;
}
}
bool ofxSurfaceGui::isSelected()
{
return bSelected;
}
ofxSurfaceGui::editMode ofxSurfaceGui::getMode()
{
return mode;
@ -270,4 +307,13 @@ bool ofxSurfaceGui::isProjectionMappingJointSelected()
bool ofxSurfaceGui::isTextureMappingJointSelected()
{
return bTextureMappingJointSelected;
}
bool ofxSurfaceGui::projectionAreaExists()
{
if ( projectionHitarea.size() > 2 ) {
return true;
} else {
return false;
}
}

6
src/ofxSurfaceGui.h

@ -24,6 +24,10 @@ public:
void mouseReleased(int x, int y, int button);
void mouseDragged(int x, int y, int button);
void setMode(editMode newMode);
void select();
void unselect();
bool hitTest(float x, float y);
bool isSelected();
editMode getMode();
@ -41,9 +45,11 @@ private:
bool bProjectionMappingJointSelected;
bool bTextureDragging;
bool bProjectionDragging;
bool bSelected;
bool isProjectionMappingJointSelected();
bool isTextureMappingJointSelected();
bool projectionAreaExists();
void addProjectionMappingJoint();
void addNumProjectionMappingJoints(int num);

142
src/ofxSurfaceManager.cpp

@ -0,0 +1,142 @@
#include "ofxSurfaceManager.h"
ofxSurfaceManager::ofxSurfaceManager()
{
}
ofxSurfaceManager::~ofxSurfaceManager()
{
}
void ofxSurfaceManager::setup()
{
}
void ofxSurfaceManager::update()
{
for ( int i=0; i<surfaceGuis.size(); i++ ) {
surfaceGuis[i]->update();
}
}
void ofxSurfaceManager::draw()
{
// Check GUI mode - we want to see the texture that we are editing
// together with the actual surface being projection mapped.
for ( int i=0; i<surfaceGuis.size(); i++ ) {
bool bDrawTexture = false;
if ( surfaceGuis[i]->isSelected() && surfaceGuis[i]->getMode() == ofxSurfaceGui::TEXTURE_MAPPING ) {
bDrawTexture = true;
}
if ( bDrawTexture ) {
// Draw texture of the surface in the background
//triangleSurface.getTexture()->draw(ofPoint(0,0));
triangleSurfaces[i]->drawTexture(ofVec2f(0, 0));
// Make the triangle surface transparent but still visible
// while we map the texture coordinates.
ofPushStyle();
ofSetColor(255, 255, 255, 200);
}
triangleSurfaces[i]->draw();
if ( bDrawTexture ) {
ofPopStyle();
}
surfaceGuis[i]->draw();
}
}
void ofxSurfaceManager::mousePressed(int x, int y, int button)
{
bool bSurfaceSelected = false;
for ( int i=0; i<surfaceGuis.size(); i++ ) {
if ( surfaceGuis[i]->hitTest(x, y) ) {
selectSurface(i);
bSurfaceSelected = true;
}
surfaceGuis[i]->mousePressed(x, y, button);
}
if (!bSurfaceSelected) {
unselectAllSurfaces();
}
}
void ofxSurfaceManager::mouseReleased(int x, int y, int button)
{
for ( int i=0; i<surfaceGuis.size(); i++ ) {
surfaceGuis[i]->mouseReleased(x, y, button);
}
}
void ofxSurfaceManager::mouseDragged(int x, int y, int button)
{
for ( int i=0; i<surfaceGuis.size(); i++ ) {
surfaceGuis[i]->mouseDragged(x, y, button);
}
}
void ofxSurfaceManager::addSurface()
{
addTriangleSurface();
}
void ofxSurfaceManager::removeSurface(int index)
{
if ( index >= surfaceGuis.size() ) {
throw std::runtime_error("Surface index out of bounds.");
return;
}
surfaceGuis.erase( surfaceGuis.begin()+index );
triangleSurfaces.erase( triangleSurfaces.begin()+index );
}
void ofxSurfaceManager::setGuiMode(ofxSurfaceGui::editMode editMode)
{
for ( int i=0; i<surfaceGuis.size(); i++ ) {
surfaceGuis[i]->setMode(editMode);
}
}
void ofxSurfaceManager::selectSurface(int index)
{
if ( index >= surfaceGuis.size() ){
throw std::runtime_error("Surface index out of bounds.");
return;
}
surfaceGuis[index]->select();
}
void ofxSurfaceManager::unselectAllSurfaces()
{
for ( int i=0; i<surfaceGuis.size(); i++ ) {
surfaceGuis[i]->unselect();
}
}
int ofxSurfaceManager::size()
{
return surfaceGuis.size();
}
void ofxSurfaceManager::addTriangleSurface()
{
triangleSurfaces.push_back( &aTriangleSurfaces[triangleSurfaces.size()] );
surfaceGuis.push_back( &aSurfaceGuis[surfaceGuis.size()] );
surfaceGuis.back()->setup( *triangleSurfaces.back() );
}
void ofxSurfaceManager::addTriangleSurface(ofVec2f v1, ofVec2f v2, ofVec2f v3, ofVec2f t1, ofVec2f t2, ofVec2f t3, ofTexture* texturePtr)
{
addTriangleSurface();
triangleSurfaces.back()->setup(v1, v2, v3, t1, t2, t3, texturePtr);
}

42
src/ofxSurfaceManager.h

@ -0,0 +1,42 @@
#ifndef H_OFX_SURFACE_MANAGER
#define H_OFX_SURFACE_MANAGER
#include "ofMain.h"
#include "ofxTriangleSurface.h"
#include "ofxSurfaceGui.h"
#define MAX_SURFACE_COUNT 25
class ofxSurfaceManager
{
public:
ofxSurfaceManager();
~ofxSurfaceManager();
void setup();
void update();
void draw();
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void mouseDragged(int x, int y, int button);
void addSurface();
void removeSurface(int index);
void setGuiMode(ofxSurfaceGui::editMode);
void selectSurface(int index);
void unselectAllSurfaces();
int size();
// TODO: add simple surface
private:
ofxTriangleSurface aTriangleSurfaces[MAX_SURFACE_COUNT];
ofxSurfaceGui aSurfaceGuis[MAX_SURFACE_COUNT];
deque<ofxTriangleSurface*> triangleSurfaces;
deque<ofxSurfaceGui*> surfaceGuis;
void addTriangleSurface();
void addTriangleSurface(ofVec2f v1, ofVec2f v2, ofVec2f v3, ofVec2f t1, ofVec2f t2, ofVec2f t3, ofTexture* texturePtr);
};
#endif
Loading…
Cancel
Save