root/OpenSceneGraph/trunk/examples/osgtessellate/osgtessellate.cpp @ 5939

Revision 5939, 33.8 kB (checked in by robert, 7 years ago)

Tessellator name changes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This application is open source and may be redistributed and/or modified   
4 * freely and without restriction, both in commericial and non commericial applications,
5 * as long as this copyright notice is maintained.
6 *
7 * This application is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10*/
11
12/* osgTessellator
13 * - this Tessellator is an extension of the basic one - rather than tessellating
14 * individual polygons, we tessellate the entire geode with multiple contours.
15 * allows for overlapping contours etc.
16 * the Tessellator has new member fuinctions
17 setTessellationType(osgUtil::Tessellator::TESS_TYPE_xxx);
18    tscx->setBoundaryOnly(bool);
19    tscx->setWindingType( osgUtil::Tessellator::TESS_WINDING_xxx);
20 * for winding rules: See the red book chap 13.
21 */
22
23#include <osgDB/ReadFile>
24#include <osgUtil/Optimizer>
25#include <osgProducer/Viewer>
26#include <osg/Projection>
27#include <osg/MatrixTransform>
28
29#include <osgText/Text>
30
31#include <osgUtil/Tessellator> // to tessellate multiple contours
32
33
34class tessellateDemoGeometry : public osg::Geometry, public osgUtil::Tessellator {
35    // We add the Tessellator to the geometry because we want to access the
36    // tessellatable contours again;  you can apply a Tessellator to a Geometry
37    // to produce exactly a required tessellation once only, and then
38    // the contours could be discarded since the geometry does not need to be retessellated.
39public:
40    tessellateDemoGeometry() {};
41
42protected:
43    virtual ~tessellateDemoGeometry() {};
44};
45
46osg::Geometry *makePolsTwo (void)
47{
48    // an example of using current geometry contours to create next tessellation
49    // this polygon disappears once the contour rules make no polygons.
50    tessellateDemoGeometry *gtess= new tessellateDemoGeometry;
51    int i;
52    osg::Vec3Array *coords = new osg::Vec3Array;
53    osg::Vec3Array *nrms = new osg::Vec3Array;
54    osg::Vec2Array *tcs = new osg::Vec2Array;
55    osg::Vec3 nrm(0,-1,0);
56    static GLdouble quadstrip[8][3] =
57    { { 1900.0, 1130.0, 0.0 },
58      { 2100.0, 1130.0, 0.0 },
59      { 1900.0, 1350.0, 0.0 },
60      { 1950.0, 1350.0, 0.0 },
61      { 1900.0, 1550.0, 0.0 },
62      { 2000.0, 1550.0, 0.0 },
63      { 1900.0, 1750.0, 0.0 },
64      { 2400.0, 1750.0, 0.0 } };
65    static GLdouble innerquadstrip[8][3] =
66    { { 2000.0, 1230.0, 0.0 },
67      { 2050.0, 1230.0, 0.0 },
68      { 1920.0, 1350.0, 0.0 },
69      { 1940.0, 1350.0, 0.0 },
70      { 1920.0, 1550.0, 0.0 },
71      { 1980.0, 1550.0, 0.0 },
72      { 2000.0, 1650.0, 0.0 },
73      { 2400.0, 1650.0, 0.0 } };
74   
75    // add one large quadstrip
76    for (i = 0; i < 8; i++)
77    {
78        coords->push_back(osg::Vec3(quadstrip[i][0],quadstrip[i][2],quadstrip[i][1]));
79        tcs->push_back(osg::Vec2(quadstrip[i][0],quadstrip[i][1])/200.0);
80        nrms->push_back(nrm);
81    }
82    for (i = 0; i < 8; i++) {
83        coords->push_back(osg::Vec3(innerquadstrip[i][0],innerquadstrip[i][2],innerquadstrip[i][1]));
84        tcs->push_back(osg::Vec2(innerquadstrip[i][0],innerquadstrip[i][1])/200.0);
85        nrms->push_back(nrm);
86    }
87    gtess->setVertexArray(coords);
88    gtess->setNormalArray(nrms);
89    gtess->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
90    gtess->setTexCoordArray(0,tcs);
91   
92    // demonstrate that the Tessellator makes textured tessellations
93    osg::StateSet* stateset = new osg::StateSet();
94   
95    osg::Image* image = osgDB::readImageFile("Cubemap_snow/posy.jpg");
96    if (image)
97    {
98        osg::Texture2D* texture = new osg::Texture2D;
99        texture->setImage(image);
100        texture->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT);
101        texture->setWrap(osg::Texture2D::WRAP_T, osg::Texture2D::REPEAT);
102        stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
103    }
104    gtess->setStateSet( stateset );
105   
106    int nstart=0;
107    // The derived class tessellateDemoGeometry retains the original contours for re-use.
108    gtess->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_STRIP,nstart,8));nstart+=8;
109    gtess->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_STRIP,nstart,8));nstart+=8;
110
111    gtess->setTessellationType(osgUtil::Tessellator::TESS_TYPE_GEOMETRY);
112    gtess->setBoundaryOnly(true);
113    gtess->setWindingType( osgUtil::Tessellator::TESS_WINDING_ABS_GEQ_TWO); // so that first change in wind type makes the commonest tessellation - ODD.
114   
115    return gtess;
116}
117
118osg::Geometry *makeSideWall (const float xpos)
119{
120    // demonstrate making a rectangular 'wall' with 2 holes in it.
121    osg::Geometry *gtess= new osg::Geometry;
122    int i;
123    osg::Vec3Array *coords = new osg::Vec3Array;
124    osg::Vec3Array *nrms = new osg::Vec3Array;
125    osg::Vec2Array *tcs = new osg::Vec2Array;
126    osg::Vec3 nrm(-1,0,0);
127    // front wall
128    static GLdouble wall[4][2] =
129    { { 1130.0, 0.0 },
130      { 1130.0, 300.0 } ,
131      { 1340.0,300.0 },
132      { 1340.0,0.0 } };
133
134    gtess->setVertexArray(coords);
135    gtess->setNormalArray(nrms);
136    gtess->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
137    gtess->setTexCoordArray(0,tcs);
138
139    for (i = 0; i < 4; i++) {
140        coords->push_back(osg::Vec3(xpos,wall[i][1],wall[i][0]));
141        tcs->push_back(osg::Vec2(wall[i][1],wall[i][0])/100.0);
142        nrms->push_back(nrm);
143    }
144    int nstart=0;
145
146    gtess->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,nstart,4));nstart+=4;
147    for (i = 0; i < 24; i++) { // make an ellipse hole
148        float y=150+50*cos(i*2*osg::PI/24.0);
149        float z=1300+30* sin(i*2*osg::PI/24.0);
150        coords->push_back(osg::Vec3(xpos,y,z));
151        tcs->push_back(osg::Vec2(y,z)/100.0);
152        nrms->push_back(nrm);
153    }
154    gtess->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POLYGON,nstart,24));nstart+=24;
155    for (i = 0; i < 5; i++) { // make a pentagonal hole
156        float y=150+50*cos(i*2*osg::PI/5.0);
157        float z=1200+40* sin(i*2*osg::PI/5.0);
158        coords->push_back(osg::Vec3(xpos,y,z));
159        tcs->push_back(osg::Vec2(y,z)/100.0);
160        nrms->push_back(nrm);
161    }
162    gtess->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POLYGON,nstart,5));nstart+=5;
163   
164    // demonstrate that the Tessellator makes textured tessellations
165    osg::StateSet* stateset = new osg::StateSet();
166   
167    osg::Image* image = osgDB::readImageFile("Cubemap_snow/posx.jpg");
168    if (image)
169    {
170        osg::Texture2D* texture = new osg::Texture2D;
171        texture->setImage(image);
172        texture->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT);
173        texture->setWrap(osg::Texture2D::WRAP_T, osg::Texture2D::REPEAT);
174        stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
175    }
176    gtess->setStateSet( stateset );
177   
178
179    osg::ref_ptr<osgUtil::Tessellator> tscx=new osgUtil::Tessellator; // the v1.2 multi-contour Tessellator.
180    // we use the geometry primitives to describe the contours which are tessellated.
181    // Winding odd means leave hole in surface where there are 2,4,6... contours circling the point.
182    tscx->setTessellationType(osgUtil::Tessellator::TESS_TYPE_GEOMETRY);
183    tscx->setBoundaryOnly(false);
184    tscx->setWindingType( osgUtil::Tessellator::TESS_WINDING_ODD); // so that first change in wind type makes the commonest tessellation - ODD.
185   
186    tscx->retessellatePolygons(*gtess);
187   
188    return gtess;
189}
190
191osg::Geometry *makeFrontWall (const float zpos) {
192    // an example of using one tessellation to make a 'house' wall
193    // describe the wall as a pentagon, then door & 4 windows are further contours
194    // tessellate the set of contours to make a 'house wall' from the Boolean-like operations.
195    int nstart=0; // counts vertices used for the geometry primitives
196
197    osg::Geometry *gtess= new osg::Geometry;
198    int i;
199    osg::Vec3Array *coords = new osg::Vec3Array;
200    osg::Vec3Array *nrms = new osg::Vec3Array;
201    osg::Vec2Array *tcs = new osg::Vec2Array;
202    osg::Vec3 nrm(0,-1,0);
203    // front wall
204    static GLdouble wall[5][2] =
205    { { 2200.0, 1130.0 },
206      { 2600.0, 1130.0 }, 
207      { 2600.0, 1340.0 },
208      { 2400.0, 1440.0 },
209      { 2200.0, 1340.0 } };
210
211    static GLdouble door[4][2] =
212    { { 2360.0, 1130.0 },
213      { 2440.0, 1130.0 },
214      { 2440.0, 1230.0 },
215      { 2360.0, 1230.0 } };
216
217    static GLdouble windows[16][2] =
218    { { 2240.0, 1180.0 },
219      { 2330.0, 1180.0 },
220      { 2330.0, 1220.0 },
221      { 2240.0, 1220.0 },
222      { 2460.0, 1180.0 },
223      { 2560.0, 1180.0 },
224      { 2560.0, 1220.0 },
225      { 2460.0, 1220.0 },
226      { 2240.0, 1280.0 },
227      { 2330.0, 1280.0 },
228      { 2330.0, 1320.0 },
229      { 2240.0, 1320.0 },
230      { 2460.0, 1280.0 },
231      { 2560.0, 1280.0 },
232      { 2560.0, 1320.0 },
233      { 2460.0, 1320.0 } };
234
235    gtess->setVertexArray(coords);
236    gtess->setNormalArray(nrms);
237    gtess->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
238    gtess->setTexCoordArray(0,tcs);
239
240    // add one large pentagon -the wall
241    for (i = 0; i < 5; i++) {
242        coords->push_back(osg::Vec3(wall[i][0],zpos,wall[i][1]));
243        tcs->push_back(osg::Vec2(wall[i][0],wall[i][1])/100.0);
244        nrms->push_back(nrm);
245    }
246    gtess->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POLYGON,nstart,5));nstart+=5;
247    // add first hole, a door
248    for (i = 0; i < 4; i++) {
249        coords->push_back(osg::Vec3(door[i][0],zpos,door[i][1]));
250        tcs->push_back(osg::Vec2(door[i][0],door[i][1])/100.0);
251        nrms->push_back(nrm);
252    }
253    // and windows
254    for (i = 0; i < 16; i++) {
255        coords->push_back(osg::Vec3(windows[i][0],zpos,windows[i][1]));
256        tcs->push_back(osg::Vec2(windows[i][0],windows[i][1])/100.0);
257        nrms->push_back(nrm);
258    }
259    gtess->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,nstart,20));nstart+=20;
260
261    // demonstrate that the Tessellator makes textured tessellations
262    osg::StateSet* stateset = new osg::StateSet();
263   
264    osg::Image* image = osgDB::readImageFile("Cubemap_snow/posy.jpg");
265    if (image)
266    {
267        osg::Texture2D* texture = new osg::Texture2D;
268        texture->setImage(image);
269        texture->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT);
270        texture->setWrap(osg::Texture2D::WRAP_T, osg::Texture2D::REPEAT);
271        stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
272    }
273    gtess->setStateSet( stateset );
274   
275    // We use a Tessellator to produce the tessellation required once only
276    // and the contours are discarded.
277    osg::ref_ptr<osgUtil::Tessellator> tscx=new osgUtil::Tessellator; // the v1.2 multi-contour Tessellator.
278    tscx->setTessellationType(osgUtil::Tessellator::TESS_TYPE_GEOMETRY);
279    tscx->setBoundaryOnly(false);
280    tscx->setWindingType( osgUtil::Tessellator::TESS_WINDING_ODD); // so that first change in wind type makes the commonest tessellation - ODD.
281   
282    tscx->retessellatePolygons(*gtess);
283   
284    return gtess;
285}
286osg::Geode *makeHouse (void) {
287    osg::Geode *gd = new osg::Geode;
288    gd->addDrawable(makeFrontWall(0.0));
289    gd->addDrawable(makeFrontWall(300.0));
290    gd->addDrawable(makeSideWall(2200.0));
291    gd->addDrawable(makeSideWall(2600.0));
292    return gd;
293}
294osg::Geometry *makePols (void) {
295    tessellateDemoGeometry *gtess= new tessellateDemoGeometry;
296    int i;
297    osg::Vec3Array *coords = new osg::Vec3Array;
298    osg::Vec3Array *nrms = new osg::Vec3Array;
299    osg::Vec2Array *tcs = new osg::Vec2Array;
300    osg::Vec3 nrm(0,-1,0);
301    // coordinates from red book code but shifted by 1000 & 2000 for alternate Tessellatory things.
302    static GLdouble rects[12][3] =
303    { { 50.0, 50.0, 0.0 },
304      { 300.0, 50.0, 0.0 },
305      { 300.0, 300.0, 0.0 },
306      { 50.0, 300.0, 0.0 },
307      { 100.0, 100.0, 0.0 },
308      { 250.0, 100.0, 0.0 },
309      { 250.0, 250.0, 0.0 },
310      { 100.0, 250.0, 0.0 },
311      { 150.0, 150.0, 0.0 },
312      { 200.0, 150.0, 0.0 },
313      { 200.0, 200.0, 0.0 },
314      { 150.0, 200.0, 0.0 } };
315
316    static GLdouble rectsMidanti[12][3] = // the centre 2 contours are traversed opposite order to outer contour.
317    { { 1050.0, 50.0, 0.0 },
318      { 1300.0, 50.0, 0.0 },
319      { 1300.0, 300.0, 0.0 },
320      { 1050.0, 300.0, 0.0 },
321      { 1250.0, 100.0, 0.0 },
322      { 1100.0, 100.0, 0.0 },
323      { 1100.0, 250.0, 0.0 },
324      { 1250.0, 250.0, 0.0 },
325      { 1200.0, 150.0, 0.0 },
326      { 1150.0, 150.0, 0.0 },
327      { 1150.0, 200.0, 0.0 },
328      { 1200.0, 200.0, 0.0 } };
329    static GLdouble spiral[16][3] = // shift by 1000; nb the order of vertices is reversed from that of the red book
330    { { 3400.0, 250.0, 0.0 },
331      { 3400.0, 50.0, 0.0 },
332      { 3050.0, 50.0, 0.0 }, 
333      { 3050.0, 400.0, 0.0 },
334      { 3350.0, 400.0, 0.0 },
335      { 3350.0, 100.0, 0.0 },
336      { 3100.0, 100.0, 0.0 },
337      { 3100.0, 350.0, 0.0 },
338      { 3300.0, 350.0, 0.0 },
339      { 3300.0, 150.0, 0.0 },
340      { 3150.0, 150.0, 0.0 },
341      { 3150.0, 300.0, 0.0 },
342      { 3250.0, 300.0, 0.0 },
343      { 3250.0, 200.0, 0.0 },
344      { 3200.0, 200.0, 0.0 },
345      { 3200.0, 250.0, 0.0 }
346    };
347    static GLdouble quad1[4][3] = // shift by 2000 for next 3 things
348    { { 2050.0, 150.0, 0.0 },
349      { 2350.0, 150.0, 0.0 },
350      { 2350.0, 200.0, 0.0 },
351      { 2050.0, 200.0, 0.0 }
352    };
353    static GLdouble quad2[4][3] =
354    { { 2100.0, 100.0, 0.0 },
355      { 2300.0, 100.0, 0.0 },
356      { 2300.0, 350.0, 0.0 },
357      { 2100.0, 350.0, 0.0 }
358    };
359    static GLdouble tri[3][3] =
360    { { 2200.0, 50.0, 0.0 },
361      { 2250.0, 300.0, 0.0 },
362      { 2150.0, 300.0, 0.0 }
363    };
364    static GLdouble quad3[4][3] =
365    { { 100.0, 1100.0, 0.0 },
366      { 1300.0, 1100.0, 0.0 },
367      { 1300.0, 2350.0, 0.0 },
368      { 100.0, 2350.0, 0.0}
369    };
370    static GLdouble quadstrip[8][3] =
371    { { 900.0, 1130.0, 0.0 },
372      { 1100.0, 1130.0, 0.0 },
373      { 900.0, 1350.0, 0.0 },
374      { 950.0, 1350.0, 0.0 },
375      { 900.0, 1550.0, 0.0 },
376      { 1000.0, 1550.0, 0.0 },
377      { 900.0, 1750.0, 0.0 },
378      { 1400.0, 1750.0, 0.0 }
379    };
380   
381    for (i = 0; i < 12; i++) {
382        coords->push_back(osg::Vec3(rects[i][0],rects[i][2],rects[i][1]));
383        tcs->push_back(osg::Vec2(rects[i][0],rects[i][1])/200.0);
384        nrms->push_back(nrm);
385    }
386    for (i = 0; i < 12; i++) {
387        coords->push_back(osg::Vec3(rectsMidanti[i][0],rectsMidanti[i][2],rectsMidanti[i][1]));
388        tcs->push_back(osg::Vec2(rectsMidanti[i][0],rectsMidanti[i][1])/200.0);
389        nrms->push_back(nrm);
390    }
391    for (i = 0; i < 16; i++) { // and reverse spiral to make same as that of red book ch 11
392        coords->push_back(osg::Vec3(spiral[15-i][0],spiral[15-i][2],spiral[15-i][1]));
393        tcs->push_back(osg::Vec2(spiral[15-i][0],spiral[15-i][1])/200.0);
394        nrms->push_back(nrm);
395    }
396    for (i = 0; i < 4; i++) {
397        coords->push_back(osg::Vec3(quad1[i][0],quad1[i][2],quad1[i][1]));
398        tcs->push_back(osg::Vec2(quad1[i][0],quad1[i][1])/200.0);
399        nrms->push_back(nrm);
400    }
401    for (i = 0; i < 4; i++) {
402        coords->push_back(osg::Vec3(quad2[i][0],quad2[i][2],quad2[i][1]));
403        tcs->push_back(osg::Vec2(quad2[i][0],quad2[i][1])/200.0);
404        nrms->push_back(nrm);
405    }
406    for (i = 0; i < 3; i++) {
407        coords->push_back(osg::Vec3(tri[i][0],tri[i][2],tri[i][1]));
408        tcs->push_back(osg::Vec2(tri[i][0],tri[i][1])/200.0);
409        nrms->push_back(nrm);
410    }
411    // add one large quad with multiple holes
412    for (i = 0; i < 4; i++) {
413        coords->push_back(osg::Vec3(quad3[i][0],quad3[i][2],quad3[i][1]));
414        tcs->push_back(osg::Vec2(quad3[i][0],quad3[i][1])/200.0);
415        nrms->push_back(nrm);
416    }
417    {
418        osg::Vec3 centre(300,0,1500);
419        for (i = 0; i < 18; i++) {
420            osg::Vec3 rim=centre+osg::Vec3(-cos(osg::DegreesToRadians((float)i*20.0)),0.0,sin(osg::DegreesToRadians((float)i*20.0)))*150.0;
421            coords->push_back(rim);
422            tcs->push_back(osg::Vec2(rim.x(),rim.z())/200.0);
423            nrms->push_back(nrm);
424        }
425    }
426    {
427        osg::Vec3 centre(400,0,1800);
428        for (i = 0; i < 18; i++) {
429            osg::Vec3 rim=centre+osg::Vec3(-cos(osg::DegreesToRadians((float)i*15.0)),0.0,sin(osg::DegreesToRadians((float)i*15.0)))*250.0;
430            coords->push_back(rim);
431            tcs->push_back(osg::Vec2(rim.x(),rim.z())/200.0);
432            nrms->push_back(nrm);
433        }
434    }
435    {
436        osg::Vec3 centre(600,0,1400);
437        for (i = 0; i < 18; i++) {
438            osg::Vec3 rim=centre+osg::Vec3(-cos(osg::DegreesToRadians((float)i*12.0)),0.0,sin(osg::DegreesToRadians((float)i*12.0)))*250.0;
439            coords->push_back(rim);
440            tcs->push_back(osg::Vec2(rim.x(),rim.z())/200.0);
441            nrms->push_back(nrm);
442        }
443    }
444    // add one large quadstrip
445    for (i = 0; i < 8; i++) {
446        coords->push_back(osg::Vec3(quadstrip[i][0],quadstrip[i][2],quadstrip[i][1]));
447        tcs->push_back(osg::Vec2(quadstrip[i][0],quadstrip[i][1])/200.0);
448        nrms->push_back(nrm);
449    }
450    gtess->setVertexArray(coords);
451    gtess->setNormalArray(nrms);
452    gtess->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
453    gtess->setTexCoordArray(0,tcs);
454   
455    // demonstrate that the Tessellator makes textured tessellations
456    osg::StateSet* stateset = new osg::StateSet();
457   
458    osg::Image* image = osgDB::readImageFile("Cubemap_snow/posz.jpg");
459    if (image)
460    {
461        osg::Texture2D* texture = new osg::Texture2D;
462        texture->setImage(image);
463        texture->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT);
464        texture->setWrap(osg::Texture2D::WRAP_T, osg::Texture2D::REPEAT);
465        stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
466    }
467    gtess->setStateSet( stateset );
468   
469    int nstart=0;
470    // the contours accepoted are polygons; quads & tris. Trifans can bve added later.
471    gtess->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,nstart,12));nstart+=12;
472    gtess->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,nstart,12));nstart+=12;
473    gtess->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POLYGON,nstart,16));nstart+=16;
474    gtess->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,nstart,4));nstart+=4;
475    gtess->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,nstart,4));nstart+=4;
476    gtess->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES,nstart,3));nstart+=3;
477    // A rectabngle with multiple holes
478    gtess->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP,nstart,4));nstart+=4;
479    gtess->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_FAN,nstart,18));nstart+=18;
480    gtess->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POLYGON,nstart,18));nstart+=18;
481    gtess->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POLYGON,nstart,18));nstart+=18;
482    // test for quad strip
483    gtess->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_STRIP,nstart,8));nstart+=8;
484   
485    // We need to access the tessellatable contours again to demonstrate all types of tessellation.
486    // I could add the Tessellator to the geometry as userdata, but here
487    // I use the derived tessellateDemoGeometry to hold both the drawable geode and the original contours.
488   
489    gtess->setTessellationType(osgUtil::Tessellator::TESS_TYPE_GEOMETRY);
490    gtess->setBoundaryOnly(true);
491    gtess->setWindingType( osgUtil::Tessellator::TESS_WINDING_ABS_GEQ_TWO); // so that first change in wind type makes the commonest tessellation - ODD.
492   
493    return gtess;
494}
495osg::Node* createHUD()
496{ // add a string reporting the type of winding rule tessellation applied
497    osg::Geode* geode = new osg::Geode();
498   
499    std::string timesFont("fonts/arial.ttf");
500
501    // turn lighting off for the text and disable depth test to ensure its always ontop.
502    osg::StateSet* stateset = geode->getOrCreateStateSet();
503    stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
504
505    // Disable depth test, and make sure that the hud is drawn after everything
506    // else so that it always appears ontop.
507    stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
508    stateset->setRenderBinDetails(11,"RenderBin");
509
510    osg::Vec3 position(150.0f,900.0f,0.0f);
511    osg::Vec3 delta(0.0f,-30.0f,0.0f);
512
513    {
514        osgText::Text* text = new  osgText::Text;
515        geode->addDrawable( text );
516
517        text->setFont(timesFont);
518        text->setPosition(position);
519        text->setText("Tessellation example - no tessellation (use 'W' wireframe to visualise)");
520        text->setColor(osg::Vec4(1.0,1.0,0.8,1.0));
521        position += delta;
522       
523    }   
524    {
525        osgText::Text* text = new  osgText::Text;
526        geode->addDrawable( text );
527
528        text->setFont(timesFont);
529        text->setPosition(position);
530        text->setText("Press 'n' to use an alternative tessellation.");
531       
532    }   
533
534    // create the hud.
535    osg::MatrixTransform* modelview_abs = new osg::MatrixTransform;
536    modelview_abs->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
537    modelview_abs->setMatrix(osg::Matrix::identity());
538    modelview_abs->addChild(geode);
539
540    osg::Projection* projection = new osg::Projection;
541    projection->setMatrix(osg::Matrix::ortho2D(0,1280,0,1024));
542    projection->addChild(modelview_abs);
543
544    return projection;
545
546}
547
548
549osg::Group *makeTessellateExample (void) {
550    osg::Group *grp=new osg::Group;
551    osg::Geode *gd=new osg::Geode;
552    gd->addDrawable(makePols());
553    gd->addDrawable(makePolsTwo());
554    grp->addChild(gd);
555
556    grp->addChild(makeHouse());
557
558    return grp;
559}
560
561class setTessellateVisitor : public osg::NodeVisitor
562{ // searches a loaded model tree for tessellatable geometries.
563    // used with any database model which has a renderGroup (Geode) named 'tessellate'
564    // or you can force a type of tess with special names or a sub-class of Geode could have extra information
565    // of course you can use any name to detect what is to be tessellated!
566    // all the polygons within the specific node are deemed to be contours, so
567    // any tessellation can be requested.
568public:
569   
570    setTessellateVisitor():osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {
571    }
572    virtual void apply(osg::Geode& geode) {
573        if (geode.getName().compare(0,9,"tessellate")==0) {
574            for(unsigned int i=0;i<geode.getNumDrawables();++i)
575            {
576                osg::Geometry* geom = dynamic_cast<osg::Geometry*>(geode.getDrawable(i));
577                if (geom) {
578                    osg::ref_ptr<osgUtil::Tessellator> tscx=new osgUtil::Tessellator();
579                    if (tscx.valid()) {
580                        tscx->setTessellationType(osgUtil::Tessellator::TESS_TYPE_GEOMETRY);
581                        if (geode.getName()== "tessellate") {
582                            // add a Tessellator so that this geom is retessellated when N is pressed
583                            tscx->setBoundaryOnly(true);
584                            tscx->setWindingType( osgUtil::Tessellator::TESS_WINDING_ABS_GEQ_TWO); // so that first change in wind type makes the commonest tessellation - ODD.
585                            geom->setUserData(tscx.get());
586                        } else if (geode.getName()== "tessellate odd") {
587                            // OR you can just apply the Tessellator once only, using these different types
588                            tscx->setWindingType( osgUtil::Tessellator::TESS_WINDING_ODD); // commonest tessellation - ODD.
589                            tscx->retessellatePolygons(*geom);
590                        } else if (geode.getName()== "tessellate odd bound") {
591                            tscx->setBoundaryOnly(true);
592                            tscx->setWindingType( osgUtil::Tessellator::TESS_WINDING_ODD); // tessellation - ODD, only show boundary.
593                            tscx->retessellatePolygons(*geom);
594                        } else if (geode.getName()== "tessellate positive") {
595                            tscx->setWindingType( osgUtil::Tessellator::TESS_WINDING_POSITIVE); // tessellation - pos.
596                            tscx->retessellatePolygons(*geom);
597                        } else if (geode.getName()== "tessellate positive bound") {
598                            tscx->setBoundaryOnly(true);
599                            tscx->setWindingType( osgUtil::Tessellator::TESS_WINDING_POSITIVE);
600                            tscx->retessellatePolygons(*geom);
601                        } else if (geode.getName()== "tessellate negative") {
602                            tscx->setWindingType( osgUtil::Tessellator::TESS_WINDING_NEGATIVE);
603                            tscx->retessellatePolygons(*geom);
604                        } else if (geode.getName()== "tessellate negative bound") {
605                            tscx->setBoundaryOnly(true);
606                            tscx->setWindingType( osgUtil::Tessellator::TESS_WINDING_NEGATIVE);
607                            tscx->retessellatePolygons(*geom);
608                        } else if (geode.getName()== "tessellate nonzero") {
609                            tscx->setWindingType( osgUtil::Tessellator::TESS_WINDING_NONZERO);
610                            tscx->retessellatePolygons(*geom);
611                        } else if (geode.getName()== "tessellate nonzero bound") {
612                            tscx->setBoundaryOnly(true);
613                            tscx->setWindingType( osgUtil::Tessellator::TESS_WINDING_NONZERO);
614                            tscx->retessellatePolygons(*geom);
615                        } else if (geode.getName()== "tessellate geq2") {
616                            tscx->setWindingType( osgUtil::Tessellator::TESS_WINDING_ABS_GEQ_TWO);
617                            tscx->retessellatePolygons(*geom);
618                        } else if (geode.getName()== "tessellate geq2 bound") {
619                            tscx->setBoundaryOnly(true);
620                            tscx->setWindingType( osgUtil::Tessellator::TESS_WINDING_ABS_GEQ_TWO);
621                            tscx->retessellatePolygons(*geom);
622                        }
623                    }
624                }
625            }
626        }
627    }   
628};
629
630class cxTessellateVisitor : public osg::NodeVisitor
631{ // special to this demo, traverses SG and finds nodes which have been tessellated
632    // for test/demo purposes these nodes are of type tessellateDemoGeometry
633    // but you could store the Tessellator as UserData or however you like.
634    // the Tessellator holds copies of the original contours used in the tessellation
635    // In this visitor, I reuse the contours to make a different type of tessellation.
636public:
637   
638    cxTessellateVisitor():osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {
639    }
640    virtual void apply(osg::Geode& geode) {
641
642        for(unsigned int i=0;i<geode.getNumDrawables();++i)
643        {
644            tessellateDemoGeometry *geom=dynamic_cast<tessellateDemoGeometry*>(geode.getDrawable(i));
645            if (geom) {
646                if (!geom->getBoundaryOnly()) { // turn on bounds only
647                    // NB this shows only the true boundary of the curves, no internal edges                   
648                    geom->setBoundaryOnly(true);
649                   
650                } else { // change to next type of tessellation...
651                    geom->setBoundaryOnly(false);
652                    switch (geom->getWindingType()) {
653                    case         osgUtil::Tessellator::TESS_WINDING_ODD:
654                        geom->setWindingType(osgUtil::Tessellator::TESS_WINDING_NONZERO);
655                        break;
656                    case    osgUtil::Tessellator::TESS_WINDING_NONZERO:
657                        geom->setWindingType( osgUtil::Tessellator::TESS_WINDING_POSITIVE);
658                        break;
659                    case    osgUtil::Tessellator::TESS_WINDING_POSITIVE:
660                        geom->setWindingType( osgUtil::Tessellator::TESS_WINDING_NEGATIVE);
661                        break;
662                    case    osgUtil::Tessellator::TESS_WINDING_NEGATIVE:
663                        geom->setWindingType( osgUtil::Tessellator::TESS_WINDING_ABS_GEQ_TWO);
664                        break;
665                    case    osgUtil::Tessellator::TESS_WINDING_ABS_GEQ_TWO:
666                        geom->setWindingType( osgUtil::Tessellator::TESS_WINDING_ODD);
667                        break;
668                    }
669                }
670               
671                switch (geom->getWindingType()) { // a text to be added to the scene.
672                case         osgUtil::Tessellator::TESS_WINDING_ODD:
673                    str="TESS_WINDING_ODD";
674                    break;
675                case    osgUtil::Tessellator::TESS_WINDING_NONZERO:
676                    str="TESS_WINDING_NONZERO";
677                    break;
678                case    osgUtil::Tessellator::TESS_WINDING_POSITIVE:
679                    str="TESS_WINDING_POSITIVE";
680                    break;
681                case    osgUtil::Tessellator::TESS_WINDING_NEGATIVE:
682                    str="TESS_WINDING_NEGATIVE";
683                    break;
684                case    osgUtil::Tessellator::TESS_WINDING_ABS_GEQ_TWO:
685                    str="TESS_WINDING_ABS_GEQ_TWO";
686                    break;
687                }
688                if (geom->getBoundaryOnly()) str += " Boundary";
689               
690                geom->retessellatePolygons(*geom);
691            }
692            osgText::Text* txt = dynamic_cast<osgText::Text*>(geode.getDrawable(i));
693            if (txt) {
694                const osg::Vec4& ct=txt->getColor(); // pick the text to be changed by its color
695                if (ct.z()<0.9) {
696                    txt->setText(str.c_str());
697                }
698            }
699        }
700        traverse(geode);
701    }
702   
703    std::string str; // a label for on screen display
704};
705
706class KeyboardEventHandler : public osgGA::GUIEventHandler
707{ // extra event handler traps 'n' key to re-tessellate any tessellated geodes.
708public:
709   
710    KeyboardEventHandler(osg::Node *nd):
711        _scene(nd) {}
712   
713        virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
714        {
715            switch(ea.getEventType())
716            {
717                case(osgGA::GUIEventAdapter::KEYDOWN):
718                {
719                    if (_scene && ea.getKey()=='n')
720                    {
721                        // re-tessellate the scene graph.
722                        // the same contours are re-tessellated using a new method. Old contours
723                        // & tessellation type are held internally in the derived Geode class tessellateDemoGeometry.
724                        cxTessellateVisitor tsv;
725                        _scene->accept(tsv);
726                       return true;
727                    }
728                    break;
729                }
730                default:
731                    break;
732            }
733            return false;
734        }
735       
736        osg::Node *_scene;
737       
738};
739
740
741int main( int argc, char **argv )
742{
743
744    // use an ArgumentParser object to manage the program arguments.
745    osg::ArgumentParser arguments(&argc,argv);
746   
747    // set up the usage document, in case we need to print out how to use this program.
748    arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
749    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is provides an example of doing tesseleation..");
750    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
751    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
752   
753
754    // construct the viewer.
755    osgProducer::Viewer viewer(arguments);
756
757    // set up the value with sensible default event handlers.
758    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
759
760    // get details on keyboard and mouse bindings used by the viewer.
761    viewer.getUsage(*arguments.getApplicationUsage());
762
763    // if user request help write it out to cout.
764    if (arguments.read("-h") || arguments.read("--help"))
765    {
766        arguments.getApplicationUsage()->write(std::cout);
767        return 1;
768    }
769
770    // report any errors if they have occured when parsing the program aguments.
771    if (arguments.errors())
772    {
773        arguments.writeErrorMessages(std::cout);
774        return 1;
775    }
776   
777    osg::Timer timer;
778    osg::Timer_t start_tick = timer.tick();
779
780    // read the scene from the list of file specified commandline args.
781    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
782
783    // if no model has been successfully loaded report failure.
784    if (!loadedModel)
785    {
786        loadedModel=makeTessellateExample();
787    } else { // if there is a loaded model:
788        // tessellate by searching for geode called tessellate & tessellate it
789        setTessellateVisitor tsv;
790        loadedModel->accept(tsv);
791    }
792
793    // create the hud.
794    osg::Group *gload= dynamic_cast<osg::Group *> (loadedModel.get());
795    gload->addChild(createHUD());
796
797   
798    // any option left unread are converted into errors to write out later.
799    arguments.reportRemainingOptionsAsUnrecognized();
800
801    // report any errors if they have occured when parsing the program aguments.
802    if (arguments.errors())
803    {
804        arguments.writeErrorMessages(std::cout);
805    }
806
807    osg::Timer_t end_tick = timer.tick();
808
809    std::cout << "Time to load = "<<timer.delta_s(start_tick,end_tick)<<std::endl;
810
811
812    osgUtil::Optimizer optimizer;
813    optimizer.optimize(loadedModel.get() );
814
815    // set the scene to render
816    viewer.setSceneData(loadedModel.get());
817
818    // add event handler for keyboard 'n' to retessellate
819    viewer.getEventHandlerList().push_front(new KeyboardEventHandler(loadedModel.get()));
820
821    // create the windows and run the threads.
822    viewer.realize();
823
824    while( !viewer.done() )
825    {
826        // wait for all cull and draw threads to complete.
827        viewer.sync();
828
829        // update the scene by traversing it with the the update visitor which will
830        // call all node update callbacks and animations.
831        viewer.update();
832         
833        // fire off the cull and draw traversals of the scene.
834        viewer.frame();
835       
836    }
837   
838    // wait for all cull and draw threads to complete.
839    viewer.sync();
840
841    // do OpenGL clean up
842    viewer.cleanup_frame();
843
844    // wait for all cull and draw threads to complete before exit.
845    viewer.sync();
846
847    return 0;
848}
Note: See TracBrowser for help on using the browser.