/** 载入MD2文件 */ bool CMD2Loader::ImportMD2(t3DModel *pModel, char *strFileName, char *strTexture) { char strMessage[255] = {0}; /** 打开文件 */ m_FilePointer = fopen(strFileName, "rb"); /** 检查文件指针 */ if(!m_FilePointer) { sprintf(strMessage, "打开文件: %s错误!", strFileName); MessageBox(NULL, strMessage, "Error", MB_OK); return false; } /** 读取文件头 */ fread(&m_Header, 1, sizeof(tMd2Header), m_FilePointer); /** 检查版本号 */ if(m_Header.version != 8) { sprintf(strMessage, "Invalid file format (Version not 8): %s!", strFileName); MessageBox(NULL, strMessage, "Error", MB_OK); return false; } /** 读取MD2文件数据 */ ReadMD2Data(); /** 将MD2数据转换为模型结构 */ ConvertDataStructures(pModel); /** 如果具有纹理文件 */ if(strTexture) { /** 材质信息结构体 */ tMaterialInfo texture; /** 复制文件名 */ strcpy(texture.strFile, strTexture); /** 纹理ID号为0 */ texture.texureId = 0; texture.uTile = texture.uTile = 1; /** 模型材质数为1 */ pModel->numOfMaterials = 1; /** 添加材质信息 */ pModel->pMaterials.push_back(texture); } /** 释放内存 */ CleanUp(); return true; /**< 成功返回 */ }
bool CLoadMD2::ImportMD2(t3DModel *pModel, char *strFileName, char *strTexture) { char strMessage[255] = {0}; // Open the MD2 file in binary m_FilePointer = fopen(strFileName, "rb"); // Make sure we have a valid file pointer (we found the file) if(!m_FilePointer) { // Display an error message and don't load anything if no file was found sprintf(strMessage, "Unable to find the file: %s!", strFileName); MessageBox(NULL, strMessage, "Error", MB_OK); return false; } // Read the header data and store it in our m_Header member variable fread(&m_Header, 1, sizeof(tMd2Header), m_FilePointer); // Make sure the version is this crazy number '8' or else it's a bad egg if(m_Header.version != 8) { // Display an error message for bad file format, then stop loading. sprintf(strMessage, "Invalid file format (Version not 8): %s!", strFileName); MessageBox(NULL, strMessage, "Error", MB_OK); return false; } // Read in the model and animation data ReadMD2Data(); // Here we pass in our model structure so it can store and read Quake data // in our own model and object structure data. ConvertDataStructures(pModel); // If there is a valid texture name passed in, we want to set the texture data if(strTexture) { // Create a local material info structure tMaterialInfo texture; // Copy the name of the file into our texture file name variable strcpy(texture.strFile, strTexture); // Since there is only one texture for a .Md2 file, the ID is always 0 texture.texureId = 0; // The tile or scale for the UV's is 1 to 1 (but Quake saves off a 0-256 ratio) texture.uTile = texture.uTile = 1; // We only have 1 material for a model pModel->numOfMaterials = 1; // Add the local material info to our model's material list pModel->pMaterials.push_back(texture); } // Clean up after everything CleanUp(); // Return a success return true; }
bool CLoadMD2::ImportMD2(t3DModel *pModel, char *strFileName, char *strTexture) { char strMessage[255] = {0}; // Open the MD2 file in binary m_FilePointer = fopen(strFileName, "rb"); // Make sure we have a valid file pointer (we found the file) if(!m_FilePointer) { // Display an error message and don't load anything if no file was found sprintf(strMessage, "Unable to find the file: %s!", strFileName); MessageBox(NULL, strMessage, "Error", MB_OK); return false; } // Just like most file formats, there is a header that needs to be read // from the .MD2 format. If you look at the tMd2Header structure you will // find all the data that will be read in. It's nice to know up front about // the data that we will be reading. This makes it easy to just to large // binary reads using fread, instead of counting and reading chunks. // Read the header data and store it in our m_Header member variable fread(&m_Header, 1, sizeof(tMd2Header), m_FilePointer); // For some reason, .Md2 files MUST have a version of 8. I am not sure why, // but if it doesn't there is something wrong and the header was read in // incorrectly, or perhaps the file format is bad. if(m_Header.version != 8) { // Display an error message for bad file format, then stop loading sprintf(strMessage, "Invalid file format (Version not 8): %s!", strFileName); MessageBox(NULL, strMessage, "Error", MB_OK); return false; } // Now that we made sure the header had correct data, we want to read in the // rest of the data. Once the data is read in, we need to convert it to our structures. // Since we are only reading in the first frame of animation, there will only // be ONE object in our t3DObject structure, held within our pModel variable. ReadMD2Data(); // Here we pass in our model structure to it can store the read Quake data // in our own model and object structure data. ConvertDataStructures(pModel); // After we have read the whole MD2 file, we want to calculate our own vertex normals. ComputeNormals(pModel); // If there is a valid texture name passed in, we want to set the texture data. if(strTexture) { // Create a local material info structure tMaterialInfo texture; // Copy the name of the file into our texture file name variable strcpy(texture.strFile, strTexture); // Since there is only one texture for a .MD2 file, the ID is always 0 texture.texureId = 0; // The tile or scale for the UV's is 1 to 1 (but Quake saves off a 0-256 ratio) texture.uTile = texture.uTile = 1; // We only have 1 material for a model pModel->numOfMaterials = 1; // Add the local material info to our model's material list pModel->pMaterials.push_back(texture); } // Clean up after everything CleanUp(); // Return a success return true; }