- added another option when converting model textures (keepsmallest)
This commit is contained in:
parent
39bf1b986e
commit
d0a95b1ff5
|
@ -863,10 +863,11 @@ void printParameterHelp(const char *argv0, bool foundInvalidArgs) {
|
||||||
printf("\n \t\tWhere x is an optional property name to filter (default shows all).");
|
printf("\n \t\tWhere x is an optional property name to filter (default shows all).");
|
||||||
printf("\n \t\texample: %s %s=DebugMode",argv0,GAME_ARGS[GAME_ARG_SHOW_INI_SETTINGS]);
|
printf("\n \t\texample: %s %s=DebugMode",argv0,GAME_ARGS[GAME_ARG_SHOW_INI_SETTINGS]);
|
||||||
|
|
||||||
printf("\n%s=x=textureformat\t\t\tconvert a model file or folder to the current g3d version format.",GAME_ARGS[GAME_ARG_CONVERT_MODELS]);
|
printf("\n%s=x=textureformat=keepsmallest\t\t\tconvert a model file or folder to the current g3d version format.",GAME_ARGS[GAME_ARG_CONVERT_MODELS]);
|
||||||
printf("\n \t\tWhere x is a filename or folder containing the g3d model(s).");
|
printf("\n \t\tWhere x is a filename or folder containing the g3d model(s).");
|
||||||
printf("\n \t\tWhere textureformat is an optional supported texture format to convert to (tga,bmp,png).");
|
printf("\n \t\tWhere textureformat is an optional supported texture format to convert to (tga,bmp,png).");
|
||||||
printf("\n \t\texample: %s %s=techs/megapack/factions/tech/units/castle/models/castle.g3d=png",argv0,GAME_ARGS[GAME_ARG_CONVERT_MODELS]);
|
printf("\n \t\tWhere keepsmallest is an optional flag indicating to keep original texture if its filesize is smaller than the converted format.");
|
||||||
|
printf("\n \t\texample: %s %s=techs/megapack/factions/tech/units/castle/models/castle.g3d=png=keepsmallest",argv0,GAME_ARGS[GAME_ARG_CONVERT_MODELS]);
|
||||||
|
|
||||||
printf("\n%s=x=textureformat\t\t\tconvert a texture file or folder to the format textureformat.",GAME_ARGS[GAME_ARG_CONVERT_TEXTURES]);
|
printf("\n%s=x=textureformat\t\t\tconvert a texture file or folder to the format textureformat.",GAME_ARGS[GAME_ARG_CONVERT_TEXTURES]);
|
||||||
printf("\n \t\tWhere x is a filename or folder containing the texture(s).");
|
printf("\n \t\tWhere x is a filename or folder containing the texture(s).");
|
||||||
|
@ -1813,6 +1814,12 @@ int glestMain(int argc, char** argv) {
|
||||||
printf("About to convert using texture format [%s]\n",textureFormat.c_str());
|
printf("About to convert using texture format [%s]\n",textureFormat.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool keepsmallest = false;
|
||||||
|
if(paramPartTokens.size() >= 4 && paramPartTokens[1].length() > 0) {
|
||||||
|
keepsmallest = (paramPartTokens[3] == "keepsmallest");
|
||||||
|
printf("About to convert using keepsmallest = %d\n",keepsmallest);
|
||||||
|
}
|
||||||
|
|
||||||
showCursor(true);
|
showCursor(true);
|
||||||
mainWindow->setUseDefaultCursorOnly(true);
|
mainWindow->setUseDefaultCursorOnly(true);
|
||||||
|
|
||||||
|
@ -1861,7 +1868,7 @@ int glestMain(int argc, char** argv) {
|
||||||
|
|
||||||
Model *model = renderer.newModel(rsGlobal);
|
Model *model = renderer.newModel(rsGlobal);
|
||||||
try {
|
try {
|
||||||
printf("About to load model [%s]\n",file.c_str());
|
printf("About to load model [%s] [%d of %lu]\n",file.c_str(),i,(long int)models.size());
|
||||||
model->load(file);
|
model->load(file);
|
||||||
modelLoadedOk = true;
|
modelLoadedOk = true;
|
||||||
}
|
}
|
||||||
|
@ -1871,7 +1878,7 @@ int glestMain(int argc, char** argv) {
|
||||||
|
|
||||||
if(modelLoadedOk == true) {
|
if(modelLoadedOk == true) {
|
||||||
printf("About to save converted model [%s]\n",file.c_str());
|
printf("About to save converted model [%s]\n",file.c_str());
|
||||||
model->save(file,textureFormat);
|
model->save(file,textureFormat,keepsmallest);
|
||||||
}
|
}
|
||||||
|
|
||||||
Renderer::getInstance().endModel(rsGlobal, model);
|
Renderer::getInstance().endModel(rsGlobal, model);
|
||||||
|
|
|
@ -140,7 +140,8 @@ public:
|
||||||
void loadV3(int meshIndex, const string &dir, FILE *f, TextureManager *textureManager,bool deletePixMapAfterLoad);
|
void loadV3(int meshIndex, const string &dir, FILE *f, TextureManager *textureManager,bool deletePixMapAfterLoad);
|
||||||
void load(int meshIndex, const string &dir, FILE *f, TextureManager *textureManager,bool deletePixMapAfterLoad);
|
void load(int meshIndex, const string &dir, FILE *f, TextureManager *textureManager,bool deletePixMapAfterLoad);
|
||||||
void save(int meshIndex, const string &dir, FILE *f, TextureManager *textureManager,
|
void save(int meshIndex, const string &dir, FILE *f, TextureManager *textureManager,
|
||||||
string convertTextureToFormat, std::map<string,int> &textureDeleteList);
|
string convertTextureToFormat, std::map<string,int> &textureDeleteList,
|
||||||
|
bool keepsmallest);
|
||||||
|
|
||||||
void deletePixels();
|
void deletePixels();
|
||||||
|
|
||||||
|
@ -194,9 +195,9 @@ public:
|
||||||
|
|
||||||
//io
|
//io
|
||||||
void load(const string &path,bool deletePixMapAfterLoad=false);
|
void load(const string &path,bool deletePixMapAfterLoad=false);
|
||||||
void save(const string &path, string convertTextureToFormat="");
|
void save(const string &path, string convertTextureToFormat,bool keepsmallest);
|
||||||
void loadG3d(const string &path,bool deletePixMapAfterLoad=false);
|
void loadG3d(const string &path,bool deletePixMapAfterLoad=false);
|
||||||
void saveG3d(const string &path, string convertTextureToFormat="");
|
void saveG3d(const string &path, string convertTextureToFormat,bool keepsmallest);
|
||||||
|
|
||||||
void setTextureManager(TextureManager *textureManager) {this->textureManager= textureManager;}
|
void setTextureManager(TextureManager *textureManager) {this->textureManager= textureManager;}
|
||||||
void deletePixels();
|
void deletePixels();
|
||||||
|
|
|
@ -153,6 +153,7 @@ string replaceAll(string& context, const string& from, const string& to);
|
||||||
bool removeFile(string file);
|
bool removeFile(string file);
|
||||||
bool renameFile(string oldFile, string newFile);
|
bool renameFile(string oldFile, string newFile);
|
||||||
void removeFolder(const string path);
|
void removeFolder(const string path);
|
||||||
|
long getFileSize(string filename);
|
||||||
|
|
||||||
int getScreenW();
|
int getScreenW();
|
||||||
int getScreenH();
|
int getScreenH();
|
||||||
|
|
|
@ -490,7 +490,8 @@ void Mesh::load(int meshIndex, const string &dir, FILE *f, TextureManager *textu
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mesh::save(int meshIndex, const string &dir, FILE *f, TextureManager *textureManager,
|
void Mesh::save(int meshIndex, const string &dir, FILE *f, TextureManager *textureManager,
|
||||||
string convertTextureToFormat, std::map<string,int> &textureDeleteList) {
|
string convertTextureToFormat, std::map<string,int> &textureDeleteList,
|
||||||
|
bool keepsmallest) {
|
||||||
MeshHeader meshHeader;
|
MeshHeader meshHeader;
|
||||||
memset(&meshHeader, 0, sizeof(struct MeshHeader));
|
memset(&meshHeader, 0, sizeof(struct MeshHeader));
|
||||||
|
|
||||||
|
@ -521,7 +522,7 @@ void Mesh::save(int meshIndex, const string &dir, FILE *f, TextureManager *textu
|
||||||
|
|
||||||
//maps
|
//maps
|
||||||
uint32 flag= 1;
|
uint32 flag= 1;
|
||||||
for(int i=0; i< meshTextureCount; ++i) {
|
for(int i = 0; i < meshTextureCount; ++i) {
|
||||||
if((meshHeader.textures & flag)) {
|
if((meshHeader.textures & flag)) {
|
||||||
uint8 cMapPath[mapPathSize];
|
uint8 cMapPath[mapPathSize];
|
||||||
memset(&cMapPath[0],0,mapPathSize);
|
memset(&cMapPath[0],0,mapPathSize);
|
||||||
|
@ -537,6 +538,9 @@ void Mesh::save(int meshIndex, const string &dir, FILE *f, TextureManager *textu
|
||||||
|
|
||||||
if(toLower(convertTextureToFormat) != "" &&
|
if(toLower(convertTextureToFormat) != "" &&
|
||||||
EndsWith(file, "." + convertTextureToFormat) == false) {
|
EndsWith(file, "." + convertTextureToFormat) == false) {
|
||||||
|
long originalSize = getFileSize(file);
|
||||||
|
long newSize = originalSize;
|
||||||
|
|
||||||
string fileExt = extractExtension(file);
|
string fileExt = extractExtension(file);
|
||||||
replaceAll(file, "." + fileExt, "." + convertTextureToFormat);
|
replaceAll(file, "." + fileExt, "." + convertTextureToFormat);
|
||||||
|
|
||||||
|
@ -544,34 +548,53 @@ void Mesh::save(int meshIndex, const string &dir, FILE *f, TextureManager *textu
|
||||||
|
|
||||||
if(convertTextureToFormat == "tga") {
|
if(convertTextureToFormat == "tga") {
|
||||||
texture->getPixmap()->saveTga(file);
|
texture->getPixmap()->saveTga(file);
|
||||||
|
newSize = getFileSize(file);
|
||||||
textureDeleteList[texture->getPath()] = textureDeleteList[texture->getPath()] + 1;
|
if(keepsmallest == false || newSize <= originalSize) {
|
||||||
|
textureDeleteList[texture->getPath()] = textureDeleteList[texture->getPath()] + 1;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
printf("Texture will not be converted, keeping smallest texture [%s]\n",texture->getPath().c_str());
|
||||||
|
textureDeleteList[file] = textureDeleteList[file] + 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if(convertTextureToFormat == "bmp") {
|
else if(convertTextureToFormat == "bmp") {
|
||||||
texture->getPixmap()->saveBmp(file);
|
texture->getPixmap()->saveBmp(file);
|
||||||
|
newSize = getFileSize(file);
|
||||||
textureDeleteList[texture->getPath()] = textureDeleteList[texture->getPath()] + 1;
|
if(keepsmallest == false || newSize <= originalSize) {
|
||||||
|
textureDeleteList[texture->getPath()] = textureDeleteList[texture->getPath()] + 1;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
printf("Texture will not be converted, keeping smallest texture [%s]\n",texture->getPath().c_str());
|
||||||
|
textureDeleteList[file] = textureDeleteList[file] + 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
//else if(convertTextureToFormat == "jpg") {
|
//else if(convertTextureToFormat == "jpg") {
|
||||||
// texture->getPixmap()->saveJpg(file);
|
// texture->getPixmap()->saveJpg(file);
|
||||||
//}
|
//}
|
||||||
else if(convertTextureToFormat == "png") {
|
else if(convertTextureToFormat == "png") {
|
||||||
texture->getPixmap()->savePng(file);
|
texture->getPixmap()->savePng(file);
|
||||||
|
newSize = getFileSize(file);
|
||||||
textureDeleteList[texture->getPath()] = textureDeleteList[texture->getPath()] + 1;
|
if(keepsmallest == false || newSize <= originalSize) {
|
||||||
|
textureDeleteList[texture->getPath()] = textureDeleteList[texture->getPath()] + 1;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
printf("Texture will not be converted, keeping smallest texture [%s]\n",texture->getPath().c_str());
|
||||||
|
textureDeleteList[file] = textureDeleteList[file] + 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw runtime_error("Unsuppoted texture format: [" + convertTextureToFormat + "]");
|
throw runtime_error("Unsupported texture format: [" + convertTextureToFormat + "]");
|
||||||
}
|
}
|
||||||
|
|
||||||
//textureManager->endTexture(texture);
|
//textureManager->endTexture(texture);
|
||||||
|
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Save, load new texture [%s] originalSize [%ld] newSize [%ld]\n",file.c_str(),originalSize,newSize);
|
||||||
|
|
||||||
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Save, load new texture [%s]\n",file.c_str());
|
if(keepsmallest == false || newSize <= originalSize) {
|
||||||
texture = loadMeshTexture(meshIndex, i, textureManager,file,
|
texture = loadMeshTexture(meshIndex, i, textureManager,file,
|
||||||
meshTextureChannelCount[i],
|
meshTextureChannelCount[i],
|
||||||
texturesOwned[i],
|
texturesOwned[i],
|
||||||
false);
|
false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
file = extractFileFromDirectoryPath(texture->getPath());
|
file = extractFileFromDirectoryPath(texture->getPath());
|
||||||
|
@ -730,12 +753,13 @@ void Model::load(const string &path, bool deletePixMapAfterLoad) {
|
||||||
this->fileName = path;
|
this->fileName = path;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Model::save(const string &path, string convertTextureToFormat) {
|
void Model::save(const string &path, string convertTextureToFormat,
|
||||||
|
bool keepsmallest) {
|
||||||
string extension= path.substr(path.find_last_of('.')+1);
|
string extension= path.substr(path.find_last_of('.')+1);
|
||||||
if(extension=="g3d" ||extension=="G3D") {
|
if(extension=="g3d" ||extension=="G3D") {
|
||||||
saveG3d(path,convertTextureToFormat);
|
saveG3d(path,convertTextureToFormat,keepsmallest);
|
||||||
}
|
}
|
||||||
else{
|
else {
|
||||||
throw runtime_error("Unknown model format: " + extension);
|
throw runtime_error("Unknown model format: " + extension);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -820,10 +844,9 @@ void Model::loadG3d(const string &path, bool deletePixMapAfterLoad) {
|
||||||
}
|
}
|
||||||
|
|
||||||
//save a model to a g3d file
|
//save a model to a g3d file
|
||||||
void Model::saveG3d(const string &path, string convertTextureToFormat) {
|
void Model::saveG3d(const string &path, string convertTextureToFormat,
|
||||||
|
bool keepsmallest) {
|
||||||
string tempModelFilename = path + "cvt";
|
string tempModelFilename = path + "cvt";
|
||||||
|
|
||||||
FILE *f= fopen(tempModelFilename.c_str(), "wb");
|
FILE *f= fopen(tempModelFilename.c_str(), "wb");
|
||||||
if(f == NULL) {
|
if(f == NULL) {
|
||||||
throw runtime_error("Cant open file for writting: [" + tempModelFilename + "]");
|
throw runtime_error("Cant open file for writting: [" + tempModelFilename + "]");
|
||||||
|
@ -852,7 +875,8 @@ void Model::saveG3d(const string &path, string convertTextureToFormat) {
|
||||||
std::map<string,int> textureDeleteList;
|
std::map<string,int> textureDeleteList;
|
||||||
for(uint32 i = 0; i < meshCount; ++i) {
|
for(uint32 i = 0; i < meshCount; ++i) {
|
||||||
meshes[i].save(i,tempModelFilename, f, textureManager,
|
meshes[i].save(i,tempModelFilename, f, textureManager,
|
||||||
convertTextureToFormat,textureDeleteList);
|
convertTextureToFormat,textureDeleteList,
|
||||||
|
keepsmallest);
|
||||||
}
|
}
|
||||||
|
|
||||||
removeFile(path);
|
removeFile(path);
|
||||||
|
|
|
@ -1384,6 +1384,12 @@ bool renameFile(string oldFile, string newFile) {
|
||||||
return (result == 0);
|
return (result == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
long getFileSize(string filename) {
|
||||||
|
struct stat stbuf;
|
||||||
|
stat(filename.c_str(), &stbuf);
|
||||||
|
return stbuf.st_size;
|
||||||
|
}
|
||||||
|
|
||||||
// =====================================
|
// =====================================
|
||||||
// ModeInfo
|
// ModeInfo
|
||||||
// =====================================
|
// =====================================
|
||||||
|
|
Loading…
Reference in New Issue
Block a user