1 /*
2     Copyright (c) 2008-2009 NetAllied Systems GmbH
3 
4     This file is part of COLLADASaxFrameworkLoader.
5 
6     Licensed under the MIT Open Source License,
7     for details please see LICENSE file or the website
8     http://www.opensource.org/licenses/mit-license.php
9 */
10 
11 #include "COLLADASaxFWLStableHeaders.h"
12 #include "COLLADASaxFWLMeshPrimitiveInputList.h"
13 #include "COLLADASaxFWLMeshLoader.h"
14 
15 
16 namespace COLLADASaxFWL
17 {
18 
19     //------------------------------
getInputBySemantic(const InputSemantic::Semantic & semantic) const20     const InputShared* MeshPrimitiveInputList::getInputBySemantic ( const InputSemantic::Semantic& semantic ) const
21     {
22         // Look for the input element in the current poly base.
23         for ( size_t i=0; i<mInputArray.getCount (); ++i )
24         {
25             InputShared* input = mInputArray [ i ];
26             if ( input->getSemantic () == semantic )
27                 return input;
28         }
29 
30         return 0;
31     }
32 
33     //------------------------------
appendInputElement(InputShared * inputShared)34     const InputShared* MeshPrimitiveInputList::appendInputElement ( InputShared* inputShared )
35     {
36         if ( inputShared != 0 )
37         {
38             // Check if we have the maximal offset value
39             unsigned long long offset = inputShared->getOffset();
40             if ( offset > mInputArrayMaxOffset ) mInputArrayMaxOffset = offset;
41 
42             // If we append the "VERTEX" input element, we directly create shared input elements
43             // with the vertex attributes.
44             if ( inputShared->getSemantic () == InputSemantic::VERTEX )
45             {
46                 const InputUnsharedArray& inputArray = mVertices.getInputArray ();
47                 size_t numInputElements = inputArray.getCount ();
48                 // Resize the input array
49                 mInputArray.reallocMemory ( numInputElements );
50                 for ( size_t i=0; i<numInputElements; ++i )
51                 {
52                     const InputUnshared* inputUnshared = inputArray [ i ];
53 
54                     InputShared* input = new InputShared ( inputUnshared->getSemantic (), inputUnshared->getSource (), inputShared->getOffset (), inputShared->getSet() );
55                     mInputArray.append ( input );
56                 }
57 				delete inputShared; // we are responsible for inputShared
58                 return mInputArray [ mInputArray.getCount () - 1 ];
59             }
60             else
61             {
62                 return mInputArray.append ( inputShared );
63             }
64         }
65         return 0;
66     }
67 
68 	//------------------------------
clearInputs()69 	void MeshPrimitiveInputList::clearInputs()
70 	{
71 		for ( size_t i = 0, count = mInputArray.getCount(); i < count; ++i)
72 			delete mInputArray[i];
73 		mInputArray.clear();
74 		mInputArrayMaxOffset = 0;
75 	}
76 } // namespace COLLADASaxFWL
77