1 // all needed include files
2 #ifdef OSG_BUILD_ACTIVE
5 #include <OSGSimpleGeometry.h>
6 #include <OSGGLUTWindow.h>
7 #include <OSGSimpleSceneManager.h>
9 #include <OpenSG/OSGGLUT.h>
10 #include <OpenSG/OSGConfig.h>
11 #include <OpenSG/OSGSimpleGeometry.h>
12 #include <OpenSG/OSGGLUTWindow.h>
13 #include <OpenSG/OSGSimpleSceneManager.h>
16 OSG::SimpleSceneManagerRefPtr mgr
;
17 OSG::NodeRecPtr scene
;
18 OSG::TransformRecPtr planetTransform
;
19 OSG::TransformRecPtr moonTransform
;
21 int setupGLUT(int *argc
, char *argv
[]);
23 OSG::NodeTransitPtr
createScenegraph(void)
25 //create sun, planet & moon geometry
27 OSG::GeometryRecPtr sun
= OSG::makeSphereGeo(3, 6);
28 OSG::NodeRecPtr planet
= OSG::makeSphere (3, 3);
29 OSG::NodeRecPtr moon
= OSG::makeSphere (2, 1);
31 //the root node will be the sun
32 OSG::NodeRecPtr root
= OSG::Node::create();
35 OSG::NodeRecPtr planetTransformNode
= OSG::Node::create();
36 OSG::NodeRecPtr moonTransformNode
= OSG::Node::create();
38 // these were declared globally
39 planetTransform
= OSG::Transform::create();
40 moonTransform
= OSG::Transform::create();
42 // Now we need to fill it with live
43 // We want to have the planet some distance away from the sun,
44 // but initial with no rotation. The same aplies to the moon
49 m
.setTranslate(20, 0, 0);
50 n
.setTranslate( 8, 0, 0);
52 planetTransform
->setMatrix(m
);
53 moonTransform
->setMatrix(n
);
55 //Insert the cores into the apropiate nodes and add the geometry
56 planetTransformNode
->setCore (planetTransform
);
57 planetTransformNode
->addChild(planet
);
59 moonTransformNode
->setCore (moonTransform
);
60 moonTransformNode
->addChild(moon
);
62 //add the planet to the sun
63 root
->addChild(planetTransformNode
);
65 //add the moon to the planet
66 planet
->addChild(moonTransformNode
);
69 return OSG::NodeTransitPtr(root
);
73 int main(int argc
, char **argv
)
75 OSG::osgInit(argc
,argv
);
78 int winid
= setupGLUT(&argc
, argv
);
79 OSG::GLUTWindowRecPtr gwin
= OSG::GLUTWindow::create();
80 gwin
->setGlutId(winid
);
83 scene
= createScenegraph();
85 mgr
= OSG::SimpleSceneManager::create();
86 mgr
->setWindow(gwin
);
98 void reshape(int w
, int h
)
106 OSG::Real32 time
= glutGet(GLUT_ELAPSED_TIME
);
108 //create the Quaternion the describes the rotation of
109 //the planet around the sun
110 OSG::Quaternion planetRot
= OSG::Quaternion(OSG::Vec3f(0,1,0),
113 //now the rotation of the moon around the planet
114 //the division by 12 speeds up the rotation by 12 compared to the
116 OSG::Quaternion moonRot
= OSG::Quaternion(OSG::Vec3f(0,1,0),
117 time
/float(1000/12));
119 //generate the Matrices
122 t
.setTransform(OSG::Vec3f(20,0,0));
123 r
.setTransform(planetRot
);
127 t
.setTransform(OSG::Vec3f(8,0,0));
128 r
.setTransform(moonRot
);
132 planetTransform
->setMatrix(p
);
133 moonTransform
->setMatrix(m
);
138 void mouse(int button
, int state
, int x
, int y
)
141 mgr
->mouseButtonRelease(button
, x
, y
);
143 mgr
->mouseButtonPress(button
, x
, y
);
148 void motion(int x
, int y
)
150 mgr
->mouseMove(x
, y
);
154 int setupGLUT(int *argc
, char *argv
[])
156 glutInit(argc
, argv
);
157 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
159 int winid
= glutCreateWindow("OpenSG First Application");
161 glutDisplayFunc(display
);
162 glutMouseFunc(mouse
);
163 glutMotionFunc(motion
);
164 glutReshapeFunc(reshape
);
165 glutIdleFunc(display
);