1 // all needed include files
2 #ifdef OSG_BUILD_ACTIVE
5 #include <OSGSimpleGeometry.h>
6 #include <OSGGLUTWindow.h>
7 #include <OSGSimpleSceneManager.h>
8 #include <OSGGeometry.h>
10 #include <OSGTypedGeoIntegralProperty.h>
11 #include <OSGTypedGeoVectorProperty.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>
18 #include <OpenSG/OSGGeometry.h>
20 #include <OpenSG/OSGTypedGeoIntegralProperty.h>
21 #include <OpenSG/OSGTypedGeoVectorProperty.h>
24 // this will specify the resolution of the mesh
27 //the two dimensional array that will store all height values
28 OSG::Real32 wMesh
[N
][N
];
30 //the origin of the water mesh
31 OSG::Pnt3f wOrigin
= OSG::Pnt3f(0,0,0);
33 //width and length of the mesh
34 OSG::UInt16 width
= 100;
35 OSG::UInt16 length
= 100;
37 OSG::SimpleSceneManagerRefPtr mgr
;
38 OSG::NodeRecPtr scene
;
40 int setupGLUT( int *argc
, char *argv
[] );
42 void updateMesh(OSG::Real32 time
)
44 for (int x
= 0; x
< N
; x
++)
45 for (int z
= 0; z
< N
; z
++)
46 wMesh
[x
][z
] = 10*cos(time
/1000.f
+ (x
+z
)/10.f
);
49 OSG::NodeTransitPtr
createScenegraph(void)
51 // the scene must be created here
52 for (int i
= 0; i
< N
; i
++)
53 for (int j
= 0; j
< N
; j
++)
56 // the types of primitives that are used - an integerer propery
57 OSG::GeoUInt8PropertyRecPtr types
= OSG::GeoUInt8Property::create();
59 // we want to use quads ONLY
60 types
->addValue(GL_QUADS
);
62 // the number of vertices (or indices) we want to use with the primitive
63 // type; types and lengths always have the same number of elements
64 // (here both have just one)
65 OSG::GeoUInt32PropertyRecPtr lengths
= OSG::GeoUInt32Property::create();
66 // the length of our quads is four ;-)
67 lengths
->addValue(4 * (N
- 1) * (N
- 1));
69 // GeoPnt3fProperty stores the positions of all vertices used in
70 // this specific geometry core
71 OSG::GeoPnt3fPropertyRecPtr pos
= OSG::GeoPnt3fProperty::create();
73 for (int x
= 0; x
< N
; x
++)
74 for (int z
= 0; z
< N
; z
++)
75 pos
->addValue(OSG::Pnt3f(x
, wMesh
[x
][z
], z
));
77 // GeoColor3fProperty stores all color values that will be used
78 OSG::GeoColor3fPropertyRecPtr colors
= OSG::GeoColor3fProperty::create();
79 for (int x
= 0; x
< N
; x
++)
80 for (int z
= 0; z
< N
; z
++)
81 colors
->addValue(OSG::Color3f(0,0,1));
83 // and finally the normals are stored in a GeoVec3fProperty
84 OSG::GeoVec3fPropertyRecPtr norms
= OSG::GeoVec3fProperty::create();
85 for (int x
= 0; x
< N
; x
++)
86 for (int z
= 0; z
< N
; z
++)
87 // As initially all heights are set to zero thus yielding a plane,
88 // we set all normals to (0,1,0) parallel to the y-axis
89 norms
->addValue(OSG::Vec3f(0,1,0));
91 OSG::SimpleMaterialRecPtr mat
= OSG::SimpleMaterial::create();
93 // Indices define the order in which the entries in the above properties
95 OSG::GeoUInt32PropertyRecPtr indices
= OSG::GeoUInt32Property::create();
96 for (int x
= 0; x
< N
-1; x
++)
98 for (int z
= 0; z
< N
-1; z
++)
100 // points to four vertices that will
101 // define a single quad
102 indices
->addValue( z
* N
+ x
);
103 indices
->addValue((z
+1) * N
+ x
);
104 indices
->addValue((z
+1) * N
+ x
+ 1);
105 indices
->addValue( z
* N
+ x
+ 1);
109 OSG::GeometryRecPtr geo
= OSG::Geometry::create();
111 geo
->setTypes (types
);
112 geo
->setLengths (lengths
);
113 geo
->setIndices (indices
);
114 geo
->setPositions(pos
);
115 geo
->setNormals (norms
);
116 geo
->setMaterial (mat
);
117 geo
->setColors (colors
);
119 // Turn off creation of display lists, since the geometry changes each
121 geo
->setDlistCache(false);
123 OSG::NodeRecPtr root
= OSG::Node::create();
126 return OSG::NodeTransitPtr(root
);
129 int main(int argc
, char **argv
)
131 OSG::osgInit(argc
,argv
);
134 int winid
= setupGLUT(&argc
, argv
);
135 OSG::GLUTWindowRecPtr gwin
= OSG::GLUTWindow::create();
136 gwin
->setGlutId(winid
);
139 scene
=createScenegraph();
141 mgr
= OSG::SimpleSceneManager::create();
142 mgr
->setWindow(gwin
);
143 mgr
->setRoot (scene
);
146 OSG::Navigator
* nav
= mgr
->getNavigator();
147 nav
->setFrom(nav
->getFrom()+OSG::Vec3f(0,50,0));
149 OSG::commitChanges();
157 void reshape(int w
, int h
)
165 OSG::Real32 time
= glutGet(GLUT_ELAPSED_TIME
);
168 // we extract the core out of the root node
169 // as we now this is a geometry node
170 OSG::GeometryRecPtr geo
= dynamic_cast<OSG::Geometry
*>(scene
->getCore());
172 //now modify it's content
174 // first we need a pointer to the position data field
175 OSG::GeoPnt3fPropertyRecPtr pos
=
176 dynamic_cast<OSG::GeoPnt3fProperty
*>(geo
->getPositions());
178 //get the data field the pointer is pointing at
179 OSG::GeoPnt3fProperty::StoredFieldType
*posfield
= pos
->editFieldPtr();
181 OSG::GeoPnt3fProperty::StoredFieldType::iterator last
, it
;
183 // set the iterator to the first data
184 it
= posfield
->begin();
186 //now simply run over all entires in the array
187 for (int x
= 0; x
< N
; x
++)
189 for (int z
= 0; z
< N
; z
++)
191 (*it
) = OSG::Pnt3f(x
, wMesh
[x
][z
], z
);
199 void mouse(int button
, int state
, int x
, int y
)
202 mgr
->mouseButtonRelease(button
, x
, y
);
204 mgr
->mouseButtonPress(button
, x
, y
);
209 void motion(int x
, int y
)
211 mgr
->mouseMove(x
, y
);
215 int setupGLUT(int *argc
, char *argv
[])
217 glutInit(argc
, argv
);
218 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
220 int winid
= glutCreateWindow("OpenSG First Application");
222 glutDisplayFunc(display
);
223 glutMouseFunc(mouse
);
224 glutMotionFunc(motion
);
225 glutReshapeFunc(reshape
);
226 glutIdleFunc(display
);