jubilant-funicular
PrimitiveBatch.h
1 #ifndef PRIMITIVEBATCH_H_INCLUDED
2 #define PRIMITIVEBATCH_H_INCLUDED
3 
4 #include "nta/SpriteBatch.h"
5 
6 namespace nta {
8  struct Primitive {
9  Primitive(){}
11  Primitive(const std::initializer_list<Vertex2D>& verts,
12  GLuint texID, float d) : depth(d), textureID(texID) {
13  vertices.insert(vertices.begin(), verts.begin(), verts.end());
14  }
15  template<class Iterator>
16  Primitive(Iterator first, Iterator last, GLuint texID, float d) : depth(d), textureID(texID) {
17  vertices.insert(vertices.begin(), first, last);
18  }
20  Primitive(std::size_t numSides, crvec2 center, float sideLength,
21  crvec4 color, float orientation, float d) : depth(d), textureID(0) {
22  std::vector<Vertex2D> verts;
23  float theta = 2.*M_PI/numSides;
24  float a = sideLength/glm::sqrt(2.-2.*glm::cos(theta));
25  for (std::size_t i = 0; i < numSides; i++) {
26  float angle = i*theta + orientation;
27  glm::vec2 translate(glm::cos(angle), glm::sin(angle));
28  verts.emplace_back(a * translate + center, color);
29  }
30  vertices.insert(vertices.begin(), std::begin(verts), std::end(verts));
31  }
34  depth = 0;
35  textureID = 0;
36  vertices.clear();
37  }
39  float depth;
41  GLuint textureID;
43  std::vector<Vertex2D> vertices;
44  };
47  private:
51  void createRenderBatches();
53  void sortPrimitives();
55  static bool compareTexture(Primitive* lhs, Primitive* rhs);
56  static bool comparePrimitive(Primitive* lhs, Primitive* rhs);
57  static bool compareDepth(Primitive* lhs, Primitive* rhs);
59  GLenum toPrimitiveType(unsigned int numVertices) const;
62  std::vector<Primitive*> m_primitives;
64  std::vector<RenderBatch> m_renderBatches;
66  GLuint m_vao = 0;
67  GLuint m_vbo = 0;
68  public:
71  ~PrimitiveBatch();
73  int numPrimitives() const;
75  void init();
77  void begin();
79  void end();
81  void addPrimitive(Primitive* primitive);
82  void addPrimitive(const std::initializer_list<Vertex2D>& vertices,
83  GLuint textureID = -1, float depth = NTA_DEFAULT_DEPTH);
84  template<class Iterator>
85  void addPrimitive(Iterator first, Iterator last, GLuint textureID = -1,
86  float depth = NTA_DEFAULT_DEPTH);
87  void addPrimitive(std::size_t numSides, crvec2 center = glm::vec2(0), float sideLength = 1.0,
88  crvec4 color = glm::vec4(1), float orientation = 0.,
89  float depth = NTA_DEFAULT_DEPTH);
91  void render() const;
92  };
93  typedef PrimitiveBatch PrimBatch;
94 
95  template<class Iterator>
96  void PrimitiveBatch::addPrimitive(Iterator first, Iterator last, GLuint textureID, float depth) {
97  addPrimitive(new Primitive(first, last, textureID, depth));
98  }
99 }
100 
101 #endif // PRIMITIVEBATCH_H_INCLUDED
nta::PrimitiveBatch::PrimitiveBatch
PrimitiveBatch()
constructor and destructor
Definition: PrimitiveBatch.cpp:6
nta::PrimitiveBatch::render
void render() const
renders the primitives
Definition: PrimitiveBatch.cpp:148
nta::PrimitiveBatch
represents a collection of primitives to be drawn
Definition: PrimitiveBatch.h:46
nta::PrimitiveBatch::createVertexArrayObject
void createVertexArrayObject()
creates vertex array object
Definition: PrimitiveBatch.cpp:27
nta::PrimitiveBatch::toPrimitiveType
GLenum toPrimitiveType(unsigned int numVertices) const
return primitive to be drawn
Definition: PrimitiveBatch.cpp:128
nta::Primitive::vertices
std::vector< Vertex2D > vertices
the vertices that make up the primitive
Definition: PrimitiveBatch.h:43
nta::Primitive::~Primitive
~Primitive()
destructor
Definition: PrimitiveBatch.h:33
nta::PrimitiveBatch::createRenderBatches
void createRenderBatches()
creates render batches
Definition: PrimitiveBatch.cpp:86
nta::PrimitiveBatch::init
void init()
initializes the batch
Definition: PrimitiveBatch.cpp:24
nta::PrimitiveBatch::numPrimitives
int numPrimitives() const
returns number of primitives to be rendered
Definition: PrimitiveBatch.cpp:21
nta::PrimitiveBatch::begin
void begin()
begins collection of primitive
Definition: PrimitiveBatch.cpp:45
nta::PrimitiveBatch::m_primitives
std::vector< Primitive * > m_primitives
Definition: PrimitiveBatch.h:62
nta::Primitive::Primitive
Primitive(const std::initializer_list< Vertex2D > &verts, GLuint texID, float d)
Constructs a primitive from a list of vertices.
Definition: PrimitiveBatch.h:11
nta
Definition: Animation2D.h:6
nta::Primitive::textureID
GLuint textureID
the texture used by the primitive
Definition: PrimitiveBatch.h:41
nta::PrimitiveBatch::m_renderBatches
std::vector< RenderBatch > m_renderBatches
the render batches used to draw the primitives
Definition: PrimitiveBatch.h:64
nta::Primitive::Primitive
Primitive(std::size_t numSides, crvec2 center, float sideLength, crvec4 color, float orientation, float d)
Constructs an untextured regular polygon.
Definition: PrimitiveBatch.h:20
nta::PrimitiveBatch::addPrimitive
void addPrimitive(Primitive *primitive)
adds a primitive to the batch
Definition: PrimitiveBatch.cpp:137
nta::PrimitiveBatch::compareTexture
static bool compareTexture(Primitive *lhs, Primitive *rhs)
comparers used for sorting
Definition: PrimitiveBatch.cpp:80
nta::PrimitiveBatch::end
void end()
ends collection of primitive and prepares for rendering
Definition: PrimitiveBatch.cpp:52
nta::Primitive
represents a primitive (point, line, triangle, etc.)
Definition: PrimitiveBatch.h:8
nta::PrimitiveBatch::m_vao
GLuint m_vao
ids for the vertex buffer object and vertex array object used for rendering
Definition: PrimitiveBatch.h:66
nta::PrimitiveBatch::sortPrimitives
void sortPrimitives()
sorts primitives
Definition: PrimitiveBatch.cpp:56
nta::Primitive::depth
float depth
the depth of the primitive
Definition: PrimitiveBatch.h:39