| [6941] | 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| [1697] | 19 | #include <osg/Notify> |
|---|
| 20 | #include <osg/Texture1D> |
|---|
| [10066] | 21 | #include <osg/TexGenNode> |
|---|
| [1697] | 22 | #include <osg/Material> |
|---|
| 23 | |
|---|
| 24 | #include <osgDB/Registry> |
|---|
| 25 | #include <osgDB/ReadFile> |
|---|
| 26 | |
|---|
| [5962] | 27 | #include <osgViewer/Viewer> |
|---|
| [1697] | 28 | |
|---|
| [5962] | 29 | #include <iostream> |
|---|
| [1697] | 30 | |
|---|
| [10066] | 31 | |
|---|
| 32 | |
|---|
| [1697] | 33 | osg::StateSet* create1DTextureStateToDecorate(osg::Node* loadedModel) |
|---|
| 34 | { |
|---|
| 35 | osg::Image* image = new osg::Image; |
|---|
| 36 | |
|---|
| 37 | int noPixels = 1024; |
|---|
| 38 | |
|---|
| [7648] | 39 | |
|---|
| [1697] | 40 | image->allocateImage(noPixels,1,1,GL_RGBA,GL_FLOAT); |
|---|
| 41 | image->setInternalTextureFormat(GL_RGBA); |
|---|
| 42 | |
|---|
| 43 | typedef std::vector<osg::Vec4> ColorBands; |
|---|
| 44 | ColorBands colorbands; |
|---|
| 45 | colorbands.push_back(osg::Vec4(0.0f,0.0,0.0,1.0f)); |
|---|
| 46 | colorbands.push_back(osg::Vec4(1.0f,0.0,0.0,1.0f)); |
|---|
| 47 | colorbands.push_back(osg::Vec4(1.0f,1.0,0.0,1.0f)); |
|---|
| 48 | colorbands.push_back(osg::Vec4(0.0f,1.0,0.0,1.0f)); |
|---|
| 49 | colorbands.push_back(osg::Vec4(0.0f,1.0,1.0,1.0f)); |
|---|
| 50 | colorbands.push_back(osg::Vec4(0.0f,0.0,1.0,1.0f)); |
|---|
| 51 | colorbands.push_back(osg::Vec4(1.0f,0.0,1.0,1.0f)); |
|---|
| 52 | colorbands.push_back(osg::Vec4(1.0f,1.0,1.0,1.0f)); |
|---|
| 53 | |
|---|
| 54 | float nobands = colorbands.size(); |
|---|
| 55 | float delta = nobands/(float)noPixels; |
|---|
| 56 | float pos = 0.0f; |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | osg::Vec4* dataPtr = (osg::Vec4*)image->data(); |
|---|
| 60 | for(int i=0;i<noPixels;++i,pos+=delta) |
|---|
| 61 | { |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | osg::Vec4 color = colorbands[(int)pos]; |
|---|
| 67 | *dataPtr++ = color; |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | osg::Texture1D* texture = new osg::Texture1D; |
|---|
| 71 | texture->setWrap(osg::Texture1D::WRAP_S,osg::Texture1D::MIRROR); |
|---|
| 72 | texture->setFilter(osg::Texture1D::MIN_FILTER,osg::Texture1D::LINEAR); |
|---|
| 73 | texture->setImage(image); |
|---|
| 74 | |
|---|
| 75 | osg::Material* material = new osg::Material; |
|---|
| 76 | |
|---|
| 77 | osg::StateSet* stateset = new osg::StateSet; |
|---|
| 78 | |
|---|
| 79 | stateset->setTextureAttribute(0,texture,osg::StateAttribute::OVERRIDE); |
|---|
| 80 | stateset->setTextureMode(0,GL_TEXTURE_1D,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE); |
|---|
| 81 | stateset->setTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::OFF|osg::StateAttribute::OVERRIDE); |
|---|
| 82 | stateset->setTextureMode(0,GL_TEXTURE_3D,osg::StateAttribute::OFF|osg::StateAttribute::OVERRIDE); |
|---|
| 83 | |
|---|
| 84 | stateset->setTextureMode(0,GL_TEXTURE_GEN_S,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE); |
|---|
| 85 | |
|---|
| 86 | stateset->setAttribute(material,osg::StateAttribute::OVERRIDE); |
|---|
| 87 | |
|---|
| 88 | return stateset; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| [10066] | 93 | class AnimateTexGenCallback : public osg::NodeCallback |
|---|
| [1697] | 94 | { |
|---|
| 95 | public: |
|---|
| [10066] | 96 | AnimateTexGenCallback() {} |
|---|
| [1697] | 97 | |
|---|
| [10066] | 98 | void animateTexGen(osg::TexGenNode* texgenNode,double time) |
|---|
| [1697] | 99 | { |
|---|
| 100 | |
|---|
| 101 | |
|---|
| [10066] | 102 | const double timeInterval = 2.0f; |
|---|
| 103 | |
|---|
| 104 | static double previousTime = time; |
|---|
| 105 | static bool state = false; |
|---|
| 106 | while (time>previousTime+timeInterval) |
|---|
| [1697] | 107 | { |
|---|
| [10066] | 108 | previousTime+=timeInterval; |
|---|
| 109 | state = !state; |
|---|
| [1697] | 110 | } |
|---|
| [10066] | 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); |
|---|
| 119 | } |
|---|
| [1697] | 120 | |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) |
|---|
| 124 | { |
|---|
| [10066] | 125 | osg::TexGenNode* texgenNode = dynamic_cast<osg::TexGenNode*>(node); |
|---|
| [1697] | 126 | |
|---|
| [10066] | 127 | if (texgenNode && nv->getFrameStamp()) |
|---|
| [1697] | 128 | { |
|---|
| 129 | |
|---|
| [10066] | 130 | animateTexGen(texgenNode,nv->getFrameStamp()->getSimulationTime()); |
|---|
| [1697] | 131 | } |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | traverse(node,nv); |
|---|
| 137 | } |
|---|
| 138 | }; |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | int main( int argc, char **argv ) |
|---|
| 142 | { |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 146 | |
|---|
| 147 | |
|---|
| [5962] | 148 | osgViewer::Viewer viewer; |
|---|
| [1697] | 149 | |
|---|
| [5962] | 150 | |
|---|
| 151 | osg::Node* loadedModel = osgDB::readNodeFiles(arguments); |
|---|
| [6916] | 152 | |
|---|
| 153 | |
|---|
| [12529] | 154 | if (!loadedModel) loadedModel = osgDB::readNodeFile("dumptruck.osgt"); |
|---|
| [5962] | 155 | |
|---|
| 156 | if (!loadedModel) |
|---|
| [1697] | 157 | { |
|---|
| [5962] | 158 | osg::notify(osg::NOTICE)<<arguments.getApplicationUsage()->getCommandLineUsage()<<std::endl; |
|---|
| 159 | return 0; |
|---|
| [1697] | 160 | } |
|---|
| 161 | |
|---|
| [5962] | 162 | osg::StateSet* stateset = create1DTextureStateToDecorate(loadedModel); |
|---|
| 163 | if (!stateset) |
|---|
| [1697] | 164 | { |
|---|
| [5962] | 165 | std::cout<<"Error: failed to create 1D texture state."<<std::endl; |
|---|
| [1697] | 166 | return 1; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | |
|---|
| [5962] | 170 | loadedModel->setStateSet(stateset); |
|---|
| [1697] | 171 | |
|---|
| [10066] | 172 | osg:: Group *root = new osg:: Group; |
|---|
| 173 | root -> addChild( loadedModel ); |
|---|
| [1697] | 174 | |
|---|
| [10066] | 175 | |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 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 ); |
|---|
| 194 | |
|---|
| [5962] | 195 | return viewer.run(); |
|---|
| [1697] | 196 | } |
|---|