| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | #include <osg/Node> |
|---|
| 13 | #include <osg/Geometry> |
|---|
| 14 | #include <osg/Geode> |
|---|
| 15 | #include <osg/Notify> |
|---|
| 16 | #include <osg/MatrixTransform> |
|---|
| 17 | #include <osg/Texture2D> |
|---|
| 18 | #include <osg/BlendFunc> |
|---|
| 19 | #include <osg/Stencil> |
|---|
| 20 | #include <osg/ColorMask> |
|---|
| 21 | #include <osg/Depth> |
|---|
| 22 | #include <osg/ClipNode> |
|---|
| 23 | #include <osg/AnimationPath> |
|---|
| 24 | |
|---|
| 25 | #include <osgDB/ReadFile> |
|---|
| 26 | #include <osgUtil/Optimizer> |
|---|
| 27 | #include <osgProducer/Viewer> |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | osg::StateSet* createMirrorTexturedState(const std::string& filename) |
|---|
| 50 | { |
|---|
| 51 | osg::StateSet* dstate = new osg::StateSet; |
|---|
| 52 | dstate->setMode(GL_CULL_FACE,osg::StateAttribute::OFF|osg::StateAttribute::PROTECTED); |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | osg::Image* image = osgDB::readImageFile(filename.c_str()); |
|---|
| 56 | if (image) |
|---|
| 57 | { |
|---|
| 58 | osg::Texture2D* texture = new osg::Texture2D; |
|---|
| 59 | texture->setImage(image); |
|---|
| 60 | dstate->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON|osg::StateAttribute::PROTECTED); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | return dstate; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | osg::Drawable* createMirrorSurface(float xMin,float xMax,float yMin,float yMax,float z) |
|---|
| 68 | { |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | osg::Geometry* geom = new osg::Geometry; |
|---|
| 74 | |
|---|
| 75 | osg::Vec3Array* coords = new osg::Vec3Array(4); |
|---|
| 76 | (*coords)[0].set(xMin,yMax,z); |
|---|
| 77 | (*coords)[1].set(xMin,yMin,z); |
|---|
| 78 | (*coords)[2].set(xMax,yMin,z); |
|---|
| 79 | (*coords)[3].set(xMax,yMax,z); |
|---|
| 80 | geom->setVertexArray(coords); |
|---|
| 81 | |
|---|
| 82 | osg::Vec3Array* norms = new osg::Vec3Array(1); |
|---|
| 83 | (*norms)[0].set(0.0f,0.0f,1.0f); |
|---|
| 84 | geom->setNormalArray(norms); |
|---|
| 85 | geom->setNormalBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 86 | |
|---|
| 87 | osg::Vec2Array* tcoords = new osg::Vec2Array(4); |
|---|
| 88 | (*tcoords)[0].set(0.0f,1.0f); |
|---|
| 89 | (*tcoords)[1].set(0.0f,0.0f); |
|---|
| 90 | (*tcoords)[2].set(1.0f,0.0f); |
|---|
| 91 | (*tcoords)[3].set(1.0f,1.0f); |
|---|
| 92 | geom->setTexCoordArray(0,tcoords); |
|---|
| 93 | |
|---|
| 94 | osg::Vec4Array* colours = new osg::Vec4Array(1); |
|---|
| 95 | (*colours)[0].set(1.0f,1.0f,1.0,1.0f); |
|---|
| 96 | geom->setColorArray(colours); |
|---|
| 97 | geom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 98 | |
|---|
| 99 | geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4)); |
|---|
| 100 | |
|---|
| 101 | return geom; |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | osg::Node* createMirroredScene(osg::Node* model) |
|---|
| 105 | { |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | const osg::BoundingSphere& bs = model->getBound(); |
|---|
| 110 | |
|---|
| 111 | float width_factor = 1.5; |
|---|
| 112 | float height_factor = 0.3; |
|---|
| 113 | |
|---|
| 114 | float xMin = bs.center().x()-bs.radius()*width_factor; |
|---|
| 115 | float xMax = bs.center().x()+bs.radius()*width_factor; |
|---|
| 116 | float yMin = bs.center().y()-bs.radius()*width_factor; |
|---|
| 117 | float yMax = bs.center().y()+bs.radius()*width_factor; |
|---|
| 118 | |
|---|
| 119 | float z = bs.center().z()-bs.radius()*height_factor; |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | osg::Drawable* mirror = createMirrorSurface(xMin,xMax,yMin,yMax,z); |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | osg::MatrixTransform* rootNode = new osg::MatrixTransform; |
|---|
| 127 | rootNode->setMatrix(osg::Matrix::rotate(osg::inDegrees(45.0f),1.0f,0.0f,0.0f)); |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | osg::ColorMask* rootColorMask = new osg::ColorMask; |
|---|
| 131 | rootColorMask->setMask(true,true,true,true); |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | osg::Depth* rootDepth = new osg::Depth; |
|---|
| 136 | rootDepth->setFunction(osg::Depth::LESS); |
|---|
| 137 | rootDepth->setRange(0.0,1.0); |
|---|
| 138 | |
|---|
| 139 | osg::StateSet* rootStateSet = new osg::StateSet(); |
|---|
| 140 | rootStateSet->setAttribute(rootColorMask); |
|---|
| 141 | rootStateSet->setAttribute(rootDepth); |
|---|
| 142 | |
|---|
| 143 | rootNode->setStateSet(rootStateSet); |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | { |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | osg::Stencil* stencil = new osg::Stencil; |
|---|
| 152 | stencil->setFunction(osg::Stencil::ALWAYS,1,~0u); |
|---|
| 153 | stencil->setOperation(osg::Stencil::KEEP, osg::Stencil::KEEP, osg::Stencil::REPLACE); |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | osg::ColorMask* colorMask = new osg::ColorMask; |
|---|
| 157 | colorMask->setMask(false,false,false,false); |
|---|
| 158 | |
|---|
| 159 | osg::StateSet* statesetBin1 = new osg::StateSet(); |
|---|
| 160 | statesetBin1->setRenderBinDetails(1,"RenderBin"); |
|---|
| 161 | statesetBin1->setMode(GL_CULL_FACE,osg::StateAttribute::OFF); |
|---|
| 162 | statesetBin1->setAttributeAndModes(stencil,osg::StateAttribute::ON); |
|---|
| 163 | statesetBin1->setAttribute(colorMask); |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | osg::Geode* geode = new osg::Geode; |
|---|
| 167 | geode->addDrawable(mirror); |
|---|
| 168 | geode->setStateSet(statesetBin1); |
|---|
| 169 | |
|---|
| 170 | rootNode->addChild(geode); |
|---|
| 171 | |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | { |
|---|
| 178 | osg::Stencil* stencil = new osg::Stencil; |
|---|
| 179 | stencil->setFunction(osg::Stencil::ALWAYS,0,~0u); |
|---|
| 180 | stencil->setOperation(osg::Stencil::KEEP, osg::Stencil::KEEP, osg::Stencil::REPLACE); |
|---|
| 181 | |
|---|
| 182 | osg::StateSet* statesetBin2 = new osg::StateSet(); |
|---|
| 183 | statesetBin2->setRenderBinDetails(2,"RenderBin"); |
|---|
| 184 | statesetBin2->setAttributeAndModes(stencil,osg::StateAttribute::ON); |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | osg::Group* groupBin2 = new osg::Group(); |
|---|
| 188 | groupBin2->setStateSet(statesetBin2); |
|---|
| 189 | groupBin2->addChild(model); |
|---|
| 190 | |
|---|
| 191 | rootNode->addChild(groupBin2); |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | { |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | osg::Stencil* stencil = new osg::Stencil; |
|---|
| 199 | stencil->setFunction(osg::Stencil::EQUAL,1,~0u); |
|---|
| 200 | stencil->setOperation(osg::Stencil::KEEP, osg::Stencil::KEEP, osg::Stencil::KEEP); |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | osg::ColorMask* colorMask = new osg::ColorMask; |
|---|
| 204 | colorMask->setMask(false,false,false,false); |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | osg::Depth* depth = new osg::Depth; |
|---|
| 208 | depth->setFunction(osg::Depth::ALWAYS); |
|---|
| 209 | depth->setRange(1.0,1.0); |
|---|
| 210 | |
|---|
| 211 | osg::StateSet* statesetBin3 = new osg::StateSet(); |
|---|
| 212 | statesetBin3->setRenderBinDetails(3,"RenderBin"); |
|---|
| 213 | statesetBin3->setMode(GL_CULL_FACE,osg::StateAttribute::OFF); |
|---|
| 214 | statesetBin3->setAttributeAndModes(stencil,osg::StateAttribute::ON); |
|---|
| 215 | statesetBin3->setAttribute(colorMask); |
|---|
| 216 | statesetBin3->setAttribute(depth); |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | osg::Geode* geode = new osg::Geode; |
|---|
| 220 | geode->addDrawable(mirror); |
|---|
| 221 | geode->setStateSet(statesetBin3); |
|---|
| 222 | |
|---|
| 223 | rootNode->addChild(geode); |
|---|
| 224 | |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | { |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | osg::ClipPlane* clipplane = new osg::ClipPlane; |
|---|
| 245 | clipplane->setClipPlane(0.0,0.0,-1.0,z); |
|---|
| 246 | clipplane->setClipPlaneNum(0); |
|---|
| 247 | |
|---|
| 248 | osg::ClipNode* clipNode = new osg::ClipNode; |
|---|
| 249 | clipNode->addClipPlane(clipplane); |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | osg::StateSet* dstate = clipNode->getOrCreateStateSet(); |
|---|
| 253 | dstate->setRenderBinDetails(4,"RenderBin"); |
|---|
| 254 | dstate->setMode(GL_CULL_FACE,osg::StateAttribute::OVERRIDE|osg::StateAttribute::OFF); |
|---|
| 255 | |
|---|
| 256 | osg::Stencil* stencil = new osg::Stencil; |
|---|
| 257 | stencil->setFunction(osg::Stencil::EQUAL,1,~0u); |
|---|
| 258 | stencil->setOperation(osg::Stencil::KEEP, osg::Stencil::KEEP, osg::Stencil::KEEP); |
|---|
| 259 | dstate->setAttributeAndModes(stencil,osg::StateAttribute::ON); |
|---|
| 260 | |
|---|
| 261 | osg::MatrixTransform* reverseMatrix = new osg::MatrixTransform; |
|---|
| 262 | reverseMatrix->setStateSet(dstate); |
|---|
| 263 | reverseMatrix->preMult(osg::Matrix::translate(0.0f,0.0f,-z)* |
|---|
| 264 | osg::Matrix::scale(1.0f,1.0f,-1.0f)* |
|---|
| 265 | osg::Matrix::translate(0.0f,0.0f,z)); |
|---|
| 266 | |
|---|
| 267 | reverseMatrix->addChild(model); |
|---|
| 268 | |
|---|
| 269 | clipNode->addChild(reverseMatrix); |
|---|
| 270 | |
|---|
| 271 | rootNode->addChild(clipNode); |
|---|
| 272 | |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | { |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | osg::Depth* depth = new osg::Depth; |
|---|
| 281 | depth->setFunction(osg::Depth::ALWAYS); |
|---|
| 282 | |
|---|
| 283 | osg::Stencil* stencil = new osg::Stencil; |
|---|
| 284 | stencil->setFunction(osg::Stencil::EQUAL,1,~0u); |
|---|
| 285 | stencil->setOperation(osg::Stencil::KEEP, osg::Stencil::KEEP, osg::Stencil::ZERO); |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | osg::BlendFunc* trans = new osg::BlendFunc; |
|---|
| 289 | trans->setFunction(osg::BlendFunc::ONE,osg::BlendFunc::ONE); |
|---|
| 290 | |
|---|
| 291 | osg::StateSet* statesetBin5 = createMirrorTexturedState("Images/tank.rgb"); |
|---|
| 292 | |
|---|
| 293 | statesetBin5->setRenderBinDetails(5,"RenderBin"); |
|---|
| 294 | statesetBin5->setMode(GL_CULL_FACE,osg::StateAttribute::OFF); |
|---|
| 295 | statesetBin5->setAttributeAndModes(stencil,osg::StateAttribute::ON); |
|---|
| 296 | statesetBin5->setAttributeAndModes(trans,osg::StateAttribute::ON); |
|---|
| 297 | statesetBin5->setAttribute(depth); |
|---|
| 298 | |
|---|
| 299 | |
|---|
| 300 | osg::Geode* geode = new osg::Geode; |
|---|
| 301 | geode->addDrawable(mirror); |
|---|
| 302 | geode->setStateSet(statesetBin5); |
|---|
| 303 | |
|---|
| 304 | rootNode->addChild(geode); |
|---|
| 305 | |
|---|
| 306 | } |
|---|
| 307 | |
|---|
| 308 | return rootNode; |
|---|
| 309 | } |
|---|
| 310 | |
|---|
| 311 | |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | |
|---|
| 316 | |
|---|
| 317 | |
|---|
| 318 | |
|---|
| 319 | |
|---|
| 320 | int main( int argc, char **argv ) |
|---|
| 321 | { |
|---|
| 322 | |
|---|
| 323 | |
|---|
| 324 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 325 | |
|---|
| 326 | |
|---|
| 327 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates the use multi-pass rendering, stencil buffer to create a planer reflection effect."); |
|---|
| 328 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); |
|---|
| 329 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 330 | |
|---|
| 331 | |
|---|
| 332 | |
|---|
| 333 | osgProducer::Viewer viewer(arguments); |
|---|
| 334 | |
|---|
| 335 | |
|---|
| 336 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 337 | |
|---|
| 338 | |
|---|
| 339 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 340 | |
|---|
| 341 | |
|---|
| 342 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 343 | { |
|---|
| 344 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 345 | return 1; |
|---|
| 346 | } |
|---|
| 347 | |
|---|
| 348 | |
|---|
| 349 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 350 | |
|---|
| 351 | |
|---|
| 352 | if (arguments.errors()) |
|---|
| 353 | { |
|---|
| 354 | arguments.writeErrorMessages(std::cout); |
|---|
| 355 | return 1; |
|---|
| 356 | } |
|---|
| 357 | |
|---|
| 358 | if (arguments.argc()<=1) |
|---|
| 359 | { |
|---|
| 360 | arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION); |
|---|
| 361 | return 1; |
|---|
| 362 | } |
|---|
| 363 | |
|---|
| 364 | |
|---|
| 365 | |
|---|
| 366 | osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments); |
|---|
| 367 | |
|---|
| 368 | |
|---|
| 369 | if (!loadedModel) |
|---|
| 370 | { |
|---|
| 371 | std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl; |
|---|
| 372 | return 1; |
|---|
| 373 | } |
|---|
| 374 | |
|---|
| 375 | |
|---|
| 376 | |
|---|
| 377 | osgUtil::Optimizer optimizer; |
|---|
| 378 | optimizer.optimize(loadedModel.get()); |
|---|
| 379 | |
|---|
| 380 | |
|---|
| 381 | osg::ref_ptr<osg::MatrixTransform> loadedModelTransform = new osg::MatrixTransform; |
|---|
| 382 | loadedModelTransform->addChild(loadedModel.get()); |
|---|
| 383 | |
|---|
| 384 | osg::ref_ptr<osg::NodeCallback> nc = new osg::AnimationPathCallback(loadedModelTransform->getBound().center(),osg::Vec3(0.0f,0.0f,1.0f),osg::inDegrees(45.0f)); |
|---|
| 385 | loadedModelTransform->setUpdateCallback(nc.get()); |
|---|
| 386 | |
|---|
| 387 | |
|---|
| 388 | |
|---|
| 389 | osg::ref_ptr<osg::Node> rootNode = createMirroredScene(loadedModelTransform.get()); |
|---|
| 390 | |
|---|
| 391 | |
|---|
| 392 | |
|---|
| 393 | viewer.setSceneData(rootNode.get()); |
|---|
| 394 | |
|---|
| 395 | |
|---|
| 396 | viewer.realize(); |
|---|
| 397 | |
|---|
| 398 | while( !viewer.done() ) |
|---|
| 399 | { |
|---|
| 400 | |
|---|
| 401 | viewer.sync(); |
|---|
| 402 | |
|---|
| 403 | |
|---|
| 404 | |
|---|
| 405 | viewer.update(); |
|---|
| 406 | |
|---|
| 407 | |
|---|
| 408 | viewer.frame(); |
|---|
| 409 | |
|---|
| 410 | } |
|---|
| 411 | |
|---|
| 412 | |
|---|
| 413 | viewer.sync(); |
|---|
| 414 | |
|---|
| 415 | return 0; |
|---|
| 416 | |
|---|
| 417 | } |
|---|