- some initial work to allow saving models in the current v4 g3d format (new commandline option to test it also) and also convert textures into png
This commit is contained in:
parent
f88836203a
commit
c67d7d48c2
|
@ -99,6 +99,7 @@ const char *GAME_ARGS[] = {
|
||||||
"--ini-path",
|
"--ini-path",
|
||||||
"--log-path",
|
"--log-path",
|
||||||
"--show-ini-settings",
|
"--show-ini-settings",
|
||||||
|
"--convert-model",
|
||||||
"--disable-backtrace",
|
"--disable-backtrace",
|
||||||
"--disable-vbo",
|
"--disable-vbo",
|
||||||
"--verbose"
|
"--verbose"
|
||||||
|
@ -122,6 +123,7 @@ enum GAME_ARG_TYPE {
|
||||||
GAME_ARG_INI_PATH,
|
GAME_ARG_INI_PATH,
|
||||||
GAME_ARG_LOG_PATH,
|
GAME_ARG_LOG_PATH,
|
||||||
GAME_ARG_SHOW_INI_SETTINGS,
|
GAME_ARG_SHOW_INI_SETTINGS,
|
||||||
|
GAME_ARG_CONVERT_MODEL,
|
||||||
GAME_ARG_DISABLE_BACKTRACE,
|
GAME_ARG_DISABLE_BACKTRACE,
|
||||||
GAME_ARG_DISABLE_VBO,
|
GAME_ARG_DISABLE_VBO,
|
||||||
GAME_ARG_VERBOSE_MODE
|
GAME_ARG_VERBOSE_MODE
|
||||||
|
@ -859,6 +861,12 @@ void printParameterHelp(const char *argv0, bool foundInvalidArgs) {
|
||||||
printf("\n%s=x\t\t\tdisplays merged ini settings information.",GAME_ARGS[GAME_ARG_SHOW_INI_SETTINGS]);
|
printf("\n%s=x\t\t\tdisplays merged ini settings information.",GAME_ARGS[GAME_ARG_SHOW_INI_SETTINGS]);
|
||||||
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=format\t\t\tconvert a model to current g3d format.",GAME_ARGS[GAME_ARG_CONVERT_MODEL]);
|
||||||
|
printf("\n \t\tWhere x is the filename for the g3d model.");
|
||||||
|
printf("\n \t\tWhere format is the optional texture format to convert to (default is png).");
|
||||||
|
printf("\n \t\texample: %s %s=techs/megapack/factions/tech/units/castle/models/castle.g3d",argv0,GAME_ARGS[GAME_ARG_CONVERT_MODEL]);
|
||||||
|
|
||||||
printf("\n%s\t\tdisables stack backtrace on errors.",GAME_ARGS[GAME_ARG_DISABLE_BACKTRACE]);
|
printf("\n%s\t\tdisables stack backtrace on errors.",GAME_ARGS[GAME_ARG_DISABLE_BACKTRACE]);
|
||||||
printf("\n%s\t\tdisables trying to use Vertex Buffer Objects.",GAME_ARGS[GAME_ARG_DISABLE_VBO]);
|
printf("\n%s\t\tdisables trying to use Vertex Buffer Objects.",GAME_ARGS[GAME_ARG_DISABLE_VBO]);
|
||||||
printf("\n%s\t\t\tdisplays verbose information in the console.",GAME_ARGS[GAME_ARG_VERBOSE_MODE]);
|
printf("\n%s\t\t\tdisplays verbose information in the console.",GAME_ARGS[GAME_ARG_VERBOSE_MODE]);
|
||||||
|
@ -1787,6 +1795,41 @@ int glestMain(int argc, char** argv) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(hasCommandArgument(argc, argv,GAME_ARGS[GAME_ARG_CONVERT_MODEL]) == true) {
|
||||||
|
int foundParamIndIndex = -1;
|
||||||
|
hasCommandArgument(argc, argv,string(GAME_ARGS[GAME_ARG_CONVERT_MODEL]) + string("="),&foundParamIndIndex);
|
||||||
|
if(foundParamIndIndex < 0) {
|
||||||
|
hasCommandArgument(argc, argv,string(GAME_ARGS[GAME_ARG_CONVERT_MODEL]),&foundParamIndIndex);
|
||||||
|
}
|
||||||
|
string paramValue = argv[foundParamIndIndex];
|
||||||
|
vector<string> paramPartTokens;
|
||||||
|
Tokenize(paramValue,paramPartTokens,"=");
|
||||||
|
if(paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) {
|
||||||
|
string modelFile = paramPartTokens[1];
|
||||||
|
string textureFormat = "png";
|
||||||
|
if(paramPartTokens.size() >= 3) {
|
||||||
|
textureFormat = paramPartTokens[2];
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("About to convert model [%s] using texture format [%s]\n",modelFile.c_str(),textureFormat.c_str());
|
||||||
|
|
||||||
|
Model *model = renderer.newModel(rsGlobal);
|
||||||
|
model->load(modelFile);
|
||||||
|
model->save(modelFile,textureFormat);
|
||||||
|
|
||||||
|
//!!!
|
||||||
|
delete mainWindow;
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
printf("\nInvalid model name specified on commandline [%s] model [%s]\n\n",argv[foundParamIndIndex],(paramPartTokens.size() >= 2 ? paramPartTokens[1].c_str() : NULL));
|
||||||
|
printParameterHelp(argv[0],foundInvalidArgs);
|
||||||
|
delete mainWindow;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
gameInitialized = true;
|
gameInitialized = true;
|
||||||
|
|
||||||
// Setup the screenshots folder
|
// Setup the screenshots folder
|
||||||
|
|
|
@ -46,6 +46,7 @@ private:
|
||||||
bool texturesOwned[meshTextureCount];
|
bool texturesOwned[meshTextureCount];
|
||||||
string texturePaths[meshTextureCount];
|
string texturePaths[meshTextureCount];
|
||||||
|
|
||||||
|
string name;
|
||||||
//vertex data counts
|
//vertex data counts
|
||||||
uint32 frameCount;
|
uint32 frameCount;
|
||||||
uint32 vertexCount;
|
uint32 vertexCount;
|
||||||
|
@ -69,6 +70,8 @@ private:
|
||||||
bool twoSided;
|
bool twoSided;
|
||||||
bool customColor;
|
bool customColor;
|
||||||
|
|
||||||
|
uint32 textureFlags;
|
||||||
|
|
||||||
InterpolationData *interpolationData;
|
InterpolationData *interpolationData;
|
||||||
TextureManager *textureManager;
|
TextureManager *textureManager;
|
||||||
|
|
||||||
|
@ -128,11 +131,15 @@ public:
|
||||||
void updateInterpolationData(float t, bool cycle);
|
void updateInterpolationData(float t, bool cycle);
|
||||||
void updateInterpolationVertices(float t, bool cycle);
|
void updateInterpolationVertices(float t, bool cycle);
|
||||||
|
|
||||||
|
Texture2D *loadMeshTexture(TextureManager *textureManager, string textureFile,
|
||||||
|
int textureChannelCount, bool &textureOwned,
|
||||||
|
bool deletePixMapAfterLoad);
|
||||||
|
|
||||||
//load
|
//load
|
||||||
void loadV2(const string &dir, FILE *f, TextureManager *textureManager,bool deletePixMapAfterLoad);
|
void loadV2(const string &dir, FILE *f, TextureManager *textureManager,bool deletePixMapAfterLoad);
|
||||||
void loadV3(const string &dir, FILE *f, TextureManager *textureManager,bool deletePixMapAfterLoad);
|
void loadV3(const string &dir, FILE *f, TextureManager *textureManager,bool deletePixMapAfterLoad);
|
||||||
void load(const string &dir, FILE *f, TextureManager *textureManager,bool deletePixMapAfterLoad);
|
void load(const string &dir, FILE *f, TextureManager *textureManager,bool deletePixMapAfterLoad);
|
||||||
void save(const string &dir, FILE *f);
|
void save(const string &dir, FILE *f, TextureManager *textureManager, string convertTextureToFormat);
|
||||||
|
|
||||||
void deletePixels();
|
void deletePixels();
|
||||||
|
|
||||||
|
@ -185,9 +192,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);
|
void save(const string &path, string convertTextureToFormat="");
|
||||||
void loadG3d(const string &path,bool deletePixMapAfterLoad=false);
|
void loadG3d(const string &path,bool deletePixMapAfterLoad=false);
|
||||||
void saveS3d(const string &path);
|
void saveG3d(const string &path, string convertTextureToFormat="");
|
||||||
|
|
||||||
void setTextureManager(TextureManager *textureManager) {this->textureManager= textureManager;}
|
void setTextureManager(TextureManager *textureManager) {this->textureManager= textureManager;}
|
||||||
void deletePixels();
|
void deletePixels();
|
||||||
|
|
|
@ -60,6 +60,8 @@ Mesh::Mesh() {
|
||||||
twoSided= false;
|
twoSided= false;
|
||||||
customColor= false;
|
customColor= false;
|
||||||
|
|
||||||
|
textureFlags=0;
|
||||||
|
|
||||||
hasBuiltVBOs = false;
|
hasBuiltVBOs = false;
|
||||||
// Vertex Buffer Object Names
|
// Vertex Buffer Object Names
|
||||||
m_nVBOVertices = 0;
|
m_nVBOVertices = 0;
|
||||||
|
@ -214,6 +216,13 @@ void Mesh::loadV2(const string &dir, FILE *f, TextureManager *textureManager,boo
|
||||||
twoSided= false;
|
twoSided= false;
|
||||||
customColor= false;
|
customColor= false;
|
||||||
|
|
||||||
|
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Load v2, this = %p Found meshHeader.hasTexture = %d, texName [%s] mtDiffuse = %d\n",this,meshHeader.hasTexture,toLower(reinterpret_cast<char*>(meshHeader.texName)).c_str(),mtDiffuse);
|
||||||
|
|
||||||
|
textureFlags= 0;
|
||||||
|
if(meshHeader.hasTexture == true) {
|
||||||
|
textureFlags= 1;
|
||||||
|
}
|
||||||
|
|
||||||
//texture
|
//texture
|
||||||
if(meshHeader.hasTexture && textureManager!=NULL){
|
if(meshHeader.hasTexture && textureManager!=NULL){
|
||||||
texturePaths[mtDiffuse]= toLower(reinterpret_cast<char*>(meshHeader.texName));
|
texturePaths[mtDiffuse]= toLower(reinterpret_cast<char*>(meshHeader.texName));
|
||||||
|
@ -272,8 +281,15 @@ void Mesh::loadV3(const string &dir, FILE *f, TextureManager *textureManager,boo
|
||||||
twoSided= (meshHeader.properties & mp3TwoSided) != 0;
|
twoSided= (meshHeader.properties & mp3TwoSided) != 0;
|
||||||
customColor= (meshHeader.properties & mp3CustomColor) != 0;
|
customColor= (meshHeader.properties & mp3CustomColor) != 0;
|
||||||
|
|
||||||
|
textureFlags= 0;
|
||||||
|
if((meshHeader.properties & mp3NoTexture) != mp3NoTexture) {
|
||||||
|
textureFlags= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Load v3, this = %p Found meshHeader.properties = %d, textureFlags = %d, texName [%s] mtDiffuse = %d\n",this,meshHeader.properties,textureFlags,toLower(reinterpret_cast<char*>(meshHeader.texName)).c_str(),mtDiffuse);
|
||||||
|
|
||||||
//texture
|
//texture
|
||||||
if(!(meshHeader.properties & mp3NoTexture) && textureManager!=NULL){
|
if((meshHeader.properties & mp3NoTexture) != mp3NoTexture && textureManager!=NULL){
|
||||||
texturePaths[mtDiffuse]= toLower(reinterpret_cast<char*>(meshHeader.texName));
|
texturePaths[mtDiffuse]= toLower(reinterpret_cast<char*>(meshHeader.texName));
|
||||||
|
|
||||||
string texPath= dir;
|
string texPath= dir;
|
||||||
|
@ -309,13 +325,37 @@ void Mesh::loadV3(const string &dir, FILE *f, TextureManager *textureManager,boo
|
||||||
readBytes = fread(indices, sizeof(uint32)*indexCount, 1, f);
|
readBytes = fread(indices, sizeof(uint32)*indexCount, 1, f);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mesh::load(const string &dir, FILE *f, TextureManager *textureManager,bool deletePixMapAfterLoad) {
|
Texture2D* Mesh::loadMeshTexture(TextureManager *textureManager, string textureFile,
|
||||||
|
int textureChannelCount, bool &textureOwned, bool deletePixMapAfterLoad) {
|
||||||
|
Texture2D* texture = static_cast<Texture2D*>(textureManager->getTexture(textureFile));
|
||||||
|
if(texture == NULL) {
|
||||||
|
texture = textureManager->newTexture2D();
|
||||||
|
if(textureChannelCount != -1) {
|
||||||
|
texture->getPixmap()->init(textureChannelCount);
|
||||||
|
}
|
||||||
|
texture->load(textureFile);
|
||||||
|
textureOwned = true;
|
||||||
|
texture->init(textureManager->getTextureFilter(),textureManager->getMaxAnisotropy());
|
||||||
|
if(deletePixMapAfterLoad == true) {
|
||||||
|
texture->deletePixels();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return texture;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Mesh::load(const string &dir, FILE *f, TextureManager *textureManager,
|
||||||
|
bool deletePixMapAfterLoad) {
|
||||||
this->textureManager = textureManager;
|
this->textureManager = textureManager;
|
||||||
|
|
||||||
//read header
|
//read header
|
||||||
MeshHeader meshHeader;
|
MeshHeader meshHeader;
|
||||||
size_t readBytes = fread(&meshHeader, sizeof(MeshHeader), 1, f);
|
size_t readBytes = fread(&meshHeader, sizeof(MeshHeader), 1, f);
|
||||||
|
|
||||||
|
name = reinterpret_cast<char*>(meshHeader.name);
|
||||||
|
|
||||||
|
//printf("Load, Found meshTextureCount = %d, meshHeader.textures = %d\n",meshTextureCount,meshHeader.textures);
|
||||||
|
|
||||||
//init
|
//init
|
||||||
frameCount= meshHeader.frameCount;
|
frameCount= meshHeader.frameCount;
|
||||||
vertexCount= meshHeader.vertexCount;
|
vertexCount= meshHeader.vertexCount;
|
||||||
|
@ -333,6 +373,10 @@ void Mesh::load(const string &dir, FILE *f, TextureManager *textureManager,bool
|
||||||
specularPower= meshHeader.specularPower;
|
specularPower= meshHeader.specularPower;
|
||||||
opacity= meshHeader.opacity;
|
opacity= meshHeader.opacity;
|
||||||
|
|
||||||
|
textureFlags= meshHeader.textures;
|
||||||
|
|
||||||
|
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Load v4, this = %p Found meshHeader.textures = %d\n",this,meshHeader.textures);
|
||||||
|
|
||||||
//maps
|
//maps
|
||||||
uint32 flag= 1;
|
uint32 flag= 1;
|
||||||
for(int i=0; i<meshTextureCount; ++i){
|
for(int i=0; i<meshTextureCount; ++i){
|
||||||
|
@ -347,19 +391,8 @@ void Mesh::load(const string &dir, FILE *f, TextureManager *textureManager,bool
|
||||||
}
|
}
|
||||||
mapFullPath += mapPath;
|
mapFullPath += mapPath;
|
||||||
|
|
||||||
textures[i]= static_cast<Texture2D*>(textureManager->getTexture(mapFullPath));
|
textures[i] = loadMeshTexture(textureManager, mapFullPath,
|
||||||
if(textures[i]==NULL){
|
meshTextureChannelCount[i],texturesOwned[i],deletePixMapAfterLoad);
|
||||||
textures[i]= textureManager->newTexture2D();
|
|
||||||
if(meshTextureChannelCount[i] != -1){
|
|
||||||
textures[i]->getPixmap()->init(meshTextureChannelCount[i]);
|
|
||||||
}
|
|
||||||
textures[i]->load(mapFullPath);
|
|
||||||
texturesOwned[i]=true;
|
|
||||||
textures[i]->init(textureManager->getTextureFilter(),textureManager->getMaxAnisotropy());
|
|
||||||
if(deletePixMapAfterLoad == true) {
|
|
||||||
textures[i]->deletePixels();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
flag*= 2;
|
flag*= 2;
|
||||||
}
|
}
|
||||||
|
@ -378,34 +411,101 @@ void Mesh::load(const string &dir, FILE *f, TextureManager *textureManager,bool
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mesh::save(const string &dir, FILE *f){
|
void Mesh::save(const string &dir, FILE *f, TextureManager *textureManager, string convertTextureToFormat) {
|
||||||
/*MeshHeader meshHeader;
|
MeshHeader meshHeader;
|
||||||
meshHeader.vertexFrameCount= vertexFrameCount;
|
memset(&meshHeader, 0, sizeof(struct MeshHeader));
|
||||||
meshHeader.normalFrameCount= normalFrameCount;
|
|
||||||
meshHeader.texCoordFrameCount= texCoordFrameCount;
|
|
||||||
meshHeader.colorFrameCount= colorFrameCount;
|
|
||||||
meshHeader.pointCount= pointCount;
|
|
||||||
meshHeader.indexCount= indexCount;
|
|
||||||
meshHeader.properties= 0;
|
|
||||||
|
|
||||||
if(twoSided) meshHeader.properties|= mpTwoSided;
|
strncpy((char*)meshHeader.name, (char*)name.c_str(), name.length());
|
||||||
if(customTexture) meshHeader.properties|= mpCustomTexture;
|
meshHeader.frameCount= frameCount;
|
||||||
|
meshHeader.vertexCount= vertexCount;
|
||||||
|
meshHeader.indexCount = indexCount;
|
||||||
|
|
||||||
if(texture==NULL){
|
//material
|
||||||
meshHeader.properties|= mpNoTexture;
|
memcpy((float32*)meshHeader.diffuseColor, (float32*)diffuseColor.ptr(), sizeof(float32) * 3);
|
||||||
meshHeader.texName[0]= '\0';
|
memcpy((float32*)meshHeader.specularColor, (float32*)specularColor.ptr(), sizeof(float32) * 3);
|
||||||
|
meshHeader.specularPower = specularPower;
|
||||||
|
meshHeader.opacity = opacity;
|
||||||
|
|
||||||
|
//properties
|
||||||
|
meshHeader.properties = 0;
|
||||||
|
if(customColor) {
|
||||||
|
meshHeader.properties |= mpfCustomColor;
|
||||||
}
|
}
|
||||||
else{
|
if(twoSided) {
|
||||||
strcpy(reinterpret_cast<char*>(meshHeader.texName), texName.c_str());
|
meshHeader.properties |= mpfTwoSided;
|
||||||
texture->getPixmap()->saveTga(dir+"/"+texName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
meshHeader.textures = textureFlags;
|
||||||
fwrite(&meshHeader, sizeof(MeshHeader), 1, f);
|
fwrite(&meshHeader, sizeof(MeshHeader), 1, f);
|
||||||
fwrite(vertices, sizeof(Vec3f)*vertexFrameCount*pointCount, 1, f);
|
|
||||||
fwrite(normals, sizeof(Vec3f)*normalFrameCount*pointCount, 1, f);
|
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Save, this = %p, Found meshTextureCount = %d, meshHeader.textures = %d\n",this,meshTextureCount,meshHeader.textures);
|
||||||
fwrite(texCoords, sizeof(Vec2f)*texCoordFrameCount*pointCount, 1, f);
|
|
||||||
fwrite(colors, sizeof(Vec4f)*colorFrameCount, 1, f);
|
//maps
|
||||||
fwrite(indices, sizeof(uint32)*indexCount, 1, f);*/
|
uint32 flag= 1;
|
||||||
|
for(int i=0; i< meshTextureCount; ++i) {
|
||||||
|
if((meshHeader.textures & flag)) {
|
||||||
|
uint8 cMapPath[mapPathSize];
|
||||||
|
memset(&cMapPath[0],0,mapPathSize);
|
||||||
|
|
||||||
|
Texture2D *texture = textures[i];
|
||||||
|
|
||||||
|
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Save, [%d] mesh texture ptr [%p]\n",i,texture);
|
||||||
|
|
||||||
|
if(texture != NULL) {
|
||||||
|
string file = toLower(texture->getPath());
|
||||||
|
|
||||||
|
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Save, Found mesh texture [%s]\n",file.c_str());
|
||||||
|
|
||||||
|
if(toLower(convertTextureToFormat) != "" &&
|
||||||
|
EndsWith(file, "." + convertTextureToFormat) == false) {
|
||||||
|
string fileExt = extractExtension(file);
|
||||||
|
replaceAll(file, "." + fileExt, "." + convertTextureToFormat);
|
||||||
|
|
||||||
|
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Save, Convert from [%s] to [%s]\n",texture->getPath().c_str(),file.c_str());
|
||||||
|
|
||||||
|
if(convertTextureToFormat == "tga") {
|
||||||
|
texture->getPixmap()->saveTga(file);
|
||||||
|
}
|
||||||
|
else if(convertTextureToFormat == "bmp") {
|
||||||
|
texture->getPixmap()->saveBmp(file);
|
||||||
|
}
|
||||||
|
//else if(convertTextureToFormat == "jpg") {
|
||||||
|
// texture->getPixmap()->saveJpg(file);
|
||||||
|
//}
|
||||||
|
else if(convertTextureToFormat == "png") {
|
||||||
|
texture->getPixmap()->savePng(file);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw runtime_error("Unsuppoted texture format: [" + convertTextureToFormat + "]");
|
||||||
|
}
|
||||||
|
|
||||||
|
//textureManager->endTexture(texture);
|
||||||
|
texture = loadMeshTexture(textureManager,file,
|
||||||
|
meshTextureChannelCount[i],
|
||||||
|
texturesOwned[i],
|
||||||
|
false);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
file = extractFileFromDirectoryPath(texture->getPath());
|
||||||
|
|
||||||
|
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Save, new texture file [%s]\n",file.c_str());
|
||||||
|
|
||||||
|
memcpy(&cMapPath[0],file.c_str(),file.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
fwrite(cMapPath, mapPathSize, 1, f);
|
||||||
|
}
|
||||||
|
flag*= 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
//read data
|
||||||
|
fwrite(vertices, sizeof(Vec3f)*frameCount*vertexCount, 1, f);
|
||||||
|
fwrite(normals, sizeof(Vec3f)*frameCount*vertexCount, 1, f);
|
||||||
|
if(meshHeader.textures != 0) {
|
||||||
|
fwrite(texCoords, sizeof(Vec2f)*vertexCount, 1, f);
|
||||||
|
}
|
||||||
|
fwrite(indices, sizeof(uint32)*indexCount, 1, f);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mesh::computeTangents(){
|
void Mesh::computeTangents(){
|
||||||
|
@ -535,10 +635,10 @@ void Model::load(const string &path, bool deletePixMapAfterLoad) {
|
||||||
this->fileName = path;
|
this->fileName = path;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Model::save(const string &path) {
|
void Model::save(const string &path, string convertTextureToFormat) {
|
||||||
string extension= path.substr(path.find_last_of('.')+1);
|
string extension= path.substr(path.find_last_of('.')+1);
|
||||||
if(extension=="g3d" ||extension=="G3D" || extension=="s3d" || extension=="S3D"){
|
if(extension=="g3d" ||extension=="G3D") {
|
||||||
saveS3d(path);
|
saveG3d(path,convertTextureToFormat);
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
throw runtime_error("Unknown model format: " + extension);
|
throw runtime_error("Unknown model format: " + extension);
|
||||||
|
@ -604,12 +704,17 @@ void Model::loadG3d(const string &path, bool deletePixMapAfterLoad) {
|
||||||
}
|
}
|
||||||
fileVersion= fileHeader.version;
|
fileVersion= fileHeader.version;
|
||||||
|
|
||||||
|
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Load model, fileVersion = %d\n",fileVersion);
|
||||||
|
|
||||||
//version 4
|
//version 4
|
||||||
if(fileHeader.version == 4) {
|
if(fileHeader.version == 4) {
|
||||||
//model header
|
//model header
|
||||||
ModelHeader modelHeader;
|
ModelHeader modelHeader;
|
||||||
readBytes = fread(&modelHeader, sizeof(ModelHeader), 1, f);
|
readBytes = fread(&modelHeader, sizeof(ModelHeader), 1, f);
|
||||||
meshCount= modelHeader.meshCount;
|
meshCount= modelHeader.meshCount;
|
||||||
|
|
||||||
|
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("meshCount = %d\n",meshCount);
|
||||||
|
|
||||||
if(modelHeader.type != mtMorphMesh) {
|
if(modelHeader.type != mtMorphMesh) {
|
||||||
throw runtime_error("Invalid model type");
|
throw runtime_error("Invalid model type");
|
||||||
}
|
}
|
||||||
|
@ -623,8 +728,10 @@ void Model::loadG3d(const string &path, bool deletePixMapAfterLoad) {
|
||||||
}
|
}
|
||||||
//version 3
|
//version 3
|
||||||
else if(fileHeader.version==3){
|
else if(fileHeader.version==3){
|
||||||
|
|
||||||
readBytes = fread(&meshCount, sizeof(meshCount), 1, f);
|
readBytes = fread(&meshCount, sizeof(meshCount), 1, f);
|
||||||
|
|
||||||
|
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("meshCount = %d\n",meshCount);
|
||||||
|
|
||||||
meshes= new Mesh[meshCount];
|
meshes= new Mesh[meshCount];
|
||||||
for(uint32 i=0; i<meshCount; ++i){
|
for(uint32 i=0; i<meshCount; ++i){
|
||||||
meshes[i].loadV3(dir, f, textureManager,deletePixMapAfterLoad);
|
meshes[i].loadV3(dir, f, textureManager,deletePixMapAfterLoad);
|
||||||
|
@ -634,6 +741,9 @@ void Model::loadG3d(const string &path, bool deletePixMapAfterLoad) {
|
||||||
//version 2
|
//version 2
|
||||||
else if(fileHeader.version==2) {
|
else if(fileHeader.version==2) {
|
||||||
readBytes = fread(&meshCount, sizeof(meshCount), 1, f);
|
readBytes = fread(&meshCount, sizeof(meshCount), 1, f);
|
||||||
|
|
||||||
|
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("meshCount = %d\n",meshCount);
|
||||||
|
|
||||||
meshes= new Mesh[meshCount];
|
meshes= new Mesh[meshCount];
|
||||||
for(uint32 i=0; i<meshCount; ++i){
|
for(uint32 i=0; i<meshCount; ++i){
|
||||||
meshes[i].loadV2(dir, f, textureManager,deletePixMapAfterLoad);
|
meshes[i].loadV2(dir, f, textureManager,deletePixMapAfterLoad);
|
||||||
|
@ -653,13 +763,13 @@ void Model::loadG3d(const string &path, bool deletePixMapAfterLoad) {
|
||||||
}
|
}
|
||||||
|
|
||||||
//save a model to a g3d file
|
//save a model to a g3d file
|
||||||
void Model::saveS3d(const string &path){
|
void Model::saveG3d(const string &path, string convertTextureToFormat) {
|
||||||
|
FILE *f= fopen(path.c_str(), "wb");
|
||||||
/*FILE *f= fopen(path.c_str(), "wb");
|
if(f == NULL) {
|
||||||
if(f==NULL){
|
|
||||||
throw runtime_error("Cant open file for writting: "+path);
|
throw runtime_error("Cant open file for writting: "+path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
ModelHeader modelHeader;
|
ModelHeader modelHeader;
|
||||||
modelHeader.id[0]= 'G';
|
modelHeader.id[0]= 'G';
|
||||||
modelHeader.id[1]= '3';
|
modelHeader.id[1]= '3';
|
||||||
|
@ -675,6 +785,53 @@ void Model::saveS3d(const string &path){
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(f);*/
|
fclose(f);*/
|
||||||
|
|
||||||
|
convertTextureToFormat = toLower(convertTextureToFormat);
|
||||||
|
|
||||||
|
//file header
|
||||||
|
FileHeader fileHeader;
|
||||||
|
fileHeader.id[0]= 'G';
|
||||||
|
fileHeader.id[1]= '3';
|
||||||
|
fileHeader.id[2]= 'D';
|
||||||
|
fileHeader.version= 4;
|
||||||
|
|
||||||
|
fwrite(&fileHeader, sizeof(FileHeader), 1, f);
|
||||||
|
|
||||||
|
//version 4
|
||||||
|
if(fileHeader.version == 4 || fileHeader.version == 3 || fileHeader.version == 2) {
|
||||||
|
//model header
|
||||||
|
ModelHeader modelHeader;
|
||||||
|
modelHeader.meshCount = meshCount;
|
||||||
|
modelHeader.type = mtMorphMesh;
|
||||||
|
|
||||||
|
fwrite(&modelHeader, sizeof(ModelHeader), 1, f);
|
||||||
|
|
||||||
|
for(uint32 i = 0; i < meshCount; ++i) {
|
||||||
|
meshes[i].save(path, f, textureManager,convertTextureToFormat);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
//version 3
|
||||||
|
else if(fileHeader.version == 3) {
|
||||||
|
|
||||||
|
fwrite(&meshCount, sizeof(meshCount), 1, f);
|
||||||
|
for(uint32 i=0; i<meshCount; ++i){
|
||||||
|
meshes[i].saveV3(dir, f, textureManager,convertTextureToFormat);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//version 2
|
||||||
|
else if(fileHeader.version == 2) {
|
||||||
|
fwrite(&meshCount, sizeof(meshCount), 1, f);
|
||||||
|
for(uint32 i=0; i<meshCount; ++i){
|
||||||
|
meshes[i].saveV2(dir, f, textureManager,convertTextureToFormat);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
else {
|
||||||
|
throw runtime_error("Invalid model version: "+ intToStr(fileHeader.version));
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Model::deletePixels() {
|
void Model::deletePixels() {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user