Browse Source

edit ofxSurfaceManager.cpp

new file:   src/ofxQuadSurface.cpp

	new file:   src/ofxQuadSurface.h
	src/ofxQuadSurface.cpp
master
sebl 11 years ago
parent
commit
6099c436bf
  1. 156
      src/ofxQuadSurface.cpp
  2. 34
      src/ofxQuadSurface.h
  3. 23
      src/ofxSurfaceManager.cpp

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

23
src/ofxSurfaceManager.cpp

@ -197,9 +197,10 @@ void ofxSurfaceManager::saveXmlSettings(string fileName)
xmlSettings.pushTag("surfaces");
for ( int i=0; i<surfaces.size(); i++ ) {
xmlSettings.addTag("surface");
xmlSettings.addTag("surface");
xmlSettings.pushTag("surface", i);
ofxBaseSurface* surface = surfaces[i];
xmlSettings.addTag("vertices");
xmlSettings.pushTag("vertices");
vector<ofVec3f>* vertices = &surface->getVertices();
@ -254,7 +255,7 @@ void ofxSurfaceManager::saveXmlSettings(string fileName)
void ofxSurfaceManager::loadXmlSettings(string fileName)
{
ofLog(OF_LOG_WARNING, "A load XML settings...");
if (!xmlSettings.loadFile(fileName)){
ofLog(OF_LOG_WARNING, "Could not load XML settings.");
@ -271,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");
@ -290,18 +290,17 @@ void ofxSurfaceManager::loadXmlSettings(string fileName)
// string surfaceType = xmlSettings.getValue("surface-type", "TRIANGLE_SURFACE");
// xmlSettings.popTag(); // type
// get vertices (only for triangle surface for now)
xmlSettings.pushTag("vertices");
vector<ofVec2f> vertices;
int vertexCount = vertices.size();
// int vertexCount = vertices->size()
int vertexCount = xmlSettings.getNumTags("vertex");
//it's a triangle ?
if (vertexCount == 3)
// if (surfaceType == TRIANGLE_SURFACE)
{
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();
@ -342,15 +341,11 @@ void ofxSurfaceManager::loadXmlSettings(string fileName)
} else {
addSurface(ofxSurfaceType::TRIANGLE_SURFACE, vertices, texCoords);
}
xmlSettings.popTag(); // surface
}
// it's a quad ?
else if (vertexCount == 4)
// if (surface-type == QUAD_SURFACE)
{
ofSendMessage("create Quad");
xmlSettings.pushTag("vertex", 0);
vertices.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 0.0f) ) );
xmlSettings.popTag();
@ -400,8 +395,10 @@ void ofxSurfaceManager::loadXmlSettings(string fileName)
addSurface(ofxSurfaceType::QUAD_SURFACE, vertices, texCoords);
}
xmlSettings.popTag(); // surface
}
xmlSettings.popTag(); // surface
}
xmlSettings.popTag(); // surfaces

Loading…
Cancel
Save