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>
9 #include <OSGTypedGeoIntegralProperty.h>
10 #include <OSGTypedGeoVectorProperty.h>
12 #include <OSGDirectionalLight.h>
14 #include <OpenSG/OSGGLUT.h>
15 #include <OpenSG/OSGConfig.h>
16 #include <OpenSG/OSGSimpleGeometry.h>
17 #include <OpenSG/OSGGLUTWindow.h>
18 #include <OpenSG/OSGSimpleSceneManager.h>
19 #include <OpenSG/OSGGeometry.h>
20 #include <OpenSG/OSGTypedGeoIntegralProperty.h>
21 #include <OpenSG/OSGTypedGeoVectorProperty.h>
23 #include <OpenSG/OSGDirectionalLight.h>
26 // this will specify the resolution of the mesh
29 //the two dimensional array that will store all height values
30 OSG::Real32 wMesh
[N
][N
];
32 //the origin of the water mesh
33 OSG::Pnt3f wOrigin
= OSG::Pnt3f(0,0,0);
35 //width and length of the mesh
36 OSG::UInt16 width
= 100;
37 OSG::UInt16 length
= 100;
39 OSG::SimpleSceneManagerRefPtr mgr
;
40 OSG::NodeRecPtr scene
;
42 int setupGLUT(int *argc
, char *argv
[]);
44 void updateMesh(OSG::Real32 time
)
46 for (int x
= 0; x
< N
; x
++)
47 for (int z
= 0; z
< N
; z
++)
48 wMesh
[x
][z
] = 10*cos(time
/1000.f
+ (x
+z
)/10.f
);
51 OSG::NodeTransitPtr
createScenegraph(void)
53 // the scene must be created here
54 for (int i
= 0; i
< N
; i
++)
55 for (int j
= 0; j
< N
; j
++)
58 // the types of primitives that are used - an integerer propery
59 OSG::GeoUInt8PropertyRecPtr types
= OSG::GeoUInt8Property::create();
61 // we want to use quads ONLY
62 types
->addValue(GL_QUADS
);
64 // the number of vertices (or indices) we want to use with the primitive
65 // type; types and lengths always have the same number of elements
66 // (here both have just one)
67 OSG::GeoUInt32PropertyRecPtr lengths
= OSG::GeoUInt32Property::create();
68 // the length of our quads is four ;-)
69 lengths
->addValue(4 * (N
- 1) * (N
- 1));
71 // GeoPnt3fProperty stores the positions of all vertices used in
72 // this specific geometry core
73 OSG::GeoPnt3fPropertyRecPtr pos
= OSG::GeoPnt3fProperty::create();
75 for (int x
= 0; x
< N
; x
++)
76 for (int z
= 0; z
< N
; z
++)
77 pos
->addValue(OSG::Pnt3f(x
, wMesh
[x
][z
], z
));
79 // GeoColor3fProperty stores all color values that will be used
80 OSG::GeoColor3fPropertyRecPtr colors
= OSG::GeoColor3fProperty::create();
81 for (int x
= 0; x
< N
; x
++)
82 for (int z
= 0; z
< N
; z
++)
83 colors
->addValue(OSG::Color3f(0,0,1));
85 // and finally the normals are stored in a GeoVec3fProperty
86 OSG::GeoVec3fPropertyRecPtr norms
= OSG::GeoVec3fProperty::create();
87 for (int x
= 0; x
< N
; x
++)
88 for (int z
= 0; z
< N
; z
++)
89 // As initially all heights are set to zero thus yielding a plane,
90 // we set all normals to (0,1,0) parallel to the y-axis
91 norms
->addValue(OSG::Vec3f(0,1,0));
93 OSG::SimpleMaterialRecPtr mat
= OSG::SimpleMaterial::create();
94 mat
->setDiffuse(OSG::Color3f(0,0,1));
96 // Indices define the order in which the entries in the above properties
98 OSG::GeoUInt32PropertyRecPtr indices
= OSG::GeoUInt32Property::create();
99 for (int x
= 0; x
< N
-1; x
++)
101 for (int z
= 0; z
< N
-1; z
++)
103 // points to four vertices that will
104 // define a single quad
105 indices
->addValue( z
* N
+ x
);
106 indices
->addValue((z
+1) * N
+ x
);
107 indices
->addValue((z
+1) * N
+ x
+ 1);
108 indices
->addValue( z
* N
+ x
+ 1);
112 OSG::GeometryRecPtr geo
= OSG::Geometry::create();
114 geo
->setTypes (types
);
115 geo
->setLengths (lengths
);
116 geo
->setIndices (indices
);
117 geo
->setPositions(pos
);
118 geo
->setNormals (norms
);
119 geo
->setMaterial (mat
);
120 // geo->setColors (colors );
121 geo
->setDlistCache(false);
123 OSG::DirectionalLightRecPtr pLight
= OSG::DirectionalLight::create();
124 OSG::NodeRecPtr root
= OSG::Node::create();
125 OSG::NodeRecPtr water
= OSG::Node::create();
126 OSG::NodeRecPtr pLightTransformNode
= OSG::Node::create();
127 OSG::TransformRecPtr pLightTransform
= OSG::Transform::create();
128 OSG::NodeRecPtr pLightNode
= OSG::Node::create();
130 pLightNode
->setCore(OSG::Group::create());
134 m
.setTranslate(50,25,50);
136 pLightTransform
->setMatrix(m
);
138 //we add a little spehere that will represent the light source
139 OSG::GeometryRecPtr sphere
= OSG::makeSphereGeo(2,2);
141 OSG::SimpleMaterialRecPtr sm
= OSG::SimpleMaterial::create();
144 sm
->setDiffuse(OSG::Color3f(1,1,1));
146 sphere
->setMaterial(sm
);
148 OSG::NodeRecPtr sphereNode
= OSG::Node::create();
149 sphereNode
->setCore(sphere
);
151 pLightTransformNode
->setCore(pLightTransform
);
152 pLightTransformNode
->addChild(pLightNode
);
153 pLightTransformNode
->addChild(sphereNode
);
155 // positions does not apply, but direction instead
156 // pLight->setPosition(Pnt3f(0,0,0));
157 pLight
->setDirection (OSG::Vec3f(0,1,0));
159 // Attenuation parameters -- do not apply to directional light
160 // pLight->setConstantAttenuation(1);
161 // pLight->setLinearAttenuation(0);
162 // pLight->setQuadraticAttenuation(0);
165 pLight
->setDiffuse(OSG::Color4f(1,1,1,1));
166 pLight
->setAmbient(OSG::Color4f(0.2,0.2,0.2,1));
167 pLight
->setSpecular(OSG::Color4f(1,1,1,1));
170 pLight
->setBeacon(pLightNode
);
174 root
->setCore(pLight
);
175 root
->addChild(water
);
176 root
->addChild(pLightTransformNode
);
178 return OSG::NodeTransitPtr(root
);
181 int main(int argc
, char **argv
)
183 OSG::osgInit(argc
,argv
);
186 int winid
= setupGLUT(&argc
, argv
);
187 OSG::GLUTWindowRecPtr gwin
= OSG::GLUTWindow::create();
188 gwin
->setGlutId(winid
);
191 scene
= createScenegraph();
193 mgr
= OSG::SimpleSceneManager::create();
194 mgr
->setWindow(gwin
);
195 mgr
->setRoot (scene
);
197 mgr
->setHeadlight(false);
199 OSG::Navigator
* nav
= mgr
->getNavigator();
200 nav
->setFrom(nav
->getFrom()+OSG::Vec3f(0,50,0));
202 OSG::commitChanges();
210 void reshape(int w
, int h
)
218 OSG::Real32 time
= glutGet(GLUT_ELAPSED_TIME
);
221 // we extract the core out of the root node
222 // as we now this is a geometry node
223 OSG::GeometryRecPtr geo
=
224 dynamic_cast<OSG::Geometry
*>(scene
->getChild(0)->getCore());
226 //now modify it's content
228 // first we need a pointer to the position data field
229 OSG::GeoPnt3fPropertyRecPtr pos
=
230 dynamic_cast<OSG::GeoPnt3fProperty
*>(geo
->getPositions());
232 //get the data field the pointer is pointing at
233 OSG::GeoPnt3fProperty::StoredFieldType
*posfield
= pos
->editFieldPtr();
235 OSG::GeoPnt3fProperty::StoredFieldType::iterator last
, it
;
237 // set the iterator to the first data
238 it
= posfield
->begin();
240 //now simply run over all entires in the array
241 for (int x
= 0; x
< N
; x
++)
243 for (int z
= 0; z
< N
; z
++)
245 (*it
) = OSG::Pnt3f(x
, wMesh
[x
][z
], z
);
253 void mouse(int button
, int state
, int x
, int y
)
256 mgr
->mouseButtonRelease(button
, x
, y
);
258 mgr
->mouseButtonPress(button
, x
, y
);
263 void motion(int x
, int y
)
265 mgr
->mouseMove(x
, y
);
269 int setupGLUT(int *argc
, char *argv
[])
271 glutInit(argc
, argv
);
272 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
274 int winid
= glutCreateWindow("OpenSG First Application");
276 glutDisplayFunc(display
);
277 glutMouseFunc(mouse
);
278 glutMotionFunc(motion
);
279 glutReshapeFunc(reshape
);
280 glutIdleFunc(display
);