1 /*****************************************************************************
2  * $LastChangedDate: 2011-04-09 21:58:06 -0400 (Sat, 09 Apr 2011) $
3  * @file
4  * @author  Jim E. Brooks  http://www.palomino3d.org
5  * @brief   SceneGraph class.
6  *//*
7  * LEGAL:   COPYRIGHT (C) 2009 JIM E. BROOKS
8  *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
9  *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
10  *****************************************************************************/
11 
12 #ifndef GRAPH_SCENE_GRAPH_HH
13 #define GRAPH_SCENE_GRAPH_HH 1
14 
15 #define GET_SCENE_GRAPH() (SceneGraph::GetInstance())
16 
17 #include "base/singleton.hh"
18 using namespace base;
19 #include "gfx/rgba.hh"
20 using namespace gfx;
21 namespace object { class Object; }
22 namespace graph { class NodeSort; }
23 #include "graph/base_scene_graph.hh"
24 #include <osg/PolygonMode>
25 
26 namespace graph {
27 
28 ////////////////////////////////////////////////////////////////////////////////
29 /// @brief SceneGraph class contains nodes.
30 /// @verbatim
31 ///                     +------------+
32 ///                     | root node  |
33 ///                 +---+------------+--+
34 ///                 |        |          |
35 ///                 |        |          |
36 ///          +--------+  +--------+  +--------+
37 ///          | branch |  | branch |  | branch |
38 ///          |  node  |  |  node  |  |  node  |
39 ///          | default|  |  [1]   |  |  [2]   |
40 ///          +--------+  +--------+  +--------+
41 /// @endverbatim
42 ///
43 /// This arrangement circumvents OSG's rule of allowing child nodes
44 /// to override similar state of its parent which wouldn't be restored
45 /// after traversal crosses over to a sibling node.
46 ///
47 /// Different ways to attach a node:
48 /// --------------------------------
49 /// AttachNode( RefPtr<Node> node );
50 /// Attach node under branch node having default state.
51 ///
52 /// AttachNode( RefPtr<Node> node, const NodeSort& nodeSort );
53 /// Attach node under a branch node having state defined by NodeSort.
54 /// NodeSort defines typical graphical-state such as transparency.
55 /// SceneGraph creates and caches branch nodes mapped by NodeSort.
56 ///
57 /// AttachBranchNode( RefPtr<GroupNode> branchNode );
58 /// For flexibility, allow client to externally create its own branch node
59 /// having special state.  Client would then attach geometry under it.
60 ///
61 /// State-sorting responsibility:
62 /// -----------------------------
63 /// Nodes with a default graphical state can be attached to the root node.
64 /// If other graphical state is needed, the users of SceneGraph
65 /// are responsible for placing nodes sharing a graphical state
66 /// under a branch node which the user creates.
67 ///
68 /// Implementation:
69 /// ---------------
70 /// SceneGraph is a singleton that provides the public interface.
71 /// SceneGraph _is_ a BaseSceneGraph and _has_ a ShadowSceneGraph.
72 /// Some base methods are overridden to fork operations to both.
73 ///
74 /// Shadows:
75 /// --------
76 /// Though not obvious, shadows are supported.
77 /// Attach shadow casters using NodeSort(SHADOW_CASTER)
78 /// and shadow receivers using NodeSort(SHADOW_RECEIVER).
79 ///
80 class SceneGraph : public BaseSceneGraph
81 {
82 PREVENT_COPYING(SceneGraph)
83 private:
84                                 SceneGraph( void );
85     virtual                     ~SceneGraph();
86     void                        ArrangeNodes( void );
87 
88 // Public interface:
89 public:
90                                 DEFINE_GetInstance( SceneGraph )  // Singleton
91     virtual void                AttachNode( RefPtr<Node> node, const NodeSort& nodeSort );
92     void                        AttachNode( RefPtr<Node> node );  // specific
93     virtual void                AttachObject( shptr<Object> object, const NodeSort& nodeSort );
94     void                        AttachObject( shptr<Object> object );  // specific
95     virtual void                DetachNode( RefPtr<Node> node );
96     virtual void                DetachObject( shptr<Object> object );
97     virtual bool                IfNodeAttached( Node& node );
98     virtual bool                IfObjectAttached( shptr<Object> object );
99     virtual void                Flush( void );
100 
101 // Public interface (specific or special modes):
102     void                        Optimize( void );
103     void                        SetBackgroundColor( const RGBA color );
104     void                        EnableWireframeMode( const bool enable );
105     void                        ToggleWireframeMode( void );
106     void                        SaveToFile( const string& filename, const bool useCompression );
107 
108 // Data members:
109 private:
110     CLASS_VAR SceneGraph*       msInstance;         ///< SceneGraph is a singleton
111     RefPtr<osg::ClearNode>      mClearNode;         ///< to get root node, use BaseSceneGraph::GetRootNode()
112     RefPtr<BaseSceneGraph>      mShadowSceneGraph;  ///< subgraph containing shadow casters/receivers (or is BaseSceneGraph if shadows disabled)
113     bool                        mEnableWireframe;   ///< wireframe mode
114     RefPtr<osg::PolygonMode>    mPolygonMode;       ///< for wireframe mode
115 };
116 
117 } // namespace graph
118 
119 #endif // GRAPH_SCENE_GRAPH_HH
120