| 1 | #include <osg/GLExtensions> |
|---|
| 2 | #include <osg/Node> |
|---|
| 3 | #include <osg/Geometry> |
|---|
| 4 | #include <osg/Notify> |
|---|
| 5 | #include <osg/MatrixTransform> |
|---|
| 6 | #include <osg/Texture2D> |
|---|
| 7 | #include <osg/Stencil> |
|---|
| 8 | #include <osg/ColorMask> |
|---|
| 9 | #include <osg/Depth> |
|---|
| 10 | #include <osg/Billboard> |
|---|
| 11 | #include <osg/Material> |
|---|
| 12 | |
|---|
| 13 | #include <osgGA/TrackballManipulator> |
|---|
| 14 | #include <osgGA/FlightManipulator> |
|---|
| 15 | #include <osgGA/DriveManipulator> |
|---|
| 16 | |
|---|
| 17 | #include <osgUtil/TransformCallback> |
|---|
| 18 | #include <osgUtil/RenderToTextureStage> |
|---|
| 19 | #include <osgUtil/SmoothingVisitor> |
|---|
| 20 | |
|---|
| 21 | #include <osgDB/Registry> |
|---|
| 22 | #include <osgDB/ReadFile> |
|---|
| 23 | |
|---|
| 24 | #include <osgProducer/Viewer> |
|---|
| 25 | |
|---|
| 26 | class MyUpdateCallback : public osg::NodeCallback |
|---|
| 27 | { |
|---|
| 28 | public: |
|---|
| 29 | |
|---|
| 30 | MyUpdateCallback(osg::Node* subgraph): |
|---|
| 31 | _subgraph(subgraph) {} |
|---|
| 32 | |
|---|
| 33 | virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) |
|---|
| 34 | { |
|---|
| 35 | |
|---|
| 36 | if (_subgraph.valid()) _subgraph->accept(*nv); |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | traverse(node,nv); |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | osg::ref_ptr<osg::Node> _subgraph; |
|---|
| 43 | }; |
|---|
| 44 | |
|---|
| 45 | class MyCullCallback : public osg::NodeCallback |
|---|
| 46 | { |
|---|
| 47 | public: |
|---|
| 48 | |
|---|
| 49 | MyCullCallback(osg::Node* subgraph,osg::Texture2D* texture): |
|---|
| 50 | _subgraph(subgraph), |
|---|
| 51 | _texture(texture), |
|---|
| 52 | _localState(new osg::StateSet) {} |
|---|
| 53 | |
|---|
| 54 | MyCullCallback(osg::Node* subgraph,osg::Image* image): |
|---|
| 55 | _subgraph(subgraph), |
|---|
| 56 | _image(image), |
|---|
| 57 | _localState(new osg::StateSet) {} |
|---|
| 58 | |
|---|
| 59 | virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) |
|---|
| 60 | { |
|---|
| 61 | |
|---|
| 62 | osgUtil::CullVisitor* cullVisitor = dynamic_cast<osgUtil::CullVisitor*>(nv); |
|---|
| 63 | if (cullVisitor && (_texture.valid()|| _image.valid()) && _subgraph.valid()) |
|---|
| 64 | { |
|---|
| 65 | doPreRender(*node,*cullVisitor); |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | traverse(node,nv); |
|---|
| 69 | |
|---|
| 70 | } |
|---|
| 71 | else |
|---|
| 72 | { |
|---|
| 73 | |
|---|
| 74 | traverse(node,nv); |
|---|
| 75 | } |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | void doPreRender(osg::Node& node, osgUtil::CullVisitor& cv); |
|---|
| 79 | |
|---|
| 80 | osg::ref_ptr<osg::Node> _subgraph; |
|---|
| 81 | osg::ref_ptr<osg::Texture2D> _texture; |
|---|
| 82 | osg::ref_ptr<osg::Image> _image; |
|---|
| 83 | osg::ref_ptr<osg::StateSet> _localState; |
|---|
| 84 | |
|---|
| 85 | }; |
|---|
| 86 | |
|---|
| 87 | void MyCullCallback::doPreRender(osg::Node&, osgUtil::CullVisitor& cv) |
|---|
| 88 | { |
|---|
| 89 | const osg::BoundingSphere& bs = _subgraph->getBound(); |
|---|
| 90 | if (!bs.valid()) |
|---|
| 91 | { |
|---|
| 92 | osg::notify(osg::WARN) << "bb invalid"<<_subgraph.get()<<std::endl; |
|---|
| 93 | return; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | osg::ref_ptr<osgUtil::RenderToTextureStage> rtts = new osgUtil::RenderToTextureStage; |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | osgUtil::RenderStage* previous_stage = cv.getCurrentRenderBin()->_stage; |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | rtts->setClearColor(osg::Vec4(0.1f,0.1f,0.3f,1.0f)); |
|---|
| 107 | rtts->setClearMask(previous_stage->getClearMask()); |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | rtts->setRenderStageLighting(previous_stage->getRenderStageLighting()); |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | osgUtil::RenderBin* previousRenderBin = cv.getCurrentRenderBin(); |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | cv.setCurrentRenderBin(rtts.get()); |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | float znear = 1.0f*bs.radius(); |
|---|
| 122 | float zfar = 3.0f*bs.radius(); |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | float top = 0.25f*znear; |
|---|
| 126 | float right = 0.5f*znear; |
|---|
| 127 | |
|---|
| 128 | znear *= 0.9f; |
|---|
| 129 | zfar *= 1.1f; |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | osg::RefMatrix* projection = new osg::RefMatrix; |
|---|
| 133 | projection->makeFrustum(-right,right,-top,top,znear,zfar); |
|---|
| 134 | |
|---|
| 135 | cv.pushProjectionMatrix(projection); |
|---|
| 136 | |
|---|
| 137 | osg::RefMatrix* matrix = new osg::RefMatrix; |
|---|
| 138 | matrix->makeLookAt(bs.center()+osg::Vec3(0.0f,2.0f,0.0f)*bs.radius(),bs.center(),osg::Vec3(0.0f,0.0f,1.0f)); |
|---|
| 139 | |
|---|
| 140 | cv.pushModelViewMatrix(matrix); |
|---|
| 141 | |
|---|
| 142 | cv.pushStateSet(_localState.get()); |
|---|
| 143 | |
|---|
| 144 | { |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | _subgraph->accept(cv); |
|---|
| 148 | |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | cv.popStateSet(); |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | cv.popModelViewMatrix(); |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | cv.popProjectionMatrix(); |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | cv.setCurrentRenderBin(previousRenderBin); |
|---|
| 161 | |
|---|
| 162 | if (rtts->_renderGraphList.size()==0 && rtts->_bins.size()==0) |
|---|
| 163 | { |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | return; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | int height = 256; |
|---|
| 172 | int width = 512; |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | const osg::Viewport& viewport = *cv.getViewport(); |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | int center_x = viewport.x()+viewport.width()/2; |
|---|
| 181 | int center_y = viewport.y()+viewport.height()/2; |
|---|
| 182 | |
|---|
| 183 | osg::Viewport* new_viewport = new osg::Viewport; |
|---|
| 184 | new_viewport->setViewport(center_x-width/2,center_y-height/2,width,height); |
|---|
| 185 | rtts->setViewport(new_viewport); |
|---|
| 186 | |
|---|
| 187 | _localState->setAttribute(new_viewport); |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | cv.getCurrentRenderBin()->_stage->addToDependencyList(rtts.get()); |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | if (_texture.valid()) rtts->setTexture(_texture.get()); |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | if (_image.valid()) rtts->setImage(_image.get()); |
|---|
| 198 | |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | class MyGeometryCallback : |
|---|
| 203 | public osg::Drawable::UpdateCallback, |
|---|
| 204 | public osg::Drawable::AttributeFunctor |
|---|
| 205 | { |
|---|
| 206 | public: |
|---|
| 207 | |
|---|
| 208 | MyGeometryCallback(const osg::Vec3& o, |
|---|
| 209 | const osg::Vec3& x,const osg::Vec3& y,const osg::Vec3& z, |
|---|
| 210 | double period,double xphase,double amplitude): |
|---|
| 211 | _firstCall(true), |
|---|
| 212 | _startTime(0.0), |
|---|
| 213 | _time(0.0), |
|---|
| 214 | _period(period), |
|---|
| 215 | _xphase(xphase), |
|---|
| 216 | _amplitude(amplitude), |
|---|
| 217 | _origin(o), |
|---|
| 218 | _xAxis(x), |
|---|
| 219 | _yAxis(y), |
|---|
| 220 | _zAxis(z) {} |
|---|
| 221 | |
|---|
| 222 | virtual void update(osg::NodeVisitor* nv,osg::Drawable* drawable) |
|---|
| 223 | { |
|---|
| 224 | const osg::FrameStamp* fs = nv->getFrameStamp(); |
|---|
| 225 | double referenceTime = fs->getReferenceTime(); |
|---|
| 226 | if (_firstCall) |
|---|
| 227 | { |
|---|
| 228 | _firstCall = false; |
|---|
| 229 | _startTime = referenceTime; |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | _time = referenceTime-_startTime; |
|---|
| 233 | |
|---|
| 234 | drawable->accept(*this); |
|---|
| 235 | drawable->dirtyBound(); |
|---|
| 236 | |
|---|
| 237 | osg::Geometry* geometry = dynamic_cast<osg::Geometry*>(drawable); |
|---|
| 238 | if (geometry) |
|---|
| 239 | { |
|---|
| 240 | osgUtil::SmoothingVisitor::smooth(*geometry); |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | virtual void apply(osg::Drawable::AttributeType type,unsigned int count,osg::Vec3* begin) |
|---|
| 246 | { |
|---|
| 247 | if (type == osg::Drawable::VERTICES) |
|---|
| 248 | { |
|---|
| 249 | const float TwoPI=2.0f*osg::PI; |
|---|
| 250 | const float phase = -_time/_period; |
|---|
| 251 | |
|---|
| 252 | osg::Vec3* end = begin+count; |
|---|
| 253 | for (osg::Vec3* itr=begin;itr<end;++itr) |
|---|
| 254 | { |
|---|
| 255 | osg::Vec3 dv(*itr-_origin); |
|---|
| 256 | osg::Vec3 local(dv*_xAxis,dv*_yAxis,dv*_zAxis); |
|---|
| 257 | |
|---|
| 258 | local.z() = local.x()*_amplitude* |
|---|
| 259 | sinf(TwoPI*(phase+local.x()*_xphase)); |
|---|
| 260 | |
|---|
| 261 | (*itr) = _origin + |
|---|
| 262 | _xAxis*local.x()+ |
|---|
| 263 | _yAxis*local.y()+ |
|---|
| 264 | _zAxis*local.z(); |
|---|
| 265 | } |
|---|
| 266 | } |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | bool _firstCall; |
|---|
| 270 | |
|---|
| 271 | double _startTime; |
|---|
| 272 | double _time; |
|---|
| 273 | |
|---|
| 274 | double _period; |
|---|
| 275 | double _xphase; |
|---|
| 276 | float _amplitude; |
|---|
| 277 | |
|---|
| 278 | osg::Vec3 _origin; |
|---|
| 279 | osg::Vec3 _xAxis; |
|---|
| 280 | osg::Vec3 _yAxis; |
|---|
| 281 | osg::Vec3 _zAxis; |
|---|
| 282 | |
|---|
| 283 | }; |
|---|
| 284 | |
|---|
| 285 | osg::Node* createPreRenderSubGraph(osg::Node* subgraph) |
|---|
| 286 | { |
|---|
| 287 | if (!subgraph) return 0; |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | osg::Geometry* polyGeom = new osg::Geometry(); |
|---|
| 291 | |
|---|
| 292 | polyGeom->setSupportsDisplayList(false); |
|---|
| 293 | |
|---|
| 294 | osg::Vec3 origin(0.0f,0.0f,0.0f); |
|---|
| 295 | osg::Vec3 xAxis(1.0f,0.0f,0.0f); |
|---|
| 296 | osg::Vec3 yAxis(0.0f,0.0f,1.0f); |
|---|
| 297 | osg::Vec3 zAxis(0.0f,-1.0f,0.0f); |
|---|
| 298 | float height = 100.0f; |
|---|
| 299 | float width = 200.0f; |
|---|
| 300 | int noSteps = 20; |
|---|
| 301 | |
|---|
| 302 | osg::Vec3Array* vertices = new osg::Vec3Array; |
|---|
| 303 | osg::Vec3 bottom = origin; |
|---|
| 304 | osg::Vec3 top = origin; top.z()+= height; |
|---|
| 305 | osg::Vec3 dv = xAxis*(width/((float)(noSteps-1))); |
|---|
| 306 | |
|---|
| 307 | osg::Vec2Array* texcoords = new osg::Vec2Array; |
|---|
| 308 | osg::Vec2 bottom_texcoord(0.0f,0.0f); |
|---|
| 309 | osg::Vec2 top_texcoord(0.0f,1.0f); |
|---|
| 310 | osg::Vec2 dv_texcoord(1.0f/(float)(noSteps-1),0.0f); |
|---|
| 311 | |
|---|
| 312 | for(int i=0;i<noSteps;++i) |
|---|
| 313 | { |
|---|
| 314 | vertices->push_back(top); |
|---|
| 315 | vertices->push_back(bottom); |
|---|
| 316 | top+=dv; |
|---|
| 317 | bottom+=dv; |
|---|
| 318 | |
|---|
| 319 | texcoords->push_back(top_texcoord); |
|---|
| 320 | texcoords->push_back(bottom_texcoord); |
|---|
| 321 | top_texcoord+=dv_texcoord; |
|---|
| 322 | bottom_texcoord+=dv_texcoord; |
|---|
| 323 | } |
|---|
| 324 | |
|---|
| 325 | |
|---|
| 326 | |
|---|
| 327 | polyGeom->setVertexArray(vertices); |
|---|
| 328 | |
|---|
| 329 | polyGeom->setTexCoordArray(0,texcoords); |
|---|
| 330 | |
|---|
| 331 | |
|---|
| 332 | osg::Vec4Array* colors = new osg::Vec4Array; |
|---|
| 333 | colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); |
|---|
| 334 | polyGeom->setColorArray(colors); |
|---|
| 335 | polyGeom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 336 | |
|---|
| 337 | polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUAD_STRIP,0,vertices->size())); |
|---|
| 338 | |
|---|
| 339 | |
|---|
| 340 | |
|---|
| 341 | osg::StateSet* stateset = new osg::StateSet; |
|---|
| 342 | |
|---|
| 343 | osg::Texture2D* texture = new osg::Texture2D; |
|---|
| 344 | texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR); |
|---|
| 345 | texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR); |
|---|
| 346 | stateset->setTextureAttributeAndModes(0, texture,osg::StateAttribute::ON); |
|---|
| 347 | |
|---|
| 348 | polyGeom->setStateSet(stateset); |
|---|
| 349 | |
|---|
| 350 | polyGeom->setUpdateCallback(new MyGeometryCallback(origin,xAxis,yAxis,zAxis,1.0,1.0/width,0.2f)); |
|---|
| 351 | |
|---|
| 352 | osg::Geode* geode = new osg::Geode(); |
|---|
| 353 | geode->addDrawable(polyGeom); |
|---|
| 354 | |
|---|
| 355 | osg::Group* parent = new osg::Group; |
|---|
| 356 | |
|---|
| 357 | parent->setUpdateCallback(new MyUpdateCallback(subgraph)); |
|---|
| 358 | |
|---|
| 359 | parent->setCullCallback(new MyCullCallback(subgraph,texture)); |
|---|
| 360 | |
|---|
| 361 | parent->addChild(geode); |
|---|
| 362 | |
|---|
| 363 | return parent; |
|---|
| 364 | } |
|---|
| 365 | |
|---|
| 366 | int main( int argc, char **argv ) |
|---|
| 367 | { |
|---|
| 368 | |
|---|
| 369 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 370 | |
|---|
| 371 | |
|---|
| 372 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getProgramName()+" [options] filename ..."); |
|---|
| 373 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 374 | |
|---|
| 375 | |
|---|
| 376 | osgProducer::Viewer viewer(arguments); |
|---|
| 377 | |
|---|
| 378 | |
|---|
| 379 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 380 | |
|---|
| 381 | |
|---|
| 382 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 383 | |
|---|
| 384 | |
|---|
| 385 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 386 | { |
|---|
| 387 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 388 | return 1; |
|---|
| 389 | } |
|---|
| 390 | |
|---|
| 391 | |
|---|
| 392 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 393 | |
|---|
| 394 | |
|---|
| 395 | if (arguments.errors()) |
|---|
| 396 | { |
|---|
| 397 | arguments.writeErrorMessages(std::cout); |
|---|
| 398 | return 1; |
|---|
| 399 | } |
|---|
| 400 | |
|---|
| 401 | |
|---|
| 402 | |
|---|
| 403 | osg::Node* loadedModel = osgDB::readNodeFiles(arguments); |
|---|
| 404 | if (!loadedModel) |
|---|
| 405 | { |
|---|
| 406 | |
|---|
| 407 | return 1; |
|---|
| 408 | } |
|---|
| 409 | |
|---|
| 410 | |
|---|
| 411 | osg::MatrixTransform* loadedModelTransform = new osg::MatrixTransform; |
|---|
| 412 | loadedModelTransform->addChild(loadedModel); |
|---|
| 413 | |
|---|
| 414 | osg::NodeCallback* nc = new osgUtil::TransformCallback(loadedModelTransform->getBound().center(),osg::Vec3(0.0f,0.0f,1.0f),osg::inDegrees(45.0f)); |
|---|
| 415 | loadedModelTransform->setUpdateCallback(nc); |
|---|
| 416 | |
|---|
| 417 | osg::Group* rootNode = new osg::Group(); |
|---|
| 418 | |
|---|
| 419 | rootNode->addChild(createPreRenderSubGraph(loadedModelTransform)); |
|---|
| 420 | |
|---|
| 421 | |
|---|
| 422 | |
|---|
| 423 | viewer.setSceneData( rootNode ); |
|---|
| 424 | |
|---|
| 425 | |
|---|
| 426 | |
|---|
| 427 | viewer.realize(Producer::CameraGroup::ThreadPerCamera); |
|---|
| 428 | |
|---|
| 429 | while( !viewer.done() ) |
|---|
| 430 | { |
|---|
| 431 | |
|---|
| 432 | viewer.sync(); |
|---|
| 433 | |
|---|
| 434 | |
|---|
| 435 | |
|---|
| 436 | viewer.update(); |
|---|
| 437 | |
|---|
| 438 | |
|---|
| 439 | viewer.frame(); |
|---|
| 440 | |
|---|
| 441 | } |
|---|
| 442 | |
|---|
| 443 | return 0; |
|---|
| 444 | } |
|---|