root/branches/Carsten_PtrWork2/Tutorials/01hello.cpp

Revision 1039, 3.3 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: Hello World
2 //
3 // Minimalistic OpenSG program
4 //
5 // This is the shortest useful OpenSG program
6 // (if you remove all the comments ;)
7 //
8 // It shows how to use OpenSG together with GLUT to create a little
9 // interactive scene viewer.
10 //
11
12 // GLUT is used for window handling
13 #include <OpenSG/OSGGLUT.h>
14
15 // General OpenSG configuration, needed everywhere
16 #include <OpenSG/OSGConfig.h>
17
18 // Methods to create simple geometry: boxes, spheres, tori etc.
19 #include <OpenSG/OSGSimpleGeometry.h>
20
21 // The GLUT-OpenSG connection class
22 #include <OpenSG/OSGGLUTWindow.h>
23
24 // A little helper to simplify scene management and interaction
25 #include <OpenSG/OSGSimpleSceneManager.h>
26
27 // Activate the OpenSG namespace
28 // This is not strictly necessary, you can also prefix all OpenSG symbols
29 // with OSG::, but that would be a bit tedious for this example
30 OSG_USING_NAMESPACE
31
32 // The SimpleSceneManager to manage simple applications
33 SimpleSceneManager *mgr;
34
35 // forward declaration so we can have the interesting stuff upfront
36 int setupGLUT( int *argc, char *argv[] );
37
38 // Initialize GLUT & OpenSG and set up the scene
39 int main(int argc, char **argv)
40 {
41     std::cerr << "-- BEGIN main --" << std::endl;
42
43     // OSG init
44     osgInit(argc,argv);
45
46     // GLUT init
47     int winid = setupGLUT(&argc, argv);
48
49     // the connection between GLUT and OpenSG
50     GLUTWindowGlobalRefPtr gwin = GLUTWindow::create();
51     gwin->setGlutId(winid);
52     gwin->init();
53
54     // create the scene
55     NodeGlobalRefPtr scene = makeTorus(.5, 2, 16, 16);
56
57     commitChanges();
58
59     // create the SimpleSceneManager helper
60     mgr = new SimpleSceneManager;
61
62     // tell the manager what to manage
63     mgr->setWindow(gwin );
64     mgr->setRoot  (scene);
65
66     // show the whole scene
67     mgr->showAll();
68    
69     // GLUT main loop
70     glutMainLoop();
71    
72     std::cerr << "-- END   main --" << std::endl;
73    
74     return 0;
75 }
76
77 //
78 // GLUT callback functions
79 //
80
81 // redraw the window
82 void display(void)
83 {
84     std::cerr << "-- BEGIN Frame --" << std::endl;
85    
86     mgr->redraw();
87    
88     std::cerr << "-- END   Frame --" << std::endl;
89 }
90
91 // react to size changes
92 void reshape(int w, int h)
93 {
94     mgr->resize(w, h);
95     glutPostRedisplay();
96 }
97
98 // react to mouse button presses
99 void mouse(int button, int state, int x, int y)
100 {
101     if (state)
102         mgr->mouseButtonRelease(button, x, y);
103     else
104         mgr->mouseButtonPress(button, x, y);
105        
106     glutPostRedisplay();
107 }
108
109 // react to mouse motions with pressed buttons
110 void motion(int x, int y)
111 {
112     mgr->mouseMove(x, y);
113     glutPostRedisplay();
114 }
115
116 // react to keys
117 void keyboard(unsigned char k, int x, int y)
118 {
119     switch(k)
120     {
121         case 27:       
122         {
123             delete mgr;
124        
125             OSG::osgExit();
126             exit(0);
127         }
128         break;
129
130         case 's':
131         {
132             mgr->setStatistics(!mgr->getStatistics());
133         }
134         break;
135     }
136 }
137
138 // setup the GLUT library which handles the windows for us
139 int setupGLUT(int *argc, char *argv[])
140 {
141     glutInit(argc, argv);
142     glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
143    
144     int winid = glutCreateWindow("OpenSG");
145    
146     glutReshapeFunc(reshape);
147     glutDisplayFunc(display);
148     glutMouseFunc(mouse);
149     glutMotionFunc(motion);
150     glutKeyboardFunc(keyboard);
151
152     return winid;
153 }
Note: See TracBrowser for help on using the browser.