Show
Ignore:
Timestamp:
04/22/09 13:00:20 (6 years ago)
Author:
robert
Message:

From Eduardo Alberto Hernández Muñoz, change to using TexGenNode? for setting up tex coordinates

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/examples/osgtexture1D/osgtexture1D.cpp

    r7648 r10066  
    1919#include <osg/Notify> 
    2020#include <osg/Texture1D> 
    21 #include <osg/TexGen> 
     21#include <osg/TexGenNode> 
    2222#include <osg/Material> 
    2323 
     
    2929#include <iostream> 
    3030 
    31 // Creates a stateset which contains a 1D texture which is populated by contour banded color 
    32 // this is then used in conjunction with TexGen to create contoured models, either in  
    33 // object linear coords - like contours on a map, or eye linear which contour the distance from 
    34 // the eye. An app callback toggles between the two tex gen modes. 
     31// Creates a stateset which contains a 1D texture which is populated by contour banded color, 
     32// and allows tex gen to override the S texture coordinate 
    3533osg::StateSet* create1DTextureStateToDecorate(osg::Node* loadedModel) 
    3634{ 
    37      
    38     const osg::BoundingSphere& bs = loadedModel->getBound(); 
    39      
    4035    osg::Image* image = new osg::Image; 
    4136 
     
    7772    texture->setFilter(osg::Texture1D::MIN_FILTER,osg::Texture1D::LINEAR); 
    7873    texture->setImage(image); 
    79  
    80     float zBase = bs.center().z()-bs.radius(); 
    81     float zScale = 2.0f/bs.radius(); 
    82      
    83     osg::TexGen* texgen = new osg::TexGen; 
    84     texgen->setMode(osg::TexGen::OBJECT_LINEAR); 
    85     texgen->setPlane(osg::TexGen::S,osg::Plane(0.0f,0.0f,zScale,-zBase)); 
    8674     
    8775    osg::Material* material = new osg::Material; 
     
    9482    stateset->setTextureMode(0,GL_TEXTURE_3D,osg::StateAttribute::OFF|osg::StateAttribute::OVERRIDE); 
    9583 
    96     stateset->setTextureAttribute(0,texgen,osg::StateAttribute::OVERRIDE); 
    9784    stateset->setTextureMode(0,GL_TEXTURE_GEN_S,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE); 
    9885     
     
    10491 
    10592// An app callback which alternates the tex gen mode between object linear and eye linear to illustrate what differences it makes. 
    106 class AnimateStateCallback : public osg::NodeCallback 
     93class AnimateTexGenCallback : public osg::NodeCallback 
    10794{ 
    10895    public: 
    109         AnimateStateCallback() {} 
     96        AnimateTexGenCallback() {} 
    11097         
    111         void animateState(osg::StateSet* stateset,double time) 
     98        void animateTexGen(osg::TexGenNode* texgenNode,double time) 
    11299        { 
    113100            // here we simply get any existing texgen, and then increment its 
    114101            // plane, pushing the R coordinate through the texture. 
    115             osg::StateAttribute* attribute = stateset->getTextureAttribute(0,osg::StateAttribute::TEXGEN); 
    116             osg::TexGen* texgen = dynamic_cast<osg::TexGen*>(attribute); 
    117             if (texgen) 
     102            const double timeInterval = 2.0f; 
     103             
     104            static double previousTime = time; 
     105            static bool state = false; 
     106            while (time>previousTime+timeInterval) 
    118107            { 
    119                 const double timeInterval = 1.0f; 
    120                  
    121                 static double previousTime = time; 
    122                 static bool state = false; 
    123                 while (time>previousTime+timeInterval) 
    124                 { 
    125                     previousTime+=timeInterval; 
    126                     state = !state; 
    127                 } 
    128              
    129                 if (state) 
    130                 { 
    131                     texgen->setMode(osg::TexGen::OBJECT_LINEAR); 
    132                 } 
    133                 else 
    134                 { 
    135                     texgen->setMode(osg::TexGen::EYE_LINEAR); 
    136                 } 
     108                previousTime+=timeInterval; 
     109                state = !state; 
     110            } 
     111         
     112            if (state) 
     113            { 
     114                texgenNode->getTexGen()->setMode(osg::TexGen::OBJECT_LINEAR); 
     115            } 
     116            else 
     117            { 
     118                texgenNode->getTexGen()->setMode(osg::TexGen::EYE_LINEAR); 
    137119            } 
    138120             
     
    141123        virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) 
    142124        {  
     125            osg::TexGenNode* texgenNode = dynamic_cast<osg::TexGenNode*>(node); 
    143126 
    144             osg::StateSet* stateset = node->getStateSet(); 
    145             if (stateset && nv->getFrameStamp()) 
     127            if (texgenNode && nv->getFrameStamp()) 
    146128            { 
    147129                // we have an exisitng stateset, so lets animate it. 
    148                 animateState(stateset,nv->getFrameStamp()->getSimulationTime()); 
     130                animateTexGen(texgenNode,nv->getFrameStamp()->getSimulationTime()); 
    149131            } 
    150132 
     
    187169 
    188170    loadedModel->setStateSet(stateset); 
    189     loadedModel->setUpdateCallback(new AnimateStateCallback()); 
    190171 
    191     // add model to viewer. 
    192     viewer.setSceneData( loadedModel ); 
     172    osg:: Group *root = new osg:: Group; 
     173    root -> addChild( loadedModel ); 
     174 
     175    // The contour banded color texture is used in conjunction with TexGenNode 
     176    // to create contoured models, either in object linear coords - like 
     177    // contours on a map, or eye linear which contour the distance from 
     178    // the eye. An app callback toggles between the two tex gen modes. 
     179    osg::TexGenNode* texgenNode = new osg::TexGenNode; 
     180    texgenNode->setReferenceFrame( osg::TexGenNode::ABSOLUTE_RF ); 
     181    texgenNode->getTexGen()->setMode( osg::TexGen::OBJECT_LINEAR ); 
     182 
     183    const osg::BoundingSphere& bs = loadedModel->getBound(); 
     184    float zBase = bs.center().z()-bs.radius(); 
     185    float zScale = 2.0f/bs.radius(); 
     186    texgenNode->getTexGen()->setPlane(osg::TexGen::S,osg::Plane(0.0f,0.0f,zScale,-zBase)); 
     187 
     188    texgenNode->setUpdateCallback(new AnimateTexGenCallback()); 
     189 
     190    root -> addChild( texgenNode ); 
     191 
     192 
     193    viewer.setSceneData( root ); 
    193194 
    194195    return viewer.run();