1 #include "nta/DebugBatch.h"
2 #include "nta/PrimitiveBatch.h"
8 DebugBatch::~DebugBatch() {
9 if (m_ibo != 0) glDeleteBuffers(1, &m_ibo);
10 if (m_vbo != 0) glDeleteBuffers(1, &m_vbo);
11 if (m_vao != 0) glDeleteVertexArrays(1, &m_vao);
12 m_ibo = m_vbo = m_vao = 0;
18 if (m_ibo == 0) glGenBuffers(1, &m_ibo);
19 if (m_vbo == 0) glGenBuffers(1, &m_vbo);
20 if (m_vao == 0) glGenVertexArrays(1, &m_vao);
22 glBindVertexArray(m_vao);
23 glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
24 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo);
26 for (
int i = 0; i < NUM_VERTEX_ATTRIBS; i++) {
27 auto& attrib = Vertex2D::attribs[i];
29 glEnableVertexAttribArray(i);
30 glVertexAttribPointer(i, attrib.size, attrib.type, attrib.normalized,
sizeof(
Vertex2D),
40 glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
41 glBufferData(GL_ARRAY_BUFFER,
sizeof(
Vertex2D)*m_vertices.size(),
nullptr, GL_DYNAMIC_DRAW);
42 glBufferSubData(GL_ARRAY_BUFFER, 0,
sizeof(
Vertex2D)*m_vertices.size(), m_vertices.data());
43 glBindBuffer(GL_ARRAY_BUFFER, 0);
45 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo);
46 glBufferData(GL_ELEMENT_ARRAY_BUFFER,
sizeof(GLuint)*m_indices.size(),
nullptr, GL_DYNAMIC_DRAW);
47 glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0,
sizeof(GLuint)*m_indices.size(), m_indices.data());
48 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
51 const int start_index = m_vertices.size();
52 const glm::vec2 diff =
end - start;
53 m_vertices.reserve(start_index + num_pieces + 1);
54 m_indices.reserve(m_indices.size() + 2*num_pieces);
55 for (
int i = 0; i <= num_pieces; i++) {
56 m_vertices.emplace_back(start + (
float)i*diff/(
float)num_pieces, color);
58 m_indices.push_back(start_index + i - 1);
59 m_indices.push_back(start_index + i);
66 void DebugBatch::addRect(crvec4 posRect, crvec4 color,
float orientation) {
67 const int start = m_vertices.size();
68 glm::vec2 half_dims(posRect[2]/2, posRect[3]/2);
69 glm::vec2 center(posRect.x + half_dims.x, posRect.y - half_dims.y);
71 glm::vec2 tl = center + glm::vec2(-half_dims.x, half_dims.y),
72 tr = center + half_dims,
73 br = center - glm::vec2(-half_dims.x, half_dims.y),
74 bl = center - half_dims;
76 m_vertices.emplace_back(center +
utils::rotate(tl, orientation), color);
77 m_vertices.emplace_back(center +
utils::rotate(tr, orientation), color);
78 m_vertices.emplace_back(center +
utils::rotate(br, orientation), color);
79 m_vertices.emplace_back(center +
utils::rotate(bl, orientation), color);
81 for (
int offset = 0; offset < 4; offset++) {
82 m_indices.push_back(start + offset);
83 m_indices.push_back(start + (offset + 1)%4);
86 void DebugBatch::addRect(crvec4 posRect,
float orientation, crvec4 color) {
87 addRect(posRect, color, orientation);
89 void DebugBatch::addCircle(crvec2 center,
float radius, crvec4 color) {
90 static const std::size_t NUM_SIDES = 100;
91 float side_length = radius * glm::sqrt(2.-2.*glm::cos(2.*M_PI/NUM_SIDES));
92 addPolygon(NUM_SIDES, center, side_length, 0., color);
94 void DebugBatch::addPolygon(std::size_t numSides, crvec2 center,
float sideLength,
95 float orientation, crvec4 color) {
96 const int start = m_vertices.size();
98 Primitive poly(numSides, center, sideLength, color, orientation, NTA_DEFAULT_DEPTH);
99 m_vertices.insert(m_vertices.end(), std::begin(poly.vertices), std::end(poly.vertices));
101 for (
int offset = 0; offset < poly.vertices.size(); offset++) {
102 m_indices.push_back(start + offset);
103 m_indices.push_back(start + (offset + 1)%poly.vertices.size());
107 glBindVertexArray(m_vao);
108 glDrawElements(GL_LINES, m_indices.size(), GL_UNSIGNED_INT, 0);
109 glBindVertexArray(0);