osgEarth 2.1.1
|
00001 /* -*-c++-*- */ 00002 /* osgEarth - Dynamic map generation toolkit for OpenSceneGraph 00003 * Copyright 2008-2010 Pelican Mapping 00004 * http://osgearth.org 00005 * 00006 * osgEarth is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU Lesser General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public License 00017 * along with this program. If not, see <http://www.gnu.org/licenses/> 00018 */ 00019 00028 #include <osg/Notify> 00029 #include <osgGA/StateSetManipulator> 00030 #include <osgViewer/Viewer> 00031 #include <osgViewer/ViewerEventHandlers> 00032 #include <osgEarthUtil/EarthManipulator> 00033 #include <osgEarth/ShaderComposition> 00034 #include <osgEarth/Registry> 00035 00036 osg::StateAttribute* createHaze(); 00037 00038 int usage( const std::string& msg ) 00039 { 00040 OE_NOTICE 00041 << msg << std::endl 00042 << "USAGE: osgearth_shadercomp <earthfile>" << std::endl; 00043 00044 return -1; 00045 } 00046 00047 int main(int argc, char** argv) 00048 { 00049 osg::ArgumentParser arguments(&argc,argv); 00050 00051 osg::Node* earthNode = osgDB::readNodeFiles( arguments ); 00052 if (!earthNode) 00053 { 00054 return usage( "Unable to load earth model." ); 00055 } 00056 00057 osg::Group* root = new osg::Group(); 00058 root->addChild( earthNode ); 00059 00060 osgViewer::Viewer viewer(arguments); 00061 viewer.setCameraManipulator( new osgEarth::Util::EarthManipulator() ); 00062 viewer.setSceneData( root ); 00063 00064 // inject the haze shader components: 00065 root->getOrCreateStateSet()->setAttributeAndModes( createHaze(), osg::StateAttribute::ON ); 00066 00067 // add some stock OSG handlers: 00068 viewer.addEventHandler(new osgViewer::StatsHandler()); 00069 viewer.addEventHandler(new osgViewer::WindowSizeHandler()); 00070 viewer.addEventHandler(new osgViewer::ThreadingHandler()); 00071 viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet())); 00072 00073 return viewer.run(); 00074 } 00075 00076 //------------------------------------------------------------------------- 00077 00078 static char s_hazeVertShader[] = 00079 "varying vec3 v_pos; \n" 00080 "void setup_haze() \n" 00081 "{ \n" 00082 " v_pos = vec3(gl_ModelViewMatrix * gl_Vertex); \n" 00083 "} \n"; 00084 00085 static char s_hazeFragShader[] = 00086 "varying vec3 v_pos; \n" 00087 "void apply_haze(inout vec4 color) \n" 00088 "{ \n" 00089 " float dist = clamp( length(v_pos)/10000000.0, 0, 0.75 ); \n" 00090 " color = mix(color, vec4(0.5, 0.5, 0.5, 1.0), dist); \n" 00091 "} \n"; 00092 00093 00094 osg::StateAttribute* 00095 createHaze() 00096 { 00097 osgEarth::VirtualProgram* vp = new osgEarth::VirtualProgram(); 00098 00099 vp->setFunction( "setup_haze", s_hazeVertShader, osgEarth::ShaderComp::LOCATION_VERTEX_POST_LIGHTING ); 00100 vp->setFunction( "apply_haze", s_hazeFragShader, osgEarth::ShaderComp::LOCATION_FRAGMENT_POST_LIGHTING ); 00101 00102 return vp; 00103 }