| | 28 | class PrecipitationGeometry : public osg::Geometry |
| | 29 | { |
| | 30 | public: |
| | 31 | virtual bool supports(const osg::PrimitiveFunctor&) const { return false; } |
| | 32 | virtual void accept(osg::PrimitiveFunctor&) const {} |
| | 33 | virtual bool supports(const osg::PrimitiveIndexFunctor&) const { return false; } |
| | 34 | virtual void accept(osg::PrimitiveIndexFunctor&) const {} |
| | 35 | |
| | 36 | }; |
| | 37 | |
| | 38 | class CullCallback : public osg::NodeCallback |
| | 39 | { |
| | 40 | public: |
| | 41 | |
| | 42 | CullCallback(osg::Uniform* uniform): |
| | 43 | _previousFrame(0), |
| | 44 | _initialized(false), |
| | 45 | _uniform(uniform) |
| | 46 | { |
| | 47 | } |
| | 48 | |
| | 49 | virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) |
| | 50 | { |
| | 51 | osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv); |
| | 52 | if (cv) |
| | 53 | { |
| | 54 | if (!_initialized) |
| | 55 | { |
| | 56 | _previousModelViewMatrix = cv->getModelViewMatrix(); |
| | 57 | _previousFrame = nv->getFrameStamp()->getFrameNumber(); |
| | 58 | _initialized = true; |
| | 59 | } |
| | 60 | |
| | 61 | _uniform->set(_previousModelViewMatrix); |
| | 62 | |
| | 63 | // osg::notify(osg::NOTICE)<<"Updating uniform "<<_previousModelViewMatrix<<std::endl; |
| | 64 | |
| | 65 | traverse(node, nv); |
| | 66 | |
| | 67 | if (_previousFrame != nv->getFrameStamp()->getFrameNumber()) |
| | 68 | { |
| | 69 | _previousModelViewMatrix = cv->getModelViewMatrix(); |
| | 70 | _previousFrame = nv->getFrameStamp()->getFrameNumber(); |
| | 71 | } |
| | 72 | } |
| | 73 | else |
| | 74 | { |
| | 75 | traverse(node, nv); |
| | 76 | } |
| | 77 | } |
| | 78 | |
| | 79 | int _previousFrame; |
| | 80 | bool _initialized; |
| | 81 | osg::Matrix _previousModelViewMatrix; |
| | 82 | osg::ref_ptr<osg::Uniform> _uniform; |
| | 83 | }; |
| | 84 | |
| 42 | | (*vertices)[i*2].set(random(bb.xMin(), bb.xMax()), random(bb.yMin(),bb.yMax()), bb.zMax()); |
| 43 | | (*vertices)[i*2+1] = (*vertices)[i*2] + frameDelta; |
| 44 | | (*offsets)[i*2] = random(0.0, 1.0); |
| 45 | | (*offsets)[i*2+1] = (*offsets)[i*2]; |
| | 106 | (*vertices)[i*4].set(random(bb.xMin(), bb.xMax()), random(bb.yMin(),bb.yMax()), bb.zMax()); |
| | 107 | (*vertices)[i*4+1] = (*vertices)[i*4]; |
| | 108 | (*vertices)[i*4+2] = (*vertices)[i*4]; |
| | 109 | (*vertices)[i*4+3] = (*vertices)[i*4]; |
| | 110 | (*offsets)[i*4].z() = random(0.0, 1.0); |
| | 111 | (*offsets)[i*4+1].z() = (*offsets)[i*4].z(); |
| | 112 | (*offsets)[i*4+2].z() = (*offsets)[i*4].z(); |
| | 113 | (*offsets)[i*4+3].z() = (*offsets)[i*4].z(); |
| | 114 | (*offsets)[i*4].x() = 0.0; |
| | 115 | (*offsets)[i*4].y() = 0.0; |
| | 116 | (*offsets)[i*4+1].x() = 0.0; |
| | 117 | (*offsets)[i*4+1].y() = 1.0; |
| | 118 | (*offsets)[i*4+2].x() = 1.0; |
| | 119 | (*offsets)[i*4+2].y() = 1.0; |
| | 120 | (*offsets)[i*4+3].x() = 1.0; |
| | 121 | (*offsets)[i*4+3].y() = 0.0; |
| | 162 | |
| | 163 | osg::Uniform* baseTextureSampler = new osg::Uniform("baseTexture",0); |
| | 164 | stateset->addUniform(baseTextureSampler); |
| | 165 | |
| | 166 | osg::Texture2D* texture = new osg::Texture2D(osgDB::readImageFile("Images/particle.rgb")); |
| | 167 | stateset->setTextureAttribute(0, texture); |
| | 168 | |
| | 169 | // make it render after the normal transparent bin |
| | 170 | stateset->setRenderBinDetails(11,"DepthSortedBin"); |
| | 171 | |
| | 172 | osg::Uniform* previousModelViewUniform = new osg::Uniform("previousModelViewMatrix",osg::Matrix()); |
| | 173 | stateset->addUniform(previousModelViewUniform); |
| | 174 | geode->setCullCallback(new CullCallback(previousModelViewUniform)); |
| | 175 | |
| 178 | | bs.radius() *= 0.75; |
| 179 | | bb.init(); |
| 180 | | bb.expandBy(bs); |
| | 264 | |
| | 265 | bb.set( -100, -100, 0, +100, +100, 10); |
| | 266 | |
| | 267 | osg::StateSet* stateset = loadedModel->getOrCreateStateSet(); |
| | 268 | |
| | 269 | osg::Fog* fog = new osg::Fog; |
| | 270 | fog->setMode(osg::Fog::LINEAR); |
| | 271 | fog->setDensity(0.1f); |
| | 272 | fog->setStart(0.0f); |
| | 273 | fog->setEnd(1000.0f); |
| | 274 | fog->setColor(osg::Vec4(0.5f,0.5f,0.5f,1.0f)); |
| | 275 | stateset->setAttributeAndModes(fog, osg::StateAttribute::ON); |
| | 276 | |
| | 277 | osg::LightSource* lightSource = new osg::LightSource; |
| | 278 | group->addChild(lightSource); |
| | 279 | |
| | 280 | osg::Light* light = lightSource->getLight(); |
| | 281 | light->setLightNum(0); |
| | 282 | light->setPosition(osg::Vec4(0.0f,0.0f,1.0f,0.0f)); // directional light from above |
| | 283 | light->setAmbient(osg::Vec4(0.8f,0.8f,0.8f,1.0f)); |
| | 284 | light->setDiffuse(osg::Vec4(0.2f,0.2f,0.2f,1.0f)); |
| | 285 | light->setSpecular(osg::Vec4(0.2f,0.2f,0.2f,1.0f)); |
| | 286 | |
| | 287 | |