added scale factor command line option

This commit is contained in:
gered 2012-10-19 18:53:25 -04:00
parent 598ce7bb54
commit 66a825a117
3 changed files with 39 additions and 8 deletions

View file

@ -1,4 +1,5 @@
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <exception>
@ -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=<scale factor>] [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;

View file

@ -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;

View file

@ -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; }