bool MeshLoader::Load(const std::string& name) { bool calculateTangents = true; bool calculateNormals = true; char msg[256]; sprintf_s(msg, "# Loading mesh %s", name.c_str()); LOG(msg); ModelOBJ* obj = new ModelOBJ(); if(!obj->import((Filesystem::Instance().GetMediaRoot()+name).c_str(), calculateNormals)) { char msg[256]; sprintf_s(msg, "# Problem loading mesh %s!", name.c_str()); LOG_ERROR(msg); return false; } MeshPtr mesh(new Mesh(name)); mesh->m_NumVertices = obj->getNumberOfVertices(); mesh->m_NumIndices = obj->getNumberOfIndices(); mesh->m_Vertices.reset(new Vertex[mesh->m_NumVertices]); mesh->m_Indices.reset(new uint32[mesh->m_NumIndices]); const ModelOBJ::Vertex* vertices = obj->getVertexBuffer(); for(uint32 i=0; i<mesh->m_NumVertices; i++) { memcpy(&(mesh->m_Vertices[i].position), vertices[i].position, sizeof(float)*3); memcpy(&(mesh->m_Vertices[i].normal), vertices[i].normal, sizeof(float)*3); memcpy(&(mesh->m_Vertices[i].texCoords), vertices[i].texCoord, sizeof(float)*2); } const std::vector<uint16>& indices = obj->getIndexBuffer(); for(uint32 i=0; i<mesh->m_NumIndices; i++) { mesh->m_Indices[i] = indices[i]; } if(calculateTangents) CalculateTangents(mesh); delete obj; m_Meshes[name] = mesh; sprintf_s(msg, "# Done loading mesh! Vertices# %i, Indices#: %i, HasTangents: %i\n", mesh->m_NumVertices, mesh->m_NumIndices, (int)calculateTangents); LOG(msg); return true; }
void LoadModel(const char *pszFilename) { // Import the OBJ file and normalize to unit length. SetCursor(LoadCursor(0, IDC_WAIT)); if (!g_model.import(pszFilename)) { SetCursor(LoadCursor(0, IDC_ARROW)); throw std::runtime_error("Failed to load model."); } //g_model.normalize(); }
bool Convert(const char* pFilePath) { ModelOBJ model; FILE* pOutput = 0; SubMesh subMesh; static char pOutPath[MAX_PATH]; //open mesh if (!model.import(pFilePath)) { printf("Failed to open file '%s'!\n", pFilePath); return false; } //open output file ConvertFileName(pFilePath, pOutPath); if (fopen_s(&pOutput, pOutPath, "wb") != 0) { printf("Could not open output file '%s'!\n", pOutPath); return false; } //write basic info int verticesCount = model.getNumberOfVertices(); int indiciesCount = model.getNumberOfIndices(); int subMeshesCount = model.getNumberOfMeshes(); printf("Veritces count: %i\n", verticesCount); printf("Indicies count: %i\n", indiciesCount); printf("Submeshes count: %i\n", subMeshesCount); fwrite("nfm", 4, 1, pOutput); //signature fwrite(&verticesCount, sizeof(int), 1, pOutput); fwrite(&indiciesCount, sizeof(int), 1, pOutput); fwrite(&subMeshesCount, sizeof(int), 1, pOutput); //write verticies XVertex* pVertices = (XVertex*)malloc(verticesCount * sizeof(XVertex)); const ModelOBJ::Vertex* pModelVertices = model.getVertexBuffer(); for (int i = 0; i < verticesCount; i++) { //vertex position pVertices[i].pos[0] = pModelVertices[i].position[0]; pVertices[i].pos[1] = pModelVertices[i].position[1]; pVertices[i].pos[2] = -pModelVertices[i].position[2]; //vertex texture coordinates pVertices[i].texCoord[0] = pModelVertices[i].texCoord[0]; pVertices[i].texCoord[1] = pModelVertices[i].texCoord[1]; //vertex normal if (ISNAN(pModelVertices[i].normal[0]) || ISNAN(pModelVertices[i].normal[1]) || ISNAN(pModelVertices[i].normal[2])) { pVertices[i].normal[0] = 0; pVertices[i].normal[1] = 127; pVertices[i].normal[2] = 0; } else { pVertices[i].normal[0] = FloatToChar(pModelVertices[i].normal[0]); pVertices[i].normal[1] = FloatToChar(pModelVertices[i].normal[1]); pVertices[i].normal[2] = FloatToChar(-pModelVertices[i].normal[2]); } //vertex tangent if (ISNAN(pModelVertices[i].tangent[0]) || ISNAN(pModelVertices[i].tangent[1]) || ISNAN(pModelVertices[i].tangent[2])) { pVertices[i].tangent[0] = 127; pVertices[i].tangent[1] = 0; pVertices[i].tangent[2] = 0; } else { pVertices[i].tangent[0] = FloatToChar(pModelVertices[i].tangent[0]); pVertices[i].tangent[1] = FloatToChar(pModelVertices[i].tangent[1]); pVertices[i].tangent[2] = FloatToChar(-pModelVertices[i].tangent[2]); } pVertices[i].normal[3] = 0; pVertices[i].tangent[3] = 0; } fwrite(pVertices, sizeof(XVertex), verticesCount, pOutput); free(pVertices); //write indicies fwrite(model.getIndexBuffer(), sizeof(int), indiciesCount, pOutput); //write submeshes UINT triCounter = 0; for (int i = 0; i < model.getNumberOfMeshes(); i++) { ModelOBJ::Mesh srcMesh = model.getMesh(i); subMesh.indexOffset = srcMesh.startIndex; subMesh.triangleCount = srcMesh.triangleCount; ZeroMemory(subMesh.materialName, MAT_NAME_MAX_LENGTH); strcpy(subMesh.materialName, srcMesh.pMaterial->name.c_str()); fwrite(&subMesh, sizeof(SubMesh), 1, pOutput); } //close output file fclose(pOutput); //generate material files int matCount = model.getNumberOfMaterials(); for (int i = 0; i < matCount; i++) { const ModelOBJ::Material& mat = model.getMaterial(i); char fileName[MAX_PATH]; ExtractFileDir(pFilePath, fileName); strcat(fileName, "..\\Materials\\"); strcat(fileName, mat.name.c_str()); strcat(fileName, ".cfg"); //check if material already exists if (FileExists(fileName)) { printf("Material %s already exists. Skipping cfg generation...\n", mat.name.c_str()); continue; } FILE* pMatFile = fopen(fileName, "w"); fprintf(pMatFile, "Layers = \n(\n\t{\n"); if (mat.colorMapFilename.length()) fprintf(pMatFile, "\t\tDiffuseTexture = \"%s\"\n", mat.colorMapFilename.c_str()); if (mat.bumpMapFilename.length()) fprintf(pMatFile, "\t\tNormalTexture = \"%s\"\n", mat.bumpMapFilename.c_str()); fprintf(pMatFile, "\t}\n);"); fclose(pMatFile); } }