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::NodeRecPtr
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
50 m
.setTranslate(20, 0, 0);
51 n
.setTranslate( 8, 0, 0);
53 planetTransform
->setMatrix(m
);
54 moonTransform
->setMatrix(n
);
56 //Insert the cores into the apropiate nodes and add the geometry
57 planetTransformNode
->setCore (planetTransform
);
58 planetTransformNode
->addChild(planet
);
60 moonTransformNode
->setCore (moonTransform
);
61 moonTransformNode
->addChild(moon
);
63 //add the planet to the sun
64 root
->addChild(planetTransformNode
);
65 root
->addChild(moonTransformNode
);
68 return OSG::NodeTransitPtr(root
);
72 int main(int argc
, char **argv
)
74 OSG::osgInit(argc
,argv
);
77 int winid
= setupGLUT(&argc
, argv
);
78 OSG::GLUTWindowRecPtr gwin
= OSG::GLUTWindow::create();
79 gwin
->setGlutId(winid
);
82 scene
= createScenegraph();
84 mgr
= OSG::SimpleSceneManager::create();
85 mgr
->setWindow(gwin
);
97 void reshape(int w
, int h
)
105 OSG::Real32 time
= glutGet(GLUT_ELAPSED_TIME
);
107 //create the Quaternion the describes the rotation of
108 //the planet around the sun
109 OSG::Quaternion planetRot
= OSG::Quaternion(OSG::Vec3f(0,1,0),
112 //now the rotation of the moon around the planet
113 //the division by 12 speeds up the rotation by 12 compared to the
115 OSG::Quaternion moonRot
= OSG::Quaternion(OSG::Vec3f(0,1,0),
116 time
/float(1000/12));
118 //generate the Matrices
119 OSG::Matrix p
,m
,t1
,r1
,t2
,r2
;
121 t1
.setTransform(OSG::Vec3f(20,0,0));
122 r1
.setTransform(planetRot
);
126 t2
.setTransform(OSG::Vec3f(8,0,0));
127 r2
.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
);