| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | #include <osgDB/ReadFile> |
|---|
| 24 | #include <osgUtil/Optimizer> |
|---|
| 25 | #include <osgViewer/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 | |
|---|
| 34 | class tessellateDemoGeometry : public osg::Geometry, public osgUtil::Tessellator { |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | public: |
|---|
| 40 | tessellateDemoGeometry() {}; |
|---|
| 41 | |
|---|
| 42 | protected: |
|---|
| 43 | virtual ~tessellateDemoGeometry() {}; |
|---|
| 44 | }; |
|---|
| 45 | |
|---|
| 46 | osg::Geometry *makePolsTwo (void) |
|---|
| 47 | { |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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); |
|---|
| 114 | |
|---|
| 115 | return gtess; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | osg::Geometry *makeSideWall (const float xpos) |
|---|
| 119 | { |
|---|
| 120 | |
|---|
| 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 | |
|---|
| 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++) { |
|---|
| 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++) { |
|---|
| 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 | |
|---|
| 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; |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | tscx->setTessellationType(osgUtil::Tessellator::TESS_TYPE_GEOMETRY); |
|---|
| 183 | tscx->setBoundaryOnly(false); |
|---|
| 184 | tscx->setWindingType( osgUtil::Tessellator::TESS_WINDING_ODD); |
|---|
| 185 | |
|---|
| 186 | tscx->retessellatePolygons(*gtess); |
|---|
| 187 | |
|---|
| 188 | return gtess; |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | osg::Geometry *makeFrontWall (const float zpos) { |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | int nstart=0; |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 276 | |
|---|
| 277 | osg::ref_ptr<osgUtil::Tessellator> tscx=new osgUtil::Tessellator; |
|---|
| 278 | tscx->setTessellationType(osgUtil::Tessellator::TESS_TYPE_GEOMETRY); |
|---|
| 279 | tscx->setBoundaryOnly(false); |
|---|
| 280 | tscx->setWindingType( osgUtil::Tessellator::TESS_WINDING_ODD); |
|---|
| 281 | |
|---|
| 282 | tscx->retessellatePolygons(*gtess); |
|---|
| 283 | |
|---|
| 284 | return gtess; |
|---|
| 285 | } |
|---|
| 286 | osg::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 | } |
|---|
| 294 | osg::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 | |
|---|
| 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] = |
|---|
| 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] = |
|---|
| 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] = |
|---|
| 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++) { |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 483 | gtess->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_STRIP,nstart,8));nstart+=8; |
|---|
| 484 | |
|---|
| 485 | |
|---|
| 486 | |
|---|
| 487 | |
|---|
| 488 | |
|---|
| 489 | gtess->setTessellationType(osgUtil::Tessellator::TESS_TYPE_GEOMETRY); |
|---|
| 490 | gtess->setBoundaryOnly(true); |
|---|
| 491 | gtess->setWindingType( osgUtil::Tessellator::TESS_WINDING_ABS_GEQ_TWO); |
|---|
| 492 | |
|---|
| 493 | return gtess; |
|---|
| 494 | } |
|---|
| 495 | osg::Node* createHUD() |
|---|
| 496 | { |
|---|
| 497 | osg::Geode* geode = new osg::Geode(); |
|---|
| 498 | |
|---|
| 499 | std::string timesFont("fonts/arial.ttf"); |
|---|
| 500 | |
|---|
| 501 | |
|---|
| 502 | osg::StateSet* stateset = geode->getOrCreateStateSet(); |
|---|
| 503 | stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
|---|
| 504 | |
|---|
| 505 | |
|---|
| 506 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 549 | osg::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 | |
|---|
| 561 | class setTessellateVisitor : public osg::NodeVisitor |
|---|
| 562 | { |
|---|
| 563 | |
|---|
| 564 | |
|---|
| 565 | |
|---|
| 566 | |
|---|
| 567 | |
|---|
| 568 | public: |
|---|
| 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 | |
|---|
| 583 | tscx->setBoundaryOnly(true); |
|---|
| 584 | tscx->setWindingType( osgUtil::Tessellator::TESS_WINDING_ABS_GEQ_TWO); |
|---|
| 585 | geom->setUserData(tscx.get()); |
|---|
| 586 | } else if (geode.getName()== "tessellate odd") { |
|---|
| 587 | |
|---|
| 588 | tscx->setWindingType( osgUtil::Tessellator::TESS_WINDING_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); |
|---|
| 593 | tscx->retessellatePolygons(*geom); |
|---|
| 594 | } else if (geode.getName()== "tessellate positive") { |
|---|
| 595 | tscx->setWindingType( osgUtil::Tessellator::TESS_WINDING_POSITIVE); |
|---|
| 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 | |
|---|
| 630 | class cxTessellateVisitor : public osg::NodeVisitor |
|---|
| 631 | { |
|---|
| 632 | |
|---|
| 633 | |
|---|
| 634 | |
|---|
| 635 | |
|---|
| 636 | public: |
|---|
| 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()) { |
|---|
| 647 | |
|---|
| 648 | geom->setBoundaryOnly(true); |
|---|
| 649 | |
|---|
| 650 | } else { |
|---|
| 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()) { |
|---|
| 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(); |
|---|
| 695 | if (ct.z()<0.9) { |
|---|
| 696 | txt->setText(str.c_str()); |
|---|
| 697 | } |
|---|
| 698 | } |
|---|
| 699 | } |
|---|
| 700 | traverse(geode); |
|---|
| 701 | } |
|---|
| 702 | |
|---|
| 703 | std::string str; |
|---|
| 704 | }; |
|---|
| 705 | |
|---|
| 706 | class KeyboardEventHandler : public osgGA::GUIEventHandler |
|---|
| 707 | { |
|---|
| 708 | public: |
|---|
| 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 | |
|---|
| 722 | |
|---|
| 723 | |
|---|
| 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 | |
|---|
| 741 | int main( int argc, char **argv ) |
|---|
| 742 | { |
|---|
| 743 | |
|---|
| 744 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 745 | |
|---|
| 746 | |
|---|
| 747 | osgViewer::Viewer viewer; |
|---|
| 748 | |
|---|
| 749 | |
|---|
| 750 | osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments); |
|---|
| 751 | |
|---|
| 752 | |
|---|
| 753 | if (!loadedModel) |
|---|
| 754 | { |
|---|
| 755 | loadedModel=makeTessellateExample(); |
|---|
| 756 | |
|---|
| 757 | } else { |
|---|
| 758 | |
|---|
| 759 | |
|---|
| 760 | setTessellateVisitor tsv; |
|---|
| 761 | loadedModel->accept(tsv); |
|---|
| 762 | |
|---|
| 763 | } |
|---|
| 764 | |
|---|
| 765 | |
|---|
| 766 | osg::Group *gload= dynamic_cast<osg::Group *> (loadedModel.get()); |
|---|
| 767 | gload->addChild(createHUD()); |
|---|
| 768 | |
|---|
| 769 | |
|---|
| 770 | osgUtil::Optimizer optimizer; |
|---|
| 771 | optimizer.optimize(loadedModel.get() ); |
|---|
| 772 | |
|---|
| 773 | |
|---|
| 774 | viewer.setSceneData(loadedModel.get()); |
|---|
| 775 | |
|---|
| 776 | |
|---|
| 777 | viewer.addEventHandler(new KeyboardEventHandler(loadedModel.get())); |
|---|
| 778 | |
|---|
| 779 | return viewer.run(); |
|---|
| 780 | } |
|---|