fixed: minor compile errors (signed/unsigned compare)
[opensg.git] / Examples / Tutorial / 10water_lit.cpp
blobcc513449ca32980760bbb745c9045802fbcf8140
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>
9 #include <OSGTypedGeoIntegralProperty.h>
10 #include <OSGTypedGeoVectorProperty.h>
12 #include <OSGPointLight.h>
13 #include <OSGSpotLight.h>
14 #else
15 #include <OpenSG/OSGGLUT.h>
16 #include <OpenSG/OSGConfig.h>
17 #include <OpenSG/OSGSimpleGeometry.h>
18 #include <OpenSG/OSGGLUTWindow.h>
19 #include <OpenSG/OSGSimpleSceneManager.h>
20 #include <OpenSG/OSGGeometry.h>
21 #include <OpenSG/OSGTypedGeoIntegralProperty.h>
22 #include <OpenSG/OSGTypedGeoVectorProperty.h>
24 #include <OpenSG/OSGPointLight.h>
25 #include <OpenSG/OSGSpotLight.h>
26 #endif
28 // this will specify the resolution of the mesh
29 #define N 100
31 //the two dimensional array that will store all height values
32 OSG::Real32 wMesh[N][N];
34 //the origin of the water mesh
35 OSG::Pnt3f wOrigin = OSG::Pnt3f(0,0,0);
37 //width and length of the mesh
38 OSG::UInt16 width = 100;
39 OSG::UInt16 length = 100;
41 OSG::SimpleSceneManagerRefPtr mgr;
42 OSG::NodeRecPtr scene;
44 int setupGLUT(int *argc, char *argv[]);
46 void updateMesh(OSG::Real32 time)
48 for (int x = 0; x < N; x++)
49 for (int z = 0; z < N; z++)
50 wMesh[x][z] = 10*cos(time/1000.f + (x+z)/10.f);
53 OSG::NodeTransitPtr createScenegraph(void)
55 // the scene must be created here
56 for (int i = 0; i < N; i++)
57 for (int j = 0; j < N; j++)
58 wMesh[i][j] = 0;
60 // the types of primitives that are used - an integerer propery
61 OSG::GeoUInt8PropertyRecPtr types = OSG::GeoUInt8Property::create();
63 // we want to use quads ONLY
64 types->addValue(GL_QUADS);
66 // the number of vertices (or indices) we want to use with the primitive
67 // type; types and lengths always have the same number of elements
68 // (here both have just one)
69 OSG::GeoUInt32PropertyRecPtr lengths = OSG::GeoUInt32Property::create();
70 // the length of our quads is four ;-)
71 lengths->addValue(4 * (N - 1) * (N - 1));
73 // GeoPnt3fProperty stores the positions of all vertices used in
74 // this specific geometry core
75 OSG::GeoPnt3fPropertyRecPtr pos = OSG::GeoPnt3fProperty::create();
76 // here they all come
77 for (int x = 0; x < N; x++)
78 for (int z = 0; z < N; z++)
79 pos->addValue(OSG::Pnt3f(x, wMesh[x][z], z));
81 // GeoColor3fProperty stores all color values that will be used
82 OSG::GeoColor3fPropertyRecPtr colors = OSG::GeoColor3fProperty::create();
83 for (int x = 0; x < N; x++)
84 for (int z = 0; z < N; z++)
85 colors->addValue(OSG::Color3f(0,0,1));
87 // and finally the normals are stored in a GeoVec3fProperty
88 OSG::GeoVec3fPropertyRecPtr norms = OSG::GeoVec3fProperty::create();
89 for (int x = 0; x < N; x++)
90 for (int z = 0; z < N; z++)
91 // As initially all heights are set to zero thus yielding a plane,
92 // we set all normals to (0,1,0) parallel to the y-axis
93 norms->addValue(OSG::Vec3f(0,1,0));
95 OSG::SimpleMaterialRecPtr mat = OSG::SimpleMaterial::create();
96 mat->setDiffuse(OSG::Color3f(0,0,1));
98 // Indices define the order in which the entries in the above properties
99 // are used
100 OSG::GeoUInt32PropertyRecPtr indices = OSG::GeoUInt32Property::create();
101 for (int x = 0; x < N-1; x++)
103 for (int z = 0; z < N-1; z++)
105 // points to four vertices that will
106 // define a single quad
107 indices->addValue( z * N + x );
108 indices->addValue((z+1) * N + x );
109 indices->addValue((z+1) * N + x + 1);
110 indices->addValue( z * N + x + 1);
114 OSG::GeometryRecPtr geo = OSG::Geometry::create();
116 geo->setTypes (types );
117 geo->setLengths (lengths);
118 geo->setIndices (indices);
119 geo->setPositions(pos );
120 geo->setNormals (norms );
121 geo->setMaterial (mat );
122 // geo->setColors (colors );
123 geo->setDlistCache(false);
125 OSG::PointLightRecPtr pLight = OSG::PointLight::create();
126 OSG::NodeRecPtr root = OSG::Node::create();
127 OSG::NodeRecPtr water = OSG::Node::create();
128 OSG::NodeRecPtr pLightTransformNode = OSG::Node::create();
129 OSG::TransformRecPtr pLightTransform = OSG::Transform::create();
130 OSG::NodeRecPtr pLightNode = OSG::Node::create();
132 pLightNode->setCore(OSG::Group::create());
134 OSG::Matrix m;
135 m.setIdentity();
136 m.setTranslate(50,25,50);
138 pLightTransform->setMatrix(m);
140 //we add a little spehere that will represent the light source
141 OSG::GeometryRecPtr sphere = OSG::makeSphereGeo(2,2);
143 OSG::SimpleMaterialRecPtr sm = OSG::SimpleMaterial::create();
145 sm->setLit(false);
146 sm->setDiffuse(OSG::Color3f(1,1,1));
148 sphere->setMaterial(sm);
150 OSG::NodeRecPtr sphereNode = OSG::Node::create();
151 sphereNode->setCore(sphere);
153 pLightTransformNode->setCore(pLightTransform);
154 pLightTransformNode->addChild(pLightNode);
155 pLightTransformNode->addChild(sphereNode);
157 pLight->setPosition(OSG::Pnt3f(0,0,0));
159 //Attenuation parameters
160 pLight->setConstantAttenuation(1);
161 pLight->setLinearAttenuation(0);
162 pLight->setQuadraticAttenuation(0);
164 //color information
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));
169 //set the beacon
170 pLight->setBeacon(pLightNode);
172 water->setCore(geo);
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);
189 gwin->init();
191 scene = createScenegraph();
193 mgr = OSG::SimpleSceneManager::create();
194 mgr->setWindow(gwin );
195 mgr->setRoot (scene);
196 mgr->showAll();
197 mgr->setHeadlight(false);
199 OSG::Navigator * nav = mgr->getNavigator();
200 nav->setFrom(nav->getFrom()+OSG::Vec3f(0,50,0));
202 OSG::commitChanges();
205 glutMainLoop();
207 return 0;
210 void reshape(int w, int h)
212 mgr->resize(w, h);
213 glutPostRedisplay();
216 void display(void)
218 OSG::Real32 time = glutGet(GLUT_ELAPSED_TIME);
219 updateMesh(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();
234 //get some iterators
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);
246 it++;
250 mgr->redraw();
253 void mouse(int button, int state, int x, int y)
255 if (state)
256 mgr->mouseButtonRelease(button, x, y);
257 else
258 mgr->mouseButtonPress(button, x, y);
260 glutPostRedisplay();
263 void motion(int x, int y)
265 mgr->mouseMove(x, y);
266 glutPostRedisplay();
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);
282 return winid;