| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | #include <osg/Notify> |
|---|
| 20 | #include <osg/Texture1D> |
|---|
| 21 | #include <osg/TexGen> |
|---|
| 22 | #include <osg/Material> |
|---|
| 23 | |
|---|
| 24 | #include <osgDB/Registry> |
|---|
| 25 | #include <osgDB/ReadFile> |
|---|
| 26 | |
|---|
| 27 | #include <osgViewer/Viewer> |
|---|
| 28 | |
|---|
| 29 | #include <iostream> |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | osg::StateSet* create1DTextureStateToDecorate(osg::Node* loadedModel) |
|---|
| 36 | { |
|---|
| 37 | |
|---|
| 38 | const osg::BoundingSphere& bs = loadedModel->getBound(); |
|---|
| 39 | |
|---|
| 40 | osg::Image* image = new osg::Image; |
|---|
| 41 | |
|---|
| 42 | int noPixels = 1024; |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | image->allocateImage(noPixels,1,1,GL_RGBA,GL_FLOAT); |
|---|
| 46 | image->setInternalTextureFormat(GL_RGBA); |
|---|
| 47 | |
|---|
| 48 | typedef std::vector<osg::Vec4> ColorBands; |
|---|
| 49 | ColorBands colorbands; |
|---|
| 50 | colorbands.push_back(osg::Vec4(0.0f,0.0,0.0,1.0f)); |
|---|
| 51 | colorbands.push_back(osg::Vec4(1.0f,0.0,0.0,1.0f)); |
|---|
| 52 | colorbands.push_back(osg::Vec4(1.0f,1.0,0.0,1.0f)); |
|---|
| 53 | colorbands.push_back(osg::Vec4(0.0f,1.0,0.0,1.0f)); |
|---|
| 54 | colorbands.push_back(osg::Vec4(0.0f,1.0,1.0,1.0f)); |
|---|
| 55 | colorbands.push_back(osg::Vec4(0.0f,0.0,1.0,1.0f)); |
|---|
| 56 | colorbands.push_back(osg::Vec4(1.0f,0.0,1.0,1.0f)); |
|---|
| 57 | colorbands.push_back(osg::Vec4(1.0f,1.0,1.0,1.0f)); |
|---|
| 58 | |
|---|
| 59 | float nobands = colorbands.size(); |
|---|
| 60 | float delta = nobands/(float)noPixels; |
|---|
| 61 | float pos = 0.0f; |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | osg::Vec4* dataPtr = (osg::Vec4*)image->data(); |
|---|
| 65 | for(int i=0;i<noPixels;++i,pos+=delta) |
|---|
| 66 | { |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | osg::Vec4 color = colorbands[(int)pos]; |
|---|
| 72 | *dataPtr++ = color; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | osg::Texture1D* texture = new osg::Texture1D; |
|---|
| 76 | texture->setWrap(osg::Texture1D::WRAP_S,osg::Texture1D::MIRROR); |
|---|
| 77 | texture->setFilter(osg::Texture1D::MIN_FILTER,osg::Texture1D::LINEAR); |
|---|
| 78 | 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 | |
|---|
| 87 | osg::Material* material = new osg::Material; |
|---|
| 88 | |
|---|
| 89 | osg::StateSet* stateset = new osg::StateSet; |
|---|
| 90 | |
|---|
| 91 | stateset->setTextureAttribute(0,texture,osg::StateAttribute::OVERRIDE); |
|---|
| 92 | stateset->setTextureMode(0,GL_TEXTURE_1D,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE); |
|---|
| 93 | stateset->setTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::OFF|osg::StateAttribute::OVERRIDE); |
|---|
| 94 | stateset->setTextureMode(0,GL_TEXTURE_3D,osg::StateAttribute::OFF|osg::StateAttribute::OVERRIDE); |
|---|
| 95 | |
|---|
| 96 | stateset->setTextureAttribute(0,texgen,osg::StateAttribute::OVERRIDE); |
|---|
| 97 | stateset->setTextureMode(0,GL_TEXTURE_GEN_S,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE); |
|---|
| 98 | |
|---|
| 99 | stateset->setAttribute(material,osg::StateAttribute::OVERRIDE); |
|---|
| 100 | |
|---|
| 101 | return stateset; |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | class AnimateStateCallback : public osg::NodeCallback |
|---|
| 107 | { |
|---|
| 108 | public: |
|---|
| 109 | AnimateStateCallback() {} |
|---|
| 110 | |
|---|
| 111 | void animateState(osg::StateSet* stateset,double time) |
|---|
| 112 | { |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | osg::StateAttribute* attribute = stateset->getTextureAttribute(0,osg::StateAttribute::TEXGEN); |
|---|
| 116 | osg::TexGen* texgen = dynamic_cast<osg::TexGen*>(attribute); |
|---|
| 117 | if (texgen) |
|---|
| 118 | { |
|---|
| 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 | } |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) |
|---|
| 142 | { |
|---|
| 143 | |
|---|
| 144 | osg::StateSet* stateset = node->getStateSet(); |
|---|
| 145 | if (stateset && nv->getFrameStamp()) |
|---|
| 146 | { |
|---|
| 147 | |
|---|
| 148 | animateState(stateset,nv->getFrameStamp()->getSimulationTime()); |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | traverse(node,nv); |
|---|
| 155 | } |
|---|
| 156 | }; |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | int main( int argc, char **argv ) |
|---|
| 160 | { |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | osgViewer::Viewer viewer; |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | osg::Node* loadedModel = osgDB::readNodeFiles(arguments); |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | if (!loadedModel) loadedModel = osgDB::readNodeFile("dumptruck.osg"); |
|---|
| 173 | |
|---|
| 174 | if (!loadedModel) |
|---|
| 175 | { |
|---|
| 176 | osg::notify(osg::NOTICE)<<arguments.getApplicationUsage()->getCommandLineUsage()<<std::endl; |
|---|
| 177 | return 0; |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | osg::StateSet* stateset = create1DTextureStateToDecorate(loadedModel); |
|---|
| 181 | if (!stateset) |
|---|
| 182 | { |
|---|
| 183 | std::cout<<"Error: failed to create 1D texture state."<<std::endl; |
|---|
| 184 | return 1; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | loadedModel->setStateSet(stateset); |
|---|
| 189 | loadedModel->setUpdateCallback(new AnimateStateCallback()); |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | viewer.setSceneData( loadedModel ); |
|---|
| 193 | |
|---|
| 194 | return viewer.run(); |
|---|
| 195 | } |
|---|