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

Revision 6941, 32.0 kB (checked in by robert, 8 years ago)

From Martin Lavery and Robert Osfield, Updated examples to use a variation of the MIT License

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