1 // Copyright (C) 2008 - 2009 Tim Moore timoore@redhat.com
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Library General Public
5 // License as published by the Free Software Foundation; either
6 // version 2 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Library General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # include <simgear_config.h>
22 #include "EffectBuilder.hxx"
23 #include "EffectGeode.hxx"
24 #include "Technique.hxx"
26 #include "TextureBuilder.hxx"
34 #include <boost/foreach.hpp>
35 #include <boost/tuple/tuple.hpp>
36 #include <boost/tuple/tuple_comparison.hpp>
38 #include <osg/AlphaFunc>
39 #include <osg/BlendFunc>
40 #include <osg/CullFace>
41 #include <osg/Drawable>
42 #include <osg/Material>
44 #include <osg/PolygonMode>
45 #include <osg/Program>
46 #include <osg/Referenced>
47 #include <osg/RenderInfo>
48 #include <osg/ShadeModel>
49 #include <osg/StateSet>
51 #include <osg/Texture1D>
52 #include <osg/Texture2D>
53 #include <osg/Texture3D>
54 #include <osg/TextureRectangle>
55 #include <osg/Uniform>
57 #include <osgUtil/CullVisitor>
58 #include <osgDB/FileUtils>
59 #include <osgDB/Input>
60 #include <osgDB/ParameterOutput>
61 #include <osgDB/ReadFile>
62 #include <osgDB/Registry>
64 #include <simgear/scene/tgdb/userdata.hxx>
65 #include <simgear/scene/util/SGSceneFeatures.hxx>
66 #include <simgear/scene/util/StateAttributeFactory.hxx>
67 #include <simgear/structure/OSGUtils.hxx>
68 #include <simgear/structure/SGExpression.hxx>
76 using namespace osgUtil;
78 using namespace effect;
81 : _cache(0), _isRealized(false)
85 Effect::Effect(const Effect& rhs, const CopyOp& copyop)
86 : root(rhs.root), parametersProp(rhs.parametersProp), _cache(0),
87 _isRealized(rhs._isRealized)
89 typedef vector<ref_ptr<Technique> > TechniqueList;
90 for (TechniqueList::const_iterator itr = rhs.techniques.begin(),
91 end = rhs.techniques.end();
94 techniques.push_back(static_cast<Technique*>(copyop(itr->get())));
97 // Assume that the last technique is always valid.
98 StateSet* Effect::getDefaultStateSet()
100 Technique* tniq = techniques.back().get();
103 Pass* pass = tniq->passes.front().get();
107 // There should always be a valid technique in an effect.
109 Technique* Effect::chooseTechnique(RenderInfo* info)
111 BOOST_FOREACH(ref_ptr<Technique>& technique, techniques)
113 if (technique->valid(info) == Technique::VALID)
114 return technique.get();
119 void Effect::resizeGLObjectBuffers(unsigned int maxSize)
121 BOOST_FOREACH(const ref_ptr<Technique>& technique, techniques)
123 technique->resizeGLObjectBuffers(maxSize);
127 void Effect::releaseGLObjects(osg::State* state) const
129 BOOST_FOREACH(const ref_ptr<Technique>& technique, techniques)
131 technique->releaseGLObjects(state);
140 void buildPass(Effect* effect, Technique* tniq, const SGPropertyNode* prop,
141 const osgDB::ReaderWriter::Options* options)
143 Pass* pass = new Pass;
144 tniq->passes.push_back(pass);
145 for (int i = 0; i < prop->nChildren(); ++i) {
146 const SGPropertyNode* attrProp = prop->getChild(i);
147 PassAttributeBuilder* builder
148 = PassAttributeBuilder::find(attrProp->getNameString());
150 builder->buildAttribute(effect, pass, attrProp, options);
152 SG_LOG(SG_INPUT, SG_ALERT,
153 "skipping unknown pass attribute " << attrProp->getName());
157 osg::Vec4f getColor(const SGPropertyNode* prop)
159 if (prop->nChildren() == 0) {
160 if (prop->getType() == props::VEC4D) {
161 return osg::Vec4f(toOsg(prop->getValue<SGVec4d>()));
162 } else if (prop->getType() == props::VEC3D) {
163 return osg::Vec4f(toOsg(prop->getValue<SGVec3d>()), 1.0f);
165 SG_LOG(SG_INPUT, SG_ALERT,
166 "invalid color property " << prop->getName() << " "
167 << prop->getStringValue());
168 return osg::Vec4f(0.0f, 0.0f, 0.0f, 1.0f);
172 static const char* colors[] = {"r", "g", "b"};
173 for (int i = 0; i < 3; ++i) {
174 const SGPropertyNode* componentProp = prop->getChild(colors[i]);
175 result[i] = componentProp ? componentProp->getValue<float>() : 0.0f;
177 const SGPropertyNode* alphaProp = prop->getChild("a");
178 result[3] = alphaProp ? alphaProp->getValue<float>() : 1.0f;
183 struct LightingBuilder : public PassAttributeBuilder
185 void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
186 const osgDB::ReaderWriter::Options* options);
189 void LightingBuilder::buildAttribute(Effect* effect, Pass* pass,
190 const SGPropertyNode* prop,
191 const osgDB::ReaderWriter::Options* options)
193 const SGPropertyNode* realProp = getEffectPropertyNode(effect, prop);
196 pass->setMode(GL_LIGHTING, (realProp->getValue<bool>() ? StateAttribute::ON
197 : StateAttribute::OFF));
200 InstallAttributeBuilder<LightingBuilder> installLighting("lighting");
202 struct ShadeModelBuilder : public PassAttributeBuilder
204 void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
205 const osgDB::ReaderWriter::Options* options)
207 const SGPropertyNode* realProp = getEffectPropertyNode(effect, prop);
210 StateAttributeFactory *attrFact = StateAttributeFactory::instance();
211 string propVal = realProp->getStringValue();
212 if (propVal == "flat")
213 pass->setAttribute(attrFact->getFlatShadeModel());
214 else if (propVal == "smooth")
215 pass->setAttribute(attrFact->getSmoothShadeModel());
217 SG_LOG(SG_INPUT, SG_ALERT,
218 "invalid shade model property " << propVal);
222 InstallAttributeBuilder<ShadeModelBuilder> installShadeModel("shade-model");
224 struct CullFaceBuilder : PassAttributeBuilder
226 void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
227 const osgDB::ReaderWriter::Options* options)
229 const SGPropertyNode* realProp = getEffectPropertyNode(effect, prop);
231 pass->setMode(GL_CULL_FACE, StateAttribute::OFF);
234 StateAttributeFactory *attrFact = StateAttributeFactory::instance();
235 string propVal = realProp->getStringValue();
236 if (propVal == "front")
237 pass->setAttributeAndModes(attrFact->getCullFaceFront());
238 else if (propVal == "back")
239 pass->setAttributeAndModes(attrFact->getCullFaceBack());
240 else if (propVal == "front-back")
241 pass->setAttributeAndModes(new CullFace(CullFace::FRONT_AND_BACK));
242 else if (propVal == "off")
243 pass->setMode(GL_CULL_FACE, StateAttribute::OFF);
245 SG_LOG(SG_INPUT, SG_ALERT,
246 "invalid cull face property " << propVal);
250 InstallAttributeBuilder<CullFaceBuilder> installCullFace("cull-face");
252 EffectNameValue<StateSet::RenderingHint> renderingHintInit[] =
254 { "default", StateSet::DEFAULT_BIN },
255 { "opaque", StateSet::OPAQUE_BIN },
256 { "transparent", StateSet::TRANSPARENT_BIN }
259 EffectPropertyMap<StateSet::RenderingHint> renderingHints(renderingHintInit);
261 struct HintBuilder : public PassAttributeBuilder
263 void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
264 const osgDB::ReaderWriter::Options* options)
266 const SGPropertyNode* realProp = getEffectPropertyNode(effect, prop);
269 StateSet::RenderingHint renderingHint = StateSet::DEFAULT_BIN;
270 findAttr(renderingHints, realProp, renderingHint);
271 pass->setRenderingHint(renderingHint);
275 InstallAttributeBuilder<HintBuilder> installHint("rendering-hint");
277 struct RenderBinBuilder : public PassAttributeBuilder
279 void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
280 const osgDB::ReaderWriter::Options* options)
282 if (!isAttributeActive(effect, prop))
284 const SGPropertyNode* binProp = prop->getChild("bin-number");
285 binProp = getEffectPropertyNode(effect, binProp);
286 const SGPropertyNode* nameProp = prop->getChild("bin-name");
287 nameProp = getEffectPropertyNode(effect, nameProp);
288 if (binProp && nameProp) {
289 pass->setRenderBinDetails(binProp->getIntValue(),
290 nameProp->getStringValue());
293 SG_LOG(SG_INPUT, SG_ALERT,
294 "No render bin number specified in render bin section");
296 SG_LOG(SG_INPUT, SG_ALERT,
297 "No render bin name specified in render bin section");
302 InstallAttributeBuilder<RenderBinBuilder> installRenderBin("render-bin");
304 struct MaterialBuilder : public PassAttributeBuilder
306 void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
307 const osgDB::ReaderWriter::Options* options);
310 EffectNameValue<Material::ColorMode> colorModeInit[] =
312 { "ambient", Material::AMBIENT },
313 { "ambient-and-diffuse", Material::AMBIENT_AND_DIFFUSE },
314 { "diffuse", Material::DIFFUSE },
315 { "emissive", Material::EMISSION },
316 { "specular", Material::SPECULAR },
317 { "off", Material::OFF }
319 EffectPropertyMap<Material::ColorMode> colorModes(colorModeInit);
321 void MaterialBuilder::buildAttribute(Effect* effect, Pass* pass,
322 const SGPropertyNode* prop,
323 const osgDB::ReaderWriter::Options* options)
325 if (!isAttributeActive(effect, prop))
327 Material* mat = new Material;
328 const SGPropertyNode* color = 0;
329 if ((color = getEffectPropertyChild(effect, prop, "ambient")))
330 mat->setAmbient(Material::FRONT_AND_BACK, getColor(color));
331 if ((color = getEffectPropertyChild(effect, prop, "ambient-front")))
332 mat->setAmbient(Material::FRONT, getColor(color));
333 if ((color = getEffectPropertyChild(effect, prop, "ambient-back")))
334 mat->setAmbient(Material::BACK, getColor(color));
335 if ((color = getEffectPropertyChild(effect, prop, "diffuse")))
336 mat->setDiffuse(Material::FRONT_AND_BACK, getColor(color));
337 if ((color = getEffectPropertyChild(effect, prop, "diffuse-front")))
338 mat->setDiffuse(Material::FRONT, getColor(color));
339 if ((color = getEffectPropertyChild(effect, prop, "diffuse-back")))
340 mat->setDiffuse(Material::BACK, getColor(color));
341 if ((color = getEffectPropertyChild(effect, prop, "specular")))
342 mat->setSpecular(Material::FRONT_AND_BACK, getColor(color));
343 if ((color = getEffectPropertyChild(effect, prop, "specular-front")))
344 mat->setSpecular(Material::FRONT, getColor(color));
345 if ((color = getEffectPropertyChild(effect, prop, "specular-back")))
346 mat->setSpecular(Material::BACK, getColor(color));
347 if ((color = getEffectPropertyChild(effect, prop, "emissive")))
348 mat->setEmission(Material::FRONT_AND_BACK, getColor(color));
349 if ((color = getEffectPropertyChild(effect, prop, "emissive-front")))
350 mat->setEmission(Material::FRONT, getColor(color));
351 if ((color = getEffectPropertyChild(effect, prop, "emissive-back")))
352 mat->setEmission(Material::BACK, getColor(color));
353 const SGPropertyNode* shininess = 0;
354 mat->setShininess(Material::FRONT_AND_BACK, 0.0f);
355 if ((shininess = getEffectPropertyChild(effect, prop, "shininess")))
356 mat->setShininess(Material::FRONT_AND_BACK, shininess->getFloatValue());
357 if ((shininess = getEffectPropertyChild(effect, prop, "shininess-front")))
358 mat->setShininess(Material::FRONT, shininess->getFloatValue());
359 if ((shininess = getEffectPropertyChild(effect, prop, "shininess-back")))
360 mat->setShininess(Material::BACK, shininess->getFloatValue());
361 Material::ColorMode colorMode = Material::OFF;
362 findAttr(colorModes, getEffectPropertyChild(effect, prop, "color-mode"),
364 mat->setColorMode(colorMode);
365 pass->setAttribute(mat);
368 InstallAttributeBuilder<MaterialBuilder> installMaterial("material");
370 EffectNameValue<BlendFunc::BlendFuncMode> blendFuncModesInit[] =
372 {"dst-alpha", BlendFunc::DST_ALPHA},
373 {"dst-color", BlendFunc::DST_COLOR},
374 {"one", BlendFunc::ONE},
375 {"one-minus-dst-alpha", BlendFunc::ONE_MINUS_DST_ALPHA},
376 {"one-minus-dst-color", BlendFunc::ONE_MINUS_DST_COLOR},
377 {"one-minus-src-alpha", BlendFunc::ONE_MINUS_SRC_ALPHA},
378 {"one-minus-src-color", BlendFunc::ONE_MINUS_SRC_COLOR},
379 {"src-alpha", BlendFunc::SRC_ALPHA},
380 {"src-alpha-saturate", BlendFunc::SRC_ALPHA_SATURATE},
381 {"src-color", BlendFunc::SRC_COLOR},
382 {"constant-color", BlendFunc::CONSTANT_COLOR},
383 {"one-minus-constant-color", BlendFunc::ONE_MINUS_CONSTANT_COLOR},
384 {"constant-alpha", BlendFunc::CONSTANT_ALPHA},
385 {"one-minus-constant-alpha", BlendFunc::ONE_MINUS_CONSTANT_ALPHA},
386 {"zero", BlendFunc::ZERO}
388 EffectPropertyMap<BlendFunc::BlendFuncMode> blendFuncModes(blendFuncModesInit);
390 struct BlendBuilder : public PassAttributeBuilder
392 void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
393 const osgDB::ReaderWriter::Options* options)
395 if (!isAttributeActive(effect, prop))
397 // XXX Compatibility with early <blend> syntax; should go away
399 const SGPropertyNode* realProp = getEffectPropertyNode(effect, prop);
402 if (realProp->nChildren() == 0) {
403 pass->setMode(GL_BLEND, (realProp->getBoolValue()
405 : StateAttribute::OFF));
409 const SGPropertyNode* pmode = getEffectPropertyChild(effect, prop,
411 // XXX When dynamic parameters are supported, this code should
412 // create the blend function even if the mode is off.
413 if (pmode && !pmode->getValue<bool>()) {
414 pass->setMode(GL_BLEND, StateAttribute::OFF);
417 const SGPropertyNode* psource
418 = getEffectPropertyChild(effect, prop, "source");
419 const SGPropertyNode* pdestination
420 = getEffectPropertyChild(effect, prop, "destination");
421 const SGPropertyNode* psourceRGB
422 = getEffectPropertyChild(effect, prop, "source-rgb");
423 const SGPropertyNode* psourceAlpha
424 = getEffectPropertyChild(effect, prop, "source-alpha");
425 const SGPropertyNode* pdestRGB
426 = getEffectPropertyChild(effect, prop, "destination-rgb");
427 const SGPropertyNode* pdestAlpha
428 = getEffectPropertyChild(effect, prop, "destination-alpha");
429 BlendFunc::BlendFuncMode sourceMode = BlendFunc::ONE;
430 BlendFunc::BlendFuncMode destMode = BlendFunc::ZERO;
432 findAttr(blendFuncModes, psource, sourceMode);
434 findAttr(blendFuncModes, pdestination, destMode);
435 if (psource && pdestination
436 && !(psourceRGB || psourceAlpha || pdestRGB || pdestAlpha)
437 && sourceMode == BlendFunc::SRC_ALPHA
438 && destMode == BlendFunc::ONE_MINUS_SRC_ALPHA) {
439 pass->setAttributeAndModes(StateAttributeFactory::instance()
440 ->getStandardBlendFunc());
443 BlendFunc* blendFunc = new BlendFunc;
445 blendFunc->setSource(sourceMode);
447 blendFunc->setDestination(destMode);
449 BlendFunc::BlendFuncMode sourceRGBMode;
450 findAttr(blendFuncModes, psourceRGB, sourceRGBMode);
451 blendFunc->setSourceRGB(sourceRGBMode);
454 BlendFunc::BlendFuncMode destRGBMode;
455 findAttr(blendFuncModes, pdestRGB, destRGBMode);
456 blendFunc->setDestinationRGB(destRGBMode);
459 BlendFunc::BlendFuncMode sourceAlphaMode;
460 findAttr(blendFuncModes, psourceAlpha, sourceAlphaMode);
461 blendFunc->setSourceAlpha(sourceAlphaMode);
464 BlendFunc::BlendFuncMode destAlphaMode;
465 findAttr(blendFuncModes, pdestAlpha, destAlphaMode);
466 blendFunc->setDestinationAlpha(destAlphaMode);
468 pass->setAttributeAndModes(blendFunc);
472 InstallAttributeBuilder<BlendBuilder> installBlend("blend");
474 EffectNameValue<AlphaFunc::ComparisonFunction> alphaComparisonInit[] =
476 {"never", AlphaFunc::NEVER},
477 {"less", AlphaFunc::LESS},
478 {"equal", AlphaFunc::EQUAL},
479 {"lequal", AlphaFunc::LEQUAL},
480 {"greater", AlphaFunc::GREATER},
481 {"notequal", AlphaFunc::NOTEQUAL},
482 {"gequal", AlphaFunc::GEQUAL},
483 {"always", AlphaFunc::ALWAYS}
485 EffectPropertyMap<AlphaFunc::ComparisonFunction>
486 alphaComparison(alphaComparisonInit);
488 struct AlphaTestBuilder : public PassAttributeBuilder
490 void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
491 const osgDB::ReaderWriter::Options* options)
493 if (!isAttributeActive(effect, prop))
495 // XXX Compatibility with early <alpha-test> syntax; should go away
497 const SGPropertyNode* realProp = getEffectPropertyNode(effect, prop);
500 if (realProp->nChildren() == 0) {
501 pass->setMode(GL_ALPHA_TEST, (realProp->getBoolValue()
503 : StateAttribute::OFF));
507 const SGPropertyNode* pmode = getEffectPropertyChild(effect, prop,
509 // XXX When dynamic parameters are supported, this code should
510 // create the blend function even if the mode is off.
511 if (pmode && !pmode->getValue<bool>()) {
512 pass->setMode(GL_ALPHA_TEST, StateAttribute::OFF);
515 const SGPropertyNode* pComp = getEffectPropertyChild(effect, prop,
517 const SGPropertyNode* pRef = getEffectPropertyChild(effect, prop,
519 AlphaFunc::ComparisonFunction func = AlphaFunc::ALWAYS;
520 float refValue = 1.0f;
522 findAttr(alphaComparison, pComp, func);
524 refValue = pRef->getValue<float>();
525 if (func == AlphaFunc::GREATER && osg::equivalent(refValue, 1.0f)) {
526 pass->setAttributeAndModes(StateAttributeFactory::instance()
527 ->getStandardAlphaFunc());
529 AlphaFunc* alphaFunc = new AlphaFunc;
530 alphaFunc->setFunction(func);
531 alphaFunc->setReferenceValue(refValue);
532 pass->setAttributeAndModes(alphaFunc);
537 InstallAttributeBuilder<AlphaTestBuilder> installAlphaTest("alpha-test");
539 InstallAttributeBuilder<TextureUnitBuilder> textureUnitBuilder("texture-unit");
541 typedef map<string, ref_ptr<Program> > ProgramMap;
542 ProgramMap programMap;
544 typedef map<string, ref_ptr<Shader> > ShaderMap;
547 void reload_shaders()
549 for(ShaderMap::iterator sitr = shaderMap.begin(); sitr != shaderMap.end(); ++sitr)
551 Shader *shader = sitr->second.get();
552 string fileName = osgDB::findDataFile(sitr->first);
553 if (!fileName.empty()) {
554 shader->loadShaderSourceFromFile(fileName);
559 struct ShaderProgramBuilder : PassAttributeBuilder
561 void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
562 const osgDB::ReaderWriter::Options* options);
565 void ShaderProgramBuilder::buildAttribute(Effect* effect, Pass* pass,
566 const SGPropertyNode* prop,
567 const osgDB::ReaderWriter::Options*
570 if (!isAttributeActive(effect, prop))
572 PropertyList pVertShaders = prop->getChildren("vertex-shader");
573 PropertyList pFragShaders = prop->getChildren("fragment-shader");
575 for (PropertyList::iterator itr = pVertShaders.begin(),
576 e = pVertShaders.end();
580 programKey += (*itr)->getStringValue();
583 for (PropertyList::iterator itr = pFragShaders.begin(),
584 e = pFragShaders.end();
588 programKey += (*itr)->getStringValue();
591 Program* program = 0;
592 ProgramMap::iterator pitr = programMap.find(programKey);
593 if (pitr != programMap.end()) {
594 program = pitr->second.get();
596 program = new Program;
597 program->setName(programKey);
598 // Add vertex shaders, then fragment shaders
599 PropertyList& pvec = pVertShaders;
600 Shader::Type stype = Shader::VERTEX;
601 for (int i = 0; i < 2; ++i) {
602 for (PropertyList::iterator nameItr = pvec.begin(), e = pvec.end();
605 string shaderName = (*nameItr)->getStringValue();
606 ShaderMap::iterator sitr = shaderMap.find(shaderName);
607 if (sitr != shaderMap.end()) {
608 program->addShader(sitr->second.get());
610 string fileName = osgDB::findDataFile(shaderName, options);
611 if (!fileName.empty()) {
612 ref_ptr<Shader> shader = new Shader(stype);
613 if (shader->loadShaderSourceFromFile(fileName)) {
614 program->addShader(shader.get());
615 shaderMap.insert(make_pair(shaderName, shader));
621 stype = Shader::FRAGMENT;
623 programMap.insert(make_pair(programKey, program));
625 pass->setAttributeAndModes(program);
628 InstallAttributeBuilder<ShaderProgramBuilder> installShaderProgram("program");
630 EffectNameValue<Uniform::Type> uniformTypesInit[] =
632 {"float", Uniform::FLOAT},
633 {"float-vec3", Uniform::FLOAT_VEC3},
634 {"float-vec4", Uniform::FLOAT_VEC4},
635 {"sampler-1d", Uniform::SAMPLER_1D},
636 {"sampler-2d", Uniform::SAMPLER_2D},
637 {"sampler-3d", Uniform::SAMPLER_3D}
639 EffectPropertyMap<Uniform::Type> uniformTypes(uniformTypesInit);
641 struct UniformBuilder :public PassAttributeBuilder
643 void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
644 const osgDB::ReaderWriter::Options* options)
646 if (!isAttributeActive(effect, prop))
648 const SGPropertyNode* nameProp = prop->getChild("name");
649 const SGPropertyNode* typeProp = prop->getChild("type");
650 const SGPropertyNode* valProp
651 = getEffectPropertyChild(effect, prop, "value");
653 Uniform::Type uniformType = Uniform::FLOAT;
655 name = nameProp->getStringValue();
657 SG_LOG(SG_INPUT, SG_ALERT, "No name for uniform property ");
661 SG_LOG(SG_INPUT, SG_ALERT, "No value for uniform property "
666 props::Type propType = valProp->getType();
670 break; // default float type;
672 uniformType = Uniform::FLOAT_VEC3;
675 uniformType = Uniform::FLOAT_VEC4;
678 SG_LOG(SG_INPUT, SG_ALERT, "Can't deduce type of uniform "
683 findAttr(uniformTypes, typeProp, uniformType);
685 ref_ptr<Uniform> uniform = new Uniform;
686 uniform->setName(name);
687 uniform->setType(uniformType);
688 switch (uniformType) {
690 uniform->set(valProp->getValue<float>());
692 case Uniform::FLOAT_VEC3:
693 uniform->set(toOsg(valProp->getValue<SGVec3d>()));
695 case Uniform::FLOAT_VEC4:
696 uniform->set(toOsg(valProp->getValue<SGVec4d>()));
698 case Uniform::SAMPLER_1D:
699 case Uniform::SAMPLER_2D:
700 case Uniform::SAMPLER_3D:
701 uniform->set(valProp->getValue<int>());
703 default: // avoid compiler warning
706 pass->addUniform(uniform.get());
710 InstallAttributeBuilder<UniformBuilder> installUniform("uniform");
712 // Not sure what to do with "name". At one point I wanted to use it to
713 // order the passes, but I do support render bin and stuff too...
715 struct NameBuilder : public PassAttributeBuilder
717 void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
718 const osgDB::ReaderWriter::Options* options)
720 // name can't use <use>
721 string name = prop->getStringValue();
727 InstallAttributeBuilder<NameBuilder> installName("name");
729 EffectNameValue<PolygonMode::Mode> polygonModeModesInit[] =
731 {"fill", PolygonMode::FILL},
732 {"line", PolygonMode::LINE},
733 {"point", PolygonMode::POINT}
735 EffectPropertyMap<PolygonMode::Mode> polygonModeModes(polygonModeModesInit);
737 struct PolygonModeBuilder : public PassAttributeBuilder
739 void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
740 const osgDB::ReaderWriter::Options* options)
742 if (!isAttributeActive(effect, prop))
744 const SGPropertyNode* frontProp
745 = getEffectPropertyChild(effect, prop, "front");
746 const SGPropertyNode* backProp
747 = getEffectPropertyChild(effect, prop, "back");
748 ref_ptr<PolygonMode> pmode = new PolygonMode;
749 PolygonMode::Mode frontMode = PolygonMode::FILL;
750 PolygonMode::Mode backMode = PolygonMode::FILL;
752 findAttr(polygonModeModes, frontProp, frontMode);
753 pmode->setMode(PolygonMode::FRONT, frontMode);
756 findAttr(polygonModeModes, backProp, backMode);
757 pmode->setMode(PolygonMode::BACK, backMode);
759 pass->setAttribute(pmode.get());
763 InstallAttributeBuilder<PolygonModeBuilder> installPolygonMode("polygon-mode");
765 struct VertexProgramTwoSideBuilder : public PassAttributeBuilder
767 void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
768 const osgDB::ReaderWriter::Options* options)
770 const SGPropertyNode* realProp = getEffectPropertyNode(effect, prop);
773 pass->setMode(GL_VERTEX_PROGRAM_TWO_SIDE,
774 (realProp->getValue<bool>()
775 ? StateAttribute::ON : StateAttribute::OFF));
779 InstallAttributeBuilder<VertexProgramTwoSideBuilder>
780 installTwoSide("vertex-program-two-side");
782 struct VertexProgramPointSizeBuilder : public PassAttributeBuilder
784 void buildAttribute(Effect* effect, Pass* pass, const SGPropertyNode* prop,
785 const osgDB::ReaderWriter::Options* options)
787 const SGPropertyNode* realProp = getEffectPropertyNode(effect, prop);
790 pass->setMode(GL_VERTEX_PROGRAM_POINT_SIZE,
791 (realProp->getValue<bool>()
792 ? StateAttribute::ON : StateAttribute::OFF));
796 InstallAttributeBuilder<VertexProgramPointSizeBuilder>
797 installPointSize("vertex-program-point-size");
799 void buildTechnique(Effect* effect, const SGPropertyNode* prop,
800 const osgDB::ReaderWriter::Options* options)
802 Technique* tniq = new Technique;
803 effect->techniques.push_back(tniq);
804 const SGPropertyNode* predProp = prop->getChild("predicate");
806 tniq->setAlwaysValid(true);
809 TechniquePredParser parser;
810 parser.setTechnique(tniq);
811 expression::BindingLayout& layout = parser.getBindingLayout();
812 /*int contextLoc = */layout.addBinding("__contextId", expression::INT);
813 SGExpressionb* validExp
814 = dynamic_cast<SGExpressionb*>(parser.read(predProp
817 tniq->setValidExpression(validExp, layout);
819 throw expression::ParseError("technique predicate is not a boolean expression");
821 catch (expression::ParseError& except)
823 SG_LOG(SG_INPUT, SG_ALERT,
824 "parsing technique predicate " << except.getMessage());
825 tniq->setAlwaysValid(false);
828 PropertyList passProps = prop->getChildren("pass");
829 for (PropertyList::iterator itr = passProps.begin(), e = passProps.end();
832 buildPass(effect, tniq, itr->ptr(), options);
836 // Specifically for .ac files...
837 bool makeParametersFromStateSet(SGPropertyNode* effectRoot, const StateSet* ss)
839 SGPropertyNode* paramRoot = makeChild(effectRoot, "parameters");
840 SGPropertyNode* matNode = paramRoot->getChild("material", 0, true);
841 Vec4f ambVal, difVal, specVal, emisVal;
842 float shininess = 0.0f;
843 const Material* mat = getStateAttribute<Material>(ss);
845 ambVal = mat->getAmbient(Material::FRONT_AND_BACK);
846 difVal = mat->getDiffuse(Material::FRONT_AND_BACK);
847 specVal = mat->getSpecular(Material::FRONT_AND_BACK);
848 emisVal = mat->getEmission(Material::FRONT_AND_BACK);
849 shininess = mat->getShininess(Material::FRONT_AND_BACK);
850 makeChild(matNode, "active")->setValue(true);
851 makeChild(matNode, "ambient")->setValue(toVec4d(toSG(ambVal)));
852 makeChild(matNode, "diffuse")->setValue(toVec4d(toSG(difVal)));
853 makeChild(matNode, "specular")->setValue(toVec4d(toSG(specVal)));
854 makeChild(matNode, "emissive")->setValue(toVec4d(toSG(emisVal)));
855 makeChild(matNode, "shininess")->setValue(shininess);
856 matNode->getChild("color-mode", 0, true)->setStringValue("diffuse");
858 makeChild(matNode, "active")->setValue(false);
860 const ShadeModel* sm = getStateAttribute<ShadeModel>(ss);
861 string shadeModelString("smooth");
863 ShadeModel::Mode smMode = sm->getMode();
864 if (smMode == ShadeModel::FLAT)
865 shadeModelString = "flat";
867 makeChild(paramRoot, "shade-model")->setStringValue(shadeModelString);
868 string cullFaceString("off");
869 const CullFace* cullFace = getStateAttribute<CullFace>(ss);
871 switch (cullFace->getMode()) {
872 case CullFace::FRONT:
873 cullFaceString = "front";
876 cullFaceString = "back";
878 case CullFace::FRONT_AND_BACK:
879 cullFaceString = "front-back";
885 makeChild(paramRoot, "cull-face")->setStringValue(cullFaceString);
886 const BlendFunc* blendFunc = getStateAttribute<BlendFunc>(ss);
887 SGPropertyNode* blendNode = makeChild(paramRoot, "blend");
889 string sourceMode = findName(blendFuncModes, blendFunc->getSource());
890 string destMode = findName(blendFuncModes, blendFunc->getDestination());
891 makeChild(blendNode, "active")->setValue(true);
892 makeChild(blendNode, "source")->setStringValue(sourceMode);
893 makeChild(blendNode, "destination")->setStringValue(destMode);
894 makeChild(blendNode, "mode")->setValue(true);
896 makeChild(blendNode, "active")->setValue(false);
898 string renderingHint = findName(renderingHints, ss->getRenderingHint());
899 makeChild(paramRoot, "rendering-hint")->setStringValue(renderingHint);
900 makeTextureParameters(paramRoot, ss);
904 // Walk the techniques property tree, building techniques and
906 bool Effect::realizeTechniques(const osgDB::ReaderWriter::Options* options)
910 PropertyList tniqList = root->getChildren("technique");
911 for (PropertyList::iterator itr = tniqList.begin(), e = tniqList.end();
914 buildTechnique(this, *itr, options);
919 void Effect::InitializeCallback::doUpdate(osg::Node* node, osg::NodeVisitor* nv)
921 EffectGeode* eg = dynamic_cast<EffectGeode*>(node);
924 Effect* effect = eg->getEffect();
927 SGPropertyNode* root = getPropertyRoot();
928 for (vector<SGSharedPtr<Updater> >::iterator itr = effect->_extraData.begin(),
929 end = effect->_extraData.end();
932 InitializeWhenAdded* adder
933 = dynamic_cast<InitializeWhenAdded*>(itr->ptr());
935 adder->initOnAdd(effect, root);
939 bool Effect::Key::EqualTo::operator()(const Effect::Key& lhs,
940 const Effect::Key& rhs) const
942 if (lhs.paths.size() != rhs.paths.size()
943 || !equal(lhs.paths.begin(), lhs.paths.end(), rhs.paths.begin()))
945 if (lhs.unmerged.valid() && rhs.unmerged.valid())
946 return props::Compare()(lhs.unmerged, rhs.unmerged);
948 return lhs.unmerged == rhs.unmerged;
951 size_t hash_value(const Effect::Key& key)
954 if (key.unmerged.valid())
955 boost::hash_combine(seed, *key.unmerged);
956 boost::hash_range(seed, key.paths.begin(), key.paths.end());
960 bool Effect_writeLocalData(const Object& obj, osgDB::Output& fw)
962 const Effect& effect = static_cast<const Effect&>(obj);
964 fw.indent() << "techniques " << effect.techniques.size() << "\n";
965 BOOST_FOREACH(const ref_ptr<Technique>& technique, effect.techniques) {
966 fw.writeObject(*technique);
973 osgDB::RegisterDotOsgWrapperProxy effectProxy
977 "Object simgear::Effect",
979 &Effect_writeLocalData
983 // Property expressions for technique predicates
984 class PropertyExpression : public SGExpression<bool>
987 PropertyExpression(SGPropertyNode* pnode) : _pnode(pnode) {}
989 void eval(bool& value, const expression::Binding*) const
991 value = _pnode->getValue<bool>();
994 SGPropertyNode_ptr _pnode;
997 class EffectPropertyListener : public SGPropertyChangeListener
1000 EffectPropertyListener(Technique* tniq) : _tniq(tniq) {}
1002 void valueChanged(SGPropertyNode* node)
1004 _tniq->refreshValidity();
1007 osg::ref_ptr<Technique> _tniq;
1010 Expression* propertyExpressionParser(const SGPropertyNode* exp,
1011 expression::Parser* parser)
1013 SGPropertyNode_ptr pnode = getPropertyRoot()->getNode(exp->getStringValue(),
1015 PropertyExpression* pexp = new PropertyExpression(pnode);
1016 TechniquePredParser* predParser
1017 = dynamic_cast<TechniquePredParser*>(parser);
1019 pnode->addChangeListener(new EffectPropertyListener(predParser
1024 expression::ExpParserRegistrar propertyRegistrar("property",
1025 propertyExpressionParser);