Browse Source

Merge pull request #2 from sebllll/QuadSurface

Add quad surface support
master
Krisjanis Rijnieks 11 years ago
parent
commit
c362ba39a7
  1. 0
      example-fboTexture/bin/data/defaultSurfaces.xml
  2. 25
      example-fboTexture/src/ofApp.cpp
  3. 1
      example-fboTexture/src/ofApp.h
  4. 0
      src/ofxProjectionEditor.h
  5. 156
      src/ofxQuadSurface.cpp
  6. 34
      src/ofxQuadSurface.h
  7. 231
      src/ofxSurfaceManager.cpp
  8. 1
      src/ofxSurfaceManager.h
  9. 3
      src/ofxSurfaceType.h
  10. 0
      src/ofxTextureEditor.cpp

0
example-fboTexture/bin/data/defaultSurfaces.xml

25
example-fboTexture/src/ofApp.cpp

@ -64,6 +64,7 @@ void ofApp::draw()
ss << " 4. Source selection mode\n\n";
ss << "You can switch between the modes by using <1>, <2>, <3> and <4> keys on the keyboard.\n\n";
ss << "Press <r> or <n> to add random or normal surface.\n";
ss << "Press <q> to add a new quad surface.\n";
ss << "Press <s> to save the composition.\n";
ss << "Press <f> to toggle fullscreen.\n";
ss << "Press <a> to reassign the fbo texture to the first surface\n";
@ -90,6 +91,7 @@ void ofApp::keyPressed(int key)
case '4': gui.setMode(ofxGuiMode::SOURCE_SELECTION); break;
case 'i': bShowInfo = !bShowInfo; break;
case 'r': addRandomSurface(); break;
case 'q': addQuadSurface(); break;
case 'n': addSurface(); break;
case 'f': ofToggleFullscreen(); break;
case 's': surfaceManager.saveXmlSettings("surfaces.xml"); break;
@ -116,6 +118,29 @@ void ofApp::addRandomSurface()
surfaceManager.selectSurface(surfaceManager.size()-1);
}
void ofApp::addQuadSurface()
{
int surfaceType = ofxSurfaceType::QUAD_SURFACE;
vector<ofVec2f> vertices;
int border = 50;
vertices.push_back( ofVec2f(border, border) );
vertices.push_back( ofVec2f(border, ofGetHeight() - border) );
vertices.push_back( ofVec2f(ofGetWidth() - border, ofGetHeight() - border) );
vertices.push_back( ofVec2f(ofGetWidth() - border, border) );
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)) );
surfaceManager.addSurface(surfaceType, vertices, texCoords);
// select this surface right away
surfaceManager.selectSurface(surfaceManager.size()-1);
}
void ofApp::addSurface()
{
int surfaceType = ofxSurfaceType::TRIANGLE_SURFACE;

1
example-fboTexture/src/ofApp.h

@ -15,6 +15,7 @@ public:
void keyPressed(int key);
void addRandomSurface();
void addQuadSurface();
void addSurface();
void setFboAsTexture();

0
src/ofxProjectionEditor.h

156
src/ofxQuadSurface.cpp

@ -0,0 +1,156 @@
#include "ofxQuadSurface.h"
ofxQuadSurface::ofxQuadSurface()
{
cout << "ofxQuadSurface constructor." << endl;
setup();
}
ofxQuadSurface::~ofxQuadSurface()
{
cout << "ofxQuadSurface destructor." << endl;
}
void ofxQuadSurface::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, texture );
}
void ofxQuadSurface::setup( ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f p4,
ofVec2f t1, ofVec2f t2, ofVec2f t3, ofVec2f t4, ofTexture* texturePtr )
{
// Assign texture
texture = texturePtr;
// 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 ofxQuadSurface::draw()
{
texture->bind();
mesh.draw();
texture->unbind();
}
void ofxQuadSurface::setVertex(int index, ofVec2f p)
{
if ( index > 3 ) {
ofLog() << "Vertex with this index does not exist: " << index << endl;
return;
}
mesh.setVertex(index, p);
}
void ofxQuadSurface::setTexCoord(int index, ofVec2f t)
{
if ( index > 3 ) {
ofLog() << "Texture coordinate with this index does not exist: " << index << endl;
return;
}
mesh.setTexCoord(index, t);
}
int ofxQuadSurface::getType()
{
return ofxSurfaceType::QUAD_SURFACE;
}
bool ofxQuadSurface::hitTest(ofVec2f p)
{
// Construct ofPolyline from vertices
ofPolyline line = getHitArea();
if ( line.inside(p.x, p.y) ) {
return true;
} else {
return false;
}
}
ofVec2f ofxQuadSurface::getVertex(int index)
{
if ( index > 3 ) {
ofLog() << "Vertex with this index does not exist: " << index << endl;
throw std::runtime_error("Vertex index out of bounds.");
}
ofVec3f vert = mesh.getVertex(index);
return ofVec2f(vert.x, vert.y);
}
ofVec2f ofxQuadSurface::getTexCoord(int index)
{
if (index > 3) {
throw std::runtime_error("Texture coordinate index out of bounds.");
}
return mesh.getTexCoord(index);
}
ofPolyline ofxQuadSurface::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 ofxQuadSurface::getTextureHitArea()
{
ofPolyline line;
vector<ofVec2f>& texCoords = mesh.getTexCoords();
ofVec2f textureSize = ofVec2f(texture->getWidth(), texture->getHeight());
for ( int i=0; i<texCoords.size(); i++ ) {
line.addVertex( ofPoint( texCoords[i] * textureSize ) );
}
line.close();
return line;
}
vector<ofVec3f>& ofxQuadSurface::getVertices()
{
// return only joint vertices
return mesh.getVertices();
}
vector<ofVec2f>& ofxQuadSurface::getTexCoords()
{
return mesh.getTexCoords();
}

34
src/ofxQuadSurface.h

@ -0,0 +1,34 @@
#ifndef H_OFX_QUAD_SURFACE
#define H_OFX_QUAD_SURFACE
#include "ofMain.h"
#include "ofxBaseSurface.h"
#include "ofxSurfaceType.h"
class ofxQuadSurface : public ofxBaseSurface
{
public:
ofxQuadSurface();
~ofxQuadSurface();
void setup();
void setup( ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f p4,
ofVec2f t1, ofVec2f t2, ofVec2f t3, ofVec2f t4,
ofTexture* texturePtr );
void draw();
void setVertex( int index, ofVec2f p );
void setTexCoord( int index, ofVec2f t );
int getType();
bool hitTest(ofVec2f p);
ofVec2f getVertex(int index);
ofVec2f getTexCoord(int index);
ofPolyline getHitArea();
ofPolyline getTextureHitArea();
vector<ofVec3f>& getVertices();
vector<ofVec2f>& getTexCoords();
};
#endif

231
src/ofxSurfaceManager.cpp

@ -21,7 +21,11 @@ void ofxSurfaceManager::addSurface(int surfaceType)
{
if ( surfaceType == ofxSurfaceType::TRIANGLE_SURFACE ) {
surfaces.push_back( new ofxTriangleSurface() );
} else {
}
else if (surfaceType == ofxSurfaceType::QUAD_SURFACE ) {
surfaces.push_back( new ofxQuadSurface() );
}
else {
throw std::runtime_error("Attempt to add non-existing surface type.");
}
}
@ -31,7 +35,12 @@ void ofxSurfaceManager::addSurface(int surfaceType, ofTexture* texturePtr)
if ( surfaceType == ofxSurfaceType::TRIANGLE_SURFACE ) {
surfaces.push_back( new ofxTriangleSurface() );
surfaces.back()->setTexture(texturePtr);
} else {
}
else if (surfaceType == ofxSurfaceType::QUAD_SURFACE ) {
surfaces.push_back( new ofxQuadSurface() );
surfaces.back()->setTexture(texturePtr);
}
else {
throw std::runtime_error("Attempt to add non-existing surface type.");
}
}
@ -43,7 +52,7 @@ void ofxSurfaceManager::addSurface(int surfaceType, vector<ofVec2f> vertices, ve
if ( vertices.size() < 3 ) {
throw std::runtime_error("There must be 3 vertices for a triangle surface.");
} else if (texCoords.size() < 3) {
throw std::runtime_error("Thre must be 3 texture coordinates for a triangle surface.");
throw std::runtime_error("There must be 3 texture coordinates for a triangle surface.");
}
surfaces.push_back( new ofxTriangleSurface() );
@ -53,7 +62,22 @@ void ofxSurfaceManager::addSurface(int surfaceType, vector<ofVec2f> vertices, ve
surfaces.back()->setTexCoord(i, texCoords[i]);
}
} else {
}
else if (surfaceType == ofxSurfaceType::QUAD_SURFACE ) {
if ( vertices.size() < 4 ) {
throw std::runtime_error("There must be 4 vertices for a quad surface.");
} else if (texCoords.size() < 4) {
throw std::runtime_error("There must be 4 texture coordinates for a quad surface.");
}
surfaces.push_back( new ofxQuadSurface() );
for ( int i=0; i<4; i++ ) {
surfaces.back()->setVertex(i, vertices[i]);
surfaces.back()->setTexCoord(i, texCoords[i]);
}
}
else {
throw std::runtime_error("Attempt to add non-existing surface type.");
}
@ -77,7 +101,23 @@ void ofxSurfaceManager::addSurface(int surfaceType, ofTexture* texturePtr, vecto
surfaces.back()->setTexCoord(i, texCoords[i]);
}
} else {
}
else if (surfaceType == ofxSurfaceType::QUAD_SURFACE ) {
if ( vertices.size() < 4 ) {
throw std::runtime_error("There must be 4 vertices for a quad surface.");
} else if (texCoords.size() < 4) {
throw std::runtime_error("Thre must be 4 texture coordinates for a quad surface.");
}
surfaces.push_back( new ofxQuadSurface() );
surfaces.back()->setTexture(texturePtr);
for ( int i=0; i<4; i++ ) {
surfaces.back()->setVertex(i, vertices[i]);
surfaces.back()->setTexCoord(i, texCoords[i]);
}
}
else {
throw std::runtime_error("Attempt to add non-existing surface type.");
}
}
@ -138,6 +178,16 @@ void ofxSurfaceManager::clear()
}
}
// String getTypeString(ofxSurfaceType e)
// {
// switch e
// {
// case TRINAGLE_SURFACE: return "TRINAGLE_SURFACE";
// case QUAD_SURFACE: return "QUAD_SURFACE";
// default: throw Exception("Bad MyEnum");
// }
// }
void ofxSurfaceManager::saveXmlSettings(string fileName)
{
xmlSettings.clear();
@ -150,7 +200,7 @@ void ofxSurfaceManager::saveXmlSettings(string fileName)
xmlSettings.addTag("surface");
xmlSettings.pushTag("surface", i);
ofxBaseSurface* surface = surfaces[i];
xmlSettings.addTag("vertices");
xmlSettings.pushTag("vertices");
vector<ofVec3f>* vertices = &surface->getVertices();
@ -186,9 +236,16 @@ void ofxSurfaceManager::saveXmlSettings(string fileName)
xmlSettings.addValue("source-type", "image");
xmlSettings.addValue("source-name", getSurfaceSourceName(surface));
//xmlSettings.addValue("source-path", "/root/etc/image.jpg");
xmlSettings.popTag(); // source
// xmlSettings.addTag("type");
// xmlSettings.pushTag("type");
// // surfaceType == ofxSurfaceType::TRIANGLE_SURFACE
// ofxSurfaceType surfaceType = &surface->getType();
// xmlSettings.addValue("surface-type", surfaceType);
// xmlSettings.popTag(); // type
xmlSettings.popTag(); // surface
}
xmlSettings.popTag(); // surfaces
@ -198,6 +255,8 @@ void ofxSurfaceManager::saveXmlSettings(string fileName)
void ofxSurfaceManager::loadXmlSettings(string fileName)
{
if (!xmlSettings.loadFile(fileName)){
ofLog(OF_LOG_WARNING, "Could not load XML settings.");
return;
@ -213,7 +272,6 @@ void ofxSurfaceManager::loadXmlSettings(string fileName)
int numSurfaces = xmlSettings.getNumTags("surface");
for ( int i=0; i<numSurfaces; i++ ) {
xmlSettings.pushTag("surface", i);
// attempt to load surface source
xmlSettings.pushTag("source");
string sourceType = xmlSettings.getValue("source-type", "image");
@ -225,54 +283,121 @@ void ofxSurfaceManager::loadXmlSettings(string fileName)
sourceTexture = loadImageSource(sourceName, ss.str());
}
xmlSettings.popTag(); // source
// get vertices (only for triangle surface for now)
// // attempt to load surface type
// ofLog(OF_LOG_WARNING, "Attempt to load surface type.");
// xmlSettings.pushTag("type");
// string surfaceType = xmlSettings.getValue("surface-type", "TRIANGLE_SURFACE");
// xmlSettings.popTag(); // type
xmlSettings.pushTag("vertices");
vector<ofVec2f> vertices;
int vertexCount = xmlSettings.getNumTags("vertex");
xmlSettings.pushTag("vertex", 0);
vertices.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 0.0f) ) );
xmlSettings.popTag();
xmlSettings.pushTag("vertex", 1);
vertices.push_back( ofVec2f( xmlSettings.getValue("x", 100.0f), xmlSettings.getValue("y", 0.0f) ) );
xmlSettings.popTag();
xmlSettings.pushTag("vertex", 2);
vertices.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 100.0f) ) );
xmlSettings.popTag();
xmlSettings.popTag(); // vertices
// get texture coordinates (only for triangle surfaces for now)
xmlSettings.pushTag("texCoords");
vector<ofVec2f> texCoords;
xmlSettings.pushTag("texCoord", 0);
texCoords.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 0.0f) ) );
xmlSettings.popTag();
xmlSettings.pushTag("texCoord", 1);
texCoords.push_back( ofVec2f( xmlSettings.getValue("x", 1.0f), xmlSettings.getValue("y", 0.0f) ) );
xmlSettings.popTag();
xmlSettings.pushTag("texCoord", 2);
texCoords.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 1.0f) ) );
xmlSettings.popTag();
xmlSettings.popTag(); // texCoords
// now we have variables sourceName and sourceTexture
// by checking those we can use one or another addSurface method
if ( sourceName != "none" && sourceTexture != NULL ) {
addSurface(ofxSurfaceType::TRIANGLE_SURFACE, sourceTexture, vertices, texCoords);
} else {
addSurface(ofxSurfaceType::TRIANGLE_SURFACE, vertices, texCoords);
//it's a triangle ?
if (vertexCount == 3)
{
ofLog(OF_LOG_NOTICE, "create Triangle");
xmlSettings.pushTag("vertex", 0);
vertices.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 0.0f) ) );
xmlSettings.popTag();
xmlSettings.pushTag("vertex", 1);
vertices.push_back( ofVec2f( xmlSettings.getValue("x", 100.0f), xmlSettings.getValue("y", 0.0f) ) );
xmlSettings.popTag();
xmlSettings.pushTag("vertex", 2);
vertices.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 100.0f) ) );
xmlSettings.popTag();
xmlSettings.popTag(); // vertices
xmlSettings.pushTag("texCoords");
vector<ofVec2f> texCoords;
xmlSettings.pushTag("texCoord", 0);
texCoords.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 0.0f) ) );
xmlSettings.popTag();
xmlSettings.pushTag("texCoord", 1);
texCoords.push_back( ofVec2f( xmlSettings.getValue("x", 1.0f), xmlSettings.getValue("y", 0.0f) ) );
xmlSettings.popTag();
xmlSettings.pushTag("texCoord", 2);
texCoords.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 1.0f) ) );
xmlSettings.popTag();
xmlSettings.popTag(); // texCoords
// now we have variables sourceName and sourceTexture
// by checking those we can use one or another addSurface method
if ( sourceName != "none" && sourceTexture != NULL ) {
addSurface(ofxSurfaceType::TRIANGLE_SURFACE, sourceTexture, vertices, texCoords);
} else {
addSurface(ofxSurfaceType::TRIANGLE_SURFACE, vertices, texCoords);
}
}
// it's a quad ?
else if (vertexCount == 4)
// if (surface-type == QUAD_SURFACE)
{
xmlSettings.pushTag("vertex", 0);
vertices.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 0.0f) ) );
xmlSettings.popTag();
xmlSettings.pushTag("vertex", 1);
vertices.push_back( ofVec2f( xmlSettings.getValue("x", 100.0f), xmlSettings.getValue("y", 0.0f) ) );
xmlSettings.popTag();
xmlSettings.pushTag("vertex", 2);
vertices.push_back( ofVec2f( xmlSettings.getValue("x", 100.0f), xmlSettings.getValue("y", 100.0f) ) );
xmlSettings.popTag();
xmlSettings.pushTag("vertex", 3);
vertices.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 100.0f) ) );
xmlSettings.popTag();
xmlSettings.popTag(); // vertices
xmlSettings.pushTag("texCoords");
vector<ofVec2f> texCoords;
xmlSettings.pushTag("texCoord", 0);
texCoords.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 0.0f) ) );
xmlSettings.popTag();
xmlSettings.pushTag("texCoord", 1);
texCoords.push_back( ofVec2f( xmlSettings.getValue("x", 1.0f), xmlSettings.getValue("y", 0.0f) ) );
xmlSettings.popTag();
xmlSettings.pushTag("texCoord", 2);
texCoords.push_back( ofVec2f( xmlSettings.getValue("x", 1.0f), xmlSettings.getValue("y", 1.0f) ) );
xmlSettings.popTag();
xmlSettings.pushTag("texCoord", 3);
texCoords.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 1.0f) ) );
xmlSettings.popTag();
xmlSettings.popTag(); // texCoords
// now we have variables sourceName and sourceTexture
// by checking those we can use one or another addSurface method
if ( sourceName != "none" && sourceTexture != NULL ) {
addSurface(ofxSurfaceType::QUAD_SURFACE, sourceTexture, vertices, texCoords);
} else {
addSurface(ofxSurfaceType::QUAD_SURFACE, vertices, texCoords);
}
}
xmlSettings.popTag(); // surface
}

1
src/ofxSurfaceManager.h

@ -3,6 +3,7 @@
#include "ofxBaseSurface.h"
#include "ofxTriangleSurface.h"
#include "ofxQuadSurface.h"
#include "ofxSurfaceType.h"
#include "ofEvents.h"
#include "ofxXmlSettings.h"

3
src/ofxSurfaceType.h

@ -4,7 +4,8 @@
struct ofxSurfaceType
{
enum {
TRIANGLE_SURFACE
TRIANGLE_SURFACE,
QUAD_SURFACE
};
};

0
src/ofxTextureEditor.cpp

Loading…
Cancel
Save