| 1 | #include <osg/Group> |
|---|
| 2 | #include <osg/StateSet> |
|---|
| 3 | #include <osg/TextureCubeMap> |
|---|
| 4 | #include <osg/TexGen> |
|---|
| 5 | #include <osg/TexEnvCombine> |
|---|
| 6 | |
|---|
| 7 | #include <osgUtil/ReflectionMapGenerator> |
|---|
| 8 | #include <osgUtil/HighlightMapGenerator> |
|---|
| 9 | #include <osgUtil/HalfWayMapGenerator> |
|---|
| 10 | #include <osgUtil/Optimizer> |
|---|
| 11 | |
|---|
| 12 | #include <osgDB/ReadFile> |
|---|
| 13 | #include <osgDB/Registry> |
|---|
| 14 | |
|---|
| 15 | #include <osgGA/TrackballManipulator> |
|---|
| 16 | #include <osgGA/FlightManipulator> |
|---|
| 17 | #include <osgGA/DriveManipulator> |
|---|
| 18 | |
|---|
| 19 | #include <osgProducer/Viewer> |
|---|
| 20 | |
|---|
| 21 | #include <iostream> |
|---|
| 22 | #include <string> |
|---|
| 23 | #include <vector> |
|---|
| 24 | |
|---|
| 25 | void create_specular_highlights(osg::Node *node) |
|---|
| 26 | { |
|---|
| 27 | osg::StateSet *ss = node->getOrCreateStateSet(); |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | osg::TextureCubeMap *tcm = new osg::TextureCubeMap; |
|---|
| 31 | tcm->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP); |
|---|
| 32 | tcm->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP); |
|---|
| 33 | tcm->setWrap(osg::Texture::WRAP_R, osg::Texture::CLAMP); |
|---|
| 34 | tcm->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR); |
|---|
| 35 | tcm->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR); |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | osgUtil::HighlightMapGenerator *mapgen = new osgUtil::HighlightMapGenerator( |
|---|
| 39 | osg::Vec3(1, 1, -1), |
|---|
| 40 | osg::Vec4(1, 0.9f, 0.8f, 1), |
|---|
| 41 | 8); |
|---|
| 42 | |
|---|
| 43 | mapgen->generateMap(); |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | tcm->setImage(osg::TextureCubeMap::POSITIVE_X, mapgen->getImage(osg::TextureCubeMap::POSITIVE_X)); |
|---|
| 47 | tcm->setImage(osg::TextureCubeMap::NEGATIVE_X, mapgen->getImage(osg::TextureCubeMap::NEGATIVE_X)); |
|---|
| 48 | tcm->setImage(osg::TextureCubeMap::POSITIVE_Y, mapgen->getImage(osg::TextureCubeMap::POSITIVE_Y)); |
|---|
| 49 | tcm->setImage(osg::TextureCubeMap::NEGATIVE_Y, mapgen->getImage(osg::TextureCubeMap::NEGATIVE_Y)); |
|---|
| 50 | tcm->setImage(osg::TextureCubeMap::POSITIVE_Z, mapgen->getImage(osg::TextureCubeMap::POSITIVE_Z)); |
|---|
| 51 | tcm->setImage(osg::TextureCubeMap::NEGATIVE_Z, mapgen->getImage(osg::TextureCubeMap::NEGATIVE_Z)); |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | ss->setTextureAttributeAndModes(0, tcm, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON); |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | osg::TexGen *tg = new osg::TexGen; |
|---|
| 58 | tg->setMode(osg::TexGen::REFLECTION_MAP); |
|---|
| 59 | ss->setTextureAttributeAndModes(0, tg, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON); |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | osg::TexEnvCombine *te = new osg::TexEnvCombine; |
|---|
| 63 | te->setCombine_RGB(osg::TexEnvCombine::ADD); |
|---|
| 64 | te->setSource0_RGB(osg::TexEnvCombine::TEXTURE); |
|---|
| 65 | te->setOperand0_RGB(osg::TexEnvCombine::SRC_COLOR); |
|---|
| 66 | te->setSource1_RGB(osg::TexEnvCombine::PRIMARY_COLOR); |
|---|
| 67 | te->setOperand1_RGB(osg::TexEnvCombine::SRC_COLOR); |
|---|
| 68 | ss->setTextureAttributeAndModes(0, te, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | int main(int argc, char *argv[]) |
|---|
| 72 | { |
|---|
| 73 | |
|---|
| 74 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getProgramName()+" [options] filename ..."); |
|---|
| 78 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | osgProducer::Viewer viewer(arguments); |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 91 | { |
|---|
| 92 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 93 | return 1; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | if (arguments.errors()) |
|---|
| 101 | { |
|---|
| 102 | arguments.writeErrorMessages(std::cout); |
|---|
| 103 | return 1; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | osg::Node* rootnode = osgDB::readNodeFiles(arguments); |
|---|
| 108 | if (!rootnode) { |
|---|
| 109 | return 1; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | create_specular_highlights(rootnode); |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | osgUtil::Optimizer optimzer; |
|---|
| 117 | optimzer.optimize(rootnode); |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | viewer.setSceneData(rootnode); |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | viewer.realize(Producer::CameraGroup::ThreadPerCamera); |
|---|
| 124 | |
|---|
| 125 | while( !viewer.done() ) |
|---|
| 126 | { |
|---|
| 127 | |
|---|
| 128 | viewer.sync(); |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | viewer.update(); |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | viewer.frame(); |
|---|
| 136 | |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | return 0; |
|---|
| 140 | } |
|---|