Salmon Engine/Resource Manager/TexList - Texture Manager

From Fish Run Games SDK
Jump to: navigation, search

TexList is a texture manager. TexList keeps information about textures loaded to video memory. Texture manager have texture reference counting and understands PNG, BMP, BMP32 and not compressed TGA formats. Compressed TGA format is not supported. Max size of texture is 2048x2048, minimal size – 2x2. There are no more requirements for texture size – when they are loaded, they scaled up to have width and height equal to power of 2.

To load texture, just call AddTexture method, giving relative path to texture file and texture name:

ResourceManager->TexList.AddTexture("chip1.bmp", "ChipTexture");

If no texture name provided, texture will have name that is similar to its file name. For example, when you add texture like this:

ResourceManager->TexList.AddTexture("textures/new_texture.bmp");

New texture will be known as "new_texture.bmp".

File type is recognized by file extension. Allowed file extensions: bmp, bmp32, png and tga. All of them must have 24 or 32 bits per pixel – RGB or RGBA.

Also, textures could be serialized.

To bind texture, you must use texture ID. To get texture ID, just request texture by name:

cardinal idTex1 = ResourceManager->TexList["ChipTexture"];
cardinal idTex2 = ResourceManager->TexList["new_texture.bmp"];

You can bind texture, using OpenGL's glBindTexture:

glBindTexture(GL_TEXTURE_2D, ResourceManager->TexList["new_texture.bmp"]);
glBindTexture(GL_TEXTURE_2D, idTex1);

Beware: If texture with the same name asked to be loaded again, it will not be loaded – instead, reference counter for that texture name will increase. Second texture with same name will not be loaded even if it is completely different from the previous one (with the same name), no matter if it stays in different directory, or have different sizes. So manage texture names correctly and do not make name collisions. To delete texture, call DeleteTexture method, passing texture name or texture ID:

//Ok
ResourceManager->TexList.DeleteTexture("ChipTexture");
//Ok
cardinal id = ResourceManager->TexList["new_texture.bmp"];
ResourceManager->TexList.DeleteTexture(id);

If texture loaded twice, then, due to reference counting, it must be deleted twice.

You don't have to delete textures when you finish your app – all loaded textures will be deleted automatically.

Coming soon: Cubemaps, empty textures for render buffer

Deeply

TexList works in a simple way – it loads file, check file format from extension, then convert file to TTextureData struct and pass this struct to load by OpenGL. Here is TTextureData struct:

struct TTextureData
{
cardinal Width;    //Width, pixels
cardinal Height;   //Height, pixels
char Format[8];    //Output format: can be "bmp24" or "bmp32"
cardinal DataSize; //Data size (bytes)
boost::shared_array<char> Data; //Image raw data
};

Format is a 7-symbol string, it can be "bmp24" (3 bytes per pixel) or "bmp32" (4 bytes per pixel).

These struct is to be filled in methods starting on "CreateTexData":

bool CreateTexDataFromBmp24(const std::string& filename, TTextureData& texData);
bool CreateTexDataFromBmp32(const std::string& filename, TTextureData& texData);
bool CreateTexDataFromTga(const std::string& filename, TTextureData& texData);
bool CreateTexDataFromPng(const std::string& filename, TTextureData& texData);

After being loaded from file, textures are resized to fit power of 2 height/width, using bilinear filtration (from boost GIL). Then, depending on format (24 or 32 bits), TTextureData struct are loaded to OpenGL. Also, instead of 1 texture, you can load cubemap, by giving pointer to array of 6 TTextureData structs. In that case, cubemap is loaded in sequence: +x, -x, +y, -y, +z, -z. You can use only bmp24 format to load to cubemap. After loading texture, TexList return texture ID. Here are those methods:

cardinal AddTextureBmp24Data(const TTextureData& texData);
cardinal AddTextureBmp32Data(const TTextureData& texData);
cardinal AddCubemapTextureBmp24Data(TTextureData* texData);

Other methods are given shortly:

void Clear();
//Clean everything

virtual void Serialize(boost::property_tree::ptree& propertyTree);


cardinal operator[](const std::string& s);
//Get texture ID by name. Returnn 0 if not found

cardinal GetTextureHeight(const std::string& texName);
//Get texture height by name

cardinal GetTextureWidth(const std::string& texName);
//Get texture width by name

cardinal AddTextureDirectly(const std::string& filename, std::string texName = ""); 
//Add texture directly from file. You must give here full file path to texture

cardinal AddTexture(const std::string& fileName, std::string texName = "");
//Add texture from resources path

cardinal AddTextureFromUserdata(const std::string& fileName, std::string texName = ""); 
//Try to add texture from user data, and, if not found – from resources

cardinal AddCubemapTexture(std::string filename[6]);
//Add cubemap made by 6 textures. Texture order: +x, -x, +y, -y, +z, -z 


cardinal AddEmptyTexture(const std::string& texName, cardinal width, cardinal height);
//Add empty texture (for frame buffer)

cardinal AddEmptyCubemapTexture(const std::string& texName, cardinal width, cardinal height);
//Add empty cubemap texture (for frame buffer)


void DeleteTexture(const std::string& texName);
//Delete texture by name. If texture loaded N times, then only reference counter is decreased

void DeleteTexture(cardinal texID);
//Delete texture by ID. If texture loaded N times, then only reference counter is decreased

void PrintTextureList();
//Print texture list to log

virtual void BindFunctions();

void SaveTexDataToPlainBmpToUserData(const std::string& fileName, TTextureData texData);
//save TTextureData to file inside user data. It is not needed to include userdata path here
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox