root/OpenSceneGraph/trunk/examples/osgoit/osgoit.cpp @ 13748

Revision 13556, 5.9 kB (checked in by robert, 14 hours ago)

From Jason Beverage, "It looks like the Callback header got accidentally removed from the CMakeLists.txt in the submission yesterday for the geometry instancing example."

  • Property svn:eol-style set to native
Line 
1/* OpenSceneGraph example, myosgoit.
2*
3*  Author: Christian Buchner, based on original osgoit by Mathias Frhlich
4*
5*  This demo provides a DepthPeeling object that can correctly compose
6*  solid and transparent geometry within the same scene. The transparent
7*  geometry can also use GLSL shaders, as demonstrated in the 3D HeatMap.
8*  The solid geometry is only rendered once, and its depth buffer blitted
9*  into the cameras rendering the transparency layers.
10*
11*  Permission is hereby granted, free of charge, to any person obtaining a copy
12*  of this software and associated documentation files (the "Software"), to deal
13*  in the Software without restriction, including without limitation the rights
14*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15*  copies of the Software, and to permit persons to whom the Software is
16*  furnished to do so, subject to the following conditions:
17*
18*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24*  THE SOFTWARE.
25*/
26
27#include "DepthPeeling.h"
28#include "HeatMap.h"
29
30#include <osgViewer/Viewer>
31#include <osgViewer/ViewerEventHandlers>
32
33#include <osg/Math>
34#include <osg/PositionAttitudeTransform>
35#include <osg/BlendFunc>
36#include <osg/Material>
37#include <osg/LightModel>
38#include <osgDB/ReadFile>
39
40#include <limits>
41#include <iostream>
42
43int main(int argc, char** argv)
44{
45    // use an ArgumentParser object to manage the program arguments.
46    osg::ArgumentParser arguments(&argc, argv);
47    arguments.getApplicationUsage()->addKeyboardMouseBinding("m", "Increase the number of depth peeling layers");
48    arguments.getApplicationUsage()->addKeyboardMouseBinding("n", "Decrease the number of depth peeling layers");
49    arguments.getApplicationUsage()->addKeyboardMouseBinding("l", "Toggle display of the individual or composed layer textures");
50    arguments.getApplicationUsage()->addKeyboardMouseBinding("p", "Increase the layer offset");
51    arguments.getApplicationUsage()->addKeyboardMouseBinding("o", "Decrease the layer offset");
52
53    // Have the usual viewer
54    osgViewer::Viewer viewer(arguments);
55
56    osg::DisplaySettings* displaySettings = new osg::DisplaySettings;
57    viewer.setDisplaySettings(displaySettings);
58   
59    // Add the stats handler
60    viewer.addEventHandler(new osgViewer::StatsHandler);
61   
62    // add the help handler
63    viewer.addEventHandler(new osgViewer::HelpHandler(arguments.getApplicationUsage()));
64
65    // any option left unread are converted into errors to write out later.
66    arguments.reportRemainingOptionsAsUnrecognized();
67   
68    // read the dump truck, we will need it twice
69    osg::ref_ptr<osg::Node> dt = osgDB::readNodeFile("dumptruck.osg");
70
71    // display a solid version of the dump truck
72    osg::ref_ptr<osg::PositionAttitudeTransform> solidModel = new osg::PositionAttitudeTransform;
73    solidModel->setPosition(osg::Vec3f(7.0f, -2.0f, 7.0f));
74    solidModel->addChild(dt.get());
75
76    // generate the 3D heatmap surface to display
77    osg::ref_ptr<Heatmap> hm = new Heatmap(30, 30, 10, 30, 30, 1.0, 0.25);
78    float data[30][30];
79    for (int x=0; x < 30; ++x)
80        for (int y=0; y < 30; ++y)
81            data[y][x] = (double)rand() / RAND_MAX;
82    hm->setData((float*)data, 10.0, 1.0, 0.25);
83
84    // add a transparent version of the truck to the scene also
85    osg::ref_ptr<osg::PositionAttitudeTransform> transparentTruck = new osg::PositionAttitudeTransform;
86    transparentTruck->setPosition(osg::Vec3f(7.0f, -25.0f, 7.0f));
87
88    // set the states of the truck so that it actually appears transparently and nicely lit.
89    osg::StateSet *state = transparentTruck->getOrCreateStateSet();
90    state->setMode(GL_BLEND, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
91    state->setAttribute(new osg::BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
92    osg::Material* material = new osg::Material;
93    material->setAmbient(osg::Material::FRONT_AND_BACK,osg::Vec4(0.2f,0.2f,0.2f,0.3f));
94    material->setDiffuse(osg::Material::FRONT_AND_BACK,osg::Vec4(0.8f,0.8f,0.8f,0.3f));
95    state->setAttribute(material,osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
96    osg::LightModel *lm = new osg::LightModel();
97    lm->setTwoSided(true);
98    state->setAttribute(lm, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
99    (transparentTruck.get())->addChild(dt.get());
100
101    // place the heatmap and a transparent dump truck in the transparent geometry group
102    osg::ref_ptr<osg::Group> transparentModel = new osg::Group;
103    (transparentModel.get())->addChild(hm.get());
104    (transparentModel.get())->addChild(transparentTruck.get());
105
106    // The initial size set to 0, 0. We get a resize event for the right size...
107    DepthPeeling* depthPeeling = new DepthPeeling(0, 0);
108    // the heat map already uses two textures bound to unit 0 and 1, so we can use TexUnit 2 for the peeling
109    depthPeeling->setTexUnit(2);
110    depthPeeling->setSolidScene(solidModel.get());
111    depthPeeling->setTransparentScene(transparentModel.get());
112    viewer.setSceneData(depthPeeling->getRoot());
113
114    // Add the event handler for the depth peeling stuff
115    viewer.addEventHandler(new DepthPeeling::EventHandler(depthPeeling));
116
117    // force a resize event, so the DepthPeeling object updates _texWidth and _texHeight
118    viewer.realize();
119    int x, y, width, height;
120    osgViewer::ViewerBase::Windows windows;
121    viewer.getWindows(windows);
122    windows.front()->getWindowRectangle(x,y,width,height);
123    viewer.getEventQueue()->windowResize(x,y,width,height);
124
125    return viewer.run();
126}
Note: See TracBrowser for help on using the browser.