Last active
August 31, 2016 17:53
-
-
Save gwaldron/ad98e47bbc7c746c472cbeb9022c5692 to your computer and use it in GitHub Desktop.
GLSL model outline for osgEarth VP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* -*-c++-*- */ | |
| /* osgEarth - Dynamic map generation toolkit for OpenSceneGraph | |
| * Copyright 2015 Pelican Mapping | |
| * http://osgearth.org | |
| * | |
| * osgEarth is free software; you can redistribute it and/or modify | |
| * it under the terms of the GNU Lesser General Public License as published by | |
| * the Free Software Foundation; either version 2 of the License, or | |
| * (at your option) any later version. | |
| * | |
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
| * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
| * IN THE SOFTWARE. | |
| * | |
| * You should have received a copy of the GNU Lesser General Public License | |
| * along with this program. If not, see <http://www.gnu.org/licenses/> | |
| */ | |
| #include <osgViewer/Viewer> | |
| #include <osg/Depth> | |
| #include <osgDB/ReadFile> | |
| #include <osgEarth/Registry> | |
| #define LC "[viewer] " | |
| using namespace osgEarth; | |
| int | |
| main(int argc, char** argv) | |
| { | |
| osg::ArgumentParser arguments(&argc,argv); | |
| osg::Node* node = osgDB::readNodeFiles(arguments); | |
| Registry::shaderGenerator().run(node); | |
| const char* VS = | |
| "#version 330 compatibility\n" | |
| "const float outline = 0.002; \n" | |
| "vec3 vp_Normal;\n" | |
| "void VS(inout vec4 clip) {\n" | |
| " vec3 n = normalize(vp_Normal); \n" | |
| " n.x *= gl_ProjectionMatrix[0][0];\n" | |
| " n.y *= gl_ProjectionMatrix[1][1];\n" | |
| " clip.xy += n.xy * clip.w * outline;\n" | |
| "}\n"; | |
| const char* FS = | |
| "#version 330\n" | |
| "void FS(inout vec4 color) { \n" | |
| " color = vec4(1,0,0,color.a);\n" | |
| "}\n"; | |
| osg::Group* outline = new osg::Group(); | |
| osg::StateSet* outlineSS = outline->getOrCreateStateSet(); | |
| outlineSS->setRenderBinDetails(10, "RenderBin"); | |
| outlineSS->setAttributeAndModes(new osg::Depth(osg::Depth::LESS, 0.0, 1.0, false)); | |
| outlineSS->setMode(GL_CULL_FACE, 0); | |
| VirtualProgram* vp = VirtualProgram::getOrCreate(outlineSS); | |
| vp->setFunction("VS", VS, ShaderComp::LOCATION_VERTEX_CLIP); | |
| vp->setFunction("FS", FS, ShaderComp::LOCATION_FRAGMENT_COLORING); | |
| outline->addChild(node); | |
| osg::Group* model = new osg::Group(); | |
| model->getOrCreateStateSet()->setRenderBinDetails(20, "RenderBin"); | |
| model->addChild(node); | |
| osg::Group* root = new osg::Group(); | |
| root->addChild(outline); | |
| root->addChild(model); | |
| osgViewer::Viewer viewer(arguments); | |
| viewer.setSceneData(root); | |
| return viewer.run(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment