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

Revision 835, 3.0 kB (checked in by vossg, 1 year ago)

ported : cluster windows (basic port still need quite some testing and verification)
added : ptr mfield copy to containers
fixed : tutorial compile problems

  • 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     // OSG init
42     osgInit(argc,argv);
43
44     // GLUT init
45     int winid = setupGLUT(&argc, argv);
46
47     // the connection between GLUT and OpenSG
48     GLUTWindowPtr gwin= GLUTWindow::create();
49     gwin->setGlutId(winid);
50     gwin->init();
51
52     // create the scene
53     NodePtr scene = makeTorus(.5, 2, 16, 16);
54
55     commitChanges();
56
57     // create the SimpleSceneManager helper
58     mgr = new SimpleSceneManager;
59
60     // tell the manager what to manage
61     mgr->setWindow(gwin );
62     mgr->setRoot  (scene);
63
64     // show the whole scene
65     mgr->showAll();
66    
67     // GLUT main loop
68     glutMainLoop();
69
70     return 0;
71 }
72
73 //
74 // GLUT callback functions
75 //
76
77 // redraw the window
78 void display(void)
79 {
80     mgr->redraw();
81 }
82
83 // react to size changes
84 void reshape(int w, int h)
85 {
86     mgr->resize(w, h);
87     glutPostRedisplay();
88 }
89
90 // react to mouse button presses
91 void mouse(int button, int state, int x, int y)
92 {
93     if (state)
94         mgr->mouseButtonRelease(button, x, y);
95     else
96         mgr->mouseButtonPress(button, x, y);
97        
98     glutPostRedisplay();
99 }
100
101 // react to mouse motions with pressed buttons
102 void motion(int x, int y)
103 {
104     mgr->mouseMove(x, y);
105     glutPostRedisplay();
106 }
107
108 // react to keys
109 void keyboard(unsigned char k, int x, int y)
110 {
111     switch(k)
112     {
113         case 27:       
114         {
115             OSG::osgExit();
116             exit(0);
117         }
118         break;
119
120         case 's':
121         {
122             mgr->setStatistics(!mgr->getStatistics());
123         }
124         break;
125     }
126 }
127
128 // setup the GLUT library which handles the windows for us
129 int setupGLUT(int *argc, char *argv[])
130 {
131     glutInit(argc, argv);
132     glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
133    
134     int winid = glutCreateWindow("OpenSG");
135    
136     glutReshapeFunc(reshape);
137     glutDisplayFunc(display);
138     glutMouseFunc(mouse);
139     glutMotionFunc(motion);
140     glutKeyboardFunc(keyboard);
141
142     return winid;
143 }
Note: See TracBrowser for help on using the browser.