| 1 | #include <osgUtil/DrawElementTypeSimplifier> |
|---|
| 2 | |
|---|
| 3 | #include <osg/Geode> |
|---|
| 4 | |
|---|
| 5 | template <typename InType, typename OutType> |
|---|
| 6 | OutType * copy(InType& array) |
|---|
| 7 | { |
|---|
| 8 | unsigned int size = array.size(); |
|---|
| 9 | OutType * newArray = new OutType(array.getMode(), size); |
|---|
| 10 | OutType & na = *newArray; |
|---|
| 11 | |
|---|
| 12 | for (unsigned int i = 0; i < size; ++i) na[i] = array[i]; |
|---|
| 13 | |
|---|
| 14 | return newArray; |
|---|
| 15 | } |
|---|
| 16 | |
|---|
| 17 | template <typename InType> |
|---|
| 18 | unsigned int getMax(InType& array) |
|---|
| 19 | { |
|---|
| 20 | unsigned int max = 0; |
|---|
| 21 | unsigned int size = array.size(); |
|---|
| 22 | |
|---|
| 23 | for (unsigned int i = 0; i < size; ++i) |
|---|
| 24 | { |
|---|
| 25 | if (array[i] > max) max = array[i]; |
|---|
| 26 | } |
|---|
| 27 | return (max); |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | namespace osgUtil |
|---|
| 31 | { |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | void DrawElementTypeSimplifier::simplify(osg::Geometry & geometry) const |
|---|
| 35 | { |
|---|
| 36 | osg::Geometry::PrimitiveSetList & psl = geometry.getPrimitiveSetList(); |
|---|
| 37 | osg::Geometry::PrimitiveSetList::iterator it, end = psl.end(); |
|---|
| 38 | |
|---|
| 39 | unsigned int max = 0; |
|---|
| 40 | |
|---|
| 41 | for (it = psl.begin(); it!=end; ++it) |
|---|
| 42 | { |
|---|
| 43 | switch ((*it)->getType()) |
|---|
| 44 | { |
|---|
| 45 | case osg::PrimitiveSet::DrawElementsUShortPrimitiveType: |
|---|
| 46 | { |
|---|
| 47 | osg::DrawElementsUShort & de = *static_cast<osg::DrawElementsUShort*>(it->get()); |
|---|
| 48 | |
|---|
| 49 | max = getMax<osg::DrawElementsUShort>(de); |
|---|
| 50 | if (max < 255) *it = copy<osg::DrawElementsUShort, osg::DrawElementsUByte>(de); |
|---|
| 51 | |
|---|
| 52 | break; |
|---|
| 53 | } |
|---|
| 54 | case osg::PrimitiveSet::DrawElementsUIntPrimitiveType: |
|---|
| 55 | { |
|---|
| 56 | osg::DrawElementsUInt & de = *static_cast<osg::DrawElementsUInt*>(it->get()); |
|---|
| 57 | |
|---|
| 58 | max = getMax<osg::DrawElementsUInt>(de); |
|---|
| 59 | if (max < 256) *it = copy<osg::DrawElementsUInt, osg::DrawElementsUByte>(de); |
|---|
| 60 | else if (max < 65536) *it = copy<osg::DrawElementsUInt, osg::DrawElementsUShort>(de); |
|---|
| 61 | |
|---|
| 62 | break; |
|---|
| 63 | } |
|---|
| 64 | default: break; |
|---|
| 65 | } |
|---|
| 66 | } |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | void DrawElementTypeSimplifierVisitor::apply(osg::Geode& node) |
|---|
| 70 | { |
|---|
| 71 | DrawElementTypeSimplifier dets; |
|---|
| 72 | |
|---|
| 73 | unsigned int numDrawables = node.getNumDrawables(); |
|---|
| 74 | for (unsigned int i = 0; i != numDrawables; ++i) |
|---|
| 75 | { |
|---|
| 76 | osg::Geometry * geom = dynamic_cast<osg::Geometry*>(node.getDrawable(i)); |
|---|
| 77 | if (geom) dets.simplify(*geom); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | osg::NodeVisitor::apply((osg::Node&)node); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | } |
|---|