From: ehofman Date: Wed, 7 Jan 2004 09:07:57 +0000 (+0000) Subject: Frederic Bouvier: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=3d43bc04801e9c3b54d298a031467971cd0d3366;p=simgear.git Frederic Bouvier: The attached patch adds a new animation class, called SGAlphaTestAnimation, to enable alpha test in 3D models. This will remove many artefact caused by painting order of translucent models, and I need it for an upcoming model. In addition, I added a min-factor and a max-factor to the range animation to modulate min-m, min-property, max-m or max-property. --- diff --git a/simgear/scene/model/animation.cxx b/simgear/scene/model/animation.cxx index 8d471a4e..30e5d16a 100644 --- a/simgear/scene/model/animation.cxx +++ b/simgear/scene/model/animation.cxx @@ -217,23 +217,34 @@ SGNullAnimation::~SGNullAnimation () SGRangeAnimation::SGRangeAnimation (SGPropertyNode *prop_root, SGPropertyNode_ptr props) : SGAnimation(props, new ssgRangeSelector), - _min(0.0), _max(0.0) + _min(0.0), _max(0.0), _min_factor(1.0), _max_factor(1.0) + { float ranges[2]; - SGPropertyNode_ptr node = props->getChild( "min-property" ); + SGPropertyNode_ptr node = props->getChild( "min-factor" ); + if (node != 0) { + _min_factor = props->getFloatValue("min-factor", 1.0); + } + node = props->getChild( "max-factor" ); + if (node != 0) { + _max_factor = props->getFloatValue("max-factor", 1.0); + } + node = props->getChild( "min-property" ); if (node != 0) { _min_prop = (SGPropertyNode *)prop_root->getNode(node->getStringValue(), true); - ranges[0] = _min_prop->getFloatValue(); + ranges[0] = _min_prop->getFloatValue() * _min_factor; } else { - ranges[0] = _min = props->getFloatValue("min-m", 0); + _min = props->getFloatValue("min-m", 0); + ranges[0] = _min * _min_factor; } node = props->getChild( "max-property" ); if (node != 0) { _max_prop = (SGPropertyNode *)prop_root->getNode(node->getStringValue(), true); - ranges[1] = _max_prop->getFloatValue(); + ranges[1] = _max_prop->getFloatValue() * _max_factor; } else { - ranges[1] = _max = props->getFloatValue("max-m", 0); + _max = props->getFloatValue("max-m", 0); + ranges[1] = _max * _max_factor; } ((ssgRangeSelector *)_branch)->setRanges(ranges, 2); } @@ -248,19 +259,20 @@ SGRangeAnimation::update() float ranges[2]; bool upd = false; if (_min_prop != 0) { - ranges[0] = _min_prop->getFloatValue(); + ranges[0] = _min_prop->getFloatValue() * _min_factor; upd = true; } else { - ranges[0] = _min; + ranges[0] = _min * _min_factor; } if (_max_prop != 0) { - ranges[1] = _max_prop->getFloatValue(); + ranges[1] = _max_prop->getFloatValue() * _max_factor; upd = true; } else { - ranges[1] = _max; + ranges[1] = _max * _max_factor; } - if (upd) + if (upd) { ((ssgRangeSelector *)_branch)->setRanges(ranges, 2); + } } @@ -854,4 +866,45 @@ SGTexMultipleAnimation::update() ((ssgTexTrans *)_branch)->setTransform(tmatrix); } + + +//////////////////////////////////////////////////////////////////////// +// Implementation of SGAlphaTestAnimation +//////////////////////////////////////////////////////////////////////// + +SGAlphaTestAnimation::SGAlphaTestAnimation (SGPropertyNode *prop_root, + SGPropertyNode_ptr props) + : SGAnimation(props, new ssgBranch), + _done(false) +{ + _alpha_clamp = props->getFloatValue("alpha-factor", 0.0); +} + +SGAlphaTestAnimation::~SGAlphaTestAnimation () +{ +} + +void SGAlphaTestAnimation::update() +{ + if (!_done) { + _done = true; + setAlphaClampToBranch(_branch,_alpha_clamp); + } +} + +void SGAlphaTestAnimation::setAlphaClampToBranch(ssgBranch *b, float clamp) +{ + int nb = b->getNumKids(); + for (int i = 0; igetKid(i); + if (e->isAKindOf(ssgTypeLeaf())) { + ssgSimpleState*s = (ssgSimpleState*)((ssgLeaf*)e)->getState(); + s->enable( GL_ALPHA_TEST ); + s->setAlphaClamp( clamp ); + } else if (e->isAKindOf(ssgTypeBranch())) { + setAlphaClampToBranch( (ssgBranch*)e, clamp ); + } + } +} + // end of animation.cxx diff --git a/simgear/scene/model/animation.hxx b/simgear/scene/model/animation.hxx index 609c33d5..d5063feb 100644 --- a/simgear/scene/model/animation.hxx +++ b/simgear/scene/model/animation.hxx @@ -108,6 +108,8 @@ private: SGPropertyNode_ptr _max_prop; float _min; float _max; + float _min_factor; + float _max_factor; }; @@ -379,4 +381,21 @@ private: }; +/** + * An animation to enable the alpha test + */ +class SGAlphaTestAnimation : public SGAnimation +{ +public: + SGAlphaTestAnimation (SGPropertyNode *prop_root, + SGPropertyNode_ptr props); + virtual ~SGAlphaTestAnimation (); + virtual void update(); +private: + void setAlphaClampToBranch(ssgBranch *b, float clamp); + bool _done; + float _alpha_clamp; +}; + + #endif // _SG_ANIMATION_HXX diff --git a/simgear/scene/model/model.cxx b/simgear/scene/model/model.cxx index 956050db..96ebebaa 100644 --- a/simgear/scene/model/model.cxx +++ b/simgear/scene/model/model.cxx @@ -130,6 +130,8 @@ sgMakeAnimation( ssgBranch * model, animation = new SGTexMultipleAnimation(prop_root, node); } else if (!strcmp("blend", type)) { animation = new SGBlendAnimation(prop_root, node); + } else if (!strcmp("alpha-test", type)) { + animation = new SGAlphaTestAnimation(prop_root, node); } else { animation = new SGNullAnimation(node); SG_LOG(SG_INPUT, SG_WARN, "Unknown animation type " << type);