Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ScenePlainOpenSceneGraph.cpp
1 #include <GLviewer/Scenes/SceneBase.hh>
2 #include <Base/Common/FileHandling.hh>
3 #include "ScenePlainOpenSceneGraph.hh"
4 #include <osg/MatrixTransform>
5 #include <osgShadow/ShadowedScene>
6 #include <osg/Depth>
7 #include <cctype>
8 
9 using namespace std;
10 using namespace BIAS;
11 
12 ScenePlainOpenSceneGraph::ScenePlainOpenSceneGraph() : SceneBase()
13 {
14  initialized_ = false;
15 }
16 
17 int ScenePlainOpenSceneGraph::Init(unsigned int width, unsigned int height)
18 {
19  if (initialized_) {
20  BIASERR("Init on SceneOpenSceneGraph should only be called once: skipping\n");
21  return 0;
22  }
23 
24  w_ = width;
25  h_ = height;
26  sceneView_ = new osgViewer::Viewer;
27  sceneView_->setThreadingModel(osgViewer::Viewer::SingleThreaded);
28  embedWindow_ = sceneView_->setUpViewerAsEmbeddedInWindow(0, 0, w_, h_);
29  sceneView_->getCamera()->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
30  sceneView_->getCamera()->setClearMask(0);
31  //sceneView_->setLightingMode(osg::View::NO_LIGHT);
32  rootNode_ = new osg::Group;
33  sceneView_->setSceneData(rootNode_.get());
34  sceneView_->realize();
35  initialized_ = true;
36  return 0;
37 }
38 
40  if (on){
41  rootNode_->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::ON);
42  }
43  else{
44  rootNode_->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
45  }
46 }
47 
49 {
50  if (initialized_) {
51  initialized_ = false;
52  // todo this is a workaround for on exit crash in glutglviewer fkellner
53  sceneView_.release();
54  sceneView_ = NULL;
55  rootNode_ = NULL;
56  }
57 }
58 
59 bool ScenePlainOpenSceneGraph::Load(std::string filename) {
61  node = osgDB::readNodeFile(filename);
62  if (node.get()) {
63  rootNode_->addChild(node.get());
64  rootNode_->dirtyBound();
65  return true;
66  }
67  else
68  return false;
69 }
70 
71 
73  node = osgDB::readNodeFile(fileName);
74  string path, base, suffix;
75  BIAS::FileHandling::SplitName(fileName, path, base, suffix);
76  std::transform(suffix.begin(), suffix.end(), suffix.begin(), (int
77  (*)(int)) std::tolower);
78  if (suffix == ".wrl") {
79  // osg wrl import automatically adds a transform node (rotation around
80  // x-axis). This needs to be dropped
81  osg::MatrixTransform *mt =
82  dynamic_cast<osg::MatrixTransform *> (node.get());
83  if (mt && mt->getNumChildren() == 1) {
84  node = mt->getChild(0);
85  }
86  }
87  rootNode_->addChild(node.get());
88  rootNode_->dirtyBound();
89  return 0;
90 }
91 
92 int ScenePlainOpenSceneGraph::AppendSubTreeFromFile(const std::string& fileName) {
94  AppendSubTreeFromFile(fileName, node);
95  return 0;
96 }
97 
99  rootNode_->addChild(node.get());
100  rootNode_->dirtyBound();
101 }
103 
104  rootNode_->removeChild(node.get());
105  rootNode_->dirtyBound();
106 }
107 
109  int children = rootNode_->getNumChildren();
110  rootNode_->removeChild(0,children);
111  rootNode_->dirtyBound();
112 }
113 
114 
116  rootNode_ = newRoot;
117  sceneView_->setSceneData(rootNode_.get());
118 }
119 
121 {
122 
123  GLint viewParams[4];
124  glGetIntegerv( GL_VIEWPORT, viewParams );
125  if (w_ != (unsigned int)(viewParams[2]) || h_ != (unsigned int)(viewParams[3])) {
126  w_ = (unsigned int)viewParams[2];
127  h_ = (unsigned int)viewParams[3];
128  embedWindow_->resized(embedWindow_->getTraits()->x, embedWindow_->getTraits()->y, w_, h_);
129  embedWindow_->getEventQueue()->windowResize(embedWindow_->getTraits()->x, embedWindow_->getTraits()->y, w_, h_ );
130  }
131 
132  GLfloat glMat[16];
133  osg::Matrixf osgMat;
134 
135  glGetFloatv( GL_PROJECTION_MATRIX, glMat );
136  osgMat.set( glMat );
137  sceneView_->getCamera()->setProjectionMatrix( osgMat );
138 
139  glGetFloatv( GL_MODELVIEW_MATRIX, glMat );
140  osgMat.set( glMat );
141  sceneView_->getCamera()->getViewMatrix().set( osgMat );
142 
143  glPushAttrib( GL_VIEWPORT_BIT );
144  glMatrixMode(GL_PROJECTION);
145  glPushMatrix();
146  glMatrixMode(GL_TEXTURE);
147  glPushMatrix();
148  glMatrixMode(GL_MODELVIEW);
149  glPushMatrix();
150  if (sceneView_.valid())
151  sceneView_->frame();
152  glMatrixMode(GL_MODELVIEW);
153  glPopMatrix();
154  glMatrixMode(GL_TEXTURE);
155  glPopMatrix();
156  glMatrixMode(GL_PROJECTION);
157  glPopMatrix();
158  glPopAttrib(); // GL_VIEWPORT_BIT
159 }
160 
161 
163 {
164  const osg::BoundingSphere& bs = rootNode_->getBound();
165 
166  BIAS::Vector3<double> center((double) bs.center()[0], (double) bs.center()[1],
167  (double) bs.center()[2]);
168 
169  double radius = (double) bs.radius();
170 
171  BIAS::Vector3<double> diag(radius, radius, radius);
172  themin = center - diag;
173  themax = center + diag;
174 }
175 
178 {
179  const osg::BoundingSphere& bs = rootNode_->getBound();
180 
181  return BIAS::Vector3<double>((double) bs.center()[0],
182  (double) bs.center()[1], (double) bs.center()[2]);
183 }
184 
virtual void Draw()
To do anything usefull, overload this method, assume context is ready and draw.
osg::ref_ptr< osg::Group > rootNode_
virtual Vector3< double > GetBoundingBoxCenter()
virtual void GetBoundingBox(BIAS::Vector3< double > &themin, BIAS::Vector3< double > &themax)
Determine and return the bouning box of all elements of the scene.
osg::ref_ptr< osgViewer::Viewer > sceneView_
static void SplitName(const std::string &fullname, std::string &dir, std::string &base, std::string &suffix)
Split full path into:
void AppendNode(osg::ref_ptr< osg::Node > node)
int AppendSubTreeFromFile(const std::string &fileName, osg::ref_ptr< osg::Node > &childNode)
Base class for all scenes.
Definition: SceneBase.hh:68
osg::observer_ptr< osgViewer::GraphicsWindow > embedWindow_
int Init(unsigned int width=1200, unsigned int height=900)
void RemoveNode(osg::ref_ptr< osg::Node > node)
void SetRootNode(osg::ref_ptr< osg::Group > newRoot)