void MeshParameterization::AddVertexSoup( RENDERBUFFERALLOCATION * renderbuffer, bool onlyIndices )
{
	if( renderbuffer)
	{
		// calculate the bbox
		HRESULT hr;
		Vec3 current;
		int numVerts = renderbuffer->m_VertexAllocation.m_Size;
		int stride = renderbuffer->m_VertexAllocation.m_Stride;
		int numIndices = renderbuffer->m_IndexAllocation.m_Size;
		m_CollapsedMesh->reserve( numVerts );
		m_Faces->reserve( numIndices/3 );

		BYTE* pVertex = NULL;
		static CHashString vp3(_T("MESH_STREAM_VERTEX_POSITION3"));
		int positionoffset = 0;
		int uvoffset = 0;
		int normalOffset = 0;
		int vertexFormatSize = renderbuffer->m_VertexAllocation.VertexFormat.size();
		for( int i = 0; i < vertexFormatSize; i++ )
		{
			ChannelDesc desc = renderbuffer->m_VertexAllocation.VertexFormat[i];
			if( desc.ChannelHash == vp3.GetUniqueID() )
			{
				break;
			}
			positionoffset += renderbuffer->m_VertexAllocation.VertexFormat[ i ].Stride;
		}
		static CHashString tc1(_T("MESH_STREAM_VERTEX_TEXTURECOORD1"));
		for( int i = 0; i < vertexFormatSize; i++ )
		{
			ChannelDesc desc = renderbuffer->m_VertexAllocation.VertexFormat[i];
			if( desc.ChannelHash == tc1.GetUniqueID() )
			{
				break;
			}
			uvoffset += renderbuffer->m_VertexAllocation.VertexFormat[ i ].Stride;
		}
		static CHashString nrm(_T("MESH_STREAM_VERTEX_NORMAL"));
		for( int i = 0; i < vertexFormatSize; i++ )
		{
			ChannelDesc desc = renderbuffer->m_VertexAllocation.VertexFormat[i];
			if( desc.ChannelHash == nrm.GetUniqueID() )
			{
				break;
			}
			normalOffset += renderbuffer->m_VertexAllocation.VertexFormat[ i ].Stride;
		}

		if( !onlyIndices )
		{
			IVertexBufferObject * vb = renderbuffer->m_VertexBufferInterface;
			if( !vb )return;
			ParameterizationVertex curVert;
			hr = vb->Lock( renderbuffer->m_VertexAllocation.m_Offset, renderbuffer->m_VertexAllocation.m_Size, (void**)&pVertex );
			if( pVertex != NULL )
			{
				for( int i=0; i < numVerts; i++ )
				{
					BYTE* posbyte = pVertex;
					BYTE* uvByte = pVertex;
					BYTE* normalByte = pVertex;
					normalByte += normalOffset;
					posbyte += positionoffset;
					uvByte += uvoffset;
					float* curpos = (float *)posbyte;
					float * curUv = (float*)uvByte;
					float * normal = (float*)normalByte;
					curVert.originalPosition.x = curpos[0];
					curVert.originalPosition.y = curpos[1];
					curVert.originalPosition.z = curpos[2];
					tmin.x = min( curVert.originalPosition.x, tmin.x );
					tmin.y = min( curVert.originalPosition.y, tmin.y );
					tmin.z = min( curVert.originalPosition.z, tmin.z );
					tmax.x = max( curVert.originalPosition.x, tmax.x );
					tmax.y = max( curVert.originalPosition.y, tmax.y );
					tmax.z = max( curVert.originalPosition.z, tmax.z );
					curVert.generatedU = curUv[ 0 ];
					curVert.generatedV = curUv[ 1 ];
					curVert.originalU = curUv[ 0 ];
					curVert.originalV = curUv[ 1 ];
					curVert.normal.x = normal[0];
					curVert.normal.y = normal[1];
					curVert.normal.z = normal[2];

					m_CollapsedMesh->push_back( curVert );
					pVertex += renderbuffer->m_VertexBufferInterface->GetStride();
				}
			}
			vb->Unlock();
		}
		//do index buffers
		IIndexBuffer * ib = renderbuffer->m_IndexBufferInterface;
		if( !ib )return;
		TriangleFace curTri;
		IMaterial * mat = renderbuffer->m_Material;
		DWORD textureID = 0;
		if( mat )
		{
			IBaseTextureObject * basetexture = mat->GetTexture(0,0);
			textureID = (DWORD)basetexture;
		}
		hr = ib->Lock( renderbuffer->m_IndexAllocation.m_Offset, renderbuffer->m_IndexAllocation.m_Size, (void**)&pVertex );
		if( pVertex != NULL )
		{
			for( int i=0; i < numIndices/3; i++ )
			{
				BYTE* posbyte = pVertex;
				short * pShort = (short*)pVertex;
				curTri.index[ 0 ] = pShort[0];
				curTri.index[ 1 ] = pShort[1];								
				curTri.index[ 2 ] = pShort[2];		
				curTri.m_Texture = textureID;
				pVertex += sizeof( short )*3;
				m_Faces->push_back( curTri );
			}
		}
		ib->Unlock();
		//Add texture if not already there
		ITextureObject * texture = NULL;
		IMaterial * curMat = renderbuffer->m_Material;
		if( curMat )
		{
			IBaseTextureObject * baseTexture = curMat->GetTexture(0,0);
			texture = dynamic_cast< ITextureObject* >( baseTexture );
			if( texture )
			{
				m_MaterialTextures.insert( texture );
			}
		}
	}
}