From de3a92081debca0ed215a58fc4debe69ae1e4e1b Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Thu, 28 Oct 2010 00:51:25 +0000 Subject: [PATCH] - a bunch of in progress work related to texture compression and more timely texture memory management cleanup. For now to test texture compression use the following ini setting: EnableTextureCompression=true --- source/glest_game/game/game.cpp | 7 + source/glest_game/main/main.cpp | 2 + source/glest_game/type_instances/faction.cpp | 8 +- source/glest_game/type_instances/faction.h | 6 +- source/glest_game/types/faction_type.cpp | 10 ++ source/glest_game/types/faction_type.h | 2 + source/glest_game/types/object_type.cpp | 49 +++++++ source/glest_game/types/object_type.h | 1 + source/glest_game/types/resource_type.cpp | 6 + source/glest_game/types/resource_type.h | 1 + source/glest_game/types/tech_tree.cpp | 26 +++- source/glest_game/types/tech_tree.h | 1 + source/glest_game/types/unit_type.cpp | 1 + source/glest_game/world/tileset.cpp | 7 +- source/glest_game/world/world.cpp | 2 +- .../include/graphics/gl/texture_gl.h | 2 +- source/shared_lib/include/graphics/model.h | 15 +- source/shared_lib/include/graphics/pixmap.h | 4 + source/shared_lib/include/graphics/texture.h | 14 +- .../include/graphics/texture_manager.h | 3 + .../sources/graphics/gl/texture_gl.cpp | 130 +++++++++--------- source/shared_lib/sources/graphics/model.cpp | 80 ++++++++--- source/shared_lib/sources/graphics/pixmap.cpp | 27 +++- .../shared_lib/sources/graphics/texture.cpp | 49 +++++++ 24 files changed, 353 insertions(+), 100 deletions(-) create mode 100644 source/glest_game/types/object_type.cpp diff --git a/source/glest_game/game/game.cpp b/source/glest_game/game/game.cpp index 272c0efc..c178357c 100644 --- a/source/glest_game/game/game.cpp +++ b/source/glest_game/game/game.cpp @@ -578,6 +578,13 @@ void Game::init(bool initForPreviewOnly) logger.add("Initializing renderer", true); renderer.initGame(this); + for(int i=0; i < world.getFactionCount(); ++i) { + Faction *faction= world.getFaction(i); + if(faction != NULL) { + faction->deletePixels(); + } + } + if(initForPreviewOnly == false) { //good_fpu_control_registers(NULL,__FILE__,__FUNCTION__,__LINE__); diff --git a/source/glest_game/main/main.cpp b/source/glest_game/main/main.cpp index d6c8cc68..b12b4c0f 100644 --- a/source/glest_game/main/main.cpp +++ b/source/glest_game/main/main.cpp @@ -656,6 +656,8 @@ int glestMain(int argc, char** argv){ SystemFlags::OutputDebug(SystemFlags::debugUnitCommands,"START\n"); SystemFlags::OutputDebug(SystemFlags::debugPathFinder,"START\n"); + Texture::useTextureCompression = config.getBool("EnableTextureCompression","false"); + // 256 for English // 30000 for Chinese Font::charCount = config.getInt("FONT_CHARCOUNT",intToStr(Font::charCount).c_str()); diff --git a/source/glest_game/type_instances/faction.cpp b/source/glest_game/type_instances/faction.cpp index 9a5c77c7..d0cd562f 100644 --- a/source/glest_game/type_instances/faction.cpp +++ b/source/glest_game/type_instances/faction.cpp @@ -53,7 +53,7 @@ Faction::~Faction() { } void Faction::init( - const FactionType *factionType, ControlType control, TechTree *techTree, Game *game, + FactionType *factionType, ControlType control, TechTree *techTree, Game *game, int factionIndex, int teamIndex, int startLocationIndex, bool thisFaction, bool giveResources) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); @@ -929,6 +929,12 @@ void Faction::addCachedPath(const Vec2i &target, Unit *unit) { } } +void Faction::deletePixels() { + if(factionType != NULL) { + factionType->deletePixels(); + } +} + std::string Faction::toString() const { std::string result = ""; diff --git a/source/glest_game/type_instances/faction.h b/source/glest_game/type_instances/faction.h index cb9a9b19..abf7b81c 100644 --- a/source/glest_game/type_instances/faction.h +++ b/source/glest_game/type_instances/faction.h @@ -75,7 +75,7 @@ private: ControlType control; Texture2D *texture; - const FactionType *factionType; + FactionType *factionType; int index; int teamIndex; @@ -97,7 +97,7 @@ public: ~Faction(); void init( - const FactionType *factionType, ControlType control, TechTree *techTree, Game *game, + FactionType *factionType, ControlType control, TechTree *techTree, Game *game, int factionIndex, int teamIndex, int startLocationIndex, bool thisFaction, bool giveResources); void end(); @@ -169,6 +169,8 @@ public: Vec2i getClosestResourceTypeTargetFromCache(Unit *unit, const ResourceType *type); void cleanupResourceTypeTargetCache(std::vector *deleteListPtr); + void deletePixels(); + std::string toString() const; private: diff --git a/source/glest_game/types/faction_type.cpp b/source/glest_game/types/faction_type.cpp index 4db1eaf5..d0cf857f 100644 --- a/source/glest_game/types/faction_type.cpp +++ b/source/glest_game/types/faction_type.cpp @@ -535,6 +535,16 @@ int FactionType::getStartingResourceAmount(const ResourceType *resourceType) con return 0; } +void FactionType::deletePixels() { + for(int i = 0; i deletePixels(); + } + } +} + std::string FactionType::toString() const { std::string result = ""; diff --git a/source/glest_game/types/faction_type.h b/source/glest_game/types/faction_type.h index 4dae8795..f5b768c9 100644 --- a/source/glest_game/types/faction_type.h +++ b/source/glest_game/types/faction_type.h @@ -72,6 +72,8 @@ public: std::vector validateFactionType(); std::vector validateFactionTypeResourceTypes(vector &resourceTypes); std::vector validateFactionTypeUpgradeTypes(); + + void deletePixels(); }; }}//end namespace diff --git a/source/glest_game/types/object_type.cpp b/source/glest_game/types/object_type.cpp new file mode 100644 index 00000000..4b1ddc73 --- /dev/null +++ b/source/glest_game/types/object_type.cpp @@ -0,0 +1,49 @@ +// ============================================================== +// This file is part of Glest (www.glest.org) +// +// Copyright (C) 2001-2008 Marti�o Figueroa +// +// You can redistribute this code and/or modify it under +// the terms of the GNU General Public License as published +// by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version +// ============================================================== + +#include "object_type.h" + +#include "renderer.h" +#include "leak_dumper.h" + +namespace Glest{ namespace Game{ + +// ===================================================== +// class ObjectType +// ===================================================== + +void ObjectType::init(int modelCount, int objectClass, bool walkable){ + models.reserve(modelCount); + this->objectClass= objectClass; + this->walkable= walkable; +} + +void ObjectType::loadModel(const string &path){ + Model *model= Renderer::getInstance().newModel(rsGame); + model->load(path); + color= Vec3f(0.f); + if(model->getMeshCount()>0 && model->getMesh(0)->getTexture(0) != NULL) { + const Pixmap2D *p= model->getMesh(0)->getTexture(0)->getPixmap(); + color= p->getPixel3f(p->getW()/2, p->getH()/2); + } + models.push_back(model); +} + +void ObjectType::deletePixels() { + for(int i = 0; i < models.size(); ++i) { + Model *model = models[i]; + if(model != NULL) { + model->deletePixels(); + } + } +} + +}}//end namespace diff --git a/source/glest_game/types/object_type.h b/source/glest_game/types/object_type.h index 20a94ce5..2bfd003b 100644 --- a/source/glest_game/types/object_type.h +++ b/source/glest_game/types/object_type.h @@ -56,6 +56,7 @@ public: int getClass() const {return objectClass;} bool getWalkable() const {return walkable;} bool isATree() const {return objectClass==tree1 || objectClass==tree2;} + void deletePixels(); }; }}//end namespace diff --git a/source/glest_game/types/resource_type.cpp b/source/glest_game/types/resource_type.cpp index a8c99d0f..911327e2 100644 --- a/source/glest_game/types/resource_type.cpp +++ b/source/glest_game/types/resource_type.cpp @@ -152,4 +152,10 @@ ResourceClass ResourceType::strToRc(const string &s){ throw runtime_error("Error converting from string ro resourceClass, found: " + s); } +void ResourceType::deletePixels() { + if(model != NULL) { + model->deletePixels(); + } +} + }}//end namespace diff --git a/source/glest_game/types/resource_type.h b/source/glest_game/types/resource_type.h index 7168c195..3c26d623 100644 --- a/source/glest_game/types/resource_type.h +++ b/source/glest_game/types/resource_type.h @@ -60,6 +60,7 @@ public: bool getRecoup_cost() const { return recoup_cost;} static ResourceClass strToRc(const string &s); + void deletePixels(); }; }} //end namespace diff --git a/source/glest_game/types/tech_tree.cpp b/source/glest_game/types/tech_tree.cpp index 469fa6a6..33270187 100644 --- a/source/glest_game/types/tech_tree.cpp +++ b/source/glest_game/types/tech_tree.cpp @@ -62,6 +62,11 @@ void TechTree::load(const string &dir, set &factions, Checksum* checksum resourceTypes[i].load(str, checksum); SDL_PumpEvents(); } + + // Cleanup pixmap memory + for(int i=0; i TechTree::validateResourceTypes() { // ==================== get ==================== -const FactionType *TechTree::getType(const string &name) const{ +FactionType *TechTree::getTypeByName(const string &name) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - for(int i=0; i pathList, const string &tilesetNa load(path, checksum); break; } - } + } } @@ -160,6 +160,11 @@ void Tileset::load(const string &dir, Checksum *checksum){ } } + // Now free up the pixmap memory + for(int i=0; igetType(gs->getFactionTypeName(i)); + FactionType *ft= techTree->getTypeByName(gs->getFactionTypeName(i)); factions[i].init( ft, gs->getFactionControl(i), techTree, game, i, gs->getTeam(i), gs->getStartLocationIndex(i), i==thisFactionIndex, gs->getDefaultResources()); diff --git a/source/shared_lib/include/graphics/gl/texture_gl.h b/source/shared_lib/include/graphics/gl/texture_gl.h index ed6bb90b..502a32fc 100644 --- a/source/shared_lib/include/graphics/gl/texture_gl.h +++ b/source/shared_lib/include/graphics/gl/texture_gl.h @@ -29,7 +29,7 @@ protected: public: GLuint getHandle() const {return handle;} - void OutputTextureDebugInfo(const Pixmap2D *pixmap,Texture::Format format, int components, const string path); + void OutputTextureDebugInfo(Texture::Format format, int components, const string path); }; // ===================================================== diff --git a/source/shared_lib/include/graphics/model.h b/source/shared_lib/include/graphics/model.h index 18abab70..8c341f7b 100644 --- a/source/shared_lib/include/graphics/model.h +++ b/source/shared_lib/include/graphics/model.h @@ -43,6 +43,7 @@ class Mesh{ private: //mesh data Texture2D *textures[meshTextureCount]; + bool texturesOwned[meshTextureCount]; string texturePaths[meshTextureCount]; //vertex data counts @@ -68,6 +69,7 @@ private: bool customColor; InterpolationData *interpolationData; + TextureManager *textureManager; public: //init & end @@ -111,11 +113,13 @@ public: void updateInterpolationVertices(float t, bool cycle) const; //load - void loadV2(const string &dir, FILE *f, TextureManager *textureManager); - void loadV3(const string &dir, FILE *f, TextureManager *textureManager); - void load(const string &dir, FILE *f, TextureManager *textureManager); + void loadV2(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 save(const string &dir, FILE *f); + void deletePixels(); + private: void computeTangents(); }; @@ -156,12 +160,13 @@ public: uint32 getVertexCount() const; //io - void load(const string &path); + void load(const string &path,bool deletePixMapAfterLoad=false); void save(const string &path); - void loadG3d(const string &path); + void loadG3d(const string &path,bool deletePixMapAfterLoad=false); void saveS3d(const string &path); void setTextureManager(TextureManager *textureManager) {this->textureManager= textureManager;} + void deletePixels(); private: void buildInterpolationData() const; diff --git a/source/shared_lib/include/graphics/pixmap.h b/source/shared_lib/include/graphics/pixmap.h index 1ec6df83..bb4ade9b 100644 --- a/source/shared_lib/include/graphics/pixmap.h +++ b/source/shared_lib/include/graphics/pixmap.h @@ -122,6 +122,7 @@ public: int getW() const {return w;} int getComponents() const {return components;} uint8 *getPixels() const {return pixels;} + void deletePixels(); string getPath() const { return path;} }; @@ -161,6 +162,7 @@ public: int getH() const {return h;} int getComponents() const {return components;} uint8 *getPixels() const {return pixels;} + void deletePixels(); //get data void getPixel(int x, int y, uint8 *value) const; @@ -236,6 +238,7 @@ public: int getD() const {return d;} int getComponents() const {return components;} uint8 *getPixels() const {return pixels;} + void deletePixels(); string getPath() const { return path;} }; @@ -271,6 +274,7 @@ public: //get Pixmap2D *getFace(int face) {return &faces[face];} const Pixmap2D *getFace(int face) const {return &faces[face];} + void deletePixels(); string getPath(int face) const { return path[face];} }; diff --git a/source/shared_lib/include/graphics/texture.h b/source/shared_lib/include/graphics/texture.h index e13e8ac3..c30e62b9 100644 --- a/source/shared_lib/include/graphics/texture.h +++ b/source/shared_lib/include/graphics/texture.h @@ -28,10 +28,11 @@ class TextureParams; // class Texture // ===================================================== -class Texture{ +class Texture { public: static const int defaultSize; static const int defaultComponents; + static bool useTextureCompression; enum WrapMode{ wmRepeat, @@ -69,7 +70,6 @@ public: WrapMode getWrapMode() const {return wrapMode;} bool getPixmapInit() const {return pixmapInit;} Format getFormat() const {return format;} - const string getPath() const {return path;} void setMipmap(bool mipmap) {this->mipmap= mipmap;} void setWrapMode(WrapMode wrapMode) {this->wrapMode= wrapMode;} @@ -78,6 +78,8 @@ public: virtual void init(Filter filter= fBilinear, int maxAnisotropy= 1)=0; virtual void end()=0; + virtual string getPath() const = 0; + virtual void deletePixels() = 0; virtual void reseInitState() { inited = false; } }; @@ -95,6 +97,8 @@ public: Pixmap1D *getPixmap() {return &pixmap;} const Pixmap1D *getPixmap() const {return &pixmap;} + virtual string getPath() const; + virtual void deletePixels(); }; // ===================================================== @@ -110,6 +114,8 @@ public: Pixmap2D *getPixmap() {return &pixmap;} const Pixmap2D *getPixmap() const {return &pixmap;} + virtual string getPath() const; + virtual void deletePixels(); }; // ===================================================== @@ -125,6 +131,8 @@ public: Pixmap3D *getPixmap() {return &pixmap;} const Pixmap3D *getPixmap() const {return &pixmap;} + virtual string getPath() const; + virtual void deletePixels(); }; // ===================================================== @@ -140,6 +148,8 @@ public: PixmapCube *getPixmap() {return &pixmap;} const PixmapCube *getPixmap() const {return &pixmap;} + virtual string getPath() const; + virtual void deletePixels(); }; }}//end namespace diff --git a/source/shared_lib/include/graphics/texture_manager.h b/source/shared_lib/include/graphics/texture_manager.h index dde152f8..e6328fa2 100644 --- a/source/shared_lib/include/graphics/texture_manager.h +++ b/source/shared_lib/include/graphics/texture_manager.h @@ -48,6 +48,9 @@ public: void endLastTexture(bool mustExistInList=false); void reinitTextures(); + Texture::Filter getTextureFilter() const {return textureFilter;} + int getMaxAnisotropy() const {return maxAnisotropy;} + Texture *getTexture(const string &path); Texture1D *newTexture1D(); Texture2D *newTexture2D(); diff --git a/source/shared_lib/sources/graphics/gl/texture_gl.cpp b/source/shared_lib/sources/graphics/gl/texture_gl.cpp index 8d128498..796ee5ed 100644 --- a/source/shared_lib/sources/graphics/gl/texture_gl.cpp +++ b/source/shared_lib/sources/graphics/gl/texture_gl.cpp @@ -23,6 +23,44 @@ namespace Shared{ namespace Graphics{ namespace Gl{ using namespace Platform; +GLint toCompressionFormatGl(GLint format) { + if(Texture::useTextureCompression == false) { + return format; + } + + //GL_COMPRESSED_ALPHA <- white things but tile ok! + //GL_COMPRESSED_LUMINANCE <- black tiles + //GL_COMPRESSED_LUMINANCE_ALPHA <- black tiles + //GL_COMPRESSED_INTENSITY <- black tiles + //GL_COMPRESSED_RGB <- black tiles + //GL_COMPRESSED_RGBA <- black tiles + + // With the following extension (GL_EXT_texture_compression_s3tc) + //GL_COMPRESSED_RGB_S3TC_DXT1_EXT + //GL_COMPRESSED_RGBA_S3TC_DXT1_EXT + //GL_COMPRESSED_RGBA_S3TC_DXT3_EXT + //GL_COMPRESSED_RGBA_S3TC_DXT5_EXT + + switch(format) { + case GL_LUMINANCE: + case GL_LUMINANCE8: + return GL_COMPRESSED_LUMINANCE; + case GL_RGB: + case GL_RGB8: + //return GL_COMPRESSED_RGB; + return GL_COMPRESSED_RGB_S3TC_DXT1_EXT; + case GL_RGBA: + case GL_RGBA8: + //return GL_COMPRESSED_RGBA; + return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; + case GL_ALPHA: + case GL_ALPHA8: + return GL_COMPRESSED_ALPHA; + default: + return format; + } +} + GLint toWrapModeGl(Texture::WrapMode wrapMode){ switch(wrapMode){ case Texture::wmClamp: @@ -96,44 +134,6 @@ GLint toInternalFormatGl(Texture::Format format, int components){ } } -GLint toCompressionFormatGl(GLint format) { - return format; - - //GL_COMPRESSED_ALPHA <- white things but tile ok! - //GL_COMPRESSED_LUMINANCE <- black tiles - //GL_COMPRESSED_LUMINANCE_ALPHA <- black tiles - //GL_COMPRESSED_INTENSITY <- black tiles - //GL_COMPRESSED_RGB <- black tiles - //GL_COMPRESSED_RGBA <- black tiles - - // With the following extension (GL_EXT_texture_compression_s3tc) - //GL_COMPRESSED_RGB_S3TC_DXT1_EXT - //GL_COMPRESSED_RGBA_S3TC_DXT1_EXT - //GL_COMPRESSED_RGBA_S3TC_DXT3_EXT - //GL_COMPRESSED_RGBA_S3TC_DXT5_EXT - -/* - switch(format) { - case GL_LUMINANCE: - case GL_LUMINANCE8: - return GL_COMPRESSED_LUMINANCE; - case GL_RGB: - case GL_RGB8: - //return GL_COMPRESSED_RGB; - return GL_COMPRESSED_RGB_S3TC_DXT1_EXT; - case GL_RGBA: - case GL_RGBA8: - //return GL_COMPRESSED_RGBA; - return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; - case GL_ALPHA: - case GL_ALPHA8: - return GL_COMPRESSED_ALPHA; - default: - return format; - } -*/ -} - // ===================================================== // class Texture1DGl // ===================================================== @@ -147,6 +147,7 @@ void Texture1DGl::init(Filter filter, int maxAnisotropy){ GLint wrap= toWrapModeGl(wrapMode); GLint glFormat= toFormatGl(format, pixmap.getComponents()); GLint glInternalFormat= toInternalFormatGl(format, pixmap.getComponents()); + GLint glCompressionFormat = toCompressionFormatGl(glInternalFormat); //pixel init var const uint8* pixels= pixmapInit? pixmap.getPixels(): NULL; @@ -171,7 +172,7 @@ void Texture1DGl::init(Filter filter, int maxAnisotropy){ glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); int error= gluBuild1DMipmaps( - GL_TEXTURE_1D, glInternalFormat, pixmap.getW(), + GL_TEXTURE_1D, glCompressionFormat, pixmap.getW(), glFormat, GL_UNSIGNED_BYTE, pixels); if(error!=0){ @@ -187,7 +188,7 @@ void Texture1DGl::init(Filter filter, int maxAnisotropy){ glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexImage1D( - GL_TEXTURE_1D, 0, glInternalFormat, pixmap.getW(), + GL_TEXTURE_1D, 0, glCompressionFormat, pixmap.getW(), 0, glFormat, GL_UNSIGNED_BYTE, pixels); GLint error= glGetError(); @@ -199,6 +200,7 @@ void Texture1DGl::init(Filter filter, int maxAnisotropy){ } } inited= true; + OutputTextureDebugInfo(format, pixmap.getComponents(),getPath()); } assertGl(); @@ -280,17 +282,17 @@ void Texture2DGl::init(Filter filter, int maxAnisotropy){ sprintf(szBuf,"Error creating texture 2D, returned: %d [%s] w = %d, h = %d, glInternalFormat = %d, glFormat = %d",error,pixmap.getPath().c_str(),pixmap.getW(),pixmap.getH(),glInternalFormat,glFormat); throw runtime_error(szBuf); } - - OutputTextureDebugInfo(&pixmap,format, pixmap.getComponents(),getPath()); } inited= true; + OutputTextureDebugInfo(format, pixmap.getComponents(),getPath()); } assertGl(); } void Texture2DGl::end(){ - if(inited){ + if(inited) { + //printf("==> Deleting GL Texture [%s] handle = %d\n",getPath().c_str(),handle); assertGl(); glDeleteTextures(1, &handle); assertGl(); @@ -310,6 +312,7 @@ void Texture3DGl::init(Filter filter, int maxAnisotropy){ GLint wrap= toWrapModeGl(wrapMode); GLint glFormat= toFormatGl(format, pixmap.getComponents()); GLint glInternalFormat= toInternalFormatGl(format, pixmap.getComponents()); + GLint glCompressionFormat = toCompressionFormatGl(glInternalFormat); //pixel init var const uint8* pixels= pixmapInit? pixmap.getPixels(): NULL; @@ -328,7 +331,7 @@ void Texture3DGl::init(Filter filter, int maxAnisotropy){ glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexImage3D( - GL_TEXTURE_3D, 0, glInternalFormat, + GL_TEXTURE_3D, 0, glCompressionFormat, pixmap.getW(), pixmap.getH(), pixmap.getD(), 0, glFormat, GL_UNSIGNED_BYTE, pixels); @@ -340,6 +343,8 @@ void Texture3DGl::init(Filter filter, int maxAnisotropy){ throw runtime_error(szBuf); } inited= true; + + OutputTextureDebugInfo(format, pixmap.getComponents(),getPath()); } assertGl(); @@ -422,7 +427,7 @@ void TextureCubeGl::init(Filter filter, int maxAnisotropy){ throw runtime_error(szBuf); } - OutputTextureDebugInfo(currentPixmap,format, currentPixmap->getComponents(),getPath()); + OutputTextureDebugInfo(format, currentPixmap->getComponents(),getPath()); } inited= true; @@ -439,29 +444,28 @@ void TextureCubeGl::end(){ } } -void TextureGl::OutputTextureDebugInfo(const Pixmap2D *pixmap,Texture::Format format, int components,const string path) { +void TextureGl::OutputTextureDebugInfo(Texture::Format format, int components,const string path) { + if(Texture::useTextureCompression == true) { + GLint glFormat= toFormatGl(format, components); -/* - GLint glFormat= toFormatGl(format, components); + printf("**** Texture filename: [%s] format = %d components = %d, glFormat = %d\n",path.c_str(),format,components,glFormat); - printf("**** Texture filename: [%s] format = %d components = %d, glFormat = %d, path [%s]\n",pixmap->getPath().c_str(),format,components,glFormat,path.c_str()); + GLint compressed=0; + glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED, &compressed); + int error = glGetError(); - GLint compressed=0; - glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED, &compressed); - int error = glGetError(); + printf("**** Texture compressed status: %d, error [%d]\n",compressed,error); - printf("**** Texture compressed status: %d, error [%d]\n",compressed,error); + compressed=0; + glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_IMAGE_SIZE, &compressed); + error = glGetError(); + printf("**** Texture image size in video RAM: %d, error [%d]\n",compressed,error); - compressed=0; - glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_IMAGE_SIZE, &compressed); - error = glGetError(); - printf("**** Texture image size in video RAM: %d, error [%d]\n",compressed,error); - - compressed=0; - glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &compressed); - error = glGetError(); - printf("**** Texture image compression format used: %d, error [%d]\n",compressed,error); -*/ + compressed=0; + glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &compressed); + error = glGetError(); + printf("**** Texture image compression format used: %d, error [%d]\n",compressed,error); + } } }}}//end namespace diff --git a/source/shared_lib/sources/graphics/model.cpp b/source/shared_lib/sources/graphics/model.cpp index 15f2231a..b7be2407 100644 --- a/source/shared_lib/sources/graphics/model.cpp +++ b/source/shared_lib/sources/graphics/model.cpp @@ -34,7 +34,8 @@ using namespace Util; // ==================== constructor & destructor ==================== -Mesh::Mesh(){ +Mesh::Mesh() { + textureManager = NULL; frameCount= 0; vertexCount= 0; indexCount= 0; @@ -48,24 +49,25 @@ Mesh::Mesh(){ for(int i=0; igetPath().c_str(),i); + textureManager->endTexture(textures[i]); + } + } + } + textureManager = NULL; } // ========================== shadows & interpolation ========================= @@ -91,7 +103,8 @@ void Mesh::updateInterpolationVertices(float t, bool cycle) const{ // ==================== load ==================== -void Mesh::loadV2(const string &dir, FILE *f, TextureManager *textureManager){ +void Mesh::loadV2(const string &dir, FILE *f, TextureManager *textureManager,bool deletePixMapAfterLoad) { + this->textureManager = textureManager; //read header MeshHeaderV2 meshHeader; size_t readBytes = fread(&meshHeader, sizeof(MeshHeaderV2), 1, f); @@ -125,6 +138,12 @@ void Mesh::loadV2(const string &dir, FILE *f, TextureManager *textureManager){ if(textures[mtDiffuse]==NULL){ textures[mtDiffuse]= textureManager->newTexture2D(); textures[mtDiffuse]->load(texPath); + texturesOwned[mtDiffuse]=true; + // M.V. Test + textures[mtDiffuse]->init(textureManager->getTextureFilter(),textureManager->getMaxAnisotropy()); + if(deletePixMapAfterLoad == true) { + textures[mtDiffuse]->deletePixels(); + } } } @@ -140,7 +159,9 @@ void Mesh::loadV2(const string &dir, FILE *f, TextureManager *textureManager){ readBytes = fread(indices, sizeof(uint32)*indexCount, 1, f); } -void Mesh::loadV3(const string &dir, FILE *f, TextureManager *textureManager){ +void Mesh::loadV3(const string &dir, FILE *f, TextureManager *textureManager,bool deletePixMapAfterLoad) { + this->textureManager = textureManager; + //read header MeshHeaderV3 meshHeader; size_t readBytes = fread(&meshHeader, sizeof(MeshHeaderV3), 1, f); @@ -170,6 +191,12 @@ void Mesh::loadV3(const string &dir, FILE *f, TextureManager *textureManager){ if(textures[mtDiffuse]==NULL){ textures[mtDiffuse]= textureManager->newTexture2D(); textures[mtDiffuse]->load(texPath); + texturesOwned[mtDiffuse]=true; + // M.V. Test + textures[mtDiffuse]->init(textureManager->getTextureFilter(),textureManager->getMaxAnisotropy()); + if(deletePixMapAfterLoad == true) { + textures[mtDiffuse]->deletePixels(); + } } } @@ -187,7 +214,9 @@ void Mesh::loadV3(const string &dir, FILE *f, TextureManager *textureManager){ readBytes = fread(indices, sizeof(uint32)*indexCount, 1, f); } -void Mesh::load(const string &dir, FILE *f, TextureManager *textureManager){ +void Mesh::load(const string &dir, FILE *f, TextureManager *textureManager,bool deletePixMapAfterLoad) { + this->textureManager = textureManager; + //read header MeshHeader meshHeader; size_t readBytes = fread(&meshHeader, sizeof(MeshHeader), 1, f); @@ -222,10 +251,15 @@ void Mesh::load(const string &dir, FILE *f, TextureManager *textureManager){ textures[i]= static_cast(textureManager->getTexture(mapFullPath)); if(textures[i]==NULL){ textures[i]= textureManager->newTexture2D(); - if(meshTextureChannelCount[i]!=-1){ + 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; @@ -313,6 +347,14 @@ void Mesh::computeTangents(){ } } +void Mesh::deletePixels() { + for(int i = 0; i < meshTextureCount; ++i) { + if(textures[i] != NULL) { + textures[i]->deletePixels(); + } + } +} + // =============================================== // class Model // =============================================== @@ -359,7 +401,7 @@ uint32 Model::getTriangleCount() const{ return triangleCount; } -uint32 Model::getVertexCount() const{ +uint32 Model::getVertexCount() const { uint32 vertexCount= 0; for(uint32 i=0; ipath= path; } +string Texture1D::getPath() const { + return (pixmap.getPath() != "" ? pixmap.getPath() : path); +} + +void Texture1D::deletePixels() { + //printf("+++> Texture pixmap deletion for [%s]\n",getPath().c_str()); + pixmap.deletePixels(); +} + // ===================================================== // class Texture2D // ===================================================== @@ -64,6 +74,15 @@ void Texture2D::load(const string &path){ this->path= path; } +string Texture2D::getPath() const { + return (pixmap.getPath() != "" ? pixmap.getPath() : path); +} + +void Texture2D::deletePixels() { + //printf("+++> Texture pixmap deletion for [%s]\n",getPath().c_str()); + pixmap.deletePixels(); +} + // ===================================================== // class Texture3D // ===================================================== @@ -79,6 +98,15 @@ void Texture3D::loadSlice(const string &path, int slice){ this->path= path; } +string Texture3D::getPath() const { + return (pixmap.getPath() != "" ? pixmap.getPath() : path); +} + +void Texture3D::deletePixels() { + //printf("+++> Texture pixmap deletion for [%s]\n",getPath().c_str()); + pixmap.deletePixels(); +} + // ===================================================== // class TextureCube // ===================================================== @@ -94,4 +122,25 @@ void TextureCube::loadFace(const string &path, int face){ this->path= path; } +string TextureCube::getPath() const { + string result = ""; + for(int i = 0; i < 6; ++i) { + if(pixmap.getPath(i) != "") { + if(result != "") { + result += ","; + } + result += pixmap.getPath(i); + } + } + if(result == "") { + result = path; + } + return result; +} + +void TextureCube::deletePixels() { + //printf("+++> Texture pixmap deletion for [%s]\n",getPath().c_str()); + pixmap.deletePixels(); +} + }}//end namespace