diff --git a/src/main.cpp b/src/main.cpp index 4b5dce0..70a5c00 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,5 @@ #include +#include #include #include @@ -10,17 +11,47 @@ int main(int argc, char **argv) if (argc == 1) { - printf("Usage: md2tomesh.exe [inputfile]\n\n"); + printf("Usage: md2tomesh.exe [--scale=] [inputfile]\n\n"); return 1; } - std::string file = argv[1]; + // input file is always the last argument + std::string file = argv[argc - 1]; + + // default option values + float scaleFactor = 1.0f; + + // find any options and update their values + for (int i = 1; i < argc - 1; ++i) + { + std::string arg = argv[i]; + + if (arg.substr(0, 8) == "--scale=") + { + // scale factor + + if (arg.length() == 8) + { + printf("Missing scale factor.\n"); + return 1; + } + + scaleFactor = (float)atof(arg.substr(8).c_str()); + + if (scaleFactor == 0.0f) + { + printf("Invalid or 0.0 scale factor.\n"); + return 1; + } + } + } + std::string extension; try { extension = file.substr(file.find_last_of('.'), std::string::npos); - for (int i = 0; i < extension.size(); ++i) + for (unsigned int i = 0; i < extension.size(); ++i) extension[i] = tolower(extension[i]); } catch (std::exception &e) @@ -42,7 +73,7 @@ int main(int argc, char **argv) printf("Error loading MD2 file.\n\n"); return 1; } - if (!md2->ConvertToMesh(meshFile)) + if (!md2->ConvertToMesh(meshFile, scaleFactor)) { printf("Error converting MD2 to MESH.\n\n"); return 1; diff --git a/src/md2/md2.cpp b/src/md2/md2.cpp index 28278c0..aaec806 100644 --- a/src/md2/md2.cpp +++ b/src/md2/md2.cpp @@ -294,7 +294,7 @@ bool Md2::Load(const std::string &file) return true; } -bool Md2::ConvertToMesh(const std::string &file) +bool Md2::ConvertToMesh(const std::string &file, float scaleFactor) { FILE *fp = fopen(file.c_str(), "wb"); if (fp == NULL) @@ -310,7 +310,7 @@ bool Md2::ConvertToMesh(const std::string &file) KeyFrame *frame = keyFramesChunk->AddFrame(); for (int j = 0; j < m_numVertices; ++j) - frame->vertices[j] = m_frames[i].vertices[j]; + frame->vertices[j] = m_frames[i].vertices[j] * scaleFactor; for (int j = 0; j < m_numVertices; ++j) frame->normals[j] = m_frames[i].normals[j]; @@ -342,7 +342,7 @@ bool Md2::ConvertToMesh(const std::string &file) if (m_animations.size() > 0) { AnimationsChunk *animationsChunk = new AnimationsChunk(); - for (long i = 0; i < m_animations.size(); ++i) + for (unsigned int i = 0; i < m_animations.size(); ++i) { AnimationSequence a; diff --git a/src/md2/md2.h b/src/md2/md2.h index 0b17143..0cf495a 100644 --- a/src/md2/md2.h +++ b/src/md2/md2.h @@ -73,7 +73,7 @@ public: void Release(); bool Load(const std::string &file); - bool ConvertToMesh(const std::string &file); + bool ConvertToMesh(const std::string &file, float scaleFactor); int GetNumFrames() { return m_numFrames; } int GetNumVertices() { return m_numVertices; }