#endif
#include "Effect.hxx"
+#include "EffectBuilder.hxx"
#include "Technique.hxx"
#include "Pass.hxx"
#include <osgDB/Registry>
#include <simgear/debug/logstream.hxx>
+#include <simgear/scene/model/SGReaderWriterXMLOptions.hxx>
#include <simgear/props/props_io.hxx>
#include <simgear/scene/util/SGSceneFeatures.hxx>
#include <simgear/scene/util/SplicingVisitor.hxx>
Effect* makeEffect(const string& name,
bool realizeTechniques,
- const osgDB::ReaderWriter::Options* options)
+ const SGReaderWriterXMLOptions* options)
{
- OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(effectMutex);
- EffectMap::iterator itr = effectMap.find(name);
- if (itr != effectMap.end())
- return itr->second.get();
+ {
+ OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(effectMutex);
+ EffectMap::iterator itr = effectMap.find(name);
+ if (itr != effectMap.end())
+ return itr->second.get();
+ }
string effectFileName(name);
effectFileName += ".eff";
string absFileName
- = osgDB::findDataFile(effectFileName, options);
+ = SGModelLib::findDataFile(effectFileName, options);
if (absFileName.empty()) {
- SG_LOG(SG_INPUT, SG_WARN, "can't find \"" << effectFileName << "\"");
+ SG_LOG(SG_INPUT, SG_ALERT, "can't find \"" << effectFileName << "\"");
return 0;
}
SGPropertyNode_ptr effectProps = new SGPropertyNode();
- readProperties(absFileName, effectProps.ptr(), 0, true);
- Effect* result = makeEffect(effectProps.ptr(), realizeTechniques, options);
- if (result)
- effectMap.insert(make_pair(name, result));
- return result;
+ try {
+ readProperties(absFileName, effectProps.ptr(), 0, true);
+ }
+ catch (sg_io_exception& e) {
+ SG_LOG(SG_INPUT, SG_ALERT, "error reading \"" << absFileName << "\": "
+ << e.getFormattedMessage());
+ return 0;
+ }
+ ref_ptr<Effect> result = makeEffect(effectProps.ptr(), realizeTechniques,
+ options);
+ if (result.valid()) {
+ OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(effectMutex);
+ pair<EffectMap::iterator, bool> irslt
+ = effectMap.insert(make_pair(name, result));
+ if (!irslt.second) {
+ // Another thread beat us to it!. Discard our newly
+ // constructed Effect and use the one in the cache.
+ result = irslt.first->second;
+ }
+ }
+ return result.release();
}
Effect* makeEffect(SGPropertyNode* prop,
bool realizeTechniques,
- const osgDB::ReaderWriter::Options* options)
+ const SGReaderWriterXMLOptions* options)
{
// Give default names to techniques and passes
vector<SGPropertyNode_ptr> techniques = prop->getChildren("technique");
}
}
}
- Effect* effect = 0;
+ ref_ptr<Effect> effect;
// Merge with the parent effect, if any
SGPropertyNode_ptr inheritProp = prop->getChild("inherits-from");
Effect* parent = 0;
//prop->removeChild("inherits-from");
parent = makeEffect(inheritProp->getStringValue(), false, options);
if (parent) {
- Effect::Cache* cache = parent->getCache();
Effect::Key key;
+ key.unmerged = prop;
if (options) {
- key = Effect::Key(prop, options->getDatabasePathList());
- } else {
- osgDB::FilePathList dummy;
- key = Effect::Key(prop, dummy);
+ key.paths = options->getDatabasePathList();
}
- Effect::Cache::iterator itr = cache->find(key);
- if (itr != cache->end()) {
- effect = itr->second.get();
- } else {
+ Effect::Cache* cache = 0;
+ Effect::Cache::iterator itr;
+ {
+ OpenThreads::ScopedLock<OpenThreads::ReentrantMutex>
+ lock(effectMutex);
+ cache = parent->getCache();
+ itr = cache->find(key);
+ if (itr != cache->end()) {
+ effect = itr->second.get();
+ effect->generator = parent->generator; // Copy the generators
+ }
+ }
+ if (!effect.valid()) {
effect = new Effect;
effect->root = new SGPropertyNode;
mergePropertyTrees(effect->root, prop, parent->root);
- cache->insert(make_pair(key, effect));
+ effect->parametersProp = effect->root->getChild("parameters");
+ OpenThreads::ScopedLock<OpenThreads::ReentrantMutex>
+ lock(effectMutex);
+ pair<Effect::Cache::iterator, bool> irslt
+ = cache->insert(make_pair(key, effect));
+ if (!irslt.second)
+ effect = irslt.first->second;
+ effect->generator = parent->generator; // Copy the generators
}
} else {
- SG_LOG(SG_INPUT, SG_WARN, "can't find base effect " <<
+ SG_LOG(SG_INPUT, SG_ALERT, "can't find base effect " <<
inheritProp->getStringValue());
+ return 0;
}
- }
- if (!effect) {
+ } else {
effect = new Effect;
effect->root = prop;
+ effect->parametersProp = effect->root->getChild("parameters");
+ }
+ const SGPropertyNode *generateProp = prop->getChild("generate");
+ if(generateProp)
+ {
+ effect->generator.clear();
+
+ // Effect needs some generated properties, like tangent vectors
+ const SGPropertyNode *parameter = generateProp->getChild("normal");
+ if(parameter) effect->setGenerator(Effect::NORMAL, parameter->getIntValue());
+
+ parameter = generateProp->getChild("tangent");
+ if(parameter) effect->setGenerator(Effect::TANGENT, parameter->getIntValue());
+
+ parameter = generateProp->getChild("binormal");
+ if(parameter) effect->setGenerator(Effect::BINORMAL, parameter->getIntValue());
+ }
+ if (realizeTechniques) {
+ try {
+ OpenThreads::ScopedLock<OpenThreads::ReentrantMutex>
+ lock(effectMutex);
+ effect->realizeTechniques(options);
+ }
+ catch (BuilderException& e) {
+ SG_LOG(SG_INPUT, SG_ALERT, "Error building technique: "
+ << e.getFormattedMessage());
+ return 0;
+ }
}
- effect->parametersProp = effect->root->getChild("parameters");
- if (realizeTechniques)
- effect->realizeTechniques(options);
- return effect;
+ return effect.release();
}
}