]> git.mxchange.org Git - simgear.git/commitdiff
Repaint 2D cloud layers using texture combiner
authortimoore <timoore>
Fri, 21 Dec 2007 06:25:13 +0000 (06:25 +0000)
committertimoore <timoore>
Fri, 21 Dec 2007 06:25:13 +0000 (06:25 +0000)
Don't change the color in the cloud layer geometry

simgear/scene/sky/cloud.cxx
simgear/scene/util/StateAttributeFactory.cxx
simgear/scene/util/StateAttributeFactory.hxx

index 0b532fd03f37df98ab499e9a0505a26298664b96..cc708724eeb8bcaff5c8eb3f396894e703ab1f3a 100644 (file)
@@ -37,6 +37,7 @@
 #include <osg/Material>
 #include <osg/ShadeModel>
 #include <osg/TexEnv>
+#include <osg/TexEnvCombine>
 #include <osg/Texture2D>
 #include <osg/TextureCubeMap>
 #include <osg/TexMat>
@@ -91,7 +92,6 @@ SGMakeState(const SGPath &path, const char* colorTexture,
                                                      options.get()));
     stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
     StateAttributeFactory* attribFactory = StateAttributeFactory::instance();
-    stateSet->setTextureAttribute(0, attribFactory->getStandardTexEnv());
     stateSet->setAttributeAndModes(attribFactory->getSmoothShadeModel());
     stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
     stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
@@ -153,7 +153,40 @@ SGCloudLayer::SGCloudLayer( const string &tex_path ) :
   layer_root->addChild(group_top.get());
   osg::StateSet *rootSet = layer_root->getOrCreateStateSet();
   rootSet->setRenderBinDetails(CLOUDS_BIN, "DepthSortedBin");
-  rootSet->setTextureAttribute(0, new osg::TexMat());
+  rootSet->setTextureAttribute(0, new osg::TexMat);
+  // Combiner for fog color and cloud alpha
+  osg::TexEnvCombine* combine0 = new osg::TexEnvCombine;
+  osg::TexEnvCombine* combine1 = new osg::TexEnvCombine;
+  combine0->setCombine_RGB(osg::TexEnvCombine::MODULATE);
+  combine0->setSource0_RGB(osg::TexEnvCombine::PREVIOUS);
+  combine0->setOperand0_RGB(osg::TexEnvCombine::SRC_COLOR);
+  combine0->setSource1_RGB(osg::TexEnvCombine::TEXTURE0);
+  combine0->setOperand1_RGB(osg::TexEnvCombine::SRC_COLOR);
+  combine0->setCombine_Alpha(osg::TexEnvCombine::MODULATE);
+  combine0->setSource0_Alpha(osg::TexEnvCombine::PREVIOUS);
+  combine0->setOperand0_Alpha(osg::TexEnvCombine::SRC_ALPHA);
+  combine0->setSource1_Alpha(osg::TexEnvCombine::TEXTURE0);
+  combine0->setOperand1_Alpha(osg::TexEnvCombine::SRC_ALPHA);
+
+  combine1->setCombine_RGB(osg::TexEnvCombine::MODULATE);
+  combine1->setSource0_RGB(osg::TexEnvCombine::PREVIOUS);
+  combine1->setOperand0_RGB(osg::TexEnvCombine::SRC_COLOR);
+  combine1->setSource1_RGB(osg::TexEnvCombine::CONSTANT);
+  combine1->setOperand1_RGB(osg::TexEnvCombine::SRC_COLOR);
+  combine1->setCombine_Alpha(osg::TexEnvCombine::MODULATE);
+  combine1->setSource0_Alpha(osg::TexEnvCombine::PREVIOUS);
+  combine1->setOperand0_Alpha(osg::TexEnvCombine::SRC_ALPHA);
+  combine1->setSource1_Alpha(osg::TexEnvCombine::CONSTANT);
+  combine1->setOperand1_Alpha(osg::TexEnvCombine::SRC_ALPHA);
+  combine1->setDataVariance(osg::Object::DYNAMIC);
+  rootSet->setTextureAttributeAndModes(0, combine0);
+  rootSet->setTextureAttributeAndModes(1, combine1);
+  rootSet->setTextureMode(1, GL_TEXTURE_2D, osg::StateAttribute::ON);
+  rootSet->setTextureAttributeAndModes(1, StateAttributeFactory::instance()
+                                       ->getWhiteTexture(),
+                                       osg::StateAttribute::ON);
+  rootSet->setDataVariance(osg::Object::DYNAMIC);
+
   base = osg::Vec2(sg_random(), sg_random());
 
   group_top->addChild(layer_transform.get());
@@ -516,8 +549,7 @@ SGCloudLayer::rebuild()
     
     //OSGFIXME: true
     if ( layer_states[layer_coverage].valid() ) {
-      osg::CopyOp copyOp(osg::CopyOp::DEEP_COPY_ALL
-                         & ~osg::CopyOp::DEEP_COPY_TEXTURES);
+      osg::CopyOp copyOp;    // shallow copy
       // render bin will be set in reposition
       osg::StateSet* stateSet = static_cast<osg::StateSet*>(layer_states2[layer_coverage]->clone(copyOp));
       stateSet->setDataVariance(osg::Object::DYNAMIC);
@@ -783,30 +815,12 @@ ss->setTextureAttributeAndModes(0, te, osg::StateAttribute::OVERRIDE | osg::Stat
 
 // repaint the cloud layer colors
 bool SGCloudLayer::repaint( const SGVec3f& fog_color ) {
-  for ( int i = 0; i < 4; i++ ) {
-    osg::Vec4 color(fog_color.osg(), 1);
-    color[3] = (i == 0) ? 0.0f : cloud_alpha * 0.15f;
-    (*cl[i])[0] = color;
-    
-    for ( int j = 0; j < 4; ++j ) {
-      color[3] =
-        ((j == 0) || (i == 3)) ?
-        ((j == 0) && (i == 3)) ? 0.0f : cloud_alpha * 0.15f : cloud_alpha;
-      (*cl[i])[(2*j) + 1] = color;
-      
-      color[3] = 
-        ((j == 3) || (i == 0)) ?
-        ((j == 3) && (i == 0)) ? 0.0f : cloud_alpha * 0.15f : cloud_alpha;
-      (*cl[i])[(2*j) + 2] = color;
-    }
-    
-    color[3] = (i == 3) ? 0.0f : cloud_alpha * 0.15f;
-    (*cl[i])[9] = color;
-    
-    cl[i]->dirty();
-  }
-
-  return true;
+    osg::Vec4f combineColor(fog_color.osg(), cloud_alpha);
+    osg::TexEnvCombine* combiner
+        = dynamic_cast<osg::TexEnvCombine*>(layer_root->getStateSet()
+                                            ->getTextureAttribute(1, osg::StateAttribute::TEXENV));
+    combiner->setConstantColor(combineColor);
+    return true;
 }
 
 // reposition the cloud layer at the specified origin and orientation
index 3071ef3efe1bb16a3e3b451ebee1a18fca46704f..17a1e305e72ea6e7835638a66ae8a0f557e97c6a 100644 (file)
@@ -1,5 +1,27 @@
+/* -*-c++-*-
+ *
+ * Copyright (C) 2007 Tim Moore
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ */
+
 #include <OpenThreads/ScopedLock>
 
+#include <osg/Image>
 #include "StateAttributeFactory.hxx"
 
 using namespace osg;
@@ -24,6 +46,17 @@ StateAttributeFactory::StateAttributeFactory()
     _standardTexEnv = new TexEnv;
     _standardTexEnv->setMode(TexEnv::MODULATE);
     _standardTexEnv->setDataVariance(Object::STATIC);
+    osg::Image *dummyImage = new osg::Image;
+    dummyImage->allocateImage(1, 1, 1, GL_LUMINANCE_ALPHA,
+                              GL_UNSIGNED_BYTE);
+    unsigned char* imageBytes = dummyImage->data(0, 0);
+    imageBytes[0] = 255;
+    imageBytes[1] = 255;
+    _whiteTexture = new osg::Texture2D;
+    _whiteTexture->setImage(dummyImage);
+    _whiteTexture->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
+    _whiteTexture->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT);
+    _whiteTexture->setDataVariance(osg::Object::STATIC);
 }
 
 osg::ref_ptr<StateAttributeFactory> StateAttributeFactory::_theInstance;
index f8192df90720eeaf98c5521482fcd3a6dc04406a..080a386f0e9bba516d20206e02058900bacb1071 100644 (file)
@@ -1,3 +1,24 @@
+/* -*-c++-*-
+ *
+ * Copyright (C) 2007 Tim Moore
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ */
+
 #ifndef SIMGEAR_STATEATTRIBUTEFACTORY_HXX
 #define SIMGEAR_STATEATTRIBUTEFACTORY_HXX 1
 
@@ -6,6 +27,7 @@
 #include <osg/AlphaFunc>
 #include <osg/BlendFunc>
 #include <osg/ShadeModel>
+#include <osg/Texture2D>
 #include <osg/TexEnv>
 
 // Return read-only instances of common OSG state attributes.
@@ -21,6 +43,8 @@ public:
     osg::TexEnv* getStandardTexEnv() { return _standardTexEnv.get(); }
     osg::ShadeModel* getSmoothShadeModel() { return _smooth.get(); }
     osg::ShadeModel* getFlatShadeModel() { return _flat.get(); }
+    // White, repeating texture
+    osg::Texture2D* getWhiteTexture() { return _whiteTexture.get(); }
     static StateAttributeFactory* instance();
 protected:
     StateAttributeFactory();
@@ -29,6 +53,7 @@ protected:
     osg::ref_ptr<osg::ShadeModel> _flat;
     osg::ref_ptr<osg::BlendFunc> _standardBlendFunc;
     osg::ref_ptr<osg::TexEnv> _standardTexEnv;
+    osg::ref_ptr<osg::Texture2D> _whiteTexture;
     static osg::ref_ptr<StateAttributeFactory> _theInstance;
     static OpenThreads::Mutex _instanceMutex;
 };