]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/model/animation.cxx
- better error message when submodel loading failed
[simgear.git] / simgear / scene / model / animation.cxx
index 92d1bda5a65d0235407c34f8082c5f284a409cfa..5dddc5bd23f567dacef0b97eefc44fd3a4c5f43a 100644 (file)
@@ -3,6 +3,9 @@
 //
 // This file is in the Public Domain, and comes with no warranty.
 
+#ifdef HAVE_CONFIG_H
+#  include <simgear_config.h>
+#endif
 
 #include <string.h>             // for strcmp()
 #include <math.h>
@@ -179,7 +182,8 @@ double SGAnimation::sim_time_sec = 0.0;
 SGPersonalityBranch *SGAnimation::current_object = 0;
 
 SGAnimation::SGAnimation (SGPropertyNode_ptr props, ssgBranch * branch)
-    : _branch(branch)
+    : _branch(branch),
+    animation_type(0)
 {
     _branch->setName(props->getStringValue("name", 0));
     if ( props->getBoolValue( "enable-hot", true ) ) {
@@ -630,14 +634,18 @@ SGRotateAnimation::SGRotateAnimation( SGPropertyNode *prop_root,
     _center[0] = 0;
     _center[1] = 0;
     _center[2] = 0;
-    if (props->hasValue("axis/x1-m")) {
+    if (props->hasValue("axis/x") || props->hasValue("axis/y") || props->hasValue("axis/z")) {
+       _axis[0] = props->getFloatValue("axis/x", 0);
+       _axis[1] = props->getFloatValue("axis/y", 0);
+       _axis[2] = props->getFloatValue("axis/z", 0);
+    } else {
         double x1,y1,z1,x2,y2,z2;
-        x1 = props->getFloatValue("axis/x1-m");
-        y1 = props->getFloatValue("axis/y1-m");
-        z1 = props->getFloatValue("axis/z1-m");
-        x2 = props->getFloatValue("axis/x2-m");
-        y2 = props->getFloatValue("axis/y2-m");
-        z2 = props->getFloatValue("axis/z2-m");
+        x1 = props->getFloatValue("axis/x1-m", 0);
+        y1 = props->getFloatValue("axis/y1-m", 0);
+        z1 = props->getFloatValue("axis/z1-m", 0);
+        x2 = props->getFloatValue("axis/x2-m", 0);
+        y2 = props->getFloatValue("axis/y2-m", 0);
+        z2 = props->getFloatValue("axis/z2-m", 0);
         _center[0] = (x1+x2)/2;
         _center[1]= (y1+y2)/2;
         _center[2] = (z1+z2)/2;
@@ -645,15 +653,12 @@ SGRotateAnimation::SGRotateAnimation( SGPropertyNode *prop_root,
         _axis[0] = (x2-x1)/vector_length;
         _axis[1] = (y2-y1)/vector_length;
         _axis[2] = (z2-z1)/vector_length;
-    } else {
-       _axis[0] = props->getFloatValue("axis/x", 0);
-       _axis[1] = props->getFloatValue("axis/y", 0);
-       _axis[2] = props->getFloatValue("axis/z", 0);
     }
-    if (props->hasValue("center/x-m")) {
-       _center[0] = props->getFloatValue("center/x-m", 0);
-       _center[1] = props->getFloatValue("center/y-m", 0);
-       _center[2] = props->getFloatValue("center/z-m", 0);
+    if (props->hasValue("center/x-m") || props->hasValue("center/y-m")
+            || props->hasValue("center/z-m")) {
+        _center[0] = props->getFloatValue("center/x-m", 0);
+        _center[1] = props->getFloatValue("center/y-m", 0);
+        _center[2] = props->getFloatValue("center/z-m", 0);
     }
     sgNormalizeVec3(_axis);
 }
@@ -770,7 +775,7 @@ SGTranslateAnimation::update()
 {
   if (_condition == 0 || _condition->test()) {
     if (_table == 0) {
-      _position_m = (_prop->getDoubleValue() + _offset_m) * _factor;
+      _position_m = (_prop->getDoubleValue() * _factor) + _offset_m;
       if (_has_min && _position_m < _min_m)
         _position_m = _min_m;
       if (_has_max && _position_m > _max_m)
@@ -1118,6 +1123,7 @@ void SGAlphaTestAnimation::setAlphaClampToBranch(ssgBranch *b, float clamp)
 SGMaterialAnimation::SGMaterialAnimation( SGPropertyNode *prop_root,
         SGPropertyNode_ptr props, const SGPath &texture_path)
     : SGAnimation(props, new ssgBranch),
+    _last_condition(false),
     _prop_root(prop_root),
     _prop_base(""),
     _texture_base(texture_path),
@@ -1146,9 +1152,29 @@ SGMaterialAnimation::SGMaterialAnimation( SGPropertyNode *prop_root,
     if (_shi >= 0.0)
         _update |= SHININESS;
 
-    _trans = props->getFloatValue("transparency", -1.0);
-    if (_trans >= 0.0)
-        _update |= TRANSPARENCY;
+    SGPropertyNode_ptr group = props->getChild("transparency");
+    if (group) {
+        _trans.value = group->getFloatValue("alpha", -1.0);
+        _trans.factor = group->getFloatValue("factor", 1.0);
+        _trans.offset = group->getFloatValue("offset", 0.0);
+        _trans.min = group->getFloatValue("min", 0.0);
+        if (_trans.min < 0.0)
+            _trans.min = 0.0;
+        _trans.max = group->getFloatValue("max", 1.0);
+        if (_trans.max > 1.0)
+            _trans.max = 1.0;
+        if (_trans.dirty())
+            _update |= TRANSPARENCY;
+
+        n = group->getChild("alpha-prop");
+        _trans.value_prop = n ? _prop_root->getNode(path(n->getStringValue()), true) : 0;
+        n = group->getChild("factor-prop");
+        _trans.factor_prop = n ? _prop_root->getNode(path(n->getStringValue()), true) : 0;
+        n = group->getChild("offset-prop");
+        _trans.offset_prop = n ? _prop_root->getNode(path(n->getStringValue()), true) : 0;
+        if (_trans.live())
+            _read |= TRANSPARENCY;
+    }
 
     _thresh = props->getFloatValue("threshold", -1.0);
     if (_thresh >= 0.0)
@@ -1163,12 +1189,12 @@ SGMaterialAnimation::SGMaterialAnimation( SGPropertyNode *prop_root,
 
     n = props->getChild("shininess-prop");
     _shi_prop = n ? _prop_root->getNode(path(n->getStringValue()), true) : 0;
-    n = props->getChild("transparency-prop");
-    _trans_prop = n ? _prop_root->getNode(path(n->getStringValue()), true) : 0;
     n = props->getChild("threshold-prop");
     _thresh_prop = n ? _prop_root->getNode(path(n->getStringValue()), true) : 0;
     n = props->getChild("texture-prop");
     _tex_prop = n ? _prop_root->getNode(path(n->getStringValue()), true) : 0;
+
+    _static_update = _update;
 }
 
 void SGMaterialAnimation::initColorGroup(SGPropertyNode_ptr group, ColorSpec *col, int flag)
@@ -1207,8 +1233,15 @@ void SGMaterialAnimation::init()
 
 int SGMaterialAnimation::update()
 {
-    if (_condition && !_condition->test())
-        return 2;
+    if (_condition) {
+        bool cond = _condition->test();
+        if (cond && !_last_condition)
+            _update |= _static_update;
+
+        _last_condition = cond;
+        if (!cond)
+            return 2;
+    }
 
     if (_read & DIFFUSE)
         updateColorGroup(&_diff, DIFFUSE);
@@ -1226,10 +1259,15 @@ int SGMaterialAnimation::update()
         if (_shi != f)
             _update |= SHININESS;
     }
-    if (_trans_prop) {
-        f = _trans;
-        _trans = _trans_prop->getFloatValue();
-        if (_trans != f)
+    if (_read & TRANSPARENCY) {
+        PropSpec tmp = _trans;
+        if (_trans.value_prop)
+            _trans.value = _trans.value_prop->getFloatValue();
+        if (_trans.factor_prop)
+            _trans.factor = _trans.factor_prop->getFloatValue();
+        if (_trans.offset_prop)
+            _trans.offset = _trans.offset_prop->getFloatValue();
+        if (_trans != tmp)
             _update |= TRANSPARENCY;
     }
     if (_thresh_prop) {
@@ -1296,16 +1334,21 @@ void SGMaterialAnimation::setMaterialBranch(ssgBranch *b)
         return;
 
     ssgSimpleState *s = (ssgSimpleState *)((ssgLeaf *)b)->getState();
-    s->disable(GL_COLOR_MATERIAL);
-    s->setColourMaterial(GL_AMBIENT_AND_DIFFUSE);
 
     if (_update & DIFFUSE) {
         float *v = _diff.rgba();
         SGfloat alpha = s->getMaterial(GL_DIFFUSE)[3];
+        s->setColourMaterial(GL_DIFFUSE);
+        s->enable(GL_COLOR_MATERIAL);
         s->setMaterial(GL_DIFFUSE, v[0], v[1], v[2], alpha);
+        s->disable(GL_COLOR_MATERIAL);
     }
-    if (_update & AMBIENT)
+    if (_update & AMBIENT) {
+        s->setColourMaterial(GL_AMBIENT);
+        s->enable(GL_COLOR_MATERIAL);
         s->setMaterial(GL_AMBIENT, _amb.rgba());
+        s->disable(GL_COLOR_MATERIAL);
+    }
     if (_update & EMISSION)
         s->setMaterial(GL_EMISSION, _emis.rgba());
     if (_update & SPECULAR)
@@ -1314,7 +1357,9 @@ void SGMaterialAnimation::setMaterialBranch(ssgBranch *b)
         s->setShininess(clamp(_shi, 0.0, 128.0));
     if (_update & TRANSPARENCY) {
         SGfloat *v = s->getMaterial(GL_DIFFUSE);
-        s->setMaterial(GL_DIFFUSE, v[0], v[1], v[2], 1.0 - clamp(_trans));
+        float trans = _trans.value * _trans.factor + _trans.offset;
+        trans = trans < _trans.min ? _trans.min : trans > _trans.max ? _trans.max : trans;
+        s->setMaterial(GL_DIFFUSE, v[0], v[1], v[2], trans);
     }
     if (_update & THRESHOLD)
         s->setAlphaClamp(clamp(_thresh));
@@ -1324,9 +1369,12 @@ void SGMaterialAnimation::setMaterialBranch(ssgBranch *b)
         SGfloat alpha = s->getMaterial(GL_DIFFUSE)[3];
         ssgTexture *tex = s->getTexture();
         if ((tex && tex->hasAlpha()) || alpha < 0.999) {
+            s->setColourMaterial(GL_DIFFUSE);
+            s->enable(GL_COLOR_MATERIAL);
             s->enable(GL_BLEND);
             s->enable(GL_ALPHA_TEST);
             s->setTranslucent();
+            s->disable(GL_COLOR_MATERIAL);
         } else {
             s->disable(GL_BLEND);
             s->disable(GL_ALPHA_TEST);
@@ -1467,4 +1515,39 @@ void SGDistScaleAnimation::distScaleCallback( sgMat4 r, sgFrustum *f, sgMat4 m )
   sgPreMultMat4( r, transform );
 }
 
+////////////////////////////////////////////////////////////////////////
+// Implementation of SGShadowAnimation
+////////////////////////////////////////////////////////////////////////
+
+SGShadowAnimation::SGShadowAnimation ( SGPropertyNode *prop_root,
+                   SGPropertyNode_ptr props )
+  : SGAnimation(props, new ssgBranch),
+    _condition(0),
+       _condition_value(true)
+{
+       animation_type = 1;
+       SGPropertyNode_ptr node = props->getChild("condition");
+       if (node != 0) {
+               _condition = sgReadCondition(prop_root, node);
+               _condition_value = false;
+       }
+}
+
+SGShadowAnimation::~SGShadowAnimation ()
+{
+       delete _condition;
+}
+
+int
+SGShadowAnimation::update()
+{
+       if (_condition)
+               _condition_value = _condition->test();
+       return 2;
+}
+
+bool SGShadowAnimation::get_condition_value(void) {
+       return _condition_value;
+}
+
 // end of animation.cxx