| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | #include <osgDB/ReadFile> |
|---|
| 13 | #include <osgDB/FileUtils> |
|---|
| 14 | #include <osgUtil/Optimizer> |
|---|
| 15 | #include <osgUtil/CullVisitor> |
|---|
| 16 | #include <osgProducer/Viewer> |
|---|
| 17 | |
|---|
| 18 | #include <osg/Point> |
|---|
| 19 | #include <osg/BlendFunc> |
|---|
| 20 | #include <osg/Texture2D> |
|---|
| 21 | #include <osg/PointSprite> |
|---|
| 22 | #include <osg/Program> |
|---|
| 23 | #include <osg/Fog> |
|---|
| 24 | #include <osg/io_utils> |
|---|
| 25 | |
|---|
| 26 | float random(float min,float max) { return min + (max-min)*(float)rand()/(float)RAND_MAX; } |
|---|
| 27 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 85 | osg::Node* createRainEffect(const osg::BoundingBox& bb, const osg::Vec3& velocity, unsigned int numParticles, bool useShaders) |
|---|
| 86 | { |
|---|
| 87 | osg::Geode* geode = new osg::Geode; |
|---|
| 88 | |
|---|
| 89 | osg::Geometry* geometry = new PrecipitationGeometry; |
|---|
| 90 | geode->addDrawable(geometry); |
|---|
| 91 | |
|---|
| 92 | osg::StateSet* stateset = geometry->getOrCreateStateSet(); |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | { |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | osg::Vec3Array* vertices = new osg::Vec3Array(numParticles*4); |
|---|
| 99 | osg::Vec3Array* offsets = new osg::Vec3Array(numParticles*4); |
|---|
| 100 | |
|---|
| 101 | osg::Vec3 frameDelta = velocity*(2.0f/60.0f); |
|---|
| 102 | float size = 1.0; |
|---|
| 103 | |
|---|
| 104 | for(unsigned int i=0; i< numParticles; ++i) |
|---|
| 105 | { |
|---|
| 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; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | geometry->setVertexArray(vertices); |
|---|
| 125 | geometry->setTexCoordArray(0, offsets); |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | osg::Vec4Array* colours = new osg::Vec4Array(1); |
|---|
| 129 | (*colours)[0].set(0.5f,0.5f,0.5f,1.0f); |
|---|
| 130 | geometry->setColorArray(colours); |
|---|
| 131 | geometry->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 132 | |
|---|
| 133 | geometry->addPrimitiveSet(new osg::DrawArrays(GL_QUADS, 0, numParticles*4)); |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | { |
|---|
| 138 | |
|---|
| 139 | float period = fabs((bb.zMax()-bb.zMin()) / velocity.z()); |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | osg::Vec3 delta = velocity * period; |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | osg::Uniform* deltaUniform = new osg::Uniform("delta",delta); |
|---|
| 146 | osg::Uniform* inversePeriodUniform = new osg::Uniform("inversePeriod",1.0f/period); |
|---|
| 147 | osg::Uniform* startTime = new osg::Uniform("startTime",0.0f); |
|---|
| 148 | |
|---|
| 149 | osg::Program* program = new osg::Program; |
|---|
| 150 | stateset->setAttribute(program); |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | program->addShader(osg::Shader::readShaderFile(osg::Shader::VERTEX, osgDB::findDataFile("rain.vert"))); |
|---|
| 154 | program->addShader(osg::Shader::readShaderFile(osg::Shader::FRAGMENT, osgDB::findDataFile("rain.frag"))); |
|---|
| 155 | |
|---|
| 156 | stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF); |
|---|
| 157 | stateset->setMode(GL_BLEND, osg::StateAttribute::ON); |
|---|
| 158 | |
|---|
| 159 | stateset->addUniform(deltaUniform); |
|---|
| 160 | stateset->addUniform(inversePeriodUniform); |
|---|
| 161 | stateset->addUniform(startTime); |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | geometry->setUseVertexBufferObjects(true); |
|---|
| 179 | geometry->setInitialBound(bb); |
|---|
| 180 | |
|---|
| 181 | return geode; |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | osg::Node* createModel(osg::Node* loadedModel, bool useShaders) |
|---|
| 252 | { |
|---|
| 253 | osg::Group* group = new osg::Group; |
|---|
| 254 | |
|---|
| 255 | osg::BoundingBox bb(0.0, 0.0, 0.0, 100.0, 100.0, 100.0); |
|---|
| 256 | osg::Vec3 velocity(0.0,0.0,-2.0); |
|---|
| 257 | unsigned int numParticles = 150000; |
|---|
| 258 | |
|---|
| 259 | if (loadedModel) |
|---|
| 260 | { |
|---|
| 261 | group->addChild(loadedModel); |
|---|
| 262 | |
|---|
| 263 | osg::BoundingSphere bs = loadedModel->getBound(); |
|---|
| 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)); |
|---|
| 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 | |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | group->addChild(createRainEffect(bb, velocity, numParticles, useShaders)); |
|---|
| 291 | |
|---|
| 292 | return group; |
|---|
| 293 | } |
|---|
| 294 | |
|---|
| 295 | int main( int argc, char **argv ) |
|---|
| 296 | { |
|---|
| 297 | |
|---|
| 298 | |
|---|
| 299 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 300 | |
|---|
| 301 | |
|---|
| 302 | arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName()); |
|---|
| 303 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" example provides an interactive viewer for visualising point clouds.."); |
|---|
| 304 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); |
|---|
| 305 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 306 | arguments.getApplicationUsage()->addCommandLineOption("--shader","Use GLSL shaders."); |
|---|
| 307 | arguments.getApplicationUsage()->addCommandLineOption("--fixed","Use fixed function pipeline."); |
|---|
| 308 | |
|---|
| 309 | |
|---|
| 310 | |
|---|
| 311 | osgProducer::Viewer viewer(arguments); |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 315 | |
|---|
| 316 | |
|---|
| 317 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 318 | |
|---|
| 319 | bool shader = true; |
|---|
| 320 | while (arguments.read("--shader")) shader = true; |
|---|
| 321 | while (arguments.read("--fixed")) shader = false; |
|---|
| 322 | |
|---|
| 323 | |
|---|
| 324 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 325 | { |
|---|
| 326 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 327 | return 1; |
|---|
| 328 | } |
|---|
| 329 | |
|---|
| 330 | |
|---|
| 331 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 332 | |
|---|
| 333 | |
|---|
| 334 | if (arguments.errors()) |
|---|
| 335 | { |
|---|
| 336 | arguments.writeErrorMessages(std::cout); |
|---|
| 337 | return 1; |
|---|
| 338 | } |
|---|
| 339 | |
|---|
| 340 | osg::Timer timer; |
|---|
| 341 | osg::Timer_t start_tick = timer.tick(); |
|---|
| 342 | |
|---|
| 343 | |
|---|
| 344 | osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments); |
|---|
| 345 | |
|---|
| 346 | loadedModel = createModel(loadedModel.get(), shader); |
|---|
| 347 | |
|---|
| 348 | |
|---|
| 349 | if (!loadedModel) |
|---|
| 350 | { |
|---|
| 351 | std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl; |
|---|
| 352 | return 1; |
|---|
| 353 | } |
|---|
| 354 | |
|---|
| 355 | osg::Timer_t end_tick = timer.tick(); |
|---|
| 356 | |
|---|
| 357 | std::cout << "Time to load = "<<timer.delta_s(start_tick,end_tick)<<std::endl; |
|---|
| 358 | |
|---|
| 359 | |
|---|
| 360 | osgUtil::Optimizer optimizer; |
|---|
| 361 | optimizer.optimize(loadedModel.get()); |
|---|
| 362 | |
|---|
| 363 | |
|---|
| 364 | viewer.setSceneData(loadedModel.get()); |
|---|
| 365 | |
|---|
| 366 | |
|---|
| 367 | viewer.realize(); |
|---|
| 368 | |
|---|
| 369 | while( !viewer.done() ) |
|---|
| 370 | { |
|---|
| 371 | |
|---|
| 372 | viewer.sync(); |
|---|
| 373 | |
|---|
| 374 | |
|---|
| 375 | |
|---|
| 376 | viewer.update(); |
|---|
| 377 | |
|---|
| 378 | |
|---|
| 379 | viewer.frame(); |
|---|
| 380 | |
|---|
| 381 | } |
|---|
| 382 | |
|---|
| 383 | |
|---|
| 384 | viewer.sync(); |
|---|
| 385 | |
|---|
| 386 | return 0; |
|---|
| 387 | } |
|---|