1 // what follows here is the smallest OpenSG programm possible
2 // most things used here are explained now or on the next few pages, so don't
3 // worry if not everythings clear right at the beginning...
5 // Some needed inlcude files - these will become more, believe me ;)
6 #ifdef OSG_BUILD_ACTIVE
9 #include <OSGSimpleGeometry.h>
10 #include <OSGGLUTWindow.h>
11 #include <OSGSimpleSceneManager.h>
13 #include <OpenSG/OSGGLUT.h>
14 #include <OpenSG/OSGConfig.h>
15 #include <OpenSG/OSGSimpleGeometry.h>
16 #include <OpenSG/OSGGLUTWindow.h>
17 #include <OpenSG/OSGSimpleSceneManager.h>
20 // The SimpleSceneManager is a little usefull class which helps us to
21 // manage little scenes. It will be discussed in detail later on
22 OSG::SimpleSceneManagerRefPtr mgr
;
23 OSG::TransformRecPtr transCore
;
25 // we have a forward declarion here, just to sort the code
26 int setupGLUT( int *argc
, char *argv
[] );
28 int main(int argc
, char **argv
)
30 // Init the OpenSG subsystem
31 OSG::osgInit(argc
,argv
);
34 // We create a GLUT Window (that is almost the same for most applications)
35 int winid
= setupGLUT(&argc
, argv
);
36 OSG::GLUTWindowRecPtr gwin
= OSG::GLUTWindow::create();
37 gwin
->setGlutId(winid
);
40 // That will be our whole scene for now : an incredible Torus
42 OSG::NodeRecPtr scene
;
44 // create all that stuff we will need:
45 //one geometry and one transform node
47 OSG::NodeRecPtr torus
= OSG::makeTorus(.5, 2, 16, 16);
48 OSG::NodeRecPtr transNode
= OSG::Node::create();
50 transCore
= OSG::Transform::create();
53 // now provide some data...
55 // no rotation at the beginning
58 // set the core to the matrix we created
59 transCore
->setMatrix(m
);
61 // now "insert" the core into the node
62 transNode
->setCore(transCore
);
63 // add the torus as a child to
64 // the transformation node
65 transNode
->addChild(torus
);
67 // "declare" the transformation as root
70 // Create and setup our little friend - the SSM
71 mgr
= OSG::SimpleSceneManager::create();
72 mgr
->setWindow(gwin
);
79 // Give Control to the GLUT Main Loop
85 // react to size changes
86 void reshape(int w
, int h
)
92 // just redraw our scene if this GLUT callback is invoked
95 //--------------------------BEGIN_ADD
98 // get the time since the apllication startet
99 OSG::Real32 time
= glutGet(GLUT_ELAPSED_TIME
);
102 m
.setRotate(OSG::Quaternion(OSG::Vec3f(0,1,0), time
/1000.f
));
104 //apply the new matrix to our transform core
105 transCore
->setMatrix(m
);
107 // Ordinarily a call to commitChanges should be placed here, since we
108 // modify the scene and therefore invalidate bounding volumes on which
109 // rendering depends.
110 // Since we are using the SimpleSceneManager though, this is not needed,
111 // it calls commitChanges internally (adding the call here anyways would
112 // not be a big issue, just slightly inefficient).
114 //--------------------------END_ADD
119 // react to mouse button presses
120 void mouse(int button
, int state
, int x
, int y
)
123 mgr
->mouseButtonRelease(button
, x
, y
);
125 mgr
->mouseButtonPress(button
, x
, y
);
130 // react to mouse motions with pressed buttons
131 void motion(int x
, int y
)
133 mgr
->mouseMove(x
, y
);
137 //The GLUT subsystem is set up here. This is very similar to other GLUT applications
138 //If you have worked with GLUT before, you may have the feeling of meeting old friends again,
139 //if you have not used GLUT before that is no problem. GLUT will be introduced shortly on the
142 int setupGLUT(int *argc
, char *argv
[])
144 glutInit(argc
, argv
);
145 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
147 int winid
= glutCreateWindow("OpenSG First Application");
149 glutDisplayFunc(display
);
150 glutMouseFunc(mouse
);
151 glutMotionFunc(motion
);
152 glutReshapeFunc(reshape
);
153 glutIdleFunc(display
);