| 406 | | try |
| 407 | | { |
| 408 | | triangle_stripper::tri_stripper stripifier(taf._in_indices); |
| 409 | | stripifier.SetCacheSize(_cacheSize); |
| 410 | | stripifier.SetMinStripSize(_minStripSize); |
| 411 | | |
| 412 | | triangle_stripper::tri_stripper::primitives_vector outPrimitives; |
| 413 | | stripifier.Strip(&outPrimitives); |
| 414 | | |
| 415 | | triangle_stripper::tri_stripper::primitives_vector::iterator pitr; |
| 416 | | if (_generateFourPointPrimitivesQuads) |
| 417 | | { |
| 418 | | osg::notify(osg::WARN)<<"Collecting all quads"<<std::endl; |
| 419 | | |
| 420 | | typedef triangle_stripper::tri_stripper::primitives_vector::iterator prim_iterator; |
| 421 | | typedef std::multimap<unsigned int,prim_iterator> QuadMap; |
| 422 | | QuadMap quadMap; |
| 423 | | |
| 424 | | // pick out quads and place them in the quadMap, and also look for the max |
| 425 | | for(pitr=outPrimitives.begin(); |
| 426 | | pitr!=outPrimitives.end(); |
| 427 | | ++pitr) |
| 428 | | { |
| 429 | | if (pitr->m_Indices.size()==4) |
| 430 | | { |
| 431 | | std::swap(pitr->m_Indices[2],pitr->m_Indices[3]); |
| 432 | | unsigned int minValue = *(std::max_element(pitr->m_Indices.begin(),pitr->m_Indices.end())); |
| 433 | | quadMap.insert(QuadMap::value_type(minValue,pitr)); |
| 434 | | } |
| 435 | | } |
| 436 | | |
| 437 | | |
| 438 | | // handle the quads |
| 439 | | if (!quadMap.empty()) |
| 440 | | { |
| 441 | | IndexList indices; |
| 442 | | indices.reserve(4*quadMap.size()); |
| 443 | | |
| 444 | | // adds all the quads into the quad primitive, in ascending order |
| 445 | | // and the QuadMap stores the quad's in ascending order. |
| 446 | | for(QuadMap::iterator qitr=quadMap.begin(); |
| 447 | | qitr!=quadMap.end(); |
| 448 | | ++qitr) |
| 449 | | { |
| 450 | | pitr = qitr->second; |
| 451 | | |
| 452 | | unsigned int min_pos = 0; |
| 453 | | for(i=1;i<4;++i) |
| 454 | | { |
| 455 | | if (pitr->m_Indices[min_pos]>pitr->m_Indices[i]) |
| 456 | | min_pos = i; |
| 457 | | } |
| 458 | | indices.push_back(pitr->m_Indices[min_pos]); |
| 459 | | indices.push_back(pitr->m_Indices[(min_pos+1)%4]); |
| 460 | | indices.push_back(pitr->m_Indices[(min_pos+2)%4]); |
| 461 | | indices.push_back(pitr->m_Indices[(min_pos+3)%4]); |
| 462 | | } |
| 463 | | |
| 464 | | bool inOrder = true; |
| 465 | | unsigned int previousValue = indices.front(); |
| 466 | | for(IndexList::iterator qi_itr=indices.begin()+1; |
| 467 | | qi_itr!=indices.end() && inOrder; |
| 468 | | ++qi_itr) |
| 469 | | { |
| 470 | | inOrder = (previousValue+1)==*qi_itr; |
| 471 | | previousValue = *qi_itr; |
| 472 | | } |
| 473 | | |
| 474 | | |
| 475 | | if (inOrder) |
| 476 | | { |
| 477 | | new_primitives.push_back(new osg::DrawArrays(GL_QUADS,indices.front(),indices.size())); |
| 478 | | } |
| 479 | | else |
| 480 | | { |
| 481 | | unsigned int maxValue = *(std::max_element(indices.begin(),indices.end())); |
| 482 | | |
| 483 | | if (maxValue>=65536) |
| 484 | | { |
| 485 | | osg::DrawElementsUInt* elements = new osg::DrawElementsUInt(GL_QUADS); |
| 486 | | std::copy(indices.begin(),indices.end(),std::back_inserter(*elements)); |
| 487 | | new_primitives.push_back(elements); |
| 488 | | } |
| 489 | | else |
| 490 | | { |
| 491 | | osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(GL_QUADS); |
| 492 | | std::copy(indices.begin(),indices.end(),std::back_inserter(*elements)); |
| 493 | | new_primitives.push_back(elements); |
| 494 | | } |
| 495 | | } |
| 496 | | } |
| 497 | | } |
| 498 | | |
| 499 | | // handle non quad primitives |
| | 406 | triangle_stripper::tri_stripper stripifier(taf._in_indices); |
| | 407 | stripifier.SetCacheSize(_cacheSize); |
| | 408 | stripifier.SetMinStripSize(_minStripSize); |
| | 409 | |
| | 410 | triangle_stripper::tri_stripper::primitives_vector outPrimitives; |
| | 411 | if (!stripifier.Strip(&outPrimitives)) |
| | 412 | { |
| | 413 | osg::notify(osg::WARN)<<"Error: TriStripVisitor::stripify(Geometry& geom) failed."<<std::endl; |
| | 414 | return; |
| | 415 | } |
| | 416 | |
| | 417 | triangle_stripper::tri_stripper::primitives_vector::iterator pitr; |
| | 418 | if (_generateFourPointPrimitivesQuads) |
| | 419 | { |
| | 420 | osg::notify(osg::INFO)<<"Collecting all quads"<<std::endl; |
| | 421 | |
| | 422 | typedef triangle_stripper::tri_stripper::primitives_vector::iterator prim_iterator; |
| | 423 | typedef std::multimap<unsigned int,prim_iterator> QuadMap; |
| | 424 | QuadMap quadMap; |
| | 425 | |
| | 426 | // pick out quads and place them in the quadMap, and also look for the max |
| 504 | | if (!_generateFourPointPrimitivesQuads || pitr->m_Indices.size()!=4) |
| 505 | | { |
| 506 | | bool inOrder = true; |
| 507 | | unsigned int previousValue = pitr->m_Indices.front(); |
| 508 | | for(triangle_stripper::tri_stripper::indices::iterator qi_itr=pitr->m_Indices.begin()+1; |
| 509 | | qi_itr!=pitr->m_Indices.end() && inOrder; |
| 510 | | ++qi_itr) |
| | 431 | if (pitr->m_Indices.size()==4) |
| | 432 | { |
| | 433 | std::swap(pitr->m_Indices[2],pitr->m_Indices[3]); |
| | 434 | unsigned int minValue = *(std::max_element(pitr->m_Indices.begin(),pitr->m_Indices.end())); |
| | 435 | quadMap.insert(QuadMap::value_type(minValue,pitr)); |
| | 436 | } |
| | 437 | } |
| | 438 | |
| | 439 | |
| | 440 | // handle the quads |
| | 441 | if (!quadMap.empty()) |
| | 442 | { |
| | 443 | IndexList indices; |
| | 444 | indices.reserve(4*quadMap.size()); |
| | 445 | |
| | 446 | // adds all the quads into the quad primitive, in ascending order |
| | 447 | // and the QuadMap stores the quad's in ascending order. |
| | 448 | for(QuadMap::iterator qitr=quadMap.begin(); |
| | 449 | qitr!=quadMap.end(); |
| | 450 | ++qitr) |
| | 451 | { |
| | 452 | pitr = qitr->second; |
| | 453 | |
| | 454 | unsigned int min_pos = 0; |
| | 455 | for(i=1;i<4;++i) |
| 522 | | unsigned int maxValue = *(std::max_element(pitr->m_Indices.begin(),pitr->m_Indices.end())); |
| 523 | | if (maxValue>=65536) |
| 524 | | { |
| 525 | | osg::DrawElementsUInt* elements = new osg::DrawElementsUInt(pitr->m_Type); |
| 526 | | elements->reserve(pitr->m_Indices.size()); |
| 527 | | std::copy(pitr->m_Indices.begin(),pitr->m_Indices.end(),std::back_inserter(*elements)); |
| 528 | | new_primitives.push_back(elements); |
| 529 | | } |
| 530 | | else |
| 531 | | { |
| 532 | | osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(pitr->m_Type); |
| 533 | | elements->reserve(pitr->m_Indices.size()); |
| 534 | | std::copy(pitr->m_Indices.begin(),pitr->m_Indices.end(),std::back_inserter(*elements)); |
| 535 | | new_primitives.push_back(elements); |
| 536 | | } |
| | 493 | osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(GL_QUADS); |
| | 494 | std::copy(indices.begin(),indices.end(),std::back_inserter(*elements)); |
| | 495 | new_primitives.push_back(elements); |
| | 498 | } |
| | 499 | } |
| | 500 | |
| | 501 | // handle non quad primitives |
| | 502 | for(pitr=outPrimitives.begin(); |
| | 503 | pitr!=outPrimitives.end(); |
| | 504 | ++pitr) |
| | 505 | { |
| | 506 | if (!_generateFourPointPrimitivesQuads || pitr->m_Indices.size()!=4) |
| | 507 | { |
| | 508 | bool inOrder = true; |
| | 509 | unsigned int previousValue = pitr->m_Indices.front(); |
| | 510 | for(triangle_stripper::tri_stripper::indices::iterator qi_itr=pitr->m_Indices.begin()+1; |
| | 511 | qi_itr!=pitr->m_Indices.end() && inOrder; |
| | 512 | ++qi_itr) |
| | 513 | { |
| | 514 | inOrder = (previousValue+1)==*qi_itr; |
| | 515 | previousValue = *qi_itr; |
| | 516 | } |
| | 517 | |
| | 518 | if (inOrder) |
| | 519 | { |
| | 520 | new_primitives.push_back(new osg::DrawArrays(pitr->m_Type,pitr->m_Indices.front(),pitr->m_Indices.size())); |
| | 521 | } |
| | 522 | else |
| | 523 | { |
| | 524 | unsigned int maxValue = *(std::max_element(pitr->m_Indices.begin(),pitr->m_Indices.end())); |
| | 525 | if (maxValue>=65536) |
| | 526 | { |
| | 527 | osg::DrawElementsUInt* elements = new osg::DrawElementsUInt(pitr->m_Type); |
| | 528 | elements->reserve(pitr->m_Indices.size()); |
| | 529 | std::copy(pitr->m_Indices.begin(),pitr->m_Indices.end(),std::back_inserter(*elements)); |
| | 530 | new_primitives.push_back(elements); |
| | 531 | } |
| | 532 | else |
| | 533 | { |
| | 534 | osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(pitr->m_Type); |
| | 535 | elements->reserve(pitr->m_Indices.size()); |
| | 536 | std::copy(pitr->m_Indices.begin(),pitr->m_Indices.end(),std::back_inserter(*elements)); |
| | 537 | new_primitives.push_back(elements); |
| | 538 | } |
| | 539 | } |
| 540 | | |
| 541 | | geom.setPrimitiveSetList(new_primitives); |
| 542 | | |
| 543 | | #if 0 |
| 544 | | // debugging code for indentifying the tri-strips. |
| 545 | | osg::Vec4Array* colors = new osg::Vec4Array(new_primitives.size()); |
| 546 | | for(i=0;i<colors->size();++i) |
| 547 | | { |
| 548 | | (*colors)[i].set(((float)rand()/(float)RAND_MAX), |
| 549 | | ((float)rand()/(float)RAND_MAX), |
| 550 | | ((float)rand()/(float)RAND_MAX), |
| 551 | | 1.0f); |
| 552 | | } |
| 553 | | geom.setColorArray(colors); |
| 554 | | geom.setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET); |
| 555 | | #endif |
| 556 | | |
| 557 | | } |
| 558 | | catch(const char* errorMessage) |
| 559 | | { |
| 560 | | osg::notify(osg::WARN)<<"Warning: '"<<errorMessage<<"' exception thrown from triangle_stripper"<<std::endl; |
| 561 | | } |
| 562 | | catch(triangle_stripper::tri_stripper::triangles_indices_error&) |
| 563 | | { |
| 564 | | osg::notify(osg::WARN)<<"Warning: triangles_indices_error exception thrown from triangle_stripper"<<std::endl; |
| 565 | | } |
| 566 | | catch(...) |
| 567 | | { |
| 568 | | osg::notify(osg::WARN)<<"Warning: Unhandled exception thrown from triangle_stripper"<<std::endl; |
| 569 | | } |
| 570 | | |
| | 541 | } |
| | 542 | |
| | 543 | geom.setPrimitiveSetList(new_primitives); |
| | 544 | |
| | 545 | #if 0 |
| | 546 | // debugging code for indentifying the tri-strips. |
| | 547 | osg::Vec4Array* colors = new osg::Vec4Array(new_primitives.size()); |
| | 548 | for(i=0;i<colors->size();++i) |
| | 549 | { |
| | 550 | (*colors)[i].set(((float)rand()/(float)RAND_MAX), |
| | 551 | ((float)rand()/(float)RAND_MAX), |
| | 552 | ((float)rand()/(float)RAND_MAX), |
| | 553 | 1.0f); |
| | 554 | } |
| | 555 | geom.setColorArray(colors); |
| | 556 | geom.setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET); |
| | 557 | #endif |