diff --git a/src/osg/BufferObject.cpp b/src/osg/BufferObject.cpp index adc8bcf7e..fcbed67f8 100644 --- a/src/osg/BufferObject.cpp +++ b/src/osg/BufferObject.cpp @@ -26,6 +26,8 @@ #include #include +// #define CHECK_CONSISTENCY + using namespace osg; // static cache of deleted buffer object lists which can only @@ -456,8 +458,9 @@ GLBufferObjectSet::~GLBufferObjectSet() bool GLBufferObjectSet::checkConsistency() const { +#ifndef CHECK_CONSISTENCY return true; - +#else // osg::notify(osg::NOTICE)<<"GLBufferObjectSet::checkConsistency()"<_next)->_previous != to) { - osg::notify(osg::NOTICE)<<"Error (to->_next)->_previous != to "<_next)->_previous != to "; + osg::notify(osg::NOTICE)<<"GLBufferObjectSet::checkConsistency() : Error (to->_next)->_previous != to "<_next)->_previous != to) { - osg::notify(osg::NOTICE)<<"Error (to->_next)->_previous != to "<_next)->_previous != to "; + osg::notify(osg::NOTICE)<<"Texture::TextureObjectSet::checkConsistency() : Error (to->_next)->_previous != to "< QuadMap; + QuadMap quadMap; - typedef triangle_stripper::tri_stripper::primitives_vector::iterator prim_iterator; - typedef std::multimap QuadMap; - QuadMap quadMap; - - // pick out quads and place them in the quadMap, and also look for the max - for(pitr=outPrimitives.begin(); - pitr!=outPrimitives.end(); - ++pitr) - { - if (pitr->m_Indices.size()==4) - { - std::swap(pitr->m_Indices[2],pitr->m_Indices[3]); - unsigned int minValue = *(std::max_element(pitr->m_Indices.begin(),pitr->m_Indices.end())); - quadMap.insert(QuadMap::value_type(minValue,pitr)); - } - } - - - // handle the quads - if (!quadMap.empty()) - { - IndexList indices; - indices.reserve(4*quadMap.size()); - - // adds all the quads into the quad primitive, in ascending order - // and the QuadMap stores the quad's in ascending order. - for(QuadMap::iterator qitr=quadMap.begin(); - qitr!=quadMap.end(); - ++qitr) - { - pitr = qitr->second; - - unsigned int min_pos = 0; - for(i=1;i<4;++i) - { - if (pitr->m_Indices[min_pos]>pitr->m_Indices[i]) - min_pos = i; - } - indices.push_back(pitr->m_Indices[min_pos]); - indices.push_back(pitr->m_Indices[(min_pos+1)%4]); - indices.push_back(pitr->m_Indices[(min_pos+2)%4]); - indices.push_back(pitr->m_Indices[(min_pos+3)%4]); - } - - bool inOrder = true; - unsigned int previousValue = indices.front(); - for(IndexList::iterator qi_itr=indices.begin()+1; - qi_itr!=indices.end() && inOrder; - ++qi_itr) - { - inOrder = (previousValue+1)==*qi_itr; - previousValue = *qi_itr; - } - - - if (inOrder) - { - new_primitives.push_back(new osg::DrawArrays(GL_QUADS,indices.front(),indices.size())); - } - else - { - unsigned int maxValue = *(std::max_element(indices.begin(),indices.end())); - - if (maxValue>=65536) - { - osg::DrawElementsUInt* elements = new osg::DrawElementsUInt(GL_QUADS); - std::copy(indices.begin(),indices.end(),std::back_inserter(*elements)); - new_primitives.push_back(elements); - } - else - { - osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(GL_QUADS); - std::copy(indices.begin(),indices.end(),std::back_inserter(*elements)); - new_primitives.push_back(elements); - } - } - } - } - - // handle non quad primitives + // pick out quads and place them in the quadMap, and also look for the max for(pitr=outPrimitives.begin(); pitr!=outPrimitives.end(); ++pitr) { - if (!_generateFourPointPrimitivesQuads || pitr->m_Indices.size()!=4) + if (pitr->m_Indices.size()==4) { - bool inOrder = true; - unsigned int previousValue = pitr->m_Indices.front(); - for(triangle_stripper::tri_stripper::indices::iterator qi_itr=pitr->m_Indices.begin()+1; - qi_itr!=pitr->m_Indices.end() && inOrder; - ++qi_itr) - { - inOrder = (previousValue+1)==*qi_itr; - previousValue = *qi_itr; - } - - if (inOrder) - { - new_primitives.push_back(new osg::DrawArrays(pitr->m_Type,pitr->m_Indices.front(),pitr->m_Indices.size())); - } - else - { - unsigned int maxValue = *(std::max_element(pitr->m_Indices.begin(),pitr->m_Indices.end())); - if (maxValue>=65536) - { - osg::DrawElementsUInt* elements = new osg::DrawElementsUInt(pitr->m_Type); - elements->reserve(pitr->m_Indices.size()); - std::copy(pitr->m_Indices.begin(),pitr->m_Indices.end(),std::back_inserter(*elements)); - new_primitives.push_back(elements); - } - else - { - osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(pitr->m_Type); - elements->reserve(pitr->m_Indices.size()); - std::copy(pitr->m_Indices.begin(),pitr->m_Indices.end(),std::back_inserter(*elements)); - new_primitives.push_back(elements); - } - } + std::swap(pitr->m_Indices[2],pitr->m_Indices[3]); + unsigned int minValue = *(std::max_element(pitr->m_Indices.begin(),pitr->m_Indices.end())); + quadMap.insert(QuadMap::value_type(minValue,pitr)); } } - geom.setPrimitiveSetList(new_primitives); - #if 0 - // debugging code for indentifying the tri-strips. - osg::Vec4Array* colors = new osg::Vec4Array(new_primitives.size()); - for(i=0;isize();++i) + // handle the quads + if (!quadMap.empty()) + { + IndexList indices; + indices.reserve(4*quadMap.size()); + + // adds all the quads into the quad primitive, in ascending order + // and the QuadMap stores the quad's in ascending order. + for(QuadMap::iterator qitr=quadMap.begin(); + qitr!=quadMap.end(); + ++qitr) + { + pitr = qitr->second; + + unsigned int min_pos = 0; + for(i=1;i<4;++i) { - (*colors)[i].set(((float)rand()/(float)RAND_MAX), - ((float)rand()/(float)RAND_MAX), - ((float)rand()/(float)RAND_MAX), - 1.0f); + if (pitr->m_Indices[min_pos]>pitr->m_Indices[i]) + min_pos = i; } - geom.setColorArray(colors); - geom.setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET); - #endif + indices.push_back(pitr->m_Indices[min_pos]); + indices.push_back(pitr->m_Indices[(min_pos+1)%4]); + indices.push_back(pitr->m_Indices[(min_pos+2)%4]); + indices.push_back(pitr->m_Indices[(min_pos+3)%4]); + } - } - catch(const char* errorMessage) + bool inOrder = true; + unsigned int previousValue = indices.front(); + for(IndexList::iterator qi_itr=indices.begin()+1; + qi_itr!=indices.end() && inOrder; + ++qi_itr) + { + inOrder = (previousValue+1)==*qi_itr; + previousValue = *qi_itr; + } + + + if (inOrder) + { + new_primitives.push_back(new osg::DrawArrays(GL_QUADS,indices.front(),indices.size())); + } + else + { + unsigned int maxValue = *(std::max_element(indices.begin(),indices.end())); + + if (maxValue>=65536) + { + osg::DrawElementsUInt* elements = new osg::DrawElementsUInt(GL_QUADS); + std::copy(indices.begin(),indices.end(),std::back_inserter(*elements)); + new_primitives.push_back(elements); + } + else + { + osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(GL_QUADS); + std::copy(indices.begin(),indices.end(),std::back_inserter(*elements)); + new_primitives.push_back(elements); + } + } + } + } + + // handle non quad primitives + for(pitr=outPrimitives.begin(); + pitr!=outPrimitives.end(); + ++pitr) { - osg::notify(osg::WARN)<<"Warning: '"<m_Indices.size()!=4) + { + bool inOrder = true; + unsigned int previousValue = pitr->m_Indices.front(); + for(triangle_stripper::tri_stripper::indices::iterator qi_itr=pitr->m_Indices.begin()+1; + qi_itr!=pitr->m_Indices.end() && inOrder; + ++qi_itr) + { + inOrder = (previousValue+1)==*qi_itr; + previousValue = *qi_itr; + } + + if (inOrder) + { + new_primitives.push_back(new osg::DrawArrays(pitr->m_Type,pitr->m_Indices.front(),pitr->m_Indices.size())); + } + else + { + unsigned int maxValue = *(std::max_element(pitr->m_Indices.begin(),pitr->m_Indices.end())); + if (maxValue>=65536) + { + osg::DrawElementsUInt* elements = new osg::DrawElementsUInt(pitr->m_Type); + elements->reserve(pitr->m_Indices.size()); + std::copy(pitr->m_Indices.begin(),pitr->m_Indices.end(),std::back_inserter(*elements)); + new_primitives.push_back(elements); + } + else + { + osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(pitr->m_Type); + elements->reserve(pitr->m_Indices.size()); + std::copy(pitr->m_Indices.begin(),pitr->m_Indices.end(),std::back_inserter(*elements)); + new_primitives.push_back(elements); + } + } + } } + geom.setPrimitiveSetList(new_primitives); + + #if 0 + // debugging code for indentifying the tri-strips. + osg::Vec4Array* colors = new osg::Vec4Array(new_primitives.size()); + for(i=0;isize();++i) + { + (*colors)[i].set(((float)rand()/(float)RAND_MAX), + ((float)rand()/(float)RAND_MAX), + ((float)rand()/(float)RAND_MAX), + 1.0f); + } + geom.setColorArray(colors); + geom.setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET); + #endif } else { diff --git a/src/osgUtil/TriStrip_tri_stripper.cpp b/src/osgUtil/TriStrip_tri_stripper.cpp index 1c08ca91b..12349c906 100644 --- a/src/osgUtil/TriStrip_tri_stripper.cpp +++ b/src/osgUtil/TriStrip_tri_stripper.cpp @@ -12,14 +12,14 @@ */ // tri_stripper.cpp: implementation of the Tri Stripper class. // -// Copyright (C) 2002 Tanguy Fautré. +// Copyright (C) 2002 Tanguy Fautr�. // For conditions of distribution and use, // see copyright notice in tri_stripper.h // ////////////////////////////////////////////////////////////////////// #include "TriStrip_tri_stripper.h" - +#include // namespace triangle_stripper @@ -38,11 +38,14 @@ namespace triangle_stripper { // Members Functions ////////////////////////////////////////////////////////////////////// -void tri_stripper::Strip(primitives_vector * out_pPrimitivesVector) +bool tri_stripper::Strip(primitives_vector * out_pPrimitivesVector) { // verify that the number of indices is correct if (m_TriIndices.size() % 3 != 0) - throw triangles_indices_error(); + { + osg::notify(osg::NOTICE)<<"Warning: tri_stripper::Strip(..) invalid number of triangle indices."<= m_MinStripSize) - BuildStrip(TriStrip); + { + if (!BuildStrip(TriStrip)) return false; + } } // We must discard the triangle we inserted in the candidate list from the heap @@ -195,6 +205,8 @@ void tri_stripper::Stripify() while ((! m_TriHeap.empty()) && (m_TriHeap.top().Degree() == 0)) m_TriHeap.pop(); } + + return true; } @@ -376,7 +388,7 @@ triangle_edge tri_stripper::GetLatestEdge(const triangle & Triangle, const trian -void tri_stripper::BuildStrip(const triangle_strip TriStrip) +bool tri_stripper::BuildStrip(const triangle_strip TriStrip) { typedef triangles_graph::const_out_arc_iterator const_tri_link_iter; typedef triangles_graph::node_iterator tri_node_iter; @@ -439,7 +451,11 @@ void tri_stripper::BuildStrip(const triangle_strip TriStrip) // Debug check: we must have found the next triangle //assert(LinkIt != TriNodeIt->out_end()); - if (LinkIt == TriNodeIt->out_end()) throw "tri_stripper::BuildStrip(,) error, next triangle not found"; + if (LinkIt == TriNodeIt->out_end()) + { + osg::notify(osg::NOTICE)<<"Warning: tri_stripper::BuildStrip(,) error, next triangle not found."< primitives_vector; - struct triangles_indices_error { }; - - // constructor/initializer inline tri_stripper(const indices & TriIndices); @@ -214,7 +211,7 @@ public: inline void SetMinStripSize(const size_t MinStripSize = 2); // Stripper - void Strip(primitives_vector * out_pPrimitivesVector); // throw triangles_indices_error(); + bool Strip(primitives_vector * out_pPrimitivesVector); private: @@ -232,7 +229,7 @@ private: void InitCache(); void InitTriGraph(); void InitTriHeap(); - void Stripify(); + bool Stripify(); void AddLeftTriangles(); void LinkNeighboursTri(const triangle_edges & TriInterface, const triangle_edge Edge); @@ -242,7 +239,7 @@ private: triangle_strip FindBestStrip(); triangle_strip ExtendTriToStrip(const size_t StartTriPos, const triangle_strip::start_order StartOrder); - void BuildStrip(const triangle_strip TriStrip); + bool BuildStrip(const triangle_strip TriStrip); void AddIndice(const indice i); void AddIndiceToCache(const indice i, bool CacheHitCount = false); void AddTriToCache(const triangle & Tri, const triangle_strip::start_order Order);