1 // animation.cxx - classes to manage model animation.
2 // Written by David Megginson, started 2002.
4 // This file is in the Public Domain, and comes with no warranty.
7 # include <simgear_config.h>
10 #include "SGMaterialAnimation.hxx"
12 #include <osg/AlphaFunc>
14 #include <osg/Drawable>
16 #include <osg/Geometry>
17 #include <osg/Material>
18 #include <osg/StateSet>
19 #include <osgDB/FileNameUtils>
20 #include <osgDB/FileUtils>
21 #include <osgDB/ReadFile>
23 #include <simgear/props/condition.hxx>
24 #include <simgear/props/props.hxx>
25 #include <simgear/scene/model/model.hxx>
29 * Get a color from properties.
32 float red, green, blue;
35 SGPropertyNode_ptr red_prop;
36 SGPropertyNode_ptr green_prop;
37 SGPropertyNode_ptr blue_prop;
38 SGPropertyNode_ptr factor_prop;
39 SGPropertyNode_ptr offset_prop;
42 ColorSpec(const SGPropertyNode* configNode, SGPropertyNode* modelRoot)
50 red = configNode->getFloatValue("red", -1.0);
51 green = configNode->getFloatValue("green", -1.0);
52 blue = configNode->getFloatValue("blue", -1.0);
53 factor = configNode->getFloatValue("factor", 1.0);
54 offset = configNode->getFloatValue("offset", 0.0);
58 const SGPropertyNode *node;
59 node = configNode->getChild("red-prop");
61 red_prop = modelRoot->getNode(node->getStringValue(), true);
62 node = configNode->getChild("green-prop");
64 green_prop = modelRoot->getNode(node->getStringValue(), true);
65 node = configNode->getChild("blue-prop");
67 blue_prop = modelRoot->getNode(node->getStringValue(), true);
68 node = configNode->getChild("factor-prop");
70 factor_prop = modelRoot->getNode(node->getStringValue(), true);
71 node = configNode->getChild("offset-prop");
73 offset_prop = modelRoot->getNode(node->getStringValue(), true);
77 return red >= 0 || green >= 0 || blue >= 0;
80 return red_prop || green_prop || blue_prop
81 || factor_prop || offset_prop;
85 red = red_prop->getFloatValue();
87 green = green_prop->getFloatValue();
89 blue = blue_prop->getFloatValue();
91 factor = factor_prop->getFloatValue();
93 offset = offset_prop->getFloatValue();
94 v[0] = SGMiscf::clip(red*factor + offset, 0, 1);
95 v[1] = SGMiscf::clip(green*factor + offset, 0, 1);
96 v[2] = SGMiscf::clip(blue*factor + offset, 0, 1);
101 osg::Vec4& rgbaVec4()
106 SGVec4f &initialRgba() {
107 v[0] = SGMiscf::clip(red*factor + offset, 0, 1);
108 v[1] = SGMiscf::clip(green*factor + offset, 0, 1);
109 v[2] = SGMiscf::clip(blue*factor + offset, 0, 1);
116 * Get a property value from a property.
124 SGPropertyNode_ptr value_prop;
125 SGPropertyNode_ptr factor_prop;
126 SGPropertyNode_ptr offset_prop;
128 PropSpec(const char* valueName, const char* valuePropName,
129 const SGPropertyNode* configNode, SGPropertyNode* modelRoot)
135 value = configNode->getFloatValue(valueName, -1);
136 factor = configNode->getFloatValue("factor", 1);
137 offset = configNode->getFloatValue("offset", 0);
138 min = configNode->getFloatValue("min", 0);
139 max = configNode->getFloatValue("max", 1);
143 const SGPropertyNode *node;
144 node = configNode->getChild(valuePropName);
146 value_prop = modelRoot->getNode(node->getStringValue(), true);
147 node = configNode->getChild("factor-prop");
149 factor_prop = modelRoot->getNode(node->getStringValue(), true);
150 node = configNode->getChild("offset-prop");
152 offset_prop = modelRoot->getNode(node->getStringValue(), true);
154 bool dirty() { return value >= 0.0; }
155 bool live() { return value_prop || factor_prop || offset_prop; }
159 value = value_prop->getFloatValue();
161 offset = offset_prop->getFloatValue();
163 factor = factor_prop->getFloatValue();
164 return SGMiscf::clip(value*factor + offset, min, max);
166 float getInitialValue()
168 return SGMiscf::clip(value*factor + offset, min, max);
173 * The possible color properties supplied by a material animation.
184 const unsigned AMBIENT_DIFFUSE = AMBIENT | DIFFUSE;
186 const int allMaterialColors = (DIFFUSE | AMBIENT | SPECULAR | EMISSION
189 // Visitor for finding default material colors in the animation node's
190 // subgraph. This makes some assumptions about the subgraph i.e.,
191 // there will be one material and one color value found. This is
192 // probably true for ac3d models and most uses of material animations,
193 // but will break down if, for example, you animate the transparency
194 // of a vertex colored model.
195 class MaterialDefaultsVisitor : public osg::NodeVisitor {
197 MaterialDefaultsVisitor()
198 : osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
199 ambientDiffuse(-1.0f, -1.0f, -1.0f, -1.0f)
201 setVisitorType(osg::NodeVisitor::NODE_VISITOR);
204 virtual void apply(osg::Node& node)
206 maybeGetMaterialValues(node.getStateSet());
210 virtual void apply(osg::Geode& node)
212 maybeGetMaterialValues(node.getStateSet());
213 int numDrawables = node.getNumDrawables();
214 for (int i = 0; i < numDrawables; i++) {
215 osg::Geometry* geom = dynamic_cast<osg::Geometry*>(node.getDrawable(i));
216 if (!geom || geom->getColorBinding() != osg::Geometry::BIND_OVERALL)
218 maybeGetMaterialValues(geom->getStateSet());
219 osg::Array* colorArray = geom->getColorArray();
220 osg::Vec4Array* colorVec4 = dynamic_cast<osg::Vec4Array*>(colorArray);
222 ambientDiffuse = (*colorVec4)[0];
225 osg::Vec3Array* colorVec3 = dynamic_cast<osg::Vec3Array*>(colorArray);
227 ambientDiffuse = osg::Vec4((*colorVec3)[0], 1.0f);
233 void maybeGetMaterialValues(osg::StateSet* stateSet)
237 osg::Material* nodeMat
238 = dynamic_cast<osg::Material*>(stateSet->getAttribute(osg::StateAttribute::MATERIAL));
244 osg::ref_ptr<osg::Material> material;
245 osg::Vec4 ambientDiffuse;
248 class UpdateCallback : public osg::NodeCallback {
250 UpdateCallback(const osgDB::FilePathList& texturePathList,
251 const SGCondition* condition,
252 const SGPropertyNode* configNode, SGPropertyNode* modelRoot) :
253 _condition(condition),
254 _ambient(configNode->getChild("ambient"), modelRoot),
255 _diffuse(configNode->getChild("diffuse"), modelRoot),
256 _specular(configNode->getChild("specular"), modelRoot),
257 _emission(configNode->getChild("emission"), modelRoot),
258 _shininess("shininess", "shininess-prop",
259 configNode->getChild("shininess"), modelRoot),
260 _transparency("alpha", "alpha-prop",
261 configNode->getChild("transparency"), modelRoot),
262 _texturePathList(texturePathList)
264 const SGPropertyNode* node;
266 node = configNode->getChild("threshold-prop");
268 _thresholdProp = modelRoot->getNode(node->getStringValue(), true);
269 node = configNode->getChild("texture-prop");
271 _textureProp = modelRoot->getNode(node->getStringValue(), true);
274 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
276 osg::StateSet* stateSet = node->getStateSet();
277 if ((!_condition || _condition->test()) && stateSet) {
279 std::string textureName = _textureProp->getStringValue();
280 if (_textureName != textureName) {
281 while (stateSet->getTextureAttribute(0,
282 osg::StateAttribute::TEXTURE)) {
283 stateSet->removeTextureAttribute(0, osg::StateAttribute::TEXTURE);
285 std::string textureFile;
286 textureFile = osgDB::findFileInPath(textureName, _texturePathList);
287 if (!textureFile.empty()) {
288 osg::Texture2D* texture2D = SGLoadTexture2D(textureFile);
290 stateSet->setTextureAttribute(0, texture2D,
291 osg::StateAttribute::OVERRIDE);
292 stateSet->setTextureMode(0, GL_TEXTURE_2D,
293 osg::StateAttribute::ON);
294 _textureName = textureName;
299 if (_thresholdProp) {
300 osg::StateSet* stateSet = node->getOrCreateStateSet();
301 osg::StateAttribute* stateAttribute;
302 stateAttribute = stateSet->getAttribute(osg::StateAttribute::ALPHAFUNC);
303 osg::AlphaFunc* alphaFunc = dynamic_cast<osg::AlphaFunc*>(stateAttribute);
305 alphaFunc->setReferenceValue(_thresholdProp->getFloatValue());
307 osg::StateAttribute* stateAttribute
308 = stateSet->getAttribute(osg::StateAttribute::MATERIAL);
309 osg::Material* material = dynamic_cast<osg::Material*>(stateAttribute);
312 material->setAmbient(osg::Material::FRONT_AND_BACK,
313 _ambient.rgbaVec4());
315 material->setDiffuse(osg::Material::FRONT_AND_BACK,
316 _diffuse.rgbaVec4());
317 if (_specular.live())
318 material->setSpecular(osg::Material::FRONT_AND_BACK,
319 _specular.rgbaVec4());
320 if (_emission.live())
321 material->setEmission(osg::Material::FRONT_AND_BACK,
322 _emission.rgbaVec4());
323 if (_shininess.live())
324 material->setShininess(osg::Material::FRONT_AND_BACK,
325 _shininess.getValue());
326 if (_transparency.live()) {
327 float alpha = _transparency.getValue();
328 material->setAlpha(osg::Material::FRONT_AND_BACK, alpha);
330 stateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
331 stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
333 stateSet->setRenderingHint(osg::StateSet::DEFAULT_BIN);
341 SGSharedPtr<const SGCondition> _condition;
342 SGSharedPtr<const SGPropertyNode> _textureProp;
343 SGSharedPtr<const SGPropertyNode> _thresholdProp;
344 std::string _textureName;
350 PropSpec _transparency;
351 osgDB::FilePathList _texturePathList;
356 SGMaterialAnimation::SGMaterialAnimation(const SGPropertyNode* configNode,
357 SGPropertyNode* modelRoot,
358 const osgDB::ReaderWriter::Options*
360 SGAnimation(configNode, modelRoot),
361 texturePathList(options->getDatabasePathList())
363 if (configNode->hasChild("global"))
364 SG_LOG(SG_IO, SG_ALERT, "Use of <global> in material animation is "
365 "no longer supported");
369 SGMaterialAnimation::createAnimationGroup(osg::Group& parent)
371 osg::Group* group = new osg::Group;
372 group->setName("material animation group");
374 SGPropertyNode* inputRoot = getModelRoot();
375 const SGPropertyNode* node = getConfig()->getChild("property-base");
377 inputRoot = getModelRoot()->getNode(node->getStringValue(), true);
378 if (getConfig()->hasChild("texture-prop"))
379 osg::StateSet* stateSet = group->getOrCreateStateSet();
380 if (getConfig()->hasChild("texture")) {
381 std::string textureName = getConfig()->getStringValue("texture");
382 std::string textureFile;
383 textureFile = osgDB::findFileInPath(textureName, texturePathList);
384 if (!textureFile.empty()) {
385 osg::StateSet* stateSet = group->getOrCreateStateSet();
386 osg::Texture2D* texture2D = SGLoadTexture2D(textureFile);
388 stateSet->setTextureAttribute(0, texture2D,
389 osg::StateAttribute::OVERRIDE);
390 stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
391 if (texture2D->getImage()->isImageTranslucent()) {
392 stateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
393 stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
398 if (getConfig()->hasChild("threshold-prop") ||
399 getConfig()->hasChild("threshold")) {
400 osg::StateSet* stateSet = group->getOrCreateStateSet();
401 osg::AlphaFunc* alphaFunc = new osg::AlphaFunc;
402 alphaFunc->setFunction(osg::AlphaFunc::GREATER);
403 float threshold = getConfig()->getFloatValue("threshold", 0);
404 alphaFunc->setReferenceValue(threshold);
405 stateSet->setAttribute(alphaFunc, osg::StateAttribute::OVERRIDE);
408 unsigned suppliedColors = 0;
409 if (getConfig()->hasChild("ambient"))
410 suppliedColors |= AMBIENT;
411 if (getConfig()->hasChild("diffuse"))
412 suppliedColors |= DIFFUSE;
413 if (getConfig()->hasChild("specular"))
414 suppliedColors |= SPECULAR;
415 if (getConfig()->hasChild("emission"))
416 suppliedColors |= EMISSION;
417 if (getConfig()->hasChild("shininess")
418 || getConfig()->hasChild("shininess-prop"))
419 suppliedColors |= SHININESS;
420 if (getConfig()->hasChild("transparency"))
421 suppliedColors |= TRANSPARENCY;
423 if (suppliedColors != 0) {
424 osg::StateSet* stateSet = group->getOrCreateStateSet();
427 if (defaultMaterial.valid()) {
428 mat = defaultMaterial.get();
431 mat = new osg::Material;
432 mat->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
434 mat->setDataVariance(osg::Object::DYNAMIC);
435 unsigned defaultColorModeMask = 0;
436 mat->setUpdateCallback(0); // Just to make sure.
437 switch (mat->getColorMode()) {
438 case osg::Material::OFF:
439 defaultColorModeMask = 0;
441 case osg::Material::AMBIENT:
442 defaultColorModeMask = AMBIENT;
444 case osg::Material::DIFFUSE:
445 defaultColorModeMask = DIFFUSE;
447 case osg::Material::AMBIENT_AND_DIFFUSE:
448 defaultColorModeMask = AMBIENT | DIFFUSE;
450 case osg::Material::SPECULAR:
451 defaultColorModeMask = SPECULAR;
453 case osg::Material::EMISSION:
454 defaultColorModeMask = EMISSION;
457 // Copy the color found by traversing geometry into the material
458 // in case we need to specify it (e.g., transparency) and it is
459 // not specified by the animation.
460 if (defaultAmbientDiffuse.x() >= 0) {
461 if (defaultColorModeMask & AMBIENT)
462 mat->setAmbient(osg::Material::FRONT_AND_BACK, defaultAmbientDiffuse);
463 if (defaultColorModeMask & DIFFUSE)
464 mat->setDiffuse(osg::Material::FRONT_AND_BACK, defaultAmbientDiffuse);
466 // Compute which colors in the animation override colors set via
467 // colorMode / glColor, and set the colorMode for the animation's
468 // material accordingly.
469 if (suppliedColors & TRANSPARENCY) {
470 // All colors will be affected by the material. Hope all the
471 // defaults are fine, if needed.
472 mat->setColorMode(osg::Material::OFF);
473 } else if ((suppliedColors & defaultColorModeMask) != 0) {
474 // First deal with the complicated AMBIENT/DIFFUSE case.
475 if ((defaultColorModeMask & AMBIENT_DIFFUSE) != 0) {
476 // glColor can supply colors not specified by the animation.
477 unsigned matColorModeMask = ((~suppliedColors & defaultColorModeMask)
479 if ((matColorModeMask & DIFFUSE) != 0)
480 mat->setColorMode(osg::Material::DIFFUSE);
481 else if ((matColorModeMask & AMBIENT) != 0)
482 mat->setColorMode(osg::Material::AMBIENT);
484 mat->setColorMode(osg::Material::OFF);
486 // The animation overrides the glColor color.
487 mat->setColorMode(osg::Material::OFF);
490 // No overlap between the animation and color mode, so leave
491 // the color mode alone.
493 stateSet->setAttribute(mat,(osg::StateAttribute::ON
494 | osg::StateAttribute::OVERRIDE));
496 group->setUpdateCallback(new UpdateCallback(texturePathList,
498 getConfig(), inputRoot));
499 parent.addChild(group);
504 SGMaterialAnimation::install(osg::Node& node)
506 SGAnimation::install(node);
508 MaterialDefaultsVisitor defaultsVisitor;
509 node.accept(defaultsVisitor);
510 if (defaultsVisitor.material.valid()) {
512 = static_cast<osg::Material*>(defaultsVisitor.material->clone(osg::CopyOp::SHALLOW_COPY));
514 defaultAmbientDiffuse = defaultsVisitor.ambientDiffuse;