| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | #include <osg/Group> |
|---|
| 20 | #include <osg/StateSet> |
|---|
| 21 | #include <osg/TextureCubeMap> |
|---|
| 22 | #include <osg/TexGen> |
|---|
| 23 | #include <osg/TexEnvCombine> |
|---|
| 24 | |
|---|
| 25 | #include <osgUtil/ReflectionMapGenerator> |
|---|
| 26 | #include <osgUtil/HighlightMapGenerator> |
|---|
| 27 | #include <osgUtil/HalfWayMapGenerator> |
|---|
| 28 | #include <osgUtil/Optimizer> |
|---|
| 29 | |
|---|
| 30 | #include <osgDB/ReadFile> |
|---|
| 31 | #include <osgDB/Registry> |
|---|
| 32 | |
|---|
| 33 | #include <osgGA/TrackballManipulator> |
|---|
| 34 | #include <osgGA/FlightManipulator> |
|---|
| 35 | #include <osgGA/DriveManipulator> |
|---|
| 36 | |
|---|
| 37 | #include <osgViewer/Viewer> |
|---|
| 38 | |
|---|
| 39 | #include <iostream> |
|---|
| 40 | #include <string> |
|---|
| 41 | #include <vector> |
|---|
| 42 | |
|---|
| 43 | void create_specular_highlights(osg::Node *node) |
|---|
| 44 | { |
|---|
| 45 | osg::StateSet *ss = node->getOrCreateStateSet(); |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | osg::TextureCubeMap *tcm = new osg::TextureCubeMap; |
|---|
| 49 | tcm->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP); |
|---|
| 50 | tcm->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP); |
|---|
| 51 | tcm->setWrap(osg::Texture::WRAP_R, osg::Texture::CLAMP); |
|---|
| 52 | tcm->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR); |
|---|
| 53 | tcm->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR); |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | osgUtil::HighlightMapGenerator *mapgen = new osgUtil::HighlightMapGenerator( |
|---|
| 57 | osg::Vec3(1, 1, -1), |
|---|
| 58 | osg::Vec4(1, 0.9f, 0.8f, 1), |
|---|
| 59 | 8); |
|---|
| 60 | |
|---|
| 61 | mapgen->generateMap(); |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | tcm->setImage(osg::TextureCubeMap::POSITIVE_X, mapgen->getImage(osg::TextureCubeMap::POSITIVE_X)); |
|---|
| 65 | tcm->setImage(osg::TextureCubeMap::NEGATIVE_X, mapgen->getImage(osg::TextureCubeMap::NEGATIVE_X)); |
|---|
| 66 | tcm->setImage(osg::TextureCubeMap::POSITIVE_Y, mapgen->getImage(osg::TextureCubeMap::POSITIVE_Y)); |
|---|
| 67 | tcm->setImage(osg::TextureCubeMap::NEGATIVE_Y, mapgen->getImage(osg::TextureCubeMap::NEGATIVE_Y)); |
|---|
| 68 | tcm->setImage(osg::TextureCubeMap::POSITIVE_Z, mapgen->getImage(osg::TextureCubeMap::POSITIVE_Z)); |
|---|
| 69 | tcm->setImage(osg::TextureCubeMap::NEGATIVE_Z, mapgen->getImage(osg::TextureCubeMap::NEGATIVE_Z)); |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | ss->setTextureAttributeAndModes(0, tcm, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON); |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | osg::TexGen *tg = new osg::TexGen; |
|---|
| 76 | tg->setMode(osg::TexGen::REFLECTION_MAP); |
|---|
| 77 | ss->setTextureAttributeAndModes(0, tg, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON); |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | osg::TexEnvCombine *te = new osg::TexEnvCombine; |
|---|
| 81 | te->setCombine_RGB(osg::TexEnvCombine::ADD); |
|---|
| 82 | te->setSource0_RGB(osg::TexEnvCombine::TEXTURE); |
|---|
| 83 | te->setOperand0_RGB(osg::TexEnvCombine::SRC_COLOR); |
|---|
| 84 | te->setSource1_RGB(osg::TexEnvCombine::PRIMARY_COLOR); |
|---|
| 85 | te->setOperand1_RGB(osg::TexEnvCombine::SRC_COLOR); |
|---|
| 86 | ss->setTextureAttributeAndModes(0, te, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | int main(int argc, char *argv[]) |
|---|
| 90 | { |
|---|
| 91 | |
|---|
| 92 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | osgViewer::Viewer viewer; |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | osg::Node* rootnode = osgDB::readNodeFiles(arguments); |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | if (!rootnode) rootnode = osgDB::readNodeFile("cessna.osgt"); |
|---|
| 102 | |
|---|
| 103 | if (!rootnode) |
|---|
| 104 | { |
|---|
| 105 | osg::notify(osg::NOTICE)<<"Please specify a model filename on the command line."<<std::endl; |
|---|
| 106 | return 1; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | create_specular_highlights(rootnode); |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | osgUtil::Optimizer optimzer; |
|---|
| 114 | optimzer.optimize(rootnode); |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | viewer.setSceneData(rootnode); |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | viewer.realize(); |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | for(unsigned int contextID = 0; |
|---|
| 124 | contextID<osg::DisplaySettings::instance()->getMaxNumberOfGraphicsContexts(); |
|---|
| 125 | ++contextID) |
|---|
| 126 | { |
|---|
| 127 | osg::TextureCubeMap::Extensions* tcmExt = osg::TextureCubeMap::getExtensions(contextID,false); |
|---|
| 128 | if (tcmExt) |
|---|
| 129 | { |
|---|
| 130 | if (!tcmExt->isCubeMapSupported()) |
|---|
| 131 | { |
|---|
| 132 | std::cout<<"Warning: texture_cube_map not supported by OpenGL drivers, unable to run application."<<std::endl; |
|---|
| 133 | return 1; |
|---|
| 134 | } |
|---|
| 135 | } |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | return viewer.run(); |
|---|
| 139 | } |
|---|