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::NodeRecPtr scene
;
25 // we have a forward declarion here, just to sort the code
26 int setupGLUT( int *argc
, char *argv
[] );
28 // This function will create our scenegraph
29 OSG::NodeTransitPtr
createScenegraph(void)
31 // we will use the variable to set our trandform matrices
34 // First we will create all needed geometry
35 // the body of the house
36 OSG::NodeRecPtr houseMain
= OSG::makeBox(20,20,20,1,1,1);
39 OSG::NodeRecPtr roof
= OSG::makeBox(14.14, 14.14, 20, 1, 1, 1);
41 // we translate the roof to the correct position
42 OSG::TransformRecPtr tRoof
= OSG::Transform::create();
44 m
.setTranslate(0,10,0);
45 m
.setRotate(OSG::Quaternion(OSG::Vec3f(0,0,1), 3.14159/4));
49 OSG::NodeRecPtr roofTrans
= OSG::Node::create();
50 roofTrans
->setCore(tRoof
);
51 roofTrans
->addChild(roof
);
53 // and the chimney - we have the top and sides generated
54 // but we have no need for the bottom (it is inside the house)
55 OSG::NodeRecPtr chimney
= OSG::makeCylinder(10,1,8,true,true,false);
57 //now we translate the chimney
59 //create the transform core
60 OSG::TransformRecPtr tChimney
= OSG::Transform::create();
62 // -5 along the x-axis and 2.5 along the z axis
63 // translates the chimney away from the center
64 // 15 along the y-axis translates the chimney to fit on top
65 // of the big box (have a look at the figure above2,5
66 m
.setTranslate(-5,15,2.5);
68 tChimney
->setMatrix(m
);
70 //insert the transform core into the node
71 OSG::NodeRecPtr chimneyTrans
= OSG::Node::create();
72 chimneyTrans
->setCore(tChimney
);
73 chimneyTrans
->addChild(chimney
);
75 // Now we create the root node and attach the geometry nodes to it
76 OSG::NodeRecPtr n
= OSG::Node::create();
77 n
->setCore(OSG::Group::create());
78 n
->addChild(houseMain
);
79 n
->addChild(roofTrans
);
80 n
->addChild(chimneyTrans
);
82 return OSG::NodeTransitPtr(n
);
85 int main(int argc
, char **argv
)
87 // Init the OpenSG subsystem
88 OSG::osgInit(argc
,argv
);
91 // We create a GLUT Window (that is almost the same for most applications)
92 int winid
= setupGLUT(&argc
, argv
);
93 OSG::GLUTWindowRecPtr gwin
= OSG::GLUTWindow::create();
94 gwin
->setGlutId(winid
);
97 // That will be our whole scene for now : an incredible Torus
98 scene
= createScenegraph();
100 // Create and setup our little friend - the SSM
101 mgr
= OSG::SimpleSceneManager::create();
102 mgr
->setWindow(gwin
);
103 mgr
->setRoot (scene
);
106 OSG::commitChanges();
109 // Give Control to the GLUT Main Loop
115 // react to size changes
116 void reshape(int w
, int h
)
122 // just redraw our scene if this GLUT callback is invoked
128 // react to mouse button presses
129 void mouse(int button
, int state
, int x
, int y
)
132 mgr
->mouseButtonRelease(button
, x
, y
);
134 mgr
->mouseButtonPress(button
, x
, y
);
139 // react to mouse motions with pressed buttons
140 void motion(int x
, int y
)
142 mgr
->mouseMove(x
, y
);
146 //The GLUT subsystem is set up here. This is very similar to other GLUT applications
147 //If you have worked with GLUT before, you may have the feeling of meeting old friends again,
148 //if you have not used GLUT before that is no problem. GLUT will be introduced shortly on the
151 int setupGLUT(int *argc
, char *argv
[])
153 glutInit(argc
, argv
);
154 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
156 int winid
= glutCreateWindow("OpenSG First Application");
158 glutDisplayFunc(display
);
159 glutMouseFunc(mouse
);
160 glutMotionFunc(motion
);
161 glutReshapeFunc(reshape
);
162 glutIdleFunc(display
);