| 1 | #include <osg/Geode> |
|---|
| 2 | #include <osg/Geometry> |
|---|
| 3 | #include <osg/Material> |
|---|
| 4 | #include <osg/Vec3> |
|---|
| 5 | #include <osg/MatrixTransform> |
|---|
| 6 | #include <osg/Texture2D> |
|---|
| 7 | #include <osg/PolygonStipple> |
|---|
| 8 | #include <osg/TriangleFunctor> |
|---|
| 9 | |
|---|
| 10 | #include <osgDB/ReadFile> |
|---|
| 11 | #include <osgDB/WriteFile> |
|---|
| 12 | |
|---|
| 13 | #include <osgGA/TrackballManipulator> |
|---|
| 14 | |
|---|
| 15 | #include <osgProducer/Viewer> |
|---|
| 16 | |
|---|
| 17 | #include <osg/Math> |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | struct NormalPrint |
|---|
| 42 | { |
|---|
| 43 | void operator() (const osg::Vec3& v1,const osg::Vec3& v2,const osg::Vec3& v3) const |
|---|
| 44 | { |
|---|
| 45 | osg::Vec3 normal = (v2-v1)^(v3-v2); |
|---|
| 46 | normal.normalize(); |
|---|
| 47 | std::cout << "\t("<<v1<<") ("<<v2<<") ("<<v3<<") "<<") normal ("<<normal<<")"<<std::endl; |
|---|
| 48 | } |
|---|
| 49 | }; |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | void printTriangles(const std::string& name, osg::Drawable& drawable) |
|---|
| 53 | { |
|---|
| 54 | std::cout<<name<<std::endl; |
|---|
| 55 | |
|---|
| 56 | osg::TriangleFunctor<NormalPrint> tf; |
|---|
| 57 | drawable.accept(tf); |
|---|
| 58 | |
|---|
| 59 | std::cout<<std::endl; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | osg::Node* createScene() |
|---|
| 64 | { |
|---|
| 65 | |
|---|
| 66 | osg::Geode* geode = new osg::Geode(); |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | { |
|---|
| 75 | |
|---|
| 76 | osg::Geometry* pointsGeom = new osg::Geometry(); |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | osg::Vec3Array* vertices = new osg::Vec3Array; |
|---|
| 84 | vertices->push_back(osg::Vec3(-1.02168, -2.15188e-09, 0.885735)); |
|---|
| 85 | vertices->push_back(osg::Vec3(-0.976368, -2.15188e-09, 0.832179)); |
|---|
| 86 | vertices->push_back(osg::Vec3(-0.873376, 9.18133e-09, 0.832179)); |
|---|
| 87 | vertices->push_back(osg::Vec3(-0.836299, -2.15188e-09, 0.885735)); |
|---|
| 88 | vertices->push_back(osg::Vec3(-0.790982, 9.18133e-09, 0.959889)); |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | pointsGeom->setVertexArray(vertices); |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | osg::Vec4Array* colors = new osg::Vec4Array; |
|---|
| 99 | |
|---|
| 100 | colors->push_back(osg::Vec4(1.0f,1.0f,0.0f,1.0f)); |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | pointsGeom->setColorArray(colors); |
|---|
| 105 | pointsGeom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | osg::Vec3Array* normals = new osg::Vec3Array; |
|---|
| 110 | normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f)); |
|---|
| 111 | pointsGeom->setNormalArray(normals); |
|---|
| 112 | pointsGeom->setNormalBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | pointsGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS,0,vertices->size())); |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | geode->addDrawable(pointsGeom); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | { |
|---|
| 129 | |
|---|
| 130 | osg::Geometry* linesGeom = new osg::Geometry(); |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | osg::Vec3Array* vertices = new osg::Vec3Array(8); |
|---|
| 136 | (*vertices)[0].set(-1.13704, -2.15188e-09, 0.40373); |
|---|
| 137 | (*vertices)[1].set(-0.856897, -2.15188e-09, 0.531441); |
|---|
| 138 | (*vertices)[2].set(-0.889855, -2.15188e-09, 0.444927); |
|---|
| 139 | (*vertices)[3].set(-0.568518, -2.15188e-09, 0.40373); |
|---|
| 140 | (*vertices)[4].set(-1.00933, -2.15188e-09, 0.370773); |
|---|
| 141 | (*vertices)[5].set(-0.716827, -2.15188e-09, 0.292498); |
|---|
| 142 | (*vertices)[6].set(-1.07936, 9.18133e-09, 0.317217); |
|---|
| 143 | (*vertices)[7].set(-0.700348, 9.18133e-09, 0.362533); |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | linesGeom->setVertexArray(vertices); |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | osg::Vec4Array* colors = new osg::Vec4Array; |
|---|
| 151 | colors->push_back(osg::Vec4(1.0f,1.0f,0.0f,1.0f)); |
|---|
| 152 | linesGeom->setColorArray(colors); |
|---|
| 153 | linesGeom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | osg::Vec3Array* normals = new osg::Vec3Array; |
|---|
| 158 | normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f)); |
|---|
| 159 | linesGeom->setNormalArray(normals); |
|---|
| 160 | linesGeom->setNormalBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | linesGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,8)); |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | geode->addDrawable(linesGeom); |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | { |
|---|
| 174 | |
|---|
| 175 | osg::Geometry* linesGeom = new osg::Geometry(); |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | osg::Vec3Array* vertices = new osg::Vec3Array(5); |
|---|
| 181 | osg::Vec3Array::iterator vitr = vertices->begin(); |
|---|
| 182 | (vitr++)->set(-0.0741545, -2.15188e-09, 0.416089); |
|---|
| 183 | (vitr++)->set(0.234823, -2.15188e-09, 0.259541); |
|---|
| 184 | (vitr++)->set(0.164788, -2.15188e-09, 0.366653); |
|---|
| 185 | (vitr++)->set(-0.0288379, -2.15188e-09, 0.333695); |
|---|
| 186 | (vitr++)->set(-0.0453167, -2.15188e-09, 0.280139); |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | linesGeom->setVertexArray(vertices); |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | osg::Vec4Array* colors = new osg::Vec4Array; |
|---|
| 193 | colors->push_back(osg::Vec4(1.0f,1.0f,0.0f,1.0f)); |
|---|
| 194 | linesGeom->setColorArray(colors); |
|---|
| 195 | linesGeom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | osg::Vec3Array* normals = new osg::Vec3Array; |
|---|
| 200 | normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f)); |
|---|
| 201 | linesGeom->setNormalArray(normals); |
|---|
| 202 | linesGeom->setNormalBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | linesGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP,0,5)); |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | geode->addDrawable(linesGeom); |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | { |
|---|
| 216 | |
|---|
| 217 | osg::Geometry* linesGeom = new osg::Geometry(); |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | osg::Vec3 myCoords[] = |
|---|
| 222 | { |
|---|
| 223 | osg::Vec3(0.741546, -2.15188e-09, 0.453167), |
|---|
| 224 | osg::Vec3(0.840418, -2.15188e-09, 0.304858), |
|---|
| 225 | osg::Vec3(1.12468, -2.15188e-09, 0.300738), |
|---|
| 226 | osg::Vec3(1.03816, 9.18133e-09, 0.453167), |
|---|
| 227 | osg::Vec3(0.968129, -2.15188e-09, 0.337815), |
|---|
| 228 | osg::Vec3(0.869256, -2.15188e-09, 0.531441) |
|---|
| 229 | }; |
|---|
| 230 | |
|---|
| 231 | int numCoords = sizeof(myCoords)/sizeof(osg::Vec3); |
|---|
| 232 | |
|---|
| 233 | osg::Vec3Array* vertices = new osg::Vec3Array(numCoords,myCoords); |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | linesGeom->setVertexArray(vertices); |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | osg::Vec4Array* colors = new osg::Vec4Array; |
|---|
| 240 | colors->push_back(osg::Vec4(1.0f,1.0f,0.0f,1.0f)); |
|---|
| 241 | linesGeom->setColorArray(colors); |
|---|
| 242 | linesGeom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | osg::Vec3Array* normals = new osg::Vec3Array; |
|---|
| 247 | normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f)); |
|---|
| 248 | linesGeom->setNormalArray(normals); |
|---|
| 249 | linesGeom->setNormalBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | linesGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP,0,numCoords)); |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | geode->addDrawable(linesGeom); |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | osg::ref_ptr<osg::Vec4Array> shared_colors = new osg::Vec4Array; |
|---|
| 272 | shared_colors->push_back(osg::Vec4(1.0f,1.0f,0.0f,1.0f)); |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | osg::ref_ptr<osg::Vec3Array> shared_normals = new osg::Vec3Array; |
|---|
| 276 | shared_normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f)); |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | |
|---|
| 283 | |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | { |
|---|
| 289 | |
|---|
| 290 | osg::Geometry* polyGeom = new osg::Geometry(); |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | |
|---|
| 297 | |
|---|
| 298 | osg::Vec3 myCoords[] = |
|---|
| 299 | { |
|---|
| 300 | osg::Vec3(-1.0464, 0.0f, -0.193626), |
|---|
| 301 | osg::Vec3(-1.0258, 0.0f, -0.26778), |
|---|
| 302 | osg::Vec3(-0.807461, 0.0f, -0.181267), |
|---|
| 303 | osg::Vec3(-0.766264, 0.0f, -0.0576758), |
|---|
| 304 | osg::Vec3(-0.980488, 0.0f, -0.094753) |
|---|
| 305 | }; |
|---|
| 306 | |
|---|
| 307 | int numCoords = sizeof(myCoords)/sizeof(osg::Vec3); |
|---|
| 308 | |
|---|
| 309 | osg::Vec3Array* vertices = new osg::Vec3Array(numCoords,myCoords); |
|---|
| 310 | |
|---|
| 311 | |
|---|
| 312 | polyGeom->setVertexArray(vertices); |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | polyGeom->setColorArray(shared_colors.get()); |
|---|
| 316 | polyGeom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 317 | |
|---|
| 318 | |
|---|
| 319 | |
|---|
| 320 | polyGeom->setNormalArray(shared_normals.get()); |
|---|
| 321 | polyGeom->setNormalBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 322 | |
|---|
| 323 | |
|---|
| 324 | |
|---|
| 325 | |
|---|
| 326 | polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POLYGON,0,numCoords)); |
|---|
| 327 | |
|---|
| 328 | printTriangles("Polygon",*polyGeom); |
|---|
| 329 | |
|---|
| 330 | |
|---|
| 331 | geode->addDrawable(polyGeom); |
|---|
| 332 | } |
|---|
| 333 | |
|---|
| 334 | |
|---|
| 335 | |
|---|
| 336 | { |
|---|
| 337 | |
|---|
| 338 | osg::Geometry* polyGeom = new osg::Geometry(); |
|---|
| 339 | |
|---|
| 340 | |
|---|
| 341 | osg::Vec3 myCoords[] = |
|---|
| 342 | { |
|---|
| 343 | osg::Vec3(0.0247182, 0.0f, -0.156548), |
|---|
| 344 | osg::Vec3(0.0247182, 0.0f, -0.00823939), |
|---|
| 345 | osg::Vec3(-0.160668, 0.0f, -0.0453167), |
|---|
| 346 | osg::Vec3(-0.222464, 0.0f, -0.13183), |
|---|
| 347 | |
|---|
| 348 | osg::Vec3(0.238942, 0.0f, -0.251302), |
|---|
| 349 | osg::Vec3(0.333696, 0.0f, 0.0329576), |
|---|
| 350 | osg::Vec3(0.164788, 0.0f, -0.0453167), |
|---|
| 351 | osg::Vec3(0.13595, 0.0f, -0.255421) |
|---|
| 352 | }; |
|---|
| 353 | |
|---|
| 354 | int numCoords = sizeof(myCoords)/sizeof(osg::Vec3); |
|---|
| 355 | |
|---|
| 356 | osg::Vec3Array* vertices = new osg::Vec3Array(numCoords,myCoords); |
|---|
| 357 | |
|---|
| 358 | |
|---|
| 359 | polyGeom->setVertexArray(vertices); |
|---|
| 360 | |
|---|
| 361 | |
|---|
| 362 | polyGeom->setColorArray(shared_colors.get()); |
|---|
| 363 | polyGeom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 364 | |
|---|
| 365 | |
|---|
| 366 | |
|---|
| 367 | polyGeom->setNormalArray(shared_normals.get()); |
|---|
| 368 | polyGeom->setNormalBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 369 | |
|---|
| 370 | |
|---|
| 371 | |
|---|
| 372 | |
|---|
| 373 | polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,numCoords)); |
|---|
| 374 | |
|---|
| 375 | |
|---|
| 376 | printTriangles("Quads",*polyGeom); |
|---|
| 377 | |
|---|
| 378 | |
|---|
| 379 | geode->addDrawable(polyGeom); |
|---|
| 380 | } |
|---|
| 381 | |
|---|
| 382 | |
|---|
| 383 | { |
|---|
| 384 | |
|---|
| 385 | osg::Geometry* polyGeom = new osg::Geometry(); |
|---|
| 386 | |
|---|
| 387 | |
|---|
| 388 | osg::Vec3 myCoords[] = |
|---|
| 389 | { |
|---|
| 390 | osg::Vec3(0.733306, -2.15188e-09, -0.0741545), |
|---|
| 391 | osg::Vec3(0.758024, -2.15188e-09, -0.205985), |
|---|
| 392 | |
|---|
| 393 | osg::Vec3(0.885735, -2.15188e-09, -0.0576757), |
|---|
| 394 | osg::Vec3(0.885735, -2.15188e-09, -0.214224), |
|---|
| 395 | |
|---|
| 396 | osg::Vec3(0.964009, 9.18133e-09, -0.0370773), |
|---|
| 397 | osg::Vec3(1.0464, 9.18133e-09, -0.173027), |
|---|
| 398 | |
|---|
| 399 | osg::Vec3(1.11232, -2.15188e-09, 0.0123591), |
|---|
| 400 | osg::Vec3(1.12468, 9.18133e-09, -0.164788), |
|---|
| 401 | }; |
|---|
| 402 | |
|---|
| 403 | int numCoords = sizeof(myCoords)/sizeof(osg::Vec3); |
|---|
| 404 | |
|---|
| 405 | osg::Vec3Array* vertices = new osg::Vec3Array(numCoords,myCoords); |
|---|
| 406 | |
|---|
| 407 | |
|---|
| 408 | polyGeom->setVertexArray(vertices); |
|---|
| 409 | |
|---|
| 410 | |
|---|
| 411 | polyGeom->setColorArray(shared_colors.get()); |
|---|
| 412 | polyGeom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 413 | |
|---|
| 414 | |
|---|
| 415 | |
|---|
| 416 | polyGeom->setNormalArray(shared_normals.get()); |
|---|
| 417 | polyGeom->setNormalBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 418 | |
|---|
| 419 | |
|---|
| 420 | |
|---|
| 421 | |
|---|
| 422 | polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUAD_STRIP,0,numCoords)); |
|---|
| 423 | |
|---|
| 424 | |
|---|
| 425 | printTriangles("Quads strip",*polyGeom); |
|---|
| 426 | |
|---|
| 427 | |
|---|
| 428 | geode->addDrawable(polyGeom); |
|---|
| 429 | } |
|---|
| 430 | |
|---|
| 431 | |
|---|
| 432 | { |
|---|
| 433 | |
|---|
| 434 | osg::Geometry* polyGeom = new osg::Geometry(); |
|---|
| 435 | |
|---|
| 436 | |
|---|
| 437 | osg::Vec3 myCoords[] = |
|---|
| 438 | { |
|---|
| 439 | |
|---|
| 440 | |
|---|
| 441 | osg::Vec3(-1.12056, -2.15188e-09, -0.840418), |
|---|
| 442 | osg::Vec3(-0.95165, -2.15188e-09, -0.840418), |
|---|
| 443 | osg::Vec3(-1.11644, 9.18133e-09, -0.716827), |
|---|
| 444 | |
|---|
| 445 | |
|---|
| 446 | osg::Vec3(-0.840418, 9.18133e-09, -0.778623), |
|---|
| 447 | osg::Vec3(-0.622074, 9.18133e-09, -0.613835), |
|---|
| 448 | osg::Vec3(-1.067, 9.18133e-09, -0.609715), |
|---|
| 449 | |
|---|
| 450 | |
|---|
| 451 | |
|---|
| 452 | |
|---|
| 453 | |
|---|
| 454 | osg::Vec3(-0.160668, -2.15188e-09, -0.531441), |
|---|
| 455 | osg::Vec3(-0.160668, -2.15188e-09, -0.749785), |
|---|
| 456 | osg::Vec3(0.0617955, 9.18133e-09, -0.531441), |
|---|
| 457 | osg::Vec3(0.168908, -2.15188e-09, -0.753905), |
|---|
| 458 | osg::Vec3(0.238942, -2.15188e-09, -0.531441), |
|---|
| 459 | osg::Vec3(0.280139, -2.15188e-09, -0.823939), |
|---|
| 460 | |
|---|
| 461 | |
|---|
| 462 | |
|---|
| 463 | osg::Vec3(0.844538, 9.18133e-09, -0.712708), |
|---|
| 464 | osg::Vec3(1.0258, 9.18133e-09, -0.799221), |
|---|
| 465 | osg::Vec3(1.03816, -2.15188e-09, -0.692109), |
|---|
| 466 | osg::Vec3(0.988727, 9.18133e-09, -0.568518), |
|---|
| 467 | osg::Vec3(0.840418, -2.15188e-09, -0.506723), |
|---|
| 468 | |
|---|
| 469 | }; |
|---|
| 470 | |
|---|
| 471 | int numCoords = sizeof(myCoords)/sizeof(osg::Vec3); |
|---|
| 472 | |
|---|
| 473 | osg::Vec3Array* vertices = new osg::Vec3Array(numCoords,myCoords); |
|---|
| 474 | |
|---|
| 475 | |
|---|
| 476 | polyGeom->setVertexArray(vertices); |
|---|
| 477 | |
|---|
| 478 | |
|---|
| 479 | polyGeom->setColorArray(shared_colors.get()); |
|---|
| 480 | polyGeom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 481 | |
|---|
| 482 | |
|---|
| 483 | |
|---|
| 484 | polyGeom->setNormalArray(shared_normals.get()); |
|---|
| 485 | polyGeom->setNormalBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 486 | |
|---|
| 487 | |
|---|
| 488 | |
|---|
| 489 | |
|---|
| 490 | polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES,0,6)); |
|---|
| 491 | polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_STRIP,6,6)); |
|---|
| 492 | polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_FAN,12,5)); |
|---|
| 493 | |
|---|
| 494 | |
|---|
| 495 | osg::StateSet* stateSet = new osg::StateSet(); |
|---|
| 496 | polyGeom->setStateSet(stateSet); |
|---|
| 497 | osg::PolygonStipple* polygonStipple = new osg::PolygonStipple; |
|---|
| 498 | stateSet->setAttributeAndModes(polygonStipple,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); |
|---|
| 499 | |
|---|
| 500 | printTriangles("Triangles/Strip/Fan",*polyGeom); |
|---|
| 501 | |
|---|
| 502 | |
|---|
| 503 | geode->addDrawable(polyGeom); |
|---|
| 504 | } |
|---|
| 505 | |
|---|
| 506 | return geode; |
|---|
| 507 | } |
|---|
| 508 | |
|---|
| 509 | |
|---|
| 510 | |
|---|
| 511 | class MyTransformCallback : public osg::NodeCallback |
|---|
| 512 | { |
|---|
| 513 | |
|---|
| 514 | public: |
|---|
| 515 | |
|---|
| 516 | MyTransformCallback(float angularVelocity) |
|---|
| 517 | { |
|---|
| 518 | _angular_velocity = angularVelocity; |
|---|
| 519 | } |
|---|
| 520 | |
|---|
| 521 | virtual void operator() (osg::Node* node, osg::NodeVisitor* nv) |
|---|
| 522 | { |
|---|
| 523 | osg::MatrixTransform* transform = dynamic_cast<osg::MatrixTransform*>(node); |
|---|
| 524 | if (nv && transform && nv->getFrameStamp()) |
|---|
| 525 | { |
|---|
| 526 | double time = nv->getFrameStamp()->getReferenceTime(); |
|---|
| 527 | transform->setMatrix(osg::Matrix::translate(0.0f,1.0f+cosf(time*_angular_velocity),0.0f)); |
|---|
| 528 | } |
|---|
| 529 | |
|---|
| 530 | |
|---|
| 531 | traverse(node,nv); |
|---|
| 532 | |
|---|
| 533 | } |
|---|
| 534 | |
|---|
| 535 | protected: |
|---|
| 536 | |
|---|
| 537 | float _angular_velocity; |
|---|
| 538 | |
|---|
| 539 | }; |
|---|
| 540 | |
|---|
| 541 | |
|---|
| 542 | osg::Node* createBackground() |
|---|
| 543 | { |
|---|
| 544 | |
|---|
| 545 | |
|---|
| 546 | osg::Image* image = osgDB::readImageFile("Images/primitives.gif"); |
|---|
| 547 | if (!image) return NULL; |
|---|
| 548 | |
|---|
| 549 | |
|---|
| 550 | |
|---|
| 551 | osg::Geometry* polyGeom = new osg::Geometry(); |
|---|
| 552 | |
|---|
| 553 | |
|---|
| 554 | osg::Vec3 myCoords[] = |
|---|
| 555 | { |
|---|
| 556 | osg::Vec3(-1.22908f,0.0f,1.0f), |
|---|
| 557 | osg::Vec3(-1.22908f,0.0f,-1.0f), |
|---|
| 558 | osg::Vec3(1.22908f,0.0f,-1.0f), |
|---|
| 559 | osg::Vec3(1.22908f,0.0f,1.0f) |
|---|
| 560 | }; |
|---|
| 561 | |
|---|
| 562 | int numCoords = sizeof(myCoords)/sizeof(osg::Vec3); |
|---|
| 563 | |
|---|
| 564 | |
|---|
| 565 | polyGeom->setVertexArray(new osg::Vec3Array(numCoords,myCoords)); |
|---|
| 566 | |
|---|
| 567 | osg::Vec4Array* colors = new osg::Vec4Array; |
|---|
| 568 | colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); |
|---|
| 569 | polyGeom->setColorArray(colors); |
|---|
| 570 | polyGeom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 571 | |
|---|
| 572 | |
|---|
| 573 | |
|---|
| 574 | osg::Vec3Array* normals = new osg::Vec3Array; |
|---|
| 575 | normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f)); |
|---|
| 576 | polyGeom->setNormalArray(normals); |
|---|
| 577 | polyGeom->setNormalBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 578 | |
|---|
| 579 | osg::Vec2 myTexCoords[] = |
|---|
| 580 | { |
|---|
| 581 | osg::Vec2(0,1), |
|---|
| 582 | osg::Vec2(0,0), |
|---|
| 583 | osg::Vec2(1,0), |
|---|
| 584 | osg::Vec2(1,1) |
|---|
| 585 | }; |
|---|
| 586 | |
|---|
| 587 | int numTexCoords = sizeof(myCoords)/sizeof(osg::Vec2); |
|---|
| 588 | |
|---|
| 589 | |
|---|
| 590 | |
|---|
| 591 | polyGeom->setTexCoordArray(0,new osg::Vec2Array(numTexCoords,myTexCoords)); |
|---|
| 592 | |
|---|
| 593 | |
|---|
| 594 | unsigned short myIndices[] = |
|---|
| 595 | { |
|---|
| 596 | 0, |
|---|
| 597 | 1, |
|---|
| 598 | 2, |
|---|
| 599 | 3 |
|---|
| 600 | }; |
|---|
| 601 | |
|---|
| 602 | int numIndices = sizeof(myIndices)/sizeof(unsigned short); |
|---|
| 603 | |
|---|
| 604 | |
|---|
| 605 | |
|---|
| 606 | |
|---|
| 607 | |
|---|
| 608 | polyGeom->addPrimitiveSet(new osg::DrawElementsUShort(osg::PrimitiveSet::QUADS,numIndices,myIndices)); |
|---|
| 609 | |
|---|
| 610 | |
|---|
| 611 | |
|---|
| 612 | osg::StateSet* stateset = new osg::StateSet; |
|---|
| 613 | |
|---|
| 614 | |
|---|
| 615 | osg::Texture2D* texture = new osg::Texture2D; |
|---|
| 616 | texture->setImage(image); |
|---|
| 617 | |
|---|
| 618 | stateset->setTextureAttributeAndModes(0, texture,osg::StateAttribute::ON); |
|---|
| 619 | |
|---|
| 620 | polyGeom->setStateSet(stateset); |
|---|
| 621 | |
|---|
| 622 | |
|---|
| 623 | |
|---|
| 624 | osg::Geode* geode = new osg::Geode(); |
|---|
| 625 | |
|---|
| 626 | |
|---|
| 627 | geode->addDrawable(polyGeom); |
|---|
| 628 | |
|---|
| 629 | |
|---|
| 630 | |
|---|
| 631 | |
|---|
| 632 | |
|---|
| 633 | osg::MatrixTransform* transform = new osg::MatrixTransform(); |
|---|
| 634 | transform->setUpdateCallback(new MyTransformCallback(1.0f)); |
|---|
| 635 | transform->addChild(geode); |
|---|
| 636 | |
|---|
| 637 | return transform; |
|---|
| 638 | } |
|---|
| 639 | |
|---|
| 640 | |
|---|
| 641 | |
|---|
| 642 | |
|---|
| 643 | int main( int argc, char **argv ) |
|---|
| 644 | { |
|---|
| 645 | |
|---|
| 646 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 647 | |
|---|
| 648 | |
|---|
| 649 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates how to create osg::Geometry."); |
|---|
| 650 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); |
|---|
| 651 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 652 | |
|---|
| 653 | |
|---|
| 654 | osgProducer::Viewer viewer(arguments); |
|---|
| 655 | |
|---|
| 656 | |
|---|
| 657 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 658 | |
|---|
| 659 | |
|---|
| 660 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 661 | |
|---|
| 662 | |
|---|
| 663 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 664 | { |
|---|
| 665 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 666 | return 1; |
|---|
| 667 | } |
|---|
| 668 | |
|---|
| 669 | |
|---|
| 670 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 671 | |
|---|
| 672 | |
|---|
| 673 | if (arguments.errors()) |
|---|
| 674 | { |
|---|
| 675 | arguments.writeErrorMessages(std::cout); |
|---|
| 676 | return 1; |
|---|
| 677 | } |
|---|
| 678 | |
|---|
| 679 | |
|---|
| 680 | |
|---|
| 681 | osg::Group* root = new osg::Group; |
|---|
| 682 | root->addChild( createScene() ); |
|---|
| 683 | root->addChild( createBackground() ); |
|---|
| 684 | |
|---|
| 685 | |
|---|
| 686 | |
|---|
| 687 | |
|---|
| 688 | |
|---|
| 689 | viewer.setSceneData( root ); |
|---|
| 690 | |
|---|
| 691 | |
|---|
| 692 | viewer.realize(); |
|---|
| 693 | |
|---|
| 694 | while( !viewer.done() ) |
|---|
| 695 | { |
|---|
| 696 | |
|---|
| 697 | viewer.sync(); |
|---|
| 698 | |
|---|
| 699 | |
|---|
| 700 | |
|---|
| 701 | viewer.update(); |
|---|
| 702 | |
|---|
| 703 | |
|---|
| 704 | viewer.frame(); |
|---|
| 705 | |
|---|
| 706 | } |
|---|
| 707 | |
|---|
| 708 | |
|---|
| 709 | viewer.sync(); |
|---|
| 710 | |
|---|
| 711 | return 0; |
|---|
| 712 | } |
|---|