root/branches/Carsten_PtrWork2/Tutorials/19LocalLights.cpp

Revision 1039, 6.1 kB (checked in by cneumann, 11 months ago)

changed: - factory functions return a TransitPtr? that can not be implicitly

converted to C Ptr. Should help with porting.

added: - GlobalRefPtr?, needed for cases where upon return from main

a RefPtr? goes out of scope (it would attempt to access the
FCFactory which is already shutdown at that point).

status: - vrml loader does not compile (needs porting to ref ptr)

  • tutorials compile, run and exit cleanly
  • multithreading and cluster are untested, yet
  • Property svn:eol-style set to native
Line 
1 // OpenSG Tutorial Example: LocalLights
2 //
3 // This example shows how to create use local light sources.
4 // It creates four light sources (red, green, blue, white)
5 // each light source lights only its subtree.
6
7 // Headers
8 #include <OpenSG/OSGGLUT.h>
9 #include <OpenSG/OSGConfig.h>
10 #include <OpenSG/OSGSimpleGeometry.h>
11 #include <OpenSG/OSGGLUTWindow.h>
12 #include <OpenSG/OSGSimpleSceneManager.h>
13 #include <OpenSG/OSGBaseFunctions.h>
14 #include <OpenSG/OSGTransform.h>
15 #include <OpenSG/OSGGroup.h>
16 #include <OpenSG/OSGPointLight.h>
17 #include <OpenSG/OSGRenderAction.h>
18 #include <OpenSG/OSGSceneFileHandler.h>
19
20 // the headers for the SimpleMaterials
21 #include <OpenSG/OSGSimpleMaterial.h>
22 #include <OpenSG/OSGImage.h>
23
24
25 // Activate the OpenSG namespace
26 OSG_USING_NAMESPACE
27
28 // The SimpleSceneManager to manage simple applications
29 SimpleSceneManager *_mgr   = NULL;
30 NodeGlobalRefPtr    _scene;
31
32 // forward declaration so we can have the interesting stuff upfront
33 int setupGLUT(int *argc, char *argv[]);
34
35 // redraw the window
36 void display(void)
37 {
38     _mgr->redraw();
39 }
40
41
42 // Initialize GLUT & OpenSG and set up the scene
43 int main(int argc, char **argv)
44 {
45     // OSG init
46     osgInit(argc,argv);
47
48     // GLUT init
49     int winid = setupGLUT(&argc, argv);
50
51     // the connection between GLUT and OpenSG
52     GLUTWindowGlobalRefPtr gwin= GLUTWindow::create();
53     gwin->setGlutId(winid);
54     gwin->init();
55
56     // create the scene
57     _scene = makeCoredNode<Group>();
58
59     // create four lights sharing the same beacon.
60     TransformGlobalRefPtr light_trans;
61     NodeGlobalRefPtr      light_beacon = makeCoredNode<Transform>(&light_trans);
62     light_trans->editMatrix().setTranslate(0.0, 0.0, 10.0);
63
64     // red light.
65     PointLightGlobalRefPtr light1_core;
66     NodeGlobalRefPtr       light1 = makeCoredNode<PointLight>(&light1_core);
67     light1_core->setAmbient(0.0,0.0,0.0,1);
68     light1_core->setDiffuse(1.0,0.0,0.0,1);
69     light1_core->setSpecular(0.8,0.8,0.8,1);
70     light1_core->setBeacon(light_beacon);
71     light1_core->setOn(true);
72
73     // green light.
74     PointLightGlobalRefPtr light2_core;
75     NodeGlobalRefPtr       light2 = makeCoredNode<PointLight>(&light2_core);
76     light2_core->setAmbient(0.0,0.0,0.0,1);
77     light2_core->setDiffuse(0.0,1.0,0.0,1);
78     light2_core->setSpecular(0.8,0.8,0.8,1);
79     light2_core->setBeacon(light_beacon);
80     light2_core->setOn(true);
81    
82     // blue light.
83     PointLightGlobalRefPtr light3_core;
84     NodeGlobalRefPtr       light3 = makeCoredNode<PointLight>(&light3_core);
85     light3_core->setAmbient(0.0,0.0,0.0,1);
86     light3_core->setDiffuse(0.0,0.0,1.0,1);
87     light3_core->setSpecular(0.8,0.8,0.8,1);
88     light3_core->setBeacon(light_beacon);
89     light3_core->setOn(true);
90
91     // white light.
92     PointLightGlobalRefPtr light4_core;
93     NodeGlobalRefPtr       light4 = makeCoredNode<PointLight>(&light4_core);
94     light4_core->setAmbient(0.0,0.0,0.0,1);
95     light4_core->setDiffuse(1.0,1.0,1.0,1);
96     light4_core->setSpecular(0.0,0.0,0.0,1);
97     light4_core->setBeacon(light_beacon);
98     light4_core->setOn(true);
99
100     NodeGlobalRefPtr bottom = makePlane(25.0, 25.0, 128, 128);
101
102     // create three spheres.
103     NodeGlobalRefPtr      sphere1 = makeLatLongSphere(50, 50, 1.0);
104     TransformGlobalRefPtr sphere1_trans_core;
105     NodeGlobalRefPtr      sphere1_trans = makeCoredNode<Transform>(&sphere1_trans_core);
106     sphere1_trans_core->editMatrix().setTranslate(-5.0, 0.0, 5.0);
107     sphere1_trans->addChild(sphere1);
108    
109     NodeGlobalRefPtr      sphere2 = makeLatLongSphere(50, 50, 1.0);
110     TransformGlobalRefPtr sphere2_trans_core;
111     NodeGlobalRefPtr      sphere2_trans = makeCoredNode<Transform>(&sphere2_trans_core);
112     sphere2_trans_core->editMatrix().setTranslate(0.0, 0.0, 5.0);
113     sphere2_trans->addChild(sphere2);
114    
115     NodeGlobalRefPtr      sphere3 = makeLatLongSphere(50, 50, 1.0);
116     TransformGlobalRefPtr sphere3_trans_core;
117     NodeGlobalRefPtr      sphere3_trans = makeCoredNode<Transform>(&sphere3_trans_core);
118     sphere3_trans_core->editMatrix().setTranslate(5.0, 0.0, 5.0);
119     sphere3_trans->addChild(sphere3);
120    
121     light1->addChild(sphere1_trans);
122     light2->addChild(sphere2_trans);
123     light3->addChild(sphere3_trans);
124     light4->addChild(bottom);
125
126     _scene->addChild(light_beacon);
127     _scene->addChild(light1);
128     _scene->addChild(light2);
129     _scene->addChild(light3);
130     _scene->addChild(light4);
131
132     commitChanges();
133
134     // create the SimpleSceneManager helper
135     _mgr = new SimpleSceneManager;
136
137     // tell the manager what to manage
138     _mgr->setWindow(gwin );
139     _mgr->setRoot  (_scene);
140     _mgr->turnHeadlightOff();
141
142     // show the whole scene
143     _mgr->showAll();
144
145 //     // enable local lights.
146 //     _mgr->getRenderAction()->setLocalLights(true);
147
148     // GLUT main loop
149     glutMainLoop();
150
151     return 0;
152 }
153
154 //
155 // GLUT callback functions
156 //
157
158 // react to size changes
159 void reshape(int w, int h)
160 {
161     _mgr->resize(w, h);
162     glutPostRedisplay();
163 }
164
165 // react to mouse button presses
166 void mouse(int button, int state, int x, int y)
167 {
168     if (state)
169         _mgr->mouseButtonRelease(button, x, y);
170     else
171         _mgr->mouseButtonPress(button, x, y);
172        
173     glutPostRedisplay();
174 }
175
176 // react to mouse motions with pressed buttons
177 void motion(int x, int y)
178 {
179     _mgr->mouseMove(x, y);
180     glutPostRedisplay();
181 }
182
183 // react to keys
184 void keyboard(unsigned char k, int x, int y)
185 {
186     switch(k)
187     {
188         case 27:
189             delete _mgr;
190        
191             OSG::osgExit();
192             exit(0);
193         break;
194         case 'w':
195             SceneFileHandler::the()->write(_scene, "scene.osb.gz", true);
196             printf("wrote scene.\n");
197         break;
198
199         case 's':
200         {
201             _mgr->setStatistics(!_mgr->getStatistics());
202         }
203         break;
204     }
205 }
206
207 // setup the GLUT library which handles the windows for us
208 int setupGLUT(int *argc, char *argv[])
209 {
210     glutInit(argc, argv);
211     glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
212    
213     int winid = glutCreateWindow("OpenSG");
214    
215     glutReshapeFunc(reshape);
216     glutDisplayFunc(display);
217     glutMouseFunc(mouse);
218     glutMotionFunc(motion);
219     glutKeyboardFunc(keyboard);
220
221     // call the redraw function whenever there's nothing else to do
222     glutIdleFunc(display);
223
224     return winid;
225 }
Note: See TracBrowser for help on using the browser.