/usr/share/openscenegraph/examples/osglight/osglight.cpp is in openscenegraph-examples 3.2.1-7ubuntu4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | /* OpenSceneGraph example, osglight.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* 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.
*/
#include <osgViewer/Viewer>
#include <osg/Group>
#include <osg/Node>
#include <osg/Light>
#include <osg/LightSource>
#include <osg/StateAttribute>
#include <osg/Geometry>
#include <osg/Point>
#include <osg/MatrixTransform>
#include <osg/PositionAttitudeTransform>
#include <osgDB/Registry>
#include <osgDB/ReadFile>
#include <osgUtil/Optimizer>
#include <osgUtil/SmoothingVisitor>
#include "stdio.h"
// callback to make the loaded model oscilate up and down.
class ModelTransformCallback : public osg::NodeCallback
{
public:
ModelTransformCallback(const osg::BoundingSphere& bs)
{
_firstTime = 0.0;
_period = 4.0f;
_range = bs.radius()*0.5f;
}
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
{
osg::PositionAttitudeTransform* pat = dynamic_cast<osg::PositionAttitudeTransform*>(node);
const osg::FrameStamp* frameStamp = nv->getFrameStamp();
if (pat && frameStamp)
{
if (_firstTime==0.0)
{
_firstTime = frameStamp->getSimulationTime();
}
double phase = (frameStamp->getSimulationTime()-_firstTime)/_period;
phase -= floor(phase);
phase *= (2.0 * osg::PI);
osg::Quat rotation;
rotation.makeRotate(phase,1.0f,1.0f,1.0f);
pat->setAttitude(rotation);
pat->setPosition(osg::Vec3(0.0f,0.0f,sin(phase))*_range);
}
// must traverse the Node's subgraph
traverse(node,nv);
}
double _firstTime;
double _period;
double _range;
};
osg::Node* createLights(osg::BoundingBox& bb,osg::StateSet* rootStateSet)
{
osg::Group* lightGroup = new osg::Group;
float modelSize = bb.radius();
// create a spot light.
osg::Light* myLight1 = new osg::Light;
myLight1->setLightNum(0);
myLight1->setPosition(osg::Vec4(bb.corner(4),1.0f));
myLight1->setAmbient(osg::Vec4(1.0f,0.0f,0.0f,1.0f));
myLight1->setDiffuse(osg::Vec4(1.0f,0.0f,0.0f,1.0f));
myLight1->setSpotCutoff(20.0f);
myLight1->setSpotExponent(50.0f);
myLight1->setDirection(osg::Vec3(1.0f,1.0f,-1.0f));
osg::LightSource* lightS1 = new osg::LightSource;
lightS1->setLight(myLight1);
lightS1->setLocalStateSetModes(osg::StateAttribute::ON);
lightS1->setStateSetModes(*rootStateSet,osg::StateAttribute::ON);
lightGroup->addChild(lightS1);
// create a local light.
osg::Light* myLight2 = new osg::Light;
myLight2->setLightNum(1);
myLight2->setPosition(osg::Vec4(0.0,0.0,0.0,1.0f));
myLight2->setAmbient(osg::Vec4(0.0f,1.0f,1.0f,1.0f));
myLight2->setDiffuse(osg::Vec4(0.0f,1.0f,1.0f,1.0f));
myLight2->setConstantAttenuation(1.0f);
myLight2->setLinearAttenuation(2.0f/modelSize);
myLight2->setQuadraticAttenuation(2.0f/osg::square(modelSize));
osg::LightSource* lightS2 = new osg::LightSource;
lightS2->setLight(myLight2);
lightS2->setLocalStateSetModes(osg::StateAttribute::ON);
lightS2->setStateSetModes(*rootStateSet,osg::StateAttribute::ON);
osg::MatrixTransform* mt = new osg::MatrixTransform();
{
// set up the animation path
osg::AnimationPath* animationPath = new osg::AnimationPath;
animationPath->insert(0.0,osg::AnimationPath::ControlPoint(bb.corner(0)));
animationPath->insert(1.0,osg::AnimationPath::ControlPoint(bb.corner(1)));
animationPath->insert(2.0,osg::AnimationPath::ControlPoint(bb.corner(2)));
animationPath->insert(3.0,osg::AnimationPath::ControlPoint(bb.corner(3)));
animationPath->insert(4.0,osg::AnimationPath::ControlPoint(bb.corner(4)));
animationPath->insert(5.0,osg::AnimationPath::ControlPoint(bb.corner(5)));
animationPath->insert(6.0,osg::AnimationPath::ControlPoint(bb.corner(6)));
animationPath->insert(7.0,osg::AnimationPath::ControlPoint(bb.corner(7)));
animationPath->insert(8.0,osg::AnimationPath::ControlPoint(bb.corner(0)));
animationPath->setLoopMode(osg::AnimationPath::SWING);
mt->setUpdateCallback(new osg::AnimationPathCallback(animationPath));
}
// create marker for point light.
osg::Geometry* marker = new osg::Geometry;
osg::Vec3Array* vertices = new osg::Vec3Array;
vertices->push_back(osg::Vec3(0.0,0.0,0.0));
marker->setVertexArray(vertices);
marker->addPrimitiveSet(new osg::DrawArrays(GL_POINTS,0,1));
osg::StateSet* stateset = new osg::StateSet;
osg::Point* point = new osg::Point;
point->setSize(4.0f);
stateset->setAttribute(point);
marker->setStateSet(stateset);
osg::Geode* markerGeode = new osg::Geode;
markerGeode->addDrawable(marker);
mt->addChild(lightS2);
mt->addChild(markerGeode);
lightGroup->addChild(mt);
return lightGroup;
}
osg::Geometry* createWall(const osg::Vec3& v1,const osg::Vec3& v2,const osg::Vec3& v3,osg::StateSet* stateset)
{
// create a drawable for occluder.
osg::Geometry* geom = new osg::Geometry;
geom->setStateSet(stateset);
unsigned int noXSteps = 100;
unsigned int noYSteps = 100;
osg::Vec3Array* coords = new osg::Vec3Array;
coords->reserve(noXSteps*noYSteps);
osg::Vec3 dx = (v2-v1)/((float)noXSteps-1.0f);
osg::Vec3 dy = (v3-v1)/((float)noYSteps-1.0f);
unsigned int row;
osg::Vec3 vRowStart = v1;
for(row=0;row<noYSteps;++row)
{
osg::Vec3 v = vRowStart;
for(unsigned int col=0;col<noXSteps;++col)
{
coords->push_back(v);
v += dx;
}
vRowStart+=dy;
}
geom->setVertexArray(coords);
osg::Vec4Array* colors = new osg::Vec4Array(1);
(*colors)[0].set(1.0f,1.0f,1.0f,1.0f);
geom->setColorArray(colors, osg::Array::BIND_OVERALL);
for(row=0;row<noYSteps-1;++row)
{
osg::DrawElementsUShort* quadstrip = new osg::DrawElementsUShort(osg::PrimitiveSet::QUAD_STRIP);
quadstrip->reserve(noXSteps*2);
for(unsigned int col=0;col<noXSteps;++col)
{
quadstrip->push_back((row+1)*noXSteps+col);
quadstrip->push_back(row*noXSteps+col);
}
geom->addPrimitiveSet(quadstrip);
}
// create the normals.
osgUtil::SmoothingVisitor::smooth(*geom);
return geom;
}
osg::Node* createRoom(osg::Node* loadedModel)
{
// default scale for this model.
osg::BoundingSphere bs(osg::Vec3(0.0f,0.0f,0.0f),1.0f);
osg::Group* root = new osg::Group;
if (loadedModel)
{
const osg::BoundingSphere& loaded_bs = loadedModel->getBound();
osg::PositionAttitudeTransform* pat = new osg::PositionAttitudeTransform();
pat->setPivotPoint(loaded_bs.center());
pat->setUpdateCallback(new ModelTransformCallback(loaded_bs));
pat->addChild(loadedModel);
bs = pat->getBound();
root->addChild(pat);
}
bs.radius()*=1.5f;
// create a bounding box, which we'll use to size the room.
osg::BoundingBox bb;
bb.expandBy(bs);
// create statesets.
osg::StateSet* rootStateSet = new osg::StateSet;
root->setStateSet(rootStateSet);
osg::StateSet* wall = new osg::StateSet;
wall->setMode(GL_CULL_FACE,osg::StateAttribute::ON);
osg::StateSet* floor = new osg::StateSet;
floor->setMode(GL_CULL_FACE,osg::StateAttribute::ON);
osg::StateSet* roof = new osg::StateSet;
roof->setMode(GL_CULL_FACE,osg::StateAttribute::ON);
osg::Geode* geode = new osg::Geode;
// create front side.
geode->addDrawable(createWall(bb.corner(0),
bb.corner(4),
bb.corner(1),
wall));
// right side
geode->addDrawable(createWall(bb.corner(1),
bb.corner(5),
bb.corner(3),
wall));
// left side
geode->addDrawable(createWall(bb.corner(2),
bb.corner(6),
bb.corner(0),
wall));
// back side
geode->addDrawable(createWall(bb.corner(3),
bb.corner(7),
bb.corner(2),
wall));
// floor
geode->addDrawable(createWall(bb.corner(0),
bb.corner(1),
bb.corner(2),
floor));
// roof
geode->addDrawable(createWall(bb.corner(6),
bb.corner(7),
bb.corner(4),
roof));
root->addChild(geode);
root->addChild(createLights(bb,rootStateSet));
return root;
}
int main( int argc, char **argv )
{
// use an ArgumentParser object to manage the program arguments.
osg::ArgumentParser arguments(&argc,argv);
// construct the viewer.
osgViewer::Viewer viewer;
// load the nodes from the commandline arguments.
osg::Node* loadedModel = osgDB::readNodeFiles(arguments);
// if not loaded assume no arguments passed in, try use default mode instead.
if (!loadedModel) loadedModel = osgDB::readNodeFile("glider.osgt");
// create a room made of foor walls, a floor, a roof, and swinging light fitting.
osg::Node* rootnode = createRoom(loadedModel);
// run optimization over the scene graph
osgUtil::Optimizer optimzer;
optimzer.optimize(rootnode);
// add a viewport to the viewer and attach the scene graph.
viewer.setSceneData( rootnode );
// create the windows and run the threads.
viewer.realize();
viewer.getCamera()->setCullingMode( viewer.getCamera()->getCullingMode() & ~osg::CullStack::SMALL_FEATURE_CULLING);
return viewer.run();
}
|