| 1 | #include <osg/Notify> |
|---|
| 2 | #include <osg/Texture1D> |
|---|
| 3 | #include <osg/TexGen> |
|---|
| 4 | #include <osg/Material> |
|---|
| 5 | |
|---|
| 6 | #include <osgDB/Registry> |
|---|
| 7 | #include <osgDB/ReadFile> |
|---|
| 8 | |
|---|
| 9 | #include <osgViewer/Viewer> |
|---|
| 10 | |
|---|
| 11 | #include <iostream> |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | osg::StateSet* create1DTextureStateToDecorate(osg::Node* loadedModel) |
|---|
| 18 | { |
|---|
| 19 | |
|---|
| 20 | const osg::BoundingSphere& bs = loadedModel->getBound(); |
|---|
| 21 | |
|---|
| 22 | osg::Image* image = new osg::Image; |
|---|
| 23 | |
|---|
| 24 | int noPixels = 1024; |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | image->allocateImage(noPixels,1,1,GL_RGBA,GL_FLOAT); |
|---|
| 28 | image->setInternalTextureFormat(GL_RGBA); |
|---|
| 29 | |
|---|
| 30 | typedef std::vector<osg::Vec4> ColorBands; |
|---|
| 31 | ColorBands colorbands; |
|---|
| 32 | colorbands.push_back(osg::Vec4(0.0f,0.0,0.0,1.0f)); |
|---|
| 33 | colorbands.push_back(osg::Vec4(1.0f,0.0,0.0,1.0f)); |
|---|
| 34 | colorbands.push_back(osg::Vec4(1.0f,1.0,0.0,1.0f)); |
|---|
| 35 | colorbands.push_back(osg::Vec4(0.0f,1.0,0.0,1.0f)); |
|---|
| 36 | colorbands.push_back(osg::Vec4(0.0f,1.0,1.0,1.0f)); |
|---|
| 37 | colorbands.push_back(osg::Vec4(0.0f,0.0,1.0,1.0f)); |
|---|
| 38 | colorbands.push_back(osg::Vec4(1.0f,0.0,1.0,1.0f)); |
|---|
| 39 | colorbands.push_back(osg::Vec4(1.0f,1.0,1.0,1.0f)); |
|---|
| 40 | |
|---|
| 41 | float nobands = colorbands.size(); |
|---|
| 42 | float delta = nobands/(float)noPixels; |
|---|
| 43 | float pos = 0.0f; |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | osg::Vec4* dataPtr = (osg::Vec4*)image->data(); |
|---|
| 47 | for(int i=0;i<noPixels;++i,pos+=delta) |
|---|
| 48 | { |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | osg::Vec4 color = colorbands[(int)pos]; |
|---|
| 54 | *dataPtr++ = color; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | osg::Texture1D* texture = new osg::Texture1D; |
|---|
| 58 | texture->setWrap(osg::Texture1D::WRAP_S,osg::Texture1D::MIRROR); |
|---|
| 59 | texture->setFilter(osg::Texture1D::MIN_FILTER,osg::Texture1D::LINEAR); |
|---|
| 60 | texture->setImage(image); |
|---|
| 61 | |
|---|
| 62 | float zBase = bs.center().z()-bs.radius(); |
|---|
| 63 | float zScale = 2.0f/bs.radius(); |
|---|
| 64 | |
|---|
| 65 | osg::TexGen* texgen = new osg::TexGen; |
|---|
| 66 | texgen->setMode(osg::TexGen::OBJECT_LINEAR); |
|---|
| 67 | texgen->setPlane(osg::TexGen::S,osg::Plane(0.0f,0.0f,zScale,-zBase)); |
|---|
| 68 | |
|---|
| 69 | osg::Material* material = new osg::Material; |
|---|
| 70 | |
|---|
| 71 | osg::StateSet* stateset = new osg::StateSet; |
|---|
| 72 | |
|---|
| 73 | stateset->setTextureAttribute(0,texture,osg::StateAttribute::OVERRIDE); |
|---|
| 74 | stateset->setTextureMode(0,GL_TEXTURE_1D,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE); |
|---|
| 75 | stateset->setTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::OFF|osg::StateAttribute::OVERRIDE); |
|---|
| 76 | stateset->setTextureMode(0,GL_TEXTURE_3D,osg::StateAttribute::OFF|osg::StateAttribute::OVERRIDE); |
|---|
| 77 | |
|---|
| 78 | stateset->setTextureAttribute(0,texgen,osg::StateAttribute::OVERRIDE); |
|---|
| 79 | stateset->setTextureMode(0,GL_TEXTURE_GEN_S,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE); |
|---|
| 80 | |
|---|
| 81 | stateset->setAttribute(material,osg::StateAttribute::OVERRIDE); |
|---|
| 82 | |
|---|
| 83 | return stateset; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | class AnimateStateCallback : public osg::NodeCallback |
|---|
| 89 | { |
|---|
| 90 | public: |
|---|
| 91 | AnimateStateCallback() {} |
|---|
| 92 | |
|---|
| 93 | void animateState(osg::StateSet* stateset,double time) |
|---|
| 94 | { |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | osg::StateAttribute* attribute = stateset->getTextureAttribute(0,osg::StateAttribute::TEXGEN); |
|---|
| 98 | osg::TexGen* texgen = dynamic_cast<osg::TexGen*>(attribute); |
|---|
| 99 | if (texgen) |
|---|
| 100 | { |
|---|
| 101 | const double timeInterval = 1.0f; |
|---|
| 102 | |
|---|
| 103 | static double previousTime = time; |
|---|
| 104 | static bool state = false; |
|---|
| 105 | while (time>previousTime+timeInterval) |
|---|
| 106 | { |
|---|
| 107 | previousTime+=timeInterval; |
|---|
| 108 | state = !state; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | if (state) |
|---|
| 112 | { |
|---|
| 113 | texgen->setMode(osg::TexGen::OBJECT_LINEAR); |
|---|
| 114 | } |
|---|
| 115 | else |
|---|
| 116 | { |
|---|
| 117 | texgen->setMode(osg::TexGen::EYE_LINEAR); |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) |
|---|
| 124 | { |
|---|
| 125 | |
|---|
| 126 | osg::StateSet* stateset = node->getStateSet(); |
|---|
| 127 | if (stateset && nv->getFrameStamp()) |
|---|
| 128 | { |
|---|
| 129 | |
|---|
| 130 | animateState(stateset,nv->getFrameStamp()->getSimulationTime()); |
|---|
| 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 | |
|---|
| 148 | osgViewer::Viewer viewer; |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | osg::Node* loadedModel = osgDB::readNodeFiles(arguments); |
|---|
| 152 | |
|---|
| 153 | if (!loadedModel) |
|---|
| 154 | { |
|---|
| 155 | osg::notify(osg::NOTICE)<<arguments.getApplicationUsage()->getCommandLineUsage()<<std::endl; |
|---|
| 156 | return 0; |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | osg::StateSet* stateset = create1DTextureStateToDecorate(loadedModel); |
|---|
| 160 | if (!stateset) |
|---|
| 161 | { |
|---|
| 162 | std::cout<<"Error: failed to create 1D texture state."<<std::endl; |
|---|
| 163 | return 1; |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | loadedModel->setStateSet(stateset); |
|---|
| 168 | loadedModel->setUpdateCallback(new AnimateStateCallback()); |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | viewer.setSceneData( loadedModel ); |
|---|
| 172 | |
|---|
| 173 | return viewer.run(); |
|---|
| 174 | } |
|---|