1 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */ 2 3 #ifndef IATLAS_ALLOC_H 4 #define IATLAS_ALLOC_H 5 6 #include "System/float4.h" 7 #include "System/type2.h" 8 #include <string> 9 #include <map> 10 11 12 class IAtlasAllocator 13 { 14 public: IAtlasAllocator()15 IAtlasAllocator() : maxsize(2048, 2048), npot(false) {} ~IAtlasAllocator()16 virtual ~IAtlasAllocator() {} 17 SetMaxSize(int xsize,int ysize)18 void SetMaxSize(int xsize, int ysize) { maxsize = int2(xsize, ysize); } SetNonPowerOfTwo(bool nonPowerOfTwo)19 void SetNonPowerOfTwo(bool nonPowerOfTwo) { npot = nonPowerOfTwo; } 20 21 public: 22 virtual bool Allocate() = 0; 23 virtual int GetMaxMipMaps() = 0; 24 25 public: 26 void AddEntry(const std::string& name, int2 size, void* data = NULL) 27 { 28 entries[name] = SAtlasEntry(size, data); 29 } 30 GetEntry(const std::string & name)31 float4 GetEntry(const std::string& name) 32 { 33 return entries[name].texCoords; 34 } 35 GetEntryData(const std::string & name)36 void*& GetEntryData(const std::string& name) 37 { 38 assert(entries[name].data); 39 return entries[name].data; 40 } 41 GetTexCoords(const std::string & name)42 float4 GetTexCoords(const std::string& name) 43 { 44 float4 uv(entries[name].texCoords); 45 uv.x1 /= atlasSize.x; 46 uv.y1 /= atlasSize.y; 47 uv.x2 /= atlasSize.x; 48 uv.y2 /= atlasSize.y; 49 50 // adjust texture coordinates by half a texel (opengl uses centeroids) 51 uv.x1 += 0.5f / atlasSize.x; 52 uv.y1 += 0.5f / atlasSize.y; 53 uv.x2 += 0.5f / atlasSize.x; 54 uv.y2 += 0.5f / atlasSize.y; 55 56 return uv; 57 } 58 contains(const std::string & name)59 bool contains(const std::string& name) const 60 { 61 return (entries.find(name) != entries.end()); 62 } 63 64 //! note: it doesn't clear the atlas! it only clears the entry db! clear()65 void clear() 66 { 67 entries.clear(); 68 } 69 //auto begin() { return entries.begin(); } 70 //auto end() { return entries.end(); } 71 GetMaxSize()72 int2 GetMaxSize() const { return maxsize; } GetAtlasSize()73 int2 GetAtlasSize() const { return atlasSize; } 74 75 protected: 76 struct SAtlasEntry 77 { SAtlasEntrySAtlasEntry78 SAtlasEntry() : data(NULL) {} sizeSAtlasEntry79 SAtlasEntry(const int2 _size, void* _data = NULL) : size(_size), data(_data) {} 80 81 int2 size; 82 float4 texCoords; 83 void* data; 84 }; 85 86 std::map<std::string, SAtlasEntry> entries; 87 88 int2 atlasSize; 89 int2 maxsize; 90 91 bool npot; 92 }; 93 94 #endif // IATLAS_ALLOC_H 95