jubilant-funicular
SpriteBatch.h
1 #ifndef NTA_SPRITEBATCH_H_INCLUDED
2 #define NTA_SPRITEBATCH_H_INCLUDED
3 
4 #include <vector>
5 
6 #include "nta/Vertex.h"
7 #include "nta/utils.h"
8 
9 #define NTA_DEFAULT_DEPTH 0.5
10 
11 namespace nta {
13  struct Glyph {
14  Glyph(crvec4 posRect, crvec4 uvRect, GLuint texture, float d, crvec4 color) :
15  textureID(texture), depth(d) {
16  topLeft.setPosition(posRect.x, posRect.y);
17  topLeft.setUV(uvRect.x, uvRect.y);
18  topLeft.color = color;
19  topLeft.hasTexture = texture != 0;
20  topRight.setPosition(posRect.x + posRect[2], posRect.y);
21  topRight.setUV(uvRect.x + uvRect[2], uvRect.y);
22  topRight.color = color;
23  topRight.hasTexture = texture != 0;
24  botRight.setPosition(posRect.x + posRect[2], posRect.y - posRect[3]);
25  botRight.setUV(uvRect.x + uvRect[2], uvRect.y + uvRect[3]);
26  botRight.color = color;
27  botRight.hasTexture = texture != 0;
28  botLeft.setPosition(posRect.x, posRect.y - posRect[3]);
29  botLeft.setUV(uvRect.x, uvRect.y + uvRect[3]);
30  botLeft.color = color;
31  botLeft.hasTexture = texture != 0;
32  }
34  Glyph(crvec4 posRect, crvec4 uvRect, GLuint tex, float d, crvec4 col, float angle) :
35  textureID(tex), depth(d) {
36  glm::vec2 extents(posRect[2]/2.f, posRect[3]/2.f), pos(posRect.x, posRect.y);
37  glm::vec2 tl(-extents.x, extents.y), tr(extents.x, extents.y),
38  bl(-extents.x, -extents.y), br(extents.x, -extents.y);
39 
40  topLeft.pos = utils::rotate(tl, angle) - tl + pos;
41  topLeft.setUV(uvRect.x, uvRect.y);
42  topLeft.color = col;
43  topLeft.hasTexture = tex != 0;
44  topRight.pos = utils::rotate(tr, angle) - tl + pos;
45  topRight.setUV(uvRect.x + uvRect[2], uvRect.y);
46  topRight.color = col;
47  topRight.hasTexture = tex != 0;
48  botRight.pos = utils::rotate(br, angle) - tl + pos;
49  botRight.setUV(uvRect.x + uvRect[2], uvRect.y + uvRect[3]);
50  botRight.color = col;
51  botRight.hasTexture = tex != 0;
52  botLeft.pos = utils::rotate(bl, angle) - tl + pos;
53  botLeft.setUV(uvRect.x, uvRect.y + uvRect[3]);
54  botLeft.color = col;
55  botLeft.hasTexture = tex != 0;
56  }
58  GLuint textureID;
60  float depth;
63  Vertex2D topRight;
64  Vertex2D botRight;
65  Vertex2D botLeft;
66  };
68  struct RenderBatch {
70  RenderBatch(GLuint t, GLuint o, GLuint n, GLenum m = GL_TRIANGLES) :
71  textureID(t), offset(o), numVertices(n), mode(m) {
72  }
74  GLuint textureID;
76  GLuint offset;
78  GLuint numVertices;
80  GLenum mode;
81  };
87  class SpriteBatch {
88  private:
92  void createRenderBatches();
94  void sortGlyphs();
96  static bool compareTexture(Glyph* lhs, Glyph* rhs);
97  static bool compareDepth(Glyph* lhs, Glyph* rhs);
99  std::vector<Glyph*> m_glyphPointers;
101  std::vector<Glyph> m_glyphs;
103  std::vector<RenderBatch> m_renderBatches;
105  GLuint m_vao = 0;
106  GLuint m_vbo = 0;
107  public:
109  SpriteBatch();
110  ~SpriteBatch();
112  void init();
114  void begin();
116  void end();
119  void addGlyph(crvec4 posRect, GLuint texture = 0,
120  crvec4 uvRect = glm::vec4(0,0,1,1),
121  crvec4 color = glm::vec4(1),
122  float depth = NTA_DEFAULT_DEPTH);
123  void addGlyph(crvec4 posRect, crvec4 uvRect, GLuint texture,
124  float depth = NTA_DEFAULT_DEPTH, crvec4 color = glm::vec4(1));
125  void addGlyph(crvec2 corner1, crvec2 corner2, crvec4 uvRect, GLuint texture,
126  float depth = NTA_DEFAULT_DEPTH, crvec4 color = glm::vec4(1));
127  void addGlyph(crvec4 posRect, crvec4 uvRect, GLuint texture, crvec4 color,
128  float angle = 0.0f, float depth = NTA_DEFAULT_DEPTH);
129  void addGlyph(crvec2 corner1, crvec2 corner2, crvec4 uvRect, GLuint texture, crvec4 color,
130  float angle = 0.0f, float depth = NTA_DEFAULT_DEPTH);
132  void render() const;
133  };
134 }
135 
136 #endif // NTA_SPRITEBATCH_H_INCLUDED
nta::RenderBatch::offset
GLuint offset
the starting point of the batch in the vertex buffer
Definition: SpriteBatch.h:76
nta::SpriteBatch::init
void init()
initializes the batch
Definition: SpriteBatch.cpp:20
nta::RenderBatch::numVertices
GLuint numVertices
the number of vertices comprising the vertex buffer
Definition: SpriteBatch.h:78
nta::SpriteBatch::end
void end()
ends collection of glyphs and prepares to render
Definition: SpriteBatch.cpp:42
nta::SpriteBatch::m_renderBatches
std::vector< RenderBatch > m_renderBatches
the render batches used for rendering the glyphs
Definition: SpriteBatch.h:103
nta::Glyph::depth
float depth
the depth of the glyph
Definition: SpriteBatch.h:60
nta::SpriteBatch
Definition: SpriteBatch.h:87
nta::SpriteBatch::createRenderBatches
void createRenderBatches()
creates the render batches
Definition: SpriteBatch.cpp:66
nta::RenderBatch
stores information about batches of vertices with the same texture in a vertex buffer object
Definition: SpriteBatch.h:68
nta::SpriteBatch::compareTexture
static bool compareTexture(Glyph *lhs, Glyph *rhs)
comparers used to sort
Definition: SpriteBatch.cpp:63
nta::SpriteBatch::addGlyph
void addGlyph(crvec4 posRect, GLuint texture=0, crvec4 uvRect=glm::vec4(0, 0, 1, 1), crvec4 color=glm::vec4(1), float depth=0.5)
Definition: SpriteBatch.cpp:100
nta
Definition: Animation2D.h:6
nta::SpriteBatch::createVertexArrayObject
void createVertexArrayObject()
creates the vertex array object
Definition: SpriteBatch.cpp:23
nta::Glyph
represents what is essentially a sprite
Definition: SpriteBatch.h:13
nta::Glyph::Glyph
Glyph(crvec4 posRect, crvec4 uvRect, GLuint tex, float d, crvec4 col, float angle)
Definition: SpriteBatch.h:34
nta::SpriteBatch::sortGlyphs
void sortGlyphs()
sorts the glyphs
Definition: SpriteBatch.cpp:50
nta::Glyph::textureID
GLuint textureID
the texture used by the glyph
Definition: SpriteBatch.h:58
nta::SpriteBatch::m_glyphPointers
std::vector< Glyph * > m_glyphPointers
pointers to the glyphs to be rendered (used for sorting)
Definition: SpriteBatch.h:99
nta::utils::rotate
glm::vec2 rotate(crvec2 pt, float angle)
Rotates a point (about the origin) by the given angle.
Definition: utils.cpp:29
nta::RenderBatch::mode
GLenum mode
the primitive type to be drawn (GL_POINTS, GL_LINES, etc.)
Definition: SpriteBatch.h:80
nta::Glyph::topLeft
Vertex2D topLeft
the vertices of the four corners of the glyph
Definition: SpriteBatch.h:62
nta::RenderBatch::textureID
GLuint textureID
the texture used by the batch
Definition: SpriteBatch.h:74
nta::Vertex2D
represents a vertex in 2 dimensions
Definition: Vertex.h:26
nta::SpriteBatch::m_glyphs
std::vector< Glyph > m_glyphs
the glyphs to be rendered (used for fast vector operations)
Definition: SpriteBatch.h:101
nta::Vertex2D::pos
glm::vec2 pos
the vertex's position, color, and uv coordinates, respectively
Definition: Vertex.h:66
nta::Vertex2D::setPosition
void setPosition(float x, float y)
sets the position of the vertex
Definition: Vertex.h:43
nta::RenderBatch::RenderBatch
RenderBatch(GLuint t, GLuint o, GLuint n, GLenum m=GL_TRIANGLES)
constructor
Definition: SpriteBatch.h:70
nta::SpriteBatch::begin
void begin()
begins collection of glyphs for the batch
Definition: SpriteBatch.cpp:38
nta::SpriteBatch::SpriteBatch
SpriteBatch()
constructor and destructor
Definition: SpriteBatch.cpp:7
nta::SpriteBatch::m_vao
GLuint m_vao
ids of the vertex buffer object and vertex array object used to render the glyphs
Definition: SpriteBatch.h:105
nta::SpriteBatch::render
void render() const
renders the batch
Definition: SpriteBatch.cpp:122
nta::Vertex2D::setUV
void setUV(float u, float v)
sets the uv coordinates of the vertex
Definition: Vertex.h:61