Transparency example 2


/ Published in: C++
Save to your folder(s)

Full example


Copy this code and paste it in your HTML
  1. Its a modification of the Viewer example from the Qucik start guide.
  2. It loads up a cow and overrides the alpha to 0.1.
  3. Hope you can start from there.
  4.  
  5. Dimi
  6.  
  7. // Viewer Example, A minimal OSG viewer
  8. #include <osgDB/WriteFile>
  9. #include <osg/Notify>
  10. #include <osgViewer/Viewer>
  11. #include <osgDB/ReadFile>
  12. #include <osg/MatrixTransform>
  13. #include <osg/Geode>
  14. #include <osg/Geometry>
  15. #include <osg/StateSet>
  16. #include <osg/StateAttribute>
  17. #include <osg/CullFace>
  18. #include <osg/Point>
  19. #include <osg/Light>
  20. #include <osg/LightSource>
  21. #include <osg/BlendFunc>
  22. #include <osg/Material>
  23. #include <osg/PolygonMode>
  24. #include <osg/Notify>
  25.  
  26. int
  27. main( int, char ** )
  28. {
  29. // Create a Viewer.
  30. osgViewer::Viewer viewer;
  31.  
  32. // Load a model and add it to the Viewer.
  33. osg::ref_ptr<osg::Node> nde = osgDB::readNodeFile( "cow.osg" );
  34.  
  35. // Create StateSet and Material
  36. osg::StateSet* state2 = nde->getOrCreateStateSet();
  37. osg::ref_ptr<osg::Material> mat2 = new osg::Material;
  38.  
  39. // Set alpha to 0.1
  40. mat2->setAlpha(osg::Material::FRONT_AND_BACK, 0.1);
  41. state2->setAttributeAndModes( mat2.get() , osg::StateAttribute::ON |
  42. osg::StateAttribute::OVERRIDE);
  43.  
  44. // Turn on blending
  45. osg::BlendFunc* bf = new osg::BlendFunc(osg::BlendFunc::SRC_ALPHA,
  46. osg::BlendFunc::ONE_MINUS_SRC_ALPHA );
  47. state2->setAttributeAndModes(bf);
  48.  
  49. viewer.setSceneData(nde.get());
  50.  
  51. if (!viewer.getSceneData())
  52. {
  53. osg::notify( osg::FATAL ) << "Unable to load data file. Exiting." <<
  54. std::endl;
  55. return 1;
  56. }
  57.  
  58. // Display, and main loop.
  59. return viewer.run();
  60. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.