Advertisement

Runtime error help

Started by May 16, 2002 03:49 PM
-1 comments, last by llvllatrix 22 years, 9 months ago
Im trying to use milkshape files to create collision models. My load function keeps screwing up for some reason. Here's the code:
        
bool CHARACTER::LoadCollisionModel( const char *filename )
{
	//load and check file

	ifstream inputFile( filename, ios::in | ios::binary | ios::nocreate );
	if ( inputFile.fail())
	{
		return false;	// "Couldn't open the model file."

	}

	char pathTemp[PATH_MAX+1];
	int pathLength;
	for ( pathLength = strlen( filename ); pathLength--; ) {
		if ( filename[pathLength] == '/' || filename[pathLength] == '\\' ) {
			break;
		}
	}
	strncpy( pathTemp, filename, pathLength );

	inputFile.seekg( 0, ios::end );
	long fileSize = inputFile.tellg();
	inputFile.seekg( 0, ios::beg );

	byte *pBuffer = new byte[fileSize];
	inputFile.read( pBuffer, fileSize );
	inputFile.close();

	const byte *pPtr = pBuffer;
	
	//check milkshape header

	MS3DHeader *pHeader = ( MS3DHeader* )pPtr;
	pPtr += sizeof( MS3DHeader );

	if ( strncmp( pHeader->m_ID, "MS3D000000", 10 ) != 0 )
	{
		return false; // "Not a valid Milkshape3D model file."

	}

	if ( pHeader->m_version < 3 || pHeader->m_version > 4 )
	{
		return false; // "Unhandled file version. Only Milkshape3D Version 1.3 and 1.4 is supported." );

	}

	int nVertices = *( word* )pPtr; 
	int m_numVertices = nVertices;
	VertexMS3D *m_pVertices = new VertexMS3D[nVertices];
	pPtr += sizeof( word );
	
	int i;
	for ( i = 0; i < nVertices; i++ )
	{
		MS3DVertex *pVertex = ( MS3DVertex* )pPtr;
		m_pVertices[i].m_boneID = pVertex->m_boneID;
		memcpy( m_pVertices[i].m_location, pVertex->m_vertex, sizeof( float )*3 );
		pPtr += sizeof( MS3DVertex );
	}

	int nTriangles = *( word* )pPtr;
	int m_numTriangles = nTriangles;
	TriangleMS3D *m_pTriangles = new TriangleMS3D[nTriangles];
	pPtr += sizeof( word );

	for ( i = 0; i < nTriangles; i++ )
	{
		MS3DTriangle *pTriangle = ( MS3DTriangle* )pPtr;
		int vertexIndices[3] = { pTriangle->m_vertexIndices[0], pTriangle->m_vertexIndices[1], pTriangle->m_vertexIndices[2] };
		memcpy( m_pTriangles[i].m_vertexIndices, vertexIndices, sizeof( int )*3 );
		pPtr += sizeof( MS3DTriangle );
	}

	int nGroups = *( word* )pPtr;
	pPtr += sizeof( word );
	
	//Establish Groups

	CollisionGroup * pCollisionGroupsCurrent;
	pCollisionGroupsFirst = new CollisionGroup;
	pCollisionGroupsCurrent = pCollisionGroupsFirst;	
	
	i = 0;
	while(true)
	{
		pPtr += sizeof( byte );	// flags

		pPtr += 32;				// name


		word nTriangles = *( word* )pPtr;
		pPtr += sizeof( word );
		int *pTriangleIndices = new int[nTriangles];

		//Establish Triangles

		pCollisionGroupsCurrent->NumTriangles = nTriangles;
		pCollisionGroupsCurrent->pTriFirst = new(TriangleList);
		TriangleList * pTriCurrent = pCollisionGroupsCurrent->pTriFirst;
		
		//Loop Through All triangles

		int j = 0;
		while(true)
		{
			pTriangleIndices[j] = *( word* )pPtr;
			pPtr += sizeof( word );

			//Assign the joint id

			if (j == 0)
				pCollisionGroupsCurrent->m_boneID = m_pVertices[m_pTriangles[pTriangleIndices[0]].m_vertexIndices[0]].m_boneID;

			//Loop through each point of a triangle

			for (int k = 0; k < 3; k++)
			{
				//Start at the top of the vertex list

				//Check first element

				if(pCollisionGroupsCurrent->pVertFirst == NULL)
				{
					pCollisionGroupsCurrent->pVertFirst = new(VertexList);
					pCollisionGroupsCurrent->pVertFirst->m_location[0] = m_pVertices[m_pTriangles[pTriangleIndices[j]].m_vertexIndices[k]].m_location[0];
					pCollisionGroupsCurrent->pVertFirst->m_location[1] = m_pVertices[m_pTriangles[pTriangleIndices[j]].m_vertexIndices[k]].m_location[1];
					pCollisionGroupsCurrent->pVertFirst->m_location[2] = m_pVertices[m_pTriangles[pTriangleIndices[j]].m_vertexIndices[k]].m_location[2];
					pTriCurrent->m_vertexIndices[k] = 0;					
				}
				else
				{
					VertexList * pVertCurrent = pCollisionGroupsCurrent->pVertFirst;
					int l = 0;
					while(1)
					{
					
						//Check middle elements

						if (pVertCurrent->m_location == m_pVertices[m_pTriangles[pTriangleIndices[j]].m_vertexIndices[k]].m_location)
						{
							break;
						}
						//Check append

						if (pVertCurrent->pNext == NULL)
						{
							pVertCurrent->pNext = new(VertexList);
							pVertCurrent = pVertCurrent->pNext;
					//Screws up right here

                                        pVertCurrent->m_location[0] = m_pVertices[m_pTriangles[pTriangleIndices[j]].m_vertexIndices[k]].m_location[0];
							pVertCurrent->m_location[1] = m_pVertices[m_pTriangles[pTriangleIndices[j]].m_vertexIndices[k]].m_location[1];
							pVertCurrent->m_location[2] = m_pVertices[m_pTriangles[pTriangleIndices[j]].m_vertexIndices[k]].m_location[2];
							l++;
							break;
						}
						//Advance the list

						pVertCurrent = pVertCurrent->pNext;
						l++;
					}
					pTriCurrent->m_vertexIndices[k] = l;
				}
			}
			
			j++;
			if(j > nTriangles)
			{
				break;
			}
			pTriCurrent->pNext = new(TriangleList);
			pTriCurrent = pTriCurrent->pNext;
			pTriCurrent->pNext = NULL;			
		}

		i++;
		if(i > nGroups)
		{
			break;
		}		
		pCollisionGroupsCurrent->pNext = new CollisionGroup;
		pCollisionGroupsCurrent = pCollisionGroupsCurrent->pNext;
		pCollisionGroupsCurrent->pNext = NULL;
		pPtr += sizeof( char );		
	}

	delete[] pBuffer;

	return true;
}
        
Anyone know why? As far as i know the problem occurs when the index for the arrays i use to get the points from in the groups section goes to -8435169... and so on. Any ideas? [edited by - llvllatrix on May 16, 2002 4:50:27 PM] [edited by - llvllatrix on May 16, 2002 4:51:52 PM] [edited by - llvllatrix on May 16, 2002 4:52:43 PM]

This topic is closed to new replies.

Advertisement