1 // OpenSG Tutorial Example: Local Lights
3 // This example shows how to create and use local light sources.
4 // It creates four light sources (red, green, blue, white)
5 // each light source lights only its subtree.
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>
17 #include <OSGPointLight.h>
18 #include <OSGRenderAction.h>
19 #include <OSGSceneFileHandler.h>
21 // the headers for the SimpleMaterials
22 #include <OSGSimpleMaterial.h>
26 #include <OpenSG/OSGGLUT.h>
27 #include <OpenSG/OSGConfig.h>
28 #include <OpenSG/OSGSimpleGeometry.h>
29 #include <OpenSG/OSGGLUTWindow.h>
30 #include <OpenSG/OSGSimpleSceneManager.h>
31 #include <OpenSG/OSGBaseFunctions.h>
32 #include <OpenSG/OSGTransform.h>
33 #include <OpenSG/OSGGroup.h>
34 #include <OpenSG/OSGPointLight.h>
35 #include <OpenSG/OSGRenderAction.h>
36 #include <OpenSG/OSGSceneFileHandler.h>
38 // the headers for the SimpleMaterials
39 #include <OpenSG/OSGSimpleMaterial.h>
40 #include <OpenSG/OSGImage.h>
44 // The SimpleSceneManager to manage simple applications
45 OSG::SimpleSceneManagerRefPtr _mgr
= NULL
;
46 OSG::NodeRefPtr _scene
;
48 // forward declaration so we can have the interesting stuff upfront
49 int setupGLUT(int *argc
, char *argv
[]);
58 // Initialize GLUT & OpenSG and set up the scene
59 int main(int argc
, char **argv
)
62 OSG::osgInit(argc
,argv
);
65 int winid
= setupGLUT(&argc
, argv
);
67 // open a new scope, because the pointers below should go out of scope
68 // before entering glutMainLoop.
69 // Otherwise OpenSG will complain about objects being alive after shutdown.
71 // the connection between GLUT and OpenSG
72 OSG::GLUTWindowRefPtr gwin
= OSG::GLUTWindow::create();
73 gwin
->setGlutId(winid
);
77 _scene
= OSG::makeCoredNode
<OSG::Group
>();
79 // create four lights sharing the same beacon.
80 OSG::TransformRefPtr light_trans
;
81 OSG::NodeRefPtr light_beacon
=
82 OSG::makeCoredNode
<OSG::Transform
>(&light_trans
);
83 light_trans
->editMatrix().setTranslate(0.0, 0.0, 10.0);
86 OSG::PointLightRefPtr light1_core
;
87 OSG::NodeRefPtr light1
=
88 OSG::makeCoredNode
<OSG::PointLight
>(&light1_core
);
89 light1_core
->setAmbient(0.0,0.0,0.0,1);
90 light1_core
->setDiffuse(1.0,0.0,0.0,1);
91 light1_core
->setSpecular(0.8f
,0.8f
,0.8f
,1);
92 light1_core
->setBeacon(light_beacon
);
93 light1_core
->setOn(true);
96 OSG::PointLightRefPtr light2_core
;
97 OSG::NodeRefPtr light2
=
98 OSG::makeCoredNode
<OSG::PointLight
>(&light2_core
);
99 light2_core
->setAmbient(0.0,0.0,0.0,1);
100 light2_core
->setDiffuse(0.0,1.0,0.0,1);
101 light2_core
->setSpecular(0.8f
,0.8f
,0.8f
,1);
102 light2_core
->setBeacon(light_beacon
);
103 light2_core
->setOn(true);
106 OSG::PointLightRefPtr light3_core
;
107 OSG::NodeRefPtr light3
=
108 OSG::makeCoredNode
<OSG::PointLight
>(&light3_core
);
109 light3_core
->setAmbient(0.0,0.0,0.0,1);
110 light3_core
->setDiffuse(0.0,0.0,1.0,1);
111 light3_core
->setSpecular(0.8f
,0.8f
,0.8f
,1);
112 light3_core
->setBeacon(light_beacon
);
113 light3_core
->setOn(true);
116 OSG::PointLightRefPtr light4_core
;
117 OSG::NodeRefPtr light4
=
118 OSG::makeCoredNode
<OSG::PointLight
>(&light4_core
);
119 light4_core
->setAmbient(0.0,0.0,0.0,1);
120 light4_core
->setDiffuse(1.0,1.0,1.0,1);
121 light4_core
->setSpecular(0.0,0.0,0.0,1);
122 light4_core
->setBeacon(light_beacon
);
123 light4_core
->setOn(true);
125 OSG::NodeRefPtr bottom
= OSG::makePlane(25.0, 25.0, 128, 128);
127 // create three spheres.
128 OSG::NodeRefPtr sphere1
= OSG::makeLatLongSphere(50, 50, 1.0);
129 OSG::TransformRefPtr sphere1_trans_core
;
130 OSG::NodeRefPtr sphere1_trans
=
131 OSG::makeCoredNode
<OSG::Transform
>(&sphere1_trans_core
);
132 sphere1_trans_core
->editMatrix().setTranslate(-5.0, 0.0, 5.0);
133 sphere1_trans
->addChild(sphere1
);
135 OSG::NodeRefPtr sphere2
= OSG::makeLatLongSphere(50, 50, 1.0);
136 OSG::TransformRefPtr sphere2_trans_core
;
137 OSG::NodeRefPtr sphere2_trans
=
138 OSG::makeCoredNode
<OSG::Transform
>(&sphere2_trans_core
);
139 sphere2_trans_core
->editMatrix().setTranslate(0.0, 0.0, 5.0);
140 sphere2_trans
->addChild(sphere2
);
142 OSG::NodeRefPtr sphere3
= OSG::makeLatLongSphere(50, 50, 1.0);
143 OSG::TransformRefPtr sphere3_trans_core
;
144 OSG::NodeRefPtr sphere3_trans
=
145 OSG::makeCoredNode
<OSG::Transform
>(&sphere3_trans_core
);
146 sphere3_trans_core
->editMatrix().setTranslate(5.0, 0.0, 5.0);
147 sphere3_trans
->addChild(sphere3
);
149 light1
->addChild(sphere1_trans
);
150 light2
->addChild(sphere2_trans
);
151 light3
->addChild(sphere3_trans
);
152 light4
->addChild(bottom
);
154 _scene
->addChild(light_beacon
);
155 _scene
->addChild(light1
);
156 _scene
->addChild(light2
);
157 _scene
->addChild(light3
);
158 _scene
->addChild(light4
);
160 OSG::commitChanges();
162 // create the SimpleSceneManager helper
163 _mgr
= OSG::SimpleSceneManager::create();
165 // tell the manager what to manage
166 _mgr
->setWindow(gwin
);
167 _mgr
->setRoot (_scene
);
168 _mgr
->turnHeadlightOff();
170 // show the whole scene
181 // GLUT callback functions
184 // react to size changes
185 void reshape(int w
, int h
)
191 // react to mouse button presses
192 void mouse(int button
, int state
, int x
, int y
)
195 _mgr
->mouseButtonRelease(button
, x
, y
);
197 _mgr
->mouseButtonPress(button
, x
, y
);
202 // react to mouse motions with pressed buttons
203 void motion(int x
, int y
)
205 _mgr
->mouseMove(x
, y
);
210 void keyboard(unsigned char k
, int x
, int y
)
216 // clean up global variables
224 OSG::SceneFileHandler::the()->write(_scene
, "scene.osb.gz", true);
225 printf("wrote scene.\n");
230 _mgr
->setStatistics(!_mgr
->getStatistics());
236 // setup the GLUT library which handles the windows for us
237 int setupGLUT(int *argc
, char *argv
[])
239 glutInit(argc
, argv
);
240 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
242 int winid
= glutCreateWindow("OpenSG");
244 glutReshapeFunc(reshape
);
245 glutDisplayFunc(display
);
246 glutMouseFunc(mouse
);
247 glutMotionFunc(motion
);
248 glutKeyboardFunc(keyboard
);
250 // call the redraw function whenever there's nothing else to do
251 glutIdleFunc(display
);