- Timestamp:
- 04/22/09 13:00:20 (4 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/examples/osgtexture1D/osgtexture1D.cpp
r7648 r10066 19 19 #include <osg/Notify> 20 20 #include <osg/Texture1D> 21 #include <osg/TexGen >21 #include <osg/TexGenNode> 22 22 #include <osg/Material> 23 23 … … 29 29 #include <iostream> 30 30 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 35 33 osg::StateSet* create1DTextureStateToDecorate(osg::Node* loadedModel) 36 34 { 37 38 const osg::BoundingSphere& bs = loadedModel->getBound();39 40 35 osg::Image* image = new osg::Image; 41 36 … … 77 72 texture->setFilter(osg::Texture1D::MIN_FILTER,osg::Texture1D::LINEAR); 78 73 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));86 74 87 75 osg::Material* material = new osg::Material; … … 94 82 stateset->setTextureMode(0,GL_TEXTURE_3D,osg::StateAttribute::OFF|osg::StateAttribute::OVERRIDE); 95 83 96 stateset->setTextureAttribute(0,texgen,osg::StateAttribute::OVERRIDE);97 84 stateset->setTextureMode(0,GL_TEXTURE_GEN_S,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE); 98 85 … … 104 91 105 92 // An app callback which alternates the tex gen mode between object linear and eye linear to illustrate what differences it makes. 106 class Animate StateCallback : public osg::NodeCallback93 class AnimateTexGenCallback : public osg::NodeCallback 107 94 { 108 95 public: 109 Animate StateCallback() {}96 AnimateTexGenCallback() {} 110 97 111 void animate State(osg::StateSet* stateset,double time)98 void animateTexGen(osg::TexGenNode* texgenNode,double time) 112 99 { 113 100 // here we simply get any existing texgen, and then increment its 114 101 // 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) 118 107 { 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); 137 119 } 138 120 … … 141 123 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) 142 124 { 125 osg::TexGenNode* texgenNode = dynamic_cast<osg::TexGenNode*>(node); 143 126 144 osg::StateSet* stateset = node->getStateSet(); 145 if (stateset && nv->getFrameStamp()) 127 if (texgenNode && nv->getFrameStamp()) 146 128 { 147 129 // we have an exisitng stateset, so lets animate it. 148 animate State(stateset,nv->getFrameStamp()->getSimulationTime());130 animateTexGen(texgenNode,nv->getFrameStamp()->getSimulationTime()); 149 131 } 150 132 … … 187 169 188 170 loadedModel->setStateSet(stateset); 189 loadedModel->setUpdateCallback(new AnimateStateCallback());190 171 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 ); 193 194 194 195 return viewer.run();
