root/branches/mixin-collappse/Tutorials/19LocalLights.cpp

Revision 637, 5.9 kB (checked in by vossg, 2 years ago)

fixed : added statistics key (s)

  • 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 NodePtr _scene = NullFC;
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     GLUTWindowPtr gwin= GLUTWindow::create();
53     gwin->setId(winid);
54     gwin->init();
55
56     // create the scene
57     _scene = makeCoredNode<Group>();
58
59     // create four lights sharing the same beacon.
60     TransformPtr light_trans;
61     NodePtr light_beacon = makeCoredNode<Transform>(&light_trans);
62     light_trans->editMatrix().setTranslate(0.0, 0.0, 10.0);
63
64     // red light.
65     PointLightPtr light1_core;
66     NodePtr 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     PointLightPtr light2_core;
75     NodePtr 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     PointLightPtr light3_core;
84     NodePtr 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     PointLightPtr light4_core;
93     NodePtr 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     NodePtr bottom = makePlane(25.0, 25.0, 128, 128);
101
102     // create three spheres.
103     NodePtr sphere1 = makeLatLongSphere(50, 50, 1.0);
104     TransformPtr sphere1_trans_core;
105     NodePtr 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     NodePtr sphere2 = makeLatLongSphere(50, 50, 1.0);
110     TransformPtr sphere2_trans_core;
111     NodePtr 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     NodePtr sphere3 = makeLatLongSphere(50, 50, 1.0);
116     TransformPtr sphere3_trans_core;
117     NodePtr 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     RenderAction *ract = (RenderAction *) _mgr->getAction();
147     ract->setLocalLights(true);
148
149     // GLUT main loop
150     glutMainLoop();
151
152     return 0;
153 }
154
155 //
156 // GLUT callback functions
157 //
158
159 // react to size changes
160 void reshape(int w, int h)
161 {
162     _mgr->resize(w, h);
163     glutPostRedisplay();
164 }
165
166 // react to mouse button presses
167 void mouse(int button, int state, int x, int y)
168 {
169     if (state)
170         _mgr->mouseButtonRelease(button, x, y);
171     else
172         _mgr->mouseButtonPress(button, x, y);
173        
174     glutPostRedisplay();
175 }
176
177 // react to mouse motions with pressed buttons
178 void motion(int x, int y)
179 {
180     _mgr->mouseMove(x, y);
181     glutPostRedisplay();
182 }
183
184 // react to keys
185 void keyboard(unsigned char k, int x, int y)
186 {
187     switch(k)
188     {
189         case 27:
190             OSG::osgExit();
191             exit(0);
192         break;
193         case 'w':
194             SceneFileHandler::the()->write(_scene, "scene.osb.gz", true);
195             printf("wrote scene.\n");
196         break;
197
198         case 's':
199         {
200             _mgr->setStatistics(!_mgr->getStatistics());
201         }
202         break;
203     }
204 }
205
206 // setup the GLUT library which handles the windows for us
207 int setupGLUT(int *argc, char *argv[])
208 {
209     glutInit(argc, argv);
210     glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
211    
212     int winid = glutCreateWindow("OpenSG");
213    
214     glutReshapeFunc(reshape);
215     glutDisplayFunc(display);
216     glutMouseFunc(mouse);
217     glutMotionFunc(motion);
218     glutKeyboardFunc(keyboard);
219
220     // call the redraw function whenever there's nothing else to do
221     glutIdleFunc(display);
222
223     return winid;
224 }
Note: See TracBrowser for help on using the browser.