⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ase.cpp

📁 Zodspark is a Car racing simulation game built on VC++ using opengl library
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//***********************************************************************//
//																		 //
//		- "Talk to me like I'm a 3 year old!" Programming Lessons -		 //
//                                                                       //
//		$Author:		DigiBen		digiben@gametutorials.com			 //
//																		 //
//		$Program:		ASE Loader										 //
//																		 //
//		$Description:	Demonstrates how to load a .Ase file format		 //
//																		 //
//		$Date:			10/13/01										 //
//																		 //
//***********************************************************************//

#include "Main.h"
#include "Ase.h"


///////////////////////////////// IMPORT ASE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
/////	This function is called to load in an .ase file by the file name
/////
///////////////////////////////// IMPORT ASE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

bool CLoadASE::ImportASE(t3DModel *pModel, char *strFileName)
{
	char strMessage[255] = {0};				// This will be used for error messages

	// Make sure we have a valid model and file name
	if(!pModel || !strFileName) return false;

	// Here we open the desired file for read only and return the file pointer
	m_FilePointer = fopen(strFileName, "r");

	// Check to make sure we have a valid file pointer
	if(!m_FilePointer) {
		// Create an error message for the attempted file
		sprintf(strMessage, "Unable to find or open the file: %s", strFileName);
		MessageBox(NULL, strMessage, "Error", MB_OK);
		return false;
	}

	// Now that we have a valid file and it's open, let's read in the info!
	ReadAseFile(pModel);

	// Now that we have the file read in, let's compute the vertex normals for lighting
	ComputeNormals(pModel);

	// Close the .ase file that we opened
	fclose(m_FilePointer);

	// Return a success!
	return true;
}


///////////////////////////////// READ ASE FILE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
/////	This function reads the data for every object and it's associated material
/////
///////////////////////////////// READ ASE FILE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

void CLoadASE::ReadAseFile(t3DModel *pModel)
{
	tMaterialInfo newMaterial = {0};		// This will be used to push on a new material
	t3DObject     newObject   = {0};		// This will be used to push on a new object
	int i = 0;	

	// This function is the head honcho for reading in the .ase data and information.
	// What happens is, we go through the whole file and count the objects, then we
	// rewind the file pointer and go through the whole file again and count the materials.
	// Then we go through the file for each material and each object and read the data in.

	// This will return the number of objects stored in the .ase file
	pModel->numOfObjects   = GetObjectCount();

	// This will return the number of materials stored in the .ase file
	pModel->numOfMaterials = GetMaterialCount();

	// Go through all the materials and fill in their data and info
	for(i = 0; i < pModel->numOfMaterials; i++)
	{
		// Add a new material to our list of materials using the STL "vector" class
		pModel->pMaterials.push_back(newMaterial);

		// Get the material info for the current material.  We add 1 because we
		// want to start at 1 and i starts at 0.
		GetTextureInfo(&(pModel->pMaterials[i]), i + 1);
	}

	// Go through all the objects and fill in their data and info
	for(i = 0; i < pModel->numOfObjects; i++)
	{	
		// Add a new object to our list of objects using the STL "vector" class
		pModel->pObject.push_back(newObject);

		// Set the material ID to -1 to initialize it.  This will be changed
		// if there is a texture/material assigned to this object.
		pModel->pObject[i].materialID = -1;

		// Move the file pointer to the desired object.  We add one because our
		// object count starts at 1 and i starts at 0
		MoveToObject(i + 1);

		// Find out the number of vertices, faces and texture coordinates for this object, 
		// then allocate the memory needed to store that amount that needs to be read in.
		ReadObjectInfo(&(pModel->pObject[i]), i + 1);
		
		// Read the vertices, face indices and texture coordinates for this object
		ReadObjectData(pModel, &(pModel->pObject[i]), i + 1);
	}
}


///////////////////////////////// GET OBJECT COUNT \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
/////	This function returns the total object count in the .ase file
/////
///////////////////////////////// GET OBJECT COUNT \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

int CLoadASE::GetObjectCount()
{
	char strWord[255] = {0};
	int objectCount = 0;

	// This function goes through the file and reads each word.  When it
	// finds an OBJECT tag, it increases the object count.

	// Go to the beginning of the file.  Then we can start fresh and get a good count.
	rewind(m_FilePointer);

	// Go through the whole file and end when we reached the END
	while (!feof(m_FilePointer))
	{
		// Get each word in the file
		fscanf(m_FilePointer, "%s", &strWord);

		// Check if we hit the start of an object
		if (!strcmp(strWord, OBJECT))
		{
			// Increase the current object we are at 
			objectCount++;
		}
		else
		{
			// Go to the next line
			fgets(strWord, 100, m_FilePointer);
		}
	}

	// Return the object count in the .ase file
	return objectCount;
}


///////////////////////////////// GET MATERIAL COUNT \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
/////	This function returns the total material count for the .ase file
/////
///////////////////////////////// GET MATERIAL COUNT \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

int CLoadASE::GetMaterialCount()
{
	char strWord[255] = {0};
	int materialCount = 0;

	// This function goes through the file and reads each word.  When it
	// finds the MATERIAL_COUNT tag, it reads the material count.

	// Go to the beginning of the file
	rewind(m_FilePointer);

	// GO through the whole file until we hit the end
	while (!feof(m_FilePointer))
	{
		// Get each word in the file
		fscanf(m_FilePointer, "%s", &strWord);

		// Check if we hit the start of an object
		if (!strcmp(strWord, MATERIAL_COUNT))
		{
			// Read in the material count
			fscanf(m_FilePointer, "%d", &materialCount);

			// Return the material count
			return materialCount;
		}
		else
		{
			// Go to the next line
			fgets(strWord, 100, m_FilePointer);
		}
	}

	// Return NO materials if we get here
	return 0;
}


///////////////////////////////// GET TEXTURE INFO \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
/////	This function reads in the information about the desired material
/////
///////////////////////////////// GET TEXTURE INFO \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

void CLoadASE::GetTextureInfo (tMaterialInfo *pTexture, int desiredMaterial)
{
	char strWord[255] = {0};
	int materialCount= 0;
	
	// In this function we check for an MATERIAL tag, then see if it's the
	// material we want by the desiredMaterial number.  If so we start reading it in.

	// Go to the beginning of the file
	rewind(m_FilePointer);

	// Go through the whole file until we reach the end
	while (!feof(m_FilePointer))
	{
		// Get each word from the file
		fscanf(m_FilePointer, "%s", &strWord);

		// Check if we hit the start of an object
		if (!strcmp(strWord, MATERIAL))
		{
			// Increase the current object we are at 
			materialCount++;

			// Check if it's the one we want to stop at, if so break
			if(materialCount == desiredMaterial)
				break;
		}
		else
		{
			// Go to the next line
			fgets(strWord, 100, m_FilePointer);
		}
	}

	// Now we are at the material we want, so let's read it's data in

	// Go through the rest of the file until we hit the end
	while (!feof(m_FilePointer))
	{
		// Get each word from the file
		fscanf(m_FilePointer, "%s", &strWord);

		// If we found a MATERIAL tag stop because we went to far
		if (!strcmp (strWord, MATERIAL))
		{
			// We hit a new material, so we need to stop
			return;
		}
		// If we hit a MATERIAL_COLOR tag, we need to get the material's color
		else if (!strcmp(strWord, MATERIAL_COLOR))
		{
			// Get the material RGB color of the object
			fscanf(m_FilePointer, " %f %f %f", &(pTexture->fColor[0]), 
											   &(pTexture->fColor[1]), 
											   &(pTexture->fColor[2]));
		}
		// If we hit a TEXTURE tag, we need to get the texture's name
		else if (!strcmp(strWord, TEXTURE))
		{
			// Get the file name of the texture
			GetTextureName(pTexture);
		}
		// If we hit a MATERIAL_NAME tag, we need to get the material's name
		else if (!strcmp(strWord, MATERIAL_NAME))
		{
			// Get the material name of the object
			GetMaterialName(pTexture);
		}
		// If we hit a UTILE tag, we need to get the U tile ratio
		else if(!strcmp(strWord, UTILE))
		{
			// Read the U tiling for the U coordinates of the texture
			pTexture->uTile = ReadFloat();
		}
		// If we hit a VTILE tag, we need to get the V tile ratio
		else if(!strcmp(strWord, VTILE))
		{
			// Read the V tiling for the V coordinates of the texture
			pTexture->vTile = ReadFloat();
		}
		// Otherwise ignore the data and read past it
		else
		{
			// Go to the next line
			fgets (strWord, 100, m_FilePointer);
		}
	}
}


///////////////////////////////// MOVE TO OBJECT \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
/////	This function moves the file pointer in the .ase file to the desired object
/////
///////////////////////////////// MOVE TO OBJECT \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

void CLoadASE::MoveToObject (int desiredObject)
{
	char strWord[255] = {0};
	int objectCount = 0;

	// This function takes the number of the desired object to move to.
	// We read through the file from the beginning and increase a counter.
	// When that counter gets to the desiredObject we then stop.  Now the file
	// pointer can read the object data from that specific object

	// Go to the beginning of the file
	rewind(m_FilePointer);

	// Go through the whole file until we reach the end
	while(!feof(m_FilePointer))
	{
		// Get each word
		fscanf(m_FilePointer, "%s", &strWord);

		// Check if we hit the start of an object
		if(!strcmp(strWord, OBJECT))
		{
			// Increase the current object we are at 
			objectCount++;

			// Check if it's the one we want to stop at, if so stop reading
			if(objectCount == desiredObject)
				return;
		}
		else
		{
			// Go to the next line and skip this current line
			fgets(strWord, 100, m_FilePointer);
		}
	}
}


///////////////////////////////// READ FLOAT \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
/////	This function reads in and returns a float from the .ase file
/////
///////////////////////////////// READ FLOAT \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

float CLoadASE::ReadFloat()
{
	float v = 0.0f;

	// Read in a float
	fscanf(m_FilePointer, " %f", &v);

	// Return the float
	return v;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -