fixed: auto_ptr -> unique_ptr
[opensg.git] / Examples / Tutorial / 09geometry_water3.cpp
blobd3deae6fe649bbc5ae9605fad7b24102ae7d17f0
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 // Turn off creation of display lists, since the geometry changes each
120 // frame
121 geo->setDlistCache(false);
123 OSG::NodeRecPtr root = OSG::Node::create();
124 root->setCore(geo);
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);
137 gwin->init();
139 scene =createScenegraph();
141 mgr = OSG::SimpleSceneManager::create();
142 mgr->setWindow(gwin );
143 mgr->setRoot (scene);
144 mgr->showAll();
146 OSG::Navigator * nav = mgr->getNavigator();
147 nav->setFrom(nav->getFrom()+OSG::Vec3f(0,50,0));
149 OSG::commitChanges();
152 glutMainLoop();
154 return 0;
157 void reshape(int w, int h)
159 mgr->resize(w, h);
160 glutPostRedisplay();
163 void display(void)
165 OSG::Real32 time = glutGet(GLUT_ELAPSED_TIME);
166 updateMesh(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();
180 //get some iterators
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);
192 it++;
196 mgr->redraw();
199 void mouse(int button, int state, int x, int y)
201 if (state)
202 mgr->mouseButtonRelease(button, x, y);
203 else
204 mgr->mouseButtonPress(button, x, y);
206 glutPostRedisplay();
209 void motion(int x, int y)
211 mgr->mouseMove(x, y);
212 glutPostRedisplay();
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);
228 return winid;