-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathatlas.h
74 lines (63 loc) · 1.66 KB
/
atlas.h
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
/* Copyright 2011 Pyarelal Knowles, under GNU LGPL (see LICENCE.txt) */
#ifndef TEXTURE_ATLAS_H
#define TEXTURE_ATLAS_H
#define TEXTURE_ATLAS_MAX_SIZE 8192
#include "vec.h"
#include "shader.h"
#include "gpu.h"
#include <vector>
#include <map>
#include <string>
/*
GLSL:
struct Tex
{
vec2 pos; //normalized position within atlas
vec2 size; //normalized size within atlas
}
*/
class TextureAtlas
{
public:
struct Tex : public ShaderUniform
{
vec2f npos;
vec2f nsize;
vec2i size;
protected:
virtual bool setUniform(Shader* program, const std::string& name) const;
};
vec2i size;
protected:
struct Source
{
std::string name;
vec2i pos;
vec2i size;
unsigned int tex;
bool packed;
//for greedy packing using std::sort
bool operator<(const Source& o) const {return size.x*size.y < o.size.x*o.size.y;}
};
vec2i packMin;
bool dirty;
int cur; //current source index in sources being packed
std::vector<Tex> textures;
std::vector<Source> sources;
std::map<std::string, int> texMap;
void recursivePack(vec2i packOffset, vec2i packSize);
virtual bool createPack(vec2i testSize); //attempts to pack into a testSize sized atlas
void upload(); //upload current packing in sources to a new texture
public:
Texture2D atlas;
TextureAtlas();
virtual ~TextureAtlas();
bool isDirty();
void cleanup(bool deleteSource); //free sources, leaving just the atlas
void add(std::string name, GLuint tex); //add a source texture
bool has(std::string name);
Tex get(std::string name);
bool pack(); //packs the added sources into the atlas
operator GLuint() const {return atlas;}
};
#endif