Browse Source

Add loading surface data from surfaces.xml

master
Krisjanis Rijnieks 11 years ago
parent
commit
0a75947b66
  1. 2
      example/bin/.gitignore
  2. 104
      example/bin/data/surfaces.xml
  3. 4
      example/src/ofApp.cpp
  4. 82
      src/ofxSurfaceManager.cpp

2
example/bin/.gitignore

@ -1,3 +1,3 @@
*.app
data/*.jpg
data/*.xml
data/settings.xml

104
example/bin/data/surfaces.xml

@ -0,0 +1,104 @@
<surfaces>
<surface>
<vertices>
<vertex>
<x>300.000000000</x>
<y>0.000000000</y>
</vertex>
<vertex>
<x>0.000000000</x>
<y>500.000000000</y>
</vertex>
<vertex>
<x>600.000000000</x>
<y>500.000000000</y>
</vertex>
</vertices>
<texCoords>
<texCoord>
<x>0.500000000</x>
<y>0.000000000</y>
</texCoord>
<texCoord>
<x>0.000000000</x>
<y>1.000000000</y>
</texCoord>
<texCoord>
<x>1.000000000</x>
<y>1.000000000</y>
</texCoord>
</texCoords>
<source>
<source-type>image</source-type>
<source-name>none</source-name>
</source>
</surface>
<surface>
<vertices>
<vertex>
<x>24.000000000</x>
<y>33.000000000</y>
</vertex>
<vertex>
<x>566.000000000</x>
<y>35.000000000</y>
</vertex>
<vertex>
<x>207.000000000</x>
<y>310.000000000</y>
</vertex>
</vertices>
<texCoords>
<texCoord>
<x>0.500000000</x>
<y>0.000000000</y>
</texCoord>
<texCoord>
<x>0.000000000</x>
<y>1.000000000</y>
</texCoord>
<texCoord>
<x>1.000000000</x>
<y>1.000000000</y>
</texCoord>
</texCoords>
<source>
<source-type>image</source-type>
<source-name>image5.jpg</source-name>
</source>
</surface>
<surface>
<vertices>
<vertex>
<x>74.709846497</x>
<y>448.636596680</y>
</vertex>
<vertex>
<x>564.630920410</x>
<y>201.926849365</y>
</vertex>
<vertex>
<x>23.396121979</x>
<y>257.170288086</y>
</vertex>
</vertices>
<texCoords>
<texCoord>
<x>0.981523871</x>
<y>0.471785098</y>
</texCoord>
<texCoord>
<x>0.245601788</x>
<y>0.110941604</y>
</texCoord>
<texCoord>
<x>0.662217021</x>
<y>0.714698017</y>
</texCoord>
</texCoords>
<source>
<source-type>image</source-type>
<source-name>image4.jpg</source-name>
</source>
</surface>
</surfaces>

4
example/src/ofApp.cpp

@ -5,12 +5,16 @@ void ofApp::setup()
image.loadImage("TestPatternInvert.jpg");
bShowInfo = false;
/*
surfaceManager.addSurface( ofxSurfaceType::TRIANGLE_SURFACE );
surfaceManager.addSurface( ofxSurfaceType::TRIANGLE_SURFACE );
surfaceManager.getSurface(1)->setVertex(0, ofVec2f(10, 10));
surfaceManager.getSurface(1)->setVertex(1, ofVec2f(400, 20));
surfaceManager.getSurface(1)->setVertex(2, ofVec2f(300, 400));
*/
surfaceManager.loadXmlSettings("surfaces.xml");
gui.setSurfaceManager( &surfaceManager );
}

82
src/ofxSurfaceManager.cpp

@ -184,7 +184,85 @@ void ofxSurfaceManager::saveXmlSettings(string fileName)
void ofxSurfaceManager::loadXmlSettings(string fileName)
{
if (!xmlSettings.loadFile(fileName)){
ofLog(OF_LOG_WARNING, "Could not load XML settings.");
return;
}
if (!xmlSettings.tagExists("surfaces")){
ofLog(OF_LOG_WARNING, "XML settings is empty or has wrong markup.");
return;
}
xmlSettings.pushTag("surfaces");
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");
string sourceName = xmlSettings.getValue("source-name", "none");
ofTexture* sourceTexture = NULL;
if ( sourceName != "none" ) {
stringstream ss;
ss << "sources/images/" << sourceName; // TODO: reuse constants here
sourceTexture = loadImageSource(sourceName, ss.str());
}
xmlSettings.popTag(); // source
// get vertices (only for triangle surface for now)
xmlSettings.pushTag("vertices");
vector<ofVec2f> vertices;
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);
}
xmlSettings.popTag(); // surface
}
xmlSettings.popTag(); // surfaces
}
ofxBaseSurface* ofxSurfaceManager::selectSurface(int index)
@ -218,7 +296,9 @@ ofTexture* ofxSurfaceManager::loadImageSource(string name, string path)
// not loaded - load
ofImage* image = new ofImage();
image->loadImage(path);
if ( !image->loadImage(path) ){
return NULL;
}
loadedImageSources.push_back(image);
loadedImageSourceNames.push_back(name);
return &image->getTextureReference();

Loading…
Cancel
Save