#include <iterator>
#include <map>
#include <utility>
+#include <boost/tr1/unordered_map.hpp>
+#include <boost/bind.hpp>
#include <boost/foreach.hpp>
+#include <boost/functional/hash.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_comparison.hpp>
#include <osg/AlphaFunc>
#include <osg/BlendFunc>
#include <osg/CullFace>
+#include <osg/Depth>
#include <osg/Drawable>
#include <osg/Material>
#include <osg/Math>
#include <osg/RenderInfo>
#include <osg/ShadeModel>
#include <osg/StateSet>
+#include <osg/Stencil>
#include <osg/TexEnv>
#include <osg/Texture1D>
#include <osg/Texture2D>
#include <osgDB/ReadFile>
#include <osgDB/Registry>
+#include <simgear/scene/model/SGReaderWriterXMLOptions.hxx>
#include <simgear/scene/tgdb/userdata.hxx>
#include <simgear/scene/util/SGSceneFeatures.hxx>
#include <simgear/scene/util/StateAttributeFactory.hxx>
}
void buildPass(Effect* effect, Technique* tniq, const SGPropertyNode* prop,
- const osgDB::ReaderWriter::Options* options)
+ const SGReaderWriterXMLOptions* options)
{
Pass* pass = new Pass;
tniq->passes.push_back(pass);
struct LightingBuilder : public PassAttributeBuilder
{
void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
- const osgDB::ReaderWriter::Options* options);
+ const SGReaderWriterXMLOptions* options);
};
void LightingBuilder::buildAttribute(Effect* effect, Pass* pass,
const SGPropertyNode* prop,
- const osgDB::ReaderWriter::Options* options)
+ const SGReaderWriterXMLOptions* options)
{
const SGPropertyNode* realProp = getEffectPropertyNode(effect, prop);
if (!realProp)
struct ShadeModelBuilder : public PassAttributeBuilder
{
void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
- const osgDB::ReaderWriter::Options* options)
+ const SGReaderWriterXMLOptions* options)
{
const SGPropertyNode* realProp = getEffectPropertyNode(effect, prop);
if (!realProp)
struct CullFaceBuilder : PassAttributeBuilder
{
void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
- const osgDB::ReaderWriter::Options* options)
+ const SGReaderWriterXMLOptions* options)
{
const SGPropertyNode* realProp = getEffectPropertyNode(effect, prop);
if (!realProp) {
InstallAttributeBuilder<CullFaceBuilder> installCullFace("cull-face");
+struct ColorMaskBuilder : PassAttributeBuilder
+{
+ void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
+ const SGReaderWriterXMLOptions* options)
+ {
+ const SGPropertyNode* realProp = getEffectPropertyNode(effect, prop);
+ if (!realProp)
+ return;
+
+ ColorMask *mask = new ColorMask;
+ Vec4 m = getColor(realProp);
+ mask->setMask(m.r(), m.g(), m.b(), m.a());
+ pass->setAttributeAndModes(mask);
+ }
+};
+
+InstallAttributeBuilder<ColorMaskBuilder> installColorMask("color-mask");
+
EffectNameValue<StateSet::RenderingHint> renderingHintInit[] =
{
{ "default", StateSet::DEFAULT_BIN },
struct HintBuilder : public PassAttributeBuilder
{
void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
- const osgDB::ReaderWriter::Options* options)
+ const SGReaderWriterXMLOptions* options)
{
const SGPropertyNode* realProp = getEffectPropertyNode(effect, prop);
if (!realProp)
struct RenderBinBuilder : public PassAttributeBuilder
{
void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
- const osgDB::ReaderWriter::Options* options)
+ const SGReaderWriterXMLOptions* options)
{
if (!isAttributeActive(effect, prop))
return;
struct MaterialBuilder : public PassAttributeBuilder
{
void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
- const osgDB::ReaderWriter::Options* options);
+ const SGReaderWriterXMLOptions* options);
};
EffectNameValue<Material::ColorMode> colorModeInit[] =
void MaterialBuilder::buildAttribute(Effect* effect, Pass* pass,
const SGPropertyNode* prop,
- const osgDB::ReaderWriter::Options* options)
+ const SGReaderWriterXMLOptions* options)
{
if (!isAttributeActive(effect, prop))
return;
struct BlendBuilder : public PassAttributeBuilder
{
void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
- const osgDB::ReaderWriter::Options* options)
+ const SGReaderWriterXMLOptions* options)
{
if (!isAttributeActive(effect, prop))
return;
InstallAttributeBuilder<BlendBuilder> installBlend("blend");
+
+EffectNameValue<Stencil::Function> stencilFunctionInit[] =
+{
+ {"never", Stencil::NEVER },
+ {"less", Stencil::LESS},
+ {"equal", Stencil::EQUAL},
+ {"less-or-equal", Stencil::LEQUAL},
+ {"greater", Stencil::GREATER},
+ {"not-equal", Stencil::NOTEQUAL},
+ {"greater-or-equal", Stencil::GEQUAL},
+ {"always", Stencil::ALWAYS}
+};
+
+EffectPropertyMap<Stencil::Function> stencilFunction(stencilFunctionInit);
+
+EffectNameValue<Stencil::Operation> stencilOperationInit[] =
+{
+ {"keep", Stencil::KEEP},
+ {"zero", Stencil::ZERO},
+ {"replace", Stencil::REPLACE},
+ {"increase", Stencil::INCR},
+ {"decrease", Stencil::DECR},
+ {"invert", Stencil::INVERT},
+ {"increase-wrap", Stencil::INCR_WRAP},
+ {"decrease-wrap", Stencil::DECR_WRAP}
+};
+
+EffectPropertyMap<Stencil::Operation> stencilOperation(stencilOperationInit);
+
+struct StencilBuilder : public PassAttributeBuilder
+{
+ void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
+ const SGReaderWriterXMLOptions* options)
+ {
+ if (!isAttributeActive(effect, prop))
+ return;
+
+ const SGPropertyNode* pmode = getEffectPropertyChild(effect, prop,
+ "mode");
+ if (pmode && !pmode->getValue<bool>()) {
+ pass->setMode(GL_STENCIL, StateAttribute::OFF);
+ return;
+ }
+ const SGPropertyNode* pfunction
+ = getEffectPropertyChild(effect, prop, "function");
+ const SGPropertyNode* pvalue
+ = getEffectPropertyChild(effect, prop, "value");
+ const SGPropertyNode* pmask
+ = getEffectPropertyChild(effect, prop, "mask");
+ const SGPropertyNode* psfail
+ = getEffectPropertyChild(effect, prop, "stencil-fail");
+ const SGPropertyNode* pzfail
+ = getEffectPropertyChild(effect, prop, "z-fail");
+ const SGPropertyNode* ppass
+ = getEffectPropertyChild(effect, prop, "pass");
+
+ Stencil::Function func = Stencil::ALWAYS; // Always pass
+ int ref = 0;
+ unsigned int mask = ~0u; // All bits on
+ Stencil::Operation sfailop = Stencil::KEEP; // Keep the old values as default
+ Stencil::Operation zfailop = Stencil::KEEP;
+ Stencil::Operation passop = Stencil::KEEP;
+
+ ref_ptr<Stencil> stencilFunc = new Stencil;
+
+ if (pfunction)
+ findAttr(stencilFunction, pfunction, func);
+ if (pvalue)
+ ref = pvalue->getIntValue();
+ if (pmask)
+ mask = pmask->getIntValue();
+
+ if (psfail)
+ findAttr(stencilOperation, psfail, sfailop);
+ if (pzfail)
+ findAttr(stencilOperation, pzfail, zfailop);
+ if (ppass)
+ findAttr(stencilOperation, ppass, passop);
+
+ // Set the stencil operation
+ stencilFunc->setFunction(func, ref, mask);
+
+ // Set the operation, s-fail, s-pass/z-fail, s-pass/z-pass
+ stencilFunc->setOperation(sfailop, zfailop, passop);
+
+ // Add the operation to pass
+ pass->setAttributeAndModes(stencilFunc.get());
+ }
+};
+
+InstallAttributeBuilder<StencilBuilder> installStencil("stencil");
+
+
EffectNameValue<AlphaFunc::ComparisonFunction> alphaComparisonInit[] =
{
{"never", AlphaFunc::NEVER},
struct AlphaTestBuilder : public PassAttributeBuilder
{
void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
- const osgDB::ReaderWriter::Options* options)
+ const SGReaderWriterXMLOptions* options)
{
if (!isAttributeActive(effect, prop))
return;
InstallAttributeBuilder<TextureUnitBuilder> textureUnitBuilder("texture-unit");
-typedef map<string, ref_ptr<Program> > ProgramMap;
+// Shader key, used both for shaders with relative and absolute names
+typedef pair<string, Shader::Type> ShaderKey;
+
+struct ProgramKey
+{
+ typedef pair<string, int> AttribKey;
+ osgDB::FilePathList paths;
+ vector<ShaderKey> shaders;
+ vector<AttribKey> attributes;
+ struct EqualTo
+ {
+ bool operator()(const ProgramKey& lhs, const ProgramKey& rhs) const
+ {
+ return (lhs.paths.size() == rhs.paths.size()
+ && equal(lhs.paths.begin(), lhs.paths.end(),
+ rhs.paths.begin())
+ && lhs.shaders.size() == rhs.shaders.size()
+ && equal (lhs.shaders.begin(), lhs.shaders.end(),
+ rhs.shaders.begin())
+ && lhs.attributes.size() == rhs.attributes.size()
+ && equal(lhs.attributes.begin(), lhs.attributes.end(),
+ rhs.attributes.begin()));
+ }
+ };
+};
+
+size_t hash_value(const ProgramKey& key)
+{
+ size_t seed = 0;
+ boost::hash_range(seed, key.paths.begin(), key.paths.end());
+ boost::hash_range(seed, key.shaders.begin(), key.shaders.end());
+ boost::hash_range(seed, key.attributes.begin(), key.attributes.end());
+ return seed;
+}
+
+// XXX Should these be protected by a mutex? Probably
+
+typedef tr1::unordered_map<ProgramKey, ref_ptr<Program>,
+ boost::hash<ProgramKey>, ProgramKey::EqualTo>
+ProgramMap;
ProgramMap programMap;
-typedef map<string, ref_ptr<Shader> > ShaderMap;
+typedef tr1::unordered_map<ShaderKey, ref_ptr<Shader>, boost::hash<ShaderKey> >
+ShaderMap;
ShaderMap shaderMap;
void reload_shaders()
for(ShaderMap::iterator sitr = shaderMap.begin(); sitr != shaderMap.end(); ++sitr)
{
Shader *shader = sitr->second.get();
- string fileName = osgDB::findDataFile(sitr->first);
+ string fileName = osgDB::findDataFile(sitr->first.first);
if (!fileName.empty()) {
shader->loadShaderSourceFromFile(fileName);
}
struct ShaderProgramBuilder : PassAttributeBuilder
{
void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
- const osgDB::ReaderWriter::Options* options);
+ const SGReaderWriterXMLOptions* options);
};
void ShaderProgramBuilder::buildAttribute(Effect* effect, Pass* pass,
const SGPropertyNode* prop,
- const osgDB::ReaderWriter::Options*
+ const SGReaderWriterXMLOptions*
options)
{
+ using namespace boost;
if (!isAttributeActive(effect, prop))
return;
PropertyList pVertShaders = prop->getChildren("vertex-shader");
PropertyList pFragShaders = prop->getChildren("fragment-shader");
- string programKey;
+ PropertyList pAttributes = prop->getChildren("attribute");
+ ProgramKey prgKey;
for (PropertyList::iterator itr = pVertShaders.begin(),
e = pVertShaders.end();
itr != e;
++itr)
- {
- programKey += (*itr)->getStringValue();
- programKey += ";";
- }
+ prgKey.shaders.push_back(ShaderKey((*itr)->getStringValue(),
+ Shader::VERTEX));
for (PropertyList::iterator itr = pFragShaders.begin(),
e = pFragShaders.end();
itr != e;
++itr)
- {
- programKey += (*itr)->getStringValue();
- programKey += ";";
+ prgKey.shaders.push_back(ShaderKey((*itr)->getStringValue(),
+ Shader::FRAGMENT));
+ for (PropertyList::iterator itr = pAttributes.begin(),
+ e = pAttributes.end();
+ itr != e;
+ ++itr) {
+ const SGPropertyNode* pName = getEffectPropertyChild(effect, *itr,
+ "name");
+ const SGPropertyNode* pIndex = getEffectPropertyChild(effect, *itr,
+ "index");
+ if (!pName || ! pIndex)
+ throw BuilderException("malformed attribute property");
+ prgKey.attributes
+ .push_back(ProgramKey::AttribKey(pName->getStringValue(),
+ pIndex->getValue<int>()));
}
+ if (options)
+ prgKey.paths = options->getDatabasePathList();
Program* program = 0;
- ProgramMap::iterator pitr = programMap.find(programKey);
+ ProgramMap::iterator pitr = programMap.find(prgKey);
if (pitr != programMap.end()) {
program = pitr->second.get();
} else {
program = new Program;
- program->setName(programKey);
// Add vertex shaders, then fragment shaders
PropertyList& pvec = pVertShaders;
Shader::Type stype = Shader::VERTEX;
nameItr != e;
++nameItr) {
string shaderName = (*nameItr)->getStringValue();
- ShaderMap::iterator sitr = shaderMap.find(shaderName);
+ string fileName = osgDB::findDataFile(shaderName, options);
+ if (fileName.empty())
+ throw BuilderException(string("couldn't find shader ") +
+ shaderName);
+ ShaderKey skey(fileName, stype);
+ ShaderMap::iterator sitr = shaderMap.find(skey);
if (sitr != shaderMap.end()) {
program->addShader(sitr->second.get());
} else {
- string fileName = osgDB::findDataFile(shaderName, options);
- if (!fileName.empty()) {
- ref_ptr<Shader> shader = new Shader(stype);
- if (shader->loadShaderSourceFromFile(fileName)) {
- program->addShader(shader.get());
- shaderMap.insert(make_pair(shaderName, shader));
- }
+ ref_ptr<Shader> shader = new Shader(stype);
+ if (shader->loadShaderSourceFromFile(fileName)) {
+ program->addShader(shader.get());
+ shaderMap.insert(ShaderMap::value_type(skey, shader));
}
}
}
pvec = pFragShaders;
stype = Shader::FRAGMENT;
}
- programMap.insert(make_pair(programKey, program));
+ BOOST_FOREACH(const ProgramKey::AttribKey& key, prgKey.attributes) {
+ program->addBindAttribLocation(key.first, key.second);
+ }
+ programMap.insert(ProgramMap::value_type(prgKey, program));
}
pass->setAttributeAndModes(program);
}
struct UniformBuilder :public PassAttributeBuilder
{
void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
- const osgDB::ReaderWriter::Options* options)
+ const SGReaderWriterXMLOptions* options)
{
if (!isAttributeActive(effect, prop))
return;
struct NameBuilder : public PassAttributeBuilder
{
void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
- const osgDB::ReaderWriter::Options* options)
+ const SGReaderWriterXMLOptions* options)
{
// name can't use <use>
string name = prop->getStringValue();
struct PolygonModeBuilder : public PassAttributeBuilder
{
void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
- const osgDB::ReaderWriter::Options* options)
+ const SGReaderWriterXMLOptions* options)
{
if (!isAttributeActive(effect, prop))
return;
struct VertexProgramTwoSideBuilder : public PassAttributeBuilder
{
void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
- const osgDB::ReaderWriter::Options* options)
+ const SGReaderWriterXMLOptions* options)
{
const SGPropertyNode* realProp = getEffectPropertyNode(effect, prop);
if (!realProp)
struct VertexProgramPointSizeBuilder : public PassAttributeBuilder
{
void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
- const osgDB::ReaderWriter::Options* options)
+ const SGReaderWriterXMLOptions* options)
{
const SGPropertyNode* realProp = getEffectPropertyNode(effect, prop);
if (!realProp)
InstallAttributeBuilder<VertexProgramPointSizeBuilder>
installPointSize("vertex-program-point-size");
+EffectNameValue<Depth::Function> depthFunctionInit[] =
+{
+ {"never", Depth::NEVER},
+ {"less", Depth::LESS},
+ {"equal", Depth::EQUAL},
+ {"lequal", Depth::LEQUAL},
+ {"greater", Depth::GREATER},
+ {"notequal", Depth::NOTEQUAL},
+ {"gequal", Depth::GEQUAL},
+ {"always", Depth::ALWAYS}
+};
+EffectPropertyMap<Depth::Function> depthFunction(depthFunctionInit);
+
+struct DepthBuilder : public PassAttributeBuilder
+{
+ void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
+ const SGReaderWriterXMLOptions* options)
+ {
+ if (!isAttributeActive(effect, prop))
+ return;
+ ref_ptr<Depth> depth = new Depth;
+ const SGPropertyNode* pfunc
+ = getEffectPropertyChild(effect, prop, "function");
+ if (pfunc) {
+ Depth::Function func = Depth::LESS;
+ findAttr(depthFunction, pfunc, func);
+ depth->setFunction(func);
+ }
+ const SGPropertyNode* pnear
+ = getEffectPropertyChild(effect, prop, "near");
+ if (pnear)
+ depth->setZNear(pnear->getValue<double>());
+ const SGPropertyNode* pfar
+ = getEffectPropertyChild(effect, prop, "far");
+ if (pfar)
+ depth->setZFar(pnear->getValue<double>());
+ const SGPropertyNode* pmask
+ = getEffectPropertyChild(effect, prop, "write-mask");
+ if (pmask)
+ depth->setWriteMask(pmask->getValue<bool>());
+ pass->setAttribute(depth.get());
+ }
+};
+
+InstallAttributeBuilder<DepthBuilder> installDepth("depth");
+
void buildTechnique(Effect* effect, const SGPropertyNode* prop,
- const osgDB::ReaderWriter::Options* options)
+ const SGReaderWriterXMLOptions* options)
{
Technique* tniq = new Technique;
effect->techniques.push_back(tniq);
// Walk the techniques property tree, building techniques and
// passes.
-bool Effect::realizeTechniques(const osgDB::ReaderWriter::Options* options)
+bool Effect::realizeTechniques(const SGReaderWriterXMLOptions* options)
{
if (_isRealized)
return true;