fixed: auto_ptr -> unique_ptr
[opensg.git] / Examples / Tutorial / 09geometry_water2.cpp
blob7c9eed5eeb9f63c857135341d67982ec64da525f
1 // all needed include files
2 #ifdef OSG_BUILD_ACTIVE
3 #include <OSGGLUT.h>
4 #include <OSGConfig.h>
5 #include <OSGSimpleGeometry.h>
6 #include <OSGGLUTWindow.h>
7 #include <OSGSimpleSceneManager.h>
8 #include <OSGGeometry.h>
10 #include <OSGTypedGeoIntegralProperty.h>
11 #include <OSGTypedGeoVectorProperty.h>
12 #else
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>
22 #endif
24 // this will specify the resolution of the mesh
25 #define N 100
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++)
54 wMesh[i][j] = 0;
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();
72 // here they all come
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
94 // are used
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 OSG::NodeRecPtr root = OSG::Node::create();
120 root->setCore(geo);
122 return OSG::NodeTransitPtr(root);
125 int main(int argc, char **argv)
127 OSG::osgInit(argc,argv);
130 int winid = setupGLUT(&argc, argv);
131 OSG::GLUTWindowRecPtr gwin = OSG::GLUTWindow::create();
132 gwin->setGlutId(winid);
133 gwin->init();
135 scene =createScenegraph();
137 mgr = OSG::SimpleSceneManager::create();
138 mgr->setWindow(gwin );
139 mgr->setRoot (scene);
140 mgr->showAll();
142 OSG::Navigator * nav = mgr->getNavigator();
143 nav->setFrom(nav->getFrom()+OSG::Vec3f(0,50,0));
145 OSG::commitChanges();
148 glutMainLoop();
150 return 0;
153 void reshape(int w, int h)
155 mgr->resize(w, h);
156 glutPostRedisplay();
159 void display(void)
161 OSG::Real32 time = glutGet(GLUT_ELAPSED_TIME);
162 updateMesh(time);
164 // we extract the core out of the root node
165 // as we now this is a geometry node
166 OSG::GeometryRecPtr geo = dynamic_cast<OSG::Geometry *>(scene->getCore());
168 //now modify it's content
170 // first we need a pointer to the position data field
171 OSG::GeoPnt3fPropertyRecPtr pos =
172 dynamic_cast<OSG::GeoPnt3fProperty *>(geo->getPositions());
174 //this loop is similar to when we generted the data during createScenegraph()
175 // here they all come
176 for (int x = 0; x < N; x++)
177 for (int z = 0; z < N; z++)
178 pos->setValue(OSG::Pnt3f(x, wMesh[x][z], z), N * x + z);
180 mgr->redraw();
183 void mouse(int button, int state, int x, int y)
185 if (state)
186 mgr->mouseButtonRelease(button, x, y);
187 else
188 mgr->mouseButtonPress(button, x, y);
190 glutPostRedisplay();
193 void motion(int x, int y)
195 mgr->mouseMove(x, y);
196 glutPostRedisplay();
199 int setupGLUT(int *argc, char *argv[])
201 glutInit(argc, argv);
202 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
204 int winid = glutCreateWindow("OpenSG First Application");
206 glutDisplayFunc(display);
207 glutMouseFunc(mouse);
208 glutMotionFunc(motion);
209 glutReshapeFunc(reshape);
210 glutIdleFunc(display);
212 return winid;