fixed: compile issue
[opensg.git] / Examples / Tutorial / 09geometry_water.cpp
blob473fe81e2fefeb890d83234f6b1aba6228ca6922
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 OSG::NodeTransitPtr createScenegraph(void)
44 // the scene must be created here
45 for (int i = 0; i < N; i++)
46 for (int j = 0; j < N; j++)
47 wMesh[i][j] = 0;
49 // the types of primitives that are used - an integerer propery
50 OSG::GeoUInt8PropertyRecPtr types = OSG::GeoUInt8Property::create();
52 // we want to use quads ONLY
53 types->addValue(GL_QUADS);
55 // the number of vertices (or indices) we want to use with the primitive
56 // type; types and lengths always have the same number of elements
57 // (here both have just one)
58 OSG::GeoUInt32PropertyRecPtr lengths = OSG::GeoUInt32Property::create();
59 // the length of our quads is four ;-)
60 lengths->addValue(4 * (N - 1) * (N - 1));
62 // GeoPnt3fProperty stores the positions of all vertices used in
63 // this specific geometry core
64 OSG::GeoPnt3fPropertyRecPtr pos = OSG::GeoPnt3fProperty::create();
65 // here they all come
66 for (int x = 0; x < N; x++)
67 for (int z = 0; z < N; z++)
68 pos->addValue(OSG::Pnt3f(x, wMesh[x][z], z));
70 // GeoColor3fProperty stores all color values that will be used
71 OSG::GeoColor3fPropertyRecPtr colors = OSG::GeoColor3fProperty::create();
72 for (int x = 0; x < N; x++)
73 for (int z = 0; z < N; z++)
74 colors->addValue(OSG::Color3f(0,0,1));
76 // and finally the normals are stored in a GeoVec3fProperty
77 OSG::GeoVec3fPropertyRecPtr norms = OSG::GeoVec3fProperty::create();
78 for (int x = 0; x < N; x++)
79 for (int z = 0; z < N; z++)
80 // As initially all heights are set to zero thus yielding a plane,
81 // we set all normals to (0,1,0) parallel to the y-axis
82 norms->addValue(OSG::Vec3f(0,1,0));
84 OSG::SimpleMaterialRecPtr mat = OSG::SimpleMaterial::create();
86 // Indices define the order in which the entries in the above properties
87 // are used
88 OSG::GeoUInt32PropertyRecPtr indices = OSG::GeoUInt32Property::create();
89 for (int x = 0; x < N-1; x++)
91 for (int z = 0; z < N-1; z++)
93 // points to four vertices that will
94 // define a single quad
95 indices->addValue( z * N + x );
96 indices->addValue((z+1) * N + x );
97 indices->addValue((z+1) * N + x + 1);
98 indices->addValue( z * N + x + 1);
102 OSG::GeometryRecPtr geo = OSG::Geometry::create();
104 geo->setTypes (types );
105 geo->setLengths (lengths);
106 geo->setIndices (indices);
107 geo->setPositions(pos );
108 geo->setNormals (norms );
109 geo->setMaterial (mat );
110 geo->setColors (colors );
112 OSG::NodeRecPtr root = OSG::Node::create();
113 root->setCore(geo);
115 return OSG::NodeTransitPtr(root);
118 int main(int argc, char **argv)
120 OSG::osgInit(argc,argv);
123 int winid = setupGLUT(&argc, argv);
124 OSG::GLUTWindowRecPtr gwin = OSG::GLUTWindow::create();
125 gwin->setGlutId(winid);
126 gwin->init();
128 scene = createScenegraph();
130 mgr = OSG::SimpleSceneManager::create();
131 mgr->setWindow(gwin );
132 mgr->setRoot (scene);
133 mgr->showAll();
135 OSG::Navigator * nav = mgr->getNavigator();
136 nav->setFrom(nav->getFrom()+OSG::Vec3f(0,2,0));
138 OSG::commitChanges();
141 glutMainLoop();
143 return 0;
146 void reshape(int w, int h)
148 mgr->resize(w, h);
149 glutPostRedisplay();
152 void display(void)
154 mgr->redraw();
157 void mouse(int button, int state, int x, int y)
159 if (state)
160 mgr->mouseButtonRelease(button, x, y);
161 else
162 mgr->mouseButtonPress(button, x, y);
164 glutPostRedisplay();
167 void motion(int x, int y)
169 mgr->mouseMove(x, y);
170 glutPostRedisplay();
173 int setupGLUT(int *argc, char *argv[])
175 glutInit(argc, argv);
176 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
178 int winid = glutCreateWindow("OpenSG First Application");
180 glutDisplayFunc(display);
181 glutMouseFunc(mouse);
182 glutMotionFunc(motion);
183 glutReshapeFunc(reshape);
184 glutIdleFunc(display);
186 return winid;