Repaint 2D cloud layers using texture combiner
Don't change the color in the cloud layer geometry
This commit is contained in:
+42
-28
@@ -37,6 +37,7 @@
|
|||||||
#include <osg/Material>
|
#include <osg/Material>
|
||||||
#include <osg/ShadeModel>
|
#include <osg/ShadeModel>
|
||||||
#include <osg/TexEnv>
|
#include <osg/TexEnv>
|
||||||
|
#include <osg/TexEnvCombine>
|
||||||
#include <osg/Texture2D>
|
#include <osg/Texture2D>
|
||||||
#include <osg/TextureCubeMap>
|
#include <osg/TextureCubeMap>
|
||||||
#include <osg/TexMat>
|
#include <osg/TexMat>
|
||||||
@@ -91,7 +92,6 @@ SGMakeState(const SGPath &path, const char* colorTexture,
|
|||||||
options.get()));
|
options.get()));
|
||||||
stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
|
stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
|
||||||
StateAttributeFactory* attribFactory = StateAttributeFactory::instance();
|
StateAttributeFactory* attribFactory = StateAttributeFactory::instance();
|
||||||
stateSet->setTextureAttribute(0, attribFactory->getStandardTexEnv());
|
|
||||||
stateSet->setAttributeAndModes(attribFactory->getSmoothShadeModel());
|
stateSet->setAttributeAndModes(attribFactory->getSmoothShadeModel());
|
||||||
stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
|
stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
|
||||||
stateSet->setMode(GL_CULL_FACE, 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());
|
layer_root->addChild(group_top.get());
|
||||||
osg::StateSet *rootSet = layer_root->getOrCreateStateSet();
|
osg::StateSet *rootSet = layer_root->getOrCreateStateSet();
|
||||||
rootSet->setRenderBinDetails(CLOUDS_BIN, "DepthSortedBin");
|
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());
|
base = osg::Vec2(sg_random(), sg_random());
|
||||||
|
|
||||||
group_top->addChild(layer_transform.get());
|
group_top->addChild(layer_transform.get());
|
||||||
@@ -516,8 +549,7 @@ SGCloudLayer::rebuild()
|
|||||||
|
|
||||||
//OSGFIXME: true
|
//OSGFIXME: true
|
||||||
if ( layer_states[layer_coverage].valid() ) {
|
if ( layer_states[layer_coverage].valid() ) {
|
||||||
osg::CopyOp copyOp(osg::CopyOp::DEEP_COPY_ALL
|
osg::CopyOp copyOp; // shallow copy
|
||||||
& ~osg::CopyOp::DEEP_COPY_TEXTURES);
|
|
||||||
// render bin will be set in reposition
|
// render bin will be set in reposition
|
||||||
osg::StateSet* stateSet = static_cast<osg::StateSet*>(layer_states2[layer_coverage]->clone(copyOp));
|
osg::StateSet* stateSet = static_cast<osg::StateSet*>(layer_states2[layer_coverage]->clone(copyOp));
|
||||||
stateSet->setDataVariance(osg::Object::DYNAMIC);
|
stateSet->setDataVariance(osg::Object::DYNAMIC);
|
||||||
@@ -783,30 +815,12 @@ ss->setTextureAttributeAndModes(0, te, osg::StateAttribute::OVERRIDE | osg::Stat
|
|||||||
|
|
||||||
// repaint the cloud layer colors
|
// repaint the cloud layer colors
|
||||||
bool SGCloudLayer::repaint( const SGVec3f& fog_color ) {
|
bool SGCloudLayer::repaint( const SGVec3f& fog_color ) {
|
||||||
for ( int i = 0; i < 4; i++ ) {
|
osg::Vec4f combineColor(fog_color.osg(), cloud_alpha);
|
||||||
osg::Vec4 color(fog_color.osg(), 1);
|
osg::TexEnvCombine* combiner
|
||||||
color[3] = (i == 0) ? 0.0f : cloud_alpha * 0.15f;
|
= dynamic_cast<osg::TexEnvCombine*>(layer_root->getStateSet()
|
||||||
(*cl[i])[0] = color;
|
->getTextureAttribute(1, osg::StateAttribute::TEXENV));
|
||||||
|
combiner->setConstantColor(combineColor);
|
||||||
for ( int j = 0; j < 4; ++j ) {
|
return true;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// reposition the cloud layer at the specified origin and orientation
|
// reposition the cloud layer at the specified origin and orientation
|
||||||
|
|||||||
@@ -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 <OpenThreads/ScopedLock>
|
||||||
|
|
||||||
|
#include <osg/Image>
|
||||||
#include "StateAttributeFactory.hxx"
|
#include "StateAttributeFactory.hxx"
|
||||||
|
|
||||||
using namespace osg;
|
using namespace osg;
|
||||||
@@ -24,6 +46,17 @@ StateAttributeFactory::StateAttributeFactory()
|
|||||||
_standardTexEnv = new TexEnv;
|
_standardTexEnv = new TexEnv;
|
||||||
_standardTexEnv->setMode(TexEnv::MODULATE);
|
_standardTexEnv->setMode(TexEnv::MODULATE);
|
||||||
_standardTexEnv->setDataVariance(Object::STATIC);
|
_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;
|
osg::ref_ptr<StateAttributeFactory> StateAttributeFactory::_theInstance;
|
||||||
|
|||||||
@@ -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
|
#ifndef SIMGEAR_STATEATTRIBUTEFACTORY_HXX
|
||||||
#define SIMGEAR_STATEATTRIBUTEFACTORY_HXX 1
|
#define SIMGEAR_STATEATTRIBUTEFACTORY_HXX 1
|
||||||
|
|
||||||
@@ -6,6 +27,7 @@
|
|||||||
#include <osg/AlphaFunc>
|
#include <osg/AlphaFunc>
|
||||||
#include <osg/BlendFunc>
|
#include <osg/BlendFunc>
|
||||||
#include <osg/ShadeModel>
|
#include <osg/ShadeModel>
|
||||||
|
#include <osg/Texture2D>
|
||||||
#include <osg/TexEnv>
|
#include <osg/TexEnv>
|
||||||
|
|
||||||
// Return read-only instances of common OSG state attributes.
|
// Return read-only instances of common OSG state attributes.
|
||||||
@@ -21,6 +43,8 @@ public:
|
|||||||
osg::TexEnv* getStandardTexEnv() { return _standardTexEnv.get(); }
|
osg::TexEnv* getStandardTexEnv() { return _standardTexEnv.get(); }
|
||||||
osg::ShadeModel* getSmoothShadeModel() { return _smooth.get(); }
|
osg::ShadeModel* getSmoothShadeModel() { return _smooth.get(); }
|
||||||
osg::ShadeModel* getFlatShadeModel() { return _flat.get(); }
|
osg::ShadeModel* getFlatShadeModel() { return _flat.get(); }
|
||||||
|
// White, repeating texture
|
||||||
|
osg::Texture2D* getWhiteTexture() { return _whiteTexture.get(); }
|
||||||
static StateAttributeFactory* instance();
|
static StateAttributeFactory* instance();
|
||||||
protected:
|
protected:
|
||||||
StateAttributeFactory();
|
StateAttributeFactory();
|
||||||
@@ -29,6 +53,7 @@ protected:
|
|||||||
osg::ref_ptr<osg::ShadeModel> _flat;
|
osg::ref_ptr<osg::ShadeModel> _flat;
|
||||||
osg::ref_ptr<osg::BlendFunc> _standardBlendFunc;
|
osg::ref_ptr<osg::BlendFunc> _standardBlendFunc;
|
||||||
osg::ref_ptr<osg::TexEnv> _standardTexEnv;
|
osg::ref_ptr<osg::TexEnv> _standardTexEnv;
|
||||||
|
osg::ref_ptr<osg::Texture2D> _whiteTexture;
|
||||||
static osg::ref_ptr<StateAttributeFactory> _theInstance;
|
static osg::ref_ptr<StateAttributeFactory> _theInstance;
|
||||||
static OpenThreads::Mutex _instanceMutex;
|
static OpenThreads::Mutex _instanceMutex;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user