From 4ef244f67231a0d220ea64c8ad355d361d7a1776 Mon Sep 17 00:00:00 2001 From: Johannes Ebersold Date: Wed, 19 Nov 2014 14:10:16 +0100 Subject: [PATCH] Extract function ColladaParser::CopyPrimitive --- code/ColladaParser.cpp | 39 ++++++++++++++++++++++++--------------- code/ColladaParser.h | 5 +++++ 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/code/ColladaParser.cpp b/code/ColladaParser.cpp index 4a6737d9d..780da154d 100644 --- a/code/ColladaParser.cpp +++ b/code/ColladaParser.cpp @@ -2132,21 +2132,7 @@ void ColladaParser::ReadPrimitives( Mesh* pMesh, std::vector& pPer // gather that number of vertices for( size_t b = 0; b < numPoints; b++) { - // read all indices for this vertex. Yes, in a hacky local array - ai_assert( numOffsets < 20 && perVertexOffset < 20); - size_t vindex[20]; - for( size_t offsets = 0; offsets < numOffsets; ++offsets) - vindex[offsets] = indices[currentPrimitive * numOffsets * numPoints + b * numOffsets + offsets]; - - // extract per-vertex channels using the global per-vertex offset - for( std::vector::iterator it = pMesh->mPerVertexData.begin(); it != pMesh->mPerVertexData.end(); ++it) - ExtractDataObjectFromChannel( *it, vindex[perVertexOffset], pMesh); - // and extract per-index channels using there specified offset - for( std::vector::iterator it = pPerIndexChannels.begin(); it != pPerIndexChannels.end(); ++it) - ExtractDataObjectFromChannel( *it, vindex[it->mOffset], pMesh); - - // store the vertex-data index for later assignment of bone vertex weights - pMesh->mFacePosIndices.push_back( vindex[perVertexOffset]); + CopyPrimitive(b, numOffsets, numPoints, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices); } } @@ -2155,6 +2141,29 @@ void ColladaParser::ReadPrimitives( Mesh* pMesh, std::vector& pPer TestClosing( "p"); } +void ColladaParser::CopyPrimitive(size_t currentVertex, size_t numOffsets, size_t numPoints, size_t perVertexOffset, Mesh* pMesh, std::vector& pPerIndexChannels, size_t currentPrimitive, const std::vector& indices){ + // don't overrun the boundaries of the index list + size_t maxIndexRequested = currentPrimitive * numOffsets * numPoints + (currentVertex + 1) * numOffsets - 1; + ai_assert(maxIndexRequested < indices.size()); + + // read all indices for this vertex. Yes, in a hacky local array + ai_assert(numOffsets < 20 && perVertexOffset < 20); + size_t vindex[20]; + + for (size_t offsets = 0; offsets < numOffsets; ++offsets) + vindex[offsets] = indices[currentPrimitive * numOffsets * numPoints + currentVertex * numOffsets + offsets]; + + // extract per-vertex channels using the global per-vertex offset + for (std::vector::iterator it = pMesh->mPerVertexData.begin(); it != pMesh->mPerVertexData.end(); ++it) + ExtractDataObjectFromChannel(*it, vindex[perVertexOffset], pMesh); + // and extract per-index channels using there specified offset + for (std::vector::iterator it = pPerIndexChannels.begin(); it != pPerIndexChannels.end(); ++it) + ExtractDataObjectFromChannel(*it, vindex[it->mOffset], pMesh); + + // store the vertex-data index for later assignment of bone vertex weights + pMesh->mFacePosIndices.push_back(vindex[perVertexOffset]); +} + // ------------------------------------------------------------------------------------------------ // Extracts a single object from an input channel and stores it in the appropriate mesh data array void ColladaParser::ExtractDataObjectFromChannel( const InputChannel& pInput, size_t pLocalIndex, Mesh* pMesh) diff --git a/code/ColladaParser.h b/code/ColladaParser.h index c19a5e100..b93cf9145 100644 --- a/code/ColladaParser.h +++ b/code/ColladaParser.h @@ -180,6 +180,11 @@ protected: void ReadPrimitives( Collada::Mesh* pMesh, std::vector& pPerIndexChannels, size_t pNumPrimitives, const std::vector& pVCount, Collada::PrimitiveType pPrimType); + /** Copies the data for a single primitive into the mesh, based on the InputChannels */ + void CopyPrimitive(size_t currentVertex, size_t numOffsets, size_t numPoints, size_t perVertexOffset, + Collada::Mesh* pMesh, std::vector& pPerIndexChannels, + size_t currentPrimitive, const std::vector& indices); + /** Extracts a single object from an input channel and stores it in the appropriate mesh data array */ void ExtractDataObjectFromChannel( const Collada::InputChannel& pInput, size_t pLocalIndex, Collada::Mesh* pMesh);