| 1 | #include <osg/Notify> |
|---|
| 2 | #include <osg/Texture1D> |
|---|
| 3 | #include <osg/TexGen> |
|---|
| 4 | #include <osg/Material> |
|---|
| 5 | |
|---|
| 6 | #include <osgGA/TrackballManipulator> |
|---|
| 7 | #include <osgGA/FlightManipulator> |
|---|
| 8 | #include <osgGA/DriveManipulator> |
|---|
| 9 | |
|---|
| 10 | #include <osgDB/Registry> |
|---|
| 11 | #include <osgDB/ReadFile> |
|---|
| 12 | |
|---|
| 13 | #include <osgProducer/Viewer> |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | osg::StateSet* create1DTextureStateToDecorate(osg::Node* loadedModel) |
|---|
| 21 | { |
|---|
| 22 | |
|---|
| 23 | const osg::BoundingSphere& bs = loadedModel->getBound(); |
|---|
| 24 | |
|---|
| 25 | osg::Image* image = new osg::Image; |
|---|
| 26 | |
|---|
| 27 | int noPixels = 1024; |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | image->allocateImage(noPixels,1,1,GL_RGBA,GL_FLOAT); |
|---|
| 31 | image->setInternalTextureFormat(GL_RGBA); |
|---|
| 32 | |
|---|
| 33 | typedef std::vector<osg::Vec4> ColorBands; |
|---|
| 34 | ColorBands colorbands; |
|---|
| 35 | colorbands.push_back(osg::Vec4(0.0f,0.0,0.0,1.0f)); |
|---|
| 36 | colorbands.push_back(osg::Vec4(1.0f,0.0,0.0,1.0f)); |
|---|
| 37 | colorbands.push_back(osg::Vec4(1.0f,1.0,0.0,1.0f)); |
|---|
| 38 | colorbands.push_back(osg::Vec4(0.0f,1.0,0.0,1.0f)); |
|---|
| 39 | colorbands.push_back(osg::Vec4(0.0f,1.0,1.0,1.0f)); |
|---|
| 40 | colorbands.push_back(osg::Vec4(0.0f,0.0,1.0,1.0f)); |
|---|
| 41 | colorbands.push_back(osg::Vec4(1.0f,0.0,1.0,1.0f)); |
|---|
| 42 | colorbands.push_back(osg::Vec4(1.0f,1.0,1.0,1.0f)); |
|---|
| 43 | |
|---|
| 44 | float nobands = colorbands.size(); |
|---|
| 45 | float delta = nobands/(float)noPixels; |
|---|
| 46 | float pos = 0.0f; |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | osg::Vec4* dataPtr = (osg::Vec4*)image->data(); |
|---|
| 50 | for(int i=0;i<noPixels;++i,pos+=delta) |
|---|
| 51 | { |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | osg::Vec4 color = colorbands[(int)pos]; |
|---|
| 57 | *dataPtr++ = color; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | osg::Texture1D* texture = new osg::Texture1D; |
|---|
| 61 | texture->setWrap(osg::Texture1D::WRAP_S,osg::Texture1D::MIRROR); |
|---|
| 62 | texture->setFilter(osg::Texture1D::MIN_FILTER,osg::Texture1D::LINEAR); |
|---|
| 63 | texture->setImage(image); |
|---|
| 64 | |
|---|
| 65 | float zBase = bs.center().z()-bs.radius(); |
|---|
| 66 | float zScale = 2.0f/bs.radius(); |
|---|
| 67 | |
|---|
| 68 | osg::TexGen* texgen = new osg::TexGen; |
|---|
| 69 | texgen->setMode(osg::TexGen::OBJECT_LINEAR); |
|---|
| 70 | texgen->setPlane(osg::TexGen::S,osg::Vec4(0.0f,0.0f,zScale,-zBase)); |
|---|
| 71 | |
|---|
| 72 | osg::Material* material = new osg::Material; |
|---|
| 73 | |
|---|
| 74 | osg::StateSet* stateset = new osg::StateSet; |
|---|
| 75 | |
|---|
| 76 | stateset->setTextureAttribute(0,texture,osg::StateAttribute::OVERRIDE); |
|---|
| 77 | stateset->setTextureMode(0,GL_TEXTURE_1D,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE); |
|---|
| 78 | stateset->setTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::OFF|osg::StateAttribute::OVERRIDE); |
|---|
| 79 | stateset->setTextureMode(0,GL_TEXTURE_3D,osg::StateAttribute::OFF|osg::StateAttribute::OVERRIDE); |
|---|
| 80 | |
|---|
| 81 | stateset->setTextureAttribute(0,texgen,osg::StateAttribute::OVERRIDE); |
|---|
| 82 | stateset->setTextureMode(0,GL_TEXTURE_GEN_S,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE); |
|---|
| 83 | |
|---|
| 84 | stateset->setAttribute(material,osg::StateAttribute::OVERRIDE); |
|---|
| 85 | |
|---|
| 86 | return stateset; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | class AnimateStateCallback : public osg::NodeCallback |
|---|
| 92 | { |
|---|
| 93 | public: |
|---|
| 94 | AnimateStateCallback() {} |
|---|
| 95 | |
|---|
| 96 | void animateState(osg::StateSet* stateset,double time) |
|---|
| 97 | { |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | osg::StateAttribute* attribute = stateset->getTextureAttribute(0,osg::StateAttribute::TEXGEN); |
|---|
| 101 | osg::TexGen* texgen = dynamic_cast<osg::TexGen*>(attribute); |
|---|
| 102 | if (texgen) |
|---|
| 103 | { |
|---|
| 104 | const double timeInterval = 1.0f; |
|---|
| 105 | |
|---|
| 106 | static double previousTime = time; |
|---|
| 107 | static bool state = false; |
|---|
| 108 | while (time>previousTime+timeInterval) |
|---|
| 109 | { |
|---|
| 110 | previousTime+=timeInterval; |
|---|
| 111 | state = !state; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | if (state) |
|---|
| 115 | { |
|---|
| 116 | texgen->setMode(osg::TexGen::OBJECT_LINEAR); |
|---|
| 117 | } |
|---|
| 118 | else |
|---|
| 119 | { |
|---|
| 120 | texgen->setMode(osg::TexGen::EYE_LINEAR); |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) |
|---|
| 127 | { |
|---|
| 128 | |
|---|
| 129 | osg::StateSet* stateset = node->getStateSet(); |
|---|
| 130 | if (stateset && nv->getFrameStamp()) |
|---|
| 131 | { |
|---|
| 132 | |
|---|
| 133 | animateState(stateset,nv->getFrameStamp()->getReferenceTime()); |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | traverse(node,nv); |
|---|
| 140 | } |
|---|
| 141 | }; |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | int main( int argc, char **argv ) |
|---|
| 145 | { |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getProgramName()+" [options] filename ..."); |
|---|
| 152 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | osgProducer::Viewer viewer(arguments); |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 165 | { |
|---|
| 166 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 167 | return 1; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | if (arguments.errors()) |
|---|
| 175 | { |
|---|
| 176 | arguments.writeErrorMessages(std::cout); |
|---|
| 177 | return 1; |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | osg::Node* loadedModel = osgDB::readNodeFiles(arguments); |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | if (loadedModel) |
|---|
| 185 | { |
|---|
| 186 | |
|---|
| 187 | osg::StateSet* stateset = create1DTextureStateToDecorate(loadedModel); |
|---|
| 188 | if (!stateset) |
|---|
| 189 | { |
|---|
| 190 | std::cout<<"Error: failed to create 1D texture state."<<std::endl; |
|---|
| 191 | return 1; |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | loadedModel->setStateSet(stateset); |
|---|
| 196 | loadedModel->setUpdateCallback(new AnimateStateCallback()); |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | viewer.setSceneData( loadedModel ); |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | viewer.realize(Producer::CameraGroup::ThreadPerCamera); |
|---|
| 203 | |
|---|
| 204 | while( !viewer.done() ) |
|---|
| 205 | { |
|---|
| 206 | |
|---|
| 207 | viewer.sync(); |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | viewer.update(); |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | viewer.frame(); |
|---|
| 215 | |
|---|
| 216 | } |
|---|
| 217 | } |
|---|
| 218 | else |
|---|
| 219 | { |
|---|
| 220 | osg::notify(osg::NOTICE)<<arguments.getApplicationUsage()->getCommandLineUsage()<<std::endl; |
|---|
| 221 | return 0; |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | return 0; |
|---|
| 227 | } |
|---|