Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "../3rdparty/tristripper/tri_stripper.cpp"
00033 #include "../3rdparty/tristripper/policy.cpp"
00034 #include "../3rdparty/tristripper/connectivity_graph.cpp"
00035
00036 using namespace triangle_stripper;
00037
00038 #include <vlGraphics/TriangleStripGenerator.hpp>
00039 #include <vlGraphics/Geometry.hpp>
00040 #include <vlGraphics/DoubleVertexRemover.hpp>
00041 #include <vlCore/Log.hpp>
00042 #include <vlCore/Say.hpp>
00043
00044 using namespace vl;
00045
00046 namespace
00047 {
00048 void fillIndices(std::vector<unsigned int>& indices, const DrawCall* dc, bool substitute_quads)
00049 {
00050 indices.clear();
00051
00052 if ( dc->primitiveType() == PT_QUADS && !substitute_quads )
00053 return;
00054
00055 indices.reserve( 1000 );
00056
00057 for(TriangleIterator trit = dc->triangleIterator(); trit.hasNext(); trit.next())
00058 {
00059 int a = trit.a();
00060 int b = trit.b();
00061 int c = trit.c();
00062
00063 if (a != b && b != c)
00064 {
00065 indices.push_back(a);
00066 indices.push_back(b);
00067 indices.push_back(c);
00068 }
00069 }
00070 }
00071 }
00072
00073 void TriangleStripGenerator::stripfy(Geometry* geom, int cache_size, bool merge_strips, bool remove_doubles, bool substitute_quads)
00074 {
00075 if (remove_doubles)
00076 {
00077 DoubleVertexRemover dvr;
00078 dvr.removeDoubles(geom);
00079 }
00080
00081 for( int idraw=geom->drawCalls()->size(); idraw--; )
00082 {
00083 DrawCall* dc = geom->drawCalls()->at(idraw);
00084
00085 triangle_stripper::indices indices;
00086
00087 fillIndices(indices, dc, substitute_quads);
00088
00089 std::vector<unsigned int> algo2_strip;
00090 std::vector<unsigned int> algo2_tris;
00091
00092
00093 tri_stripper striper(indices);
00094 striper.SetCacheSize(cache_size);
00095 striper.SetMinStripSize(4);
00096 primitive_vector out;
00097 striper.Strip(&out);
00098
00099
00100 if (out.size())
00101 {
00102 geom->drawCalls()->erase(idraw,1);
00103 algo2_strip.reserve(indices.size());
00104 for(unsigned s=0; s<out.size(); ++s)
00105 {
00106 if (out[s].Type == TRIANGLE_STRIP)
00107 {
00108 algo2_strip.clear();
00109 for(unsigned p=0; p<out[s].Indices.size(); ++p)
00110 algo2_strip.push_back(out[s].Indices[p]);
00111
00112 ref<DrawElementsUInt> draw_elems = new DrawElementsUInt(PT_TRIANGLE_STRIP);
00113 draw_elems->indexBuffer()->resize(algo2_strip.size());
00114 memcpy(draw_elems->indexBuffer()->ptr(), &algo2_strip[0], sizeof(unsigned int)*algo2_strip.size());
00115 geom->drawCalls()->push_back(draw_elems.get());
00116 }
00117 else
00118 {
00119 algo2_tris.clear();
00120 for(unsigned p=0; p<out[s].Indices.size(); ++p)
00121 algo2_tris.push_back(out[s].Indices[p]);
00122
00123 ref<DrawElementsUInt> draw_elems = new DrawElementsUInt(PT_TRIANGLES);
00124 draw_elems->indexBuffer()->resize(algo2_tris.size());
00125 memcpy(draw_elems->indexBuffer()->ptr(), &algo2_tris[0], sizeof(unsigned int)*algo2_tris.size());
00126 geom->drawCalls()->push_back(draw_elems.get());
00127 }
00128 }
00129 }
00130 }
00131
00132 if (merge_strips)
00133 geom->mergeTriangleStrips();
00134 }
00135