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