www.pudn.com > 3ds-load.rar > 3ds.cpp


// 3ds.cpp: implementation of the C3ds class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "3ds.h" 
#include  
#include "gamehead.h" 
#include "Frustum.h" 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
//#define INT_MAX 2147483647  
//#define INT_MIN 2147483647-1  
#define  FLT_MAX 3.402823466e+38F  
#define  FLT_MIN 1.175494351e-38F  
C3DS::C3DS() 
{ 
//	m_Model.numOfMaterials = 0; 
//	m_Model.numOfObjects = 0; 
} 
 
C3DS::~C3DS() 
{ 
	for(int i = 0;ivObject.push_back(obj); 
 
			} 
			break; 
 
			//--------------- OBJ_TRIMESH --------------- 
			// Description: Triangular mesh, contains chunks for 3d mesh info 
			// Chunk ID: 4100 (hex) 
			// Chunk Lenght: 0 + sub chunks 
			//------------------------------------------- 
			case 0x4100: 
			break; 
			 
			//--------------- TRI_VERTEXL --------------- 
			// Description: Vertices list 
			// Chunk ID: 4110 (hex) 
			// Chunk Lenght: 1 x unsigned short (number of vertices)  
			//             + 3 x float (vertex coordinates) x (number of vertices) 
			//             + sub chunks 
			//------------------------------------------- 
			case 0x4110:  
				fread (&l_qty, sizeof (unsigned short), 1, l_file); 
				pObj = &pModel->vObject[pModel->vObject.size()-1]; 
                pObj->numOfVerts = l_qty; 
				pObj->pVerts = new tVector3[l_qty]; 
                for (i=0; ipVerts[i].x, sizeof(float), 1, l_file); 
					fread (&pObj->pVerts[i].z, sizeof(float), 1, l_file); 
                    fread (&pObj->pVerts[i].y, sizeof(float), 1, l_file); 
			//		pObj->pVerts[i].x = -pObj->pVerts[i].x; 
				} 
				break; 
 
			//--------------- TRI_FACEL1 ---------------- 
			// Description: Polygons (faces) list 
			// Chunk ID: 4120 (hex) 
			// Chunk Lenght: 1 x unsigned short (number of polygons)  
			//             + 3 x unsigned short (polygon points) x (number of polygons) 
			//             + sub chunks 
			//------------------------------------------- 
			case 0x4120://face 
				fread (&l_qty, sizeof (unsigned short), 1, l_file); 
				pObj = &pModel->vObject[pModel->vObject.size()-1]; 
                pObj->numOfFaces = l_qty; 
				pObj->pFaces = new tFace[l_qty]; 
			//	memset(pObj->pFaces,0,sizeof(tFace)*l_qty); 
                for (i=0; ipFaces[i].a, sizeof (unsigned short), 1, l_file); 
					fread (&pObj->pFaces[i].b, sizeof (unsigned short), 1, l_file); 
					fread (&pObj->pFaces[i].c, sizeof (unsigned short), 1, l_file); 
					fread (&l_face_flags, sizeof (unsigned short), 1, l_file); 
					pObj->pFaces[i].matIndex = -1; 
				} 
                break; 
			case 0x4130:// This is found if the object has a material, either texture map or color 
			{ 
				char strMaterial[255]; 
				ReadString(l_file,strMaterial); 
				short n,nf; 
				fread (&n, sizeof(short), 1, l_file); 
				 
				pObj = &pModel->vObject[pModel->vObject.size()-1]; 
				// Go through all of the textures 
				for( i = 0; i < m_Model.vMaterials.size(); i++) 
				{ 
					// If the material we just read in matches the current texture name 
					if(strcmp(strMaterial, m_Model.vMaterials[i].strName) == 0) 
					{ 
						while (n-- > 0)  
						{	// Material number assigned to any face 
							// Numero di material assegnato ad ogni faccia 
							fread (&nf, sizeof(short), 1, l_file); 
							pObj->pFaces[nf].matIndex = i; 
						} 
						// Now that we found the material, check if it's a texture map. 
						// If the strFile has a string length of 1 and over it's a texture 
						if(strlen(pModel->vMaterials[i].strFile) > 0)  
						{ 
						// Set the object's flag to say it has a texture map to bind. 
						//	pObj->bHasTexture = true; 
						}	 
						break; 
					} 
				} 
				 
				break; 
			} 
			//------------- TRI_MAPPINGCOORS ------------ 
			// Description: Vertices list 
			// Chunk ID: 4140 (hex) 
			// Chunk Lenght: 1 x unsigned short (number of mapping points)  
			//             + 2 x float (mapping coordinates) x (number of mapping points) 
			//             + sub chunks 
			//------------------------------------------- 
			case 0x4140: 
				fread (&l_qty, sizeof (unsigned short), 1, l_file); 
				pObj = &pModel->vObject[pModel->vObject.size()-1]; 
				pObj->pTexVerts = new tVector2[l_qty]; 
				for (i=0; ipTexVerts[i].x, sizeof (float), 1, l_file); 
                    fread (&pObj->pTexVerts[i].y, sizeof (float), 1, l_file); 
				} 
                break; 
			case 0xAFFF://MATERIAL: 
			{ 
				tMaterialInfo newTexture ;//= {0}; 
			//	memset(&newTexture,0,sizeof(tMaterialInfo)); 
				newTexture.strFile[0] = '\0'; 
				newTexture.strName[0] = '\0'; 
				m_Model.vMaterials.push_back(newTexture); 
				break; 
			} 
			case 0xA000://// This holds the material name 
			{ 
				tMaterialInfo &matInfo = m_Model.vMaterials[m_Model.vMaterials.size()-1]; 
				ReadString(l_file,matInfo.strName); 
				break; 
			} 
			case 0xA300: //// This holds the file name of the texture 
			{ 
				tMaterialInfo &matInfo = m_Model.vMaterials[m_Model.vMaterials.size()-1]; 
				ReadString(l_file,matInfo.strFile); 
				break; 
			} 
			case 0xA200:	//texture map flag//	// This is the header for the texture info 
				break; 
			//----------- Skip unknow chunks ------------ 
			//We need to skip all the chunks that currently we don't use 
			//We use the chunk lenght information to set the file pointer 
			//to the same level next chunk 
			//------------------------------------------- 
			default: 
				 fseek(l_file, l_chunk_lenght-6, SEEK_CUR); 
        }  
	} 
	fclose (l_file); // Closes the file stream 
	return true; // Returns ok 
} 
bool C3DS::Init(char *sFileName) 
{ 
	if(!Load3DS(sFileName)) 
		return false; 
	for(int i = 0;i0) 
		{ 
			m_Model.vMaterials[i].cTxt.LoadBMP(m_Model.vMaterials[i].strFile); 
		} 
	} 
	return true; 
} 
void C3DS::Draw() 
{ 
	glEnable(GL_DEPTH_TEST); 
	glEnable(GL_TEXTURE_2D); 
	glColor3ub(255, 255, 255); 
	for(int i = 0;i