1 // OpenSG Tutorial Example: Share
3 // This example shows how to use core sharing to create copies
4 // of an object, e.g. a geometry, which take up less memory.
7 #ifdef OSG_BUILD_ACTIVE
10 #include <OSGConfig.h>
11 #include <OSGSimpleGeometry.h>
12 #include <OSGGLUTWindow.h>
13 #include <OSGSimpleSceneManager.h>
14 #include <OSGBaseFunctions.h>
15 #include <OSGTransform.h>
19 // the Group node core
23 #include <OpenSG/OSGGLUT.h>
24 #include <OpenSG/OSGConfig.h>
25 #include <OpenSG/OSGSimpleGeometry.h>
26 #include <OpenSG/OSGGLUTWindow.h>
27 #include <OpenSG/OSGSimpleSceneManager.h>
28 #include <OpenSG/OSGBaseFunctions.h>
29 #include <OpenSG/OSGTransform.h>
33 // the Group node core
34 #include <OpenSG/OSGGroup.h>
38 // number of copies to create
39 const OSG::UInt16 ncopies
= 20;
41 // a seprate transformation for every copy
42 OSG::TransformRefPtr trans
[ncopies
];
45 // The SimpleSceneManager to manage simple applications
46 OSG::SimpleSceneManagerRefPtr mgr
;
48 // forward declaration so we can have the interesting stuff upfront
49 int setupGLUT( int *argc
, char *argv
[] );
56 OSG::Real32 t
= glutGet(GLUT_ELAPSED_TIME
);
58 // set the transforms' matrices
59 for(OSG::UInt16 i
=0; i
< ncopies
; ++i
)
61 m
.setTransform(OSG::Vec3f(
62 OSG::osgSin(t
/ 1000.f
+ i
* 4 * ncopies
/ OSG::Pi
),
63 OSG::osgCos(t
/ 1000.f
+ i
* 6 * ncopies
/ OSG::Pi
),
64 OSG::osgSin(t
/ 1000.f
+ i
* 7 * ncopies
/ OSG::Pi
)),
65 OSG::Quaternion(OSG::Vec3f (1,1,0),
66 t
/ 1000.f
+ i
* 4 * ncopies
/ OSG::Pi
));
68 trans
[i
]->setMatrix(m
);
76 // Initialize GLUT & OpenSG and set up the scene
77 int main(int argc
, char **argv
)
80 OSG::osgInit(argc
,argv
);
83 int winid
= setupGLUT(&argc
, argv
);
85 // open a new scope, because the pointers below should go out of scope
86 // before entering glutMainLoop.
87 // Otherwise OpenSG will complain about objects being alive after shutdown.
89 // the connection between GLUT and OpenSG
90 OSG::GLUTWindowRefPtr gwin
= OSG::GLUTWindow::create();
91 gwin
->setGlutId(winid
);
97 Scenegraph nodes in OpenSG consist of two parts: the Node and its Core.
99 The Node contains the general information that anchors an object in the
100 graph: the parent, a list of children, a bounding volume. Note that the
101 Node contains a single parent, a Node can only be connected to a graph
104 There is only one Node class, all nodes in the scenegraph use the same
105 Node. The specific information that distinguishes node kinds from each
106 other is stored in the NodeCore.
108 Consequently there is a different kind of NodeCore for the different
109 kinds of functions a node can have. The NodeCore contains all the
110 information that the Node doesn't.
112 NodeCores can be used by multiple Nodes. In this example the Geometry
113 NodeCore of the torus is used to display multiple tori.
116 // this time, create just the core of the geometry
117 OSG::GeometryRefPtr torus
= OSG::makeTorusGeo( .5, 2, 8, 12 );
120 // the scene has a single group with ncopies transformations below,
121 // each of these carries a Node that shares the geometry
124 The Group NodeCore is the basic type to create the hierarchical graph.
125 It does very little to its children, just calls all of them when asked
126 to do anything, and collecting their information if necessary.
129 // create the root Group node
130 OSG::NodeRefPtr scene
= OSG::Node::create();
131 OSG::GroupRefPtr g
= OSG::Group::create();
135 // create the copied geometry nodes and their transformations
136 for(OSG::UInt16 i
= 0; i
< ncopies
; ++i
)
138 // create the nodes for the shared Geometry core
139 OSG::NodeRefPtr geonode
= OSG::Node::create();
141 // assign the Core to the Node
142 geonode
->setCore(torus
);
144 // add a transformation for every Geometry
145 OSG::NodeRefPtr transnode
= OSG::Node::create();
147 trans
[i
] = OSG::Transform::create();
149 transnode
->setCore (trans
[i
]);
150 transnode
->addChild(geonode
);
152 scene
->addChild(transnode
);
155 OSG::commitChanges();
157 // create the SimpleSceneManager helper
158 mgr
= OSG::SimpleSceneManager::create();
160 // tell the manager what to manage
161 mgr
->setWindow(gwin
);
162 mgr
->setRoot (scene
);
164 // show the whole scene
175 // GLUT callback functions
178 // react to size changes
179 void reshape(int w
, int h
)
185 // react to mouse button presses
186 void mouse(int button
, int state
, int x
, int y
)
189 mgr
->mouseButtonRelease(button
, x
, y
);
191 mgr
->mouseButtonPress(button
, x
, y
);
196 // react to mouse motions with pressed buttons
197 void motion(int x
, int y
)
199 mgr
->mouseMove(x
, y
);
204 void keyboard(unsigned char k
, int x
, int y
)
210 // clean up global variables
211 for(OSG::UInt16 i
= 0; i
< ncopies
; ++i
)
223 mgr
->setStatistics(!mgr
->getStatistics());
229 // setup the GLUT library which handles the windows for us
230 int setupGLUT(int *argc
, char *argv
[])
232 glutInit(argc
, argv
);
233 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
235 int winid
= glutCreateWindow("OpenSG");
237 glutReshapeFunc(reshape
);
238 glutDisplayFunc(display
);
239 glutMouseFunc(mouse
);
240 glutMotionFunc(motion
);
241 glutKeyboardFunc(keyboard
);
243 // call the redraw function whenever there's nothing else to do
244 glutIdleFunc(display
);