BARE2D
DebugRenderer.cpp
Go to the documentation of this file.
1 #include "DebugRenderer.hpp"
2 
3 #include "GLContextManager.hpp"
4 
5 #define CIRCLE_LINES 90
6 
7 const char* fragShaderSource = "#version 130\n"
8  "in vec3 fragmentPosition;\n"
9  "in vec4 fragmentColour;\n"
10  "out vec4 colour;\n"
11  "void main() {\n"
12  " colour = fragmentColour;\n"
13  "}\n"
14  "\0";
15 
16 const char* vertShaderSource = "#version 130\n"
17  "in vec3 vertexPosition;\n"
18  "in vec4 vertexColour;\n"
19  "out vec3 fragmentPosition;\n"
20  "out vec4 fragmentColour;\n"
21  "void main() {\n"
22  " gl_Position = vec4(vertexPosition.xyz, 1.0);\n"
23  " fragmentPosition = vertexPosition;\n"
24  " fragmentColour = vertexColour;\n"
25  "}\n"
26  "\0";
27 
28 namespace BARE2D {
29 
30  void Line::construct(glm::vec2 p0, glm::vec2 p1, float& thick, Colour& col) {
31  point0 = p0;
32  point1 = p1;
33  thickness = thick;
34  colour = col;
35  }
36 
37  void Circle::construct(glm::vec2& p0, float& lineThick, float& radius, Colour& fillCol) {
38  origin = p0;
39 
40  float lastX = 0;
41  float lastY = radius;
42 
43  for(unsigned int i = 1; i < CIRCLE_LINES + 1.0f; i++) {
44  float radians = 2.0f * M_PI * ((float)i / ((float)CIRCLE_LINES - 2.0f));
45  float x0 = std::sin(radians) * radius;
46  float y0 = std::cos(radians) * radius;
47 
48  // Construct a new line from last to here
49  Line l;
50  glm::vec2 newPoint0 = glm::vec2(lastX, lastY) + p0, newPoint1 = glm::vec2(x0, y0) + p0;
51  l.construct(newPoint0, newPoint1, lineThick, fillCol);
52  lines.push_back(l);
53 
54  lastX = x0;
55  lastY = y0;
56  }
57  }
58 
59  void Rectangle::construct(glm::vec4& posSize, float& lineThick, Colour& fillCol) {
60  destRect = posSize;
61 
62  glm::vec2 pos(posSize.x, posSize.y), size(posSize.z, posSize.w);
63 
64  lines[0].construct(pos, pos + size * glm::vec2(0.0f, 1.0f), lineThick, fillCol);
65  lines[1].construct(pos + size * glm::vec2(1.0f, 0.0f), pos + size * glm::vec2(1.0f, 1.0f), lineThick, fillCol);
66  }
67 
69  }
70 
72  }
73 
75  // Make shaders lol
77 
78  link({"vertexPosition", "vertexColour"});
79 
81 
82  m_vertexArrayObject.addVertexAttribute(3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, position));
84  GL_UNSIGNED_BYTE,
85  GL_TRUE,
86  sizeof(Vertex),
87  (void*)offsetof(Vertex, colour));
88  }
89 
92 
93  m_lines.clear();
94  m_circles.clear();
95  m_rectangles.clear();
96  }
97 
98  void DebugRenderer::drawLine(glm::vec2 point0, glm::vec2 point1, float thickness, Colour colour) {
99  m_lines.emplace_back();
100  m_lines.back().construct(point0, point1, thickness, colour);
101  }
102 
103  void DebugRenderer::drawCircle(glm::vec2 centre, float lineThickness, float radius, Colour fillColour) {
104  m_circles.emplace_back();
105  m_circles.back().construct(centre, lineThickness, radius, fillColour);
106  }
107 
108  void DebugRenderer::drawRectangle(glm::vec4 destRect, float lineThickness, Colour fillColour) {
109  m_rectangles.emplace_back();
110  m_rectangles.back().construct(destRect, lineThickness, fillColour);
111  }
112 
114  // Create all vertices for each line, circle, and rectangle
115  std::vector<Vertex> lineVertices;
116  lineVertices.resize(m_lines.size() * 2); // 2 vertices/line
117 
118  std::vector<Vertex> circleVertices;
119  circleVertices.resize(m_circles.size() *
120  CIRCLE_LINES); // we only actually use one vertex per line, as OpenGL fills in the rest.
121 
122  std::vector<Vertex> rectangleVertices;
123  rectangleVertices.resize(m_rectangles.size() *
124  4); // OpenGL uses 4 vertices for a polygon, so that's what we'll do.
125 
126  // Set up line vertices
127  for(unsigned int i = 0; i < m_lines.size(); i++) {
128  // Set the positions
129  lineVertices[2 * i].setPosition(m_lines[i].point0.x, m_lines[i].point0.y, 0.0f);
130  lineVertices[2 * i + 1].setPosition(m_lines[i].point1.x, m_lines[i].point1.y, 0.0f);
131 
132  // Set the colours
133  lineVertices[2 * i].colour = m_lines[i].colour;
134  lineVertices[2 * i + 1].colour = m_lines[i].colour;
135 
136  // No UV.
137  }
138 
139  // Set up circle vertices
140  for(unsigned int i = 0; i < m_circles.size(); i++) {
141  circleVertices[i * CIRCLE_LINES].setPosition(m_circles[i].origin.x, m_circles[i].origin.y, 0.0f);
142  circleVertices[i * CIRCLE_LINES].colour = m_circles[i].lines[0].colour;
143  for(unsigned int vertex = 1; vertex < CIRCLE_LINES; vertex++) {
144  // Set the position
145  circleVertices[vertex + CIRCLE_LINES * i].setPosition(m_circles[i].lines[vertex].point0.x,
146  m_circles[i].lines[vertex].point0.y,
147  0.0f);
148 
149  // Set the colours
150  circleVertices[vertex + CIRCLE_LINES * i].colour = m_circles[i].lines[0].colour;
151 
152  // No UV.
153  }
154  }
155 
156  // Set up the rectangle vertices
157  for(unsigned int i = 0; i < m_rectangles.size(); i++) {
158  // Set the positions
159  rectangleVertices[4 * i].setPosition(m_rectangles[i].lines[0].point0.x,
160  m_rectangles[i].lines[0].point0.y,
161  0.0f);
162  rectangleVertices[4 * i + 1].setPosition(m_rectangles[i].lines[0].point1.x,
163  m_rectangles[i].lines[0].point1.y,
164  0.0f);
165  rectangleVertices[4 * i + 2].setPosition(m_rectangles[i].lines[1].point0.x,
166  m_rectangles[i].lines[1].point0.y,
167  0.0f);
168  rectangleVertices[4 * i + 3].setPosition(m_rectangles[i].lines[1].point1.x,
169  m_rectangles[i].lines[1].point1.y,
170  0.0f);
171 
172  // Set the colours
173  rectangleVertices[4 * i].colour = m_rectangles[i].lines[0].colour;
174  rectangleVertices[4 * i + 1].colour = m_rectangles[i].lines[0].colour;
175  rectangleVertices[4 * i + 2].colour = m_rectangles[i].lines[0].colour;
176  rectangleVertices[4 * i + 3].colour = m_rectangles[i].lines[0].colour;
177 
178  // No UV.
179  }
180 
181  // Now actually upload the data!
182  // Bind the VBO
184 
185  // Combine vectors
186  std::vector<Vertex> verticesTotal;
187  verticesTotal.reserve(lineVertices.size() + circleVertices.size() + rectangleVertices.size());
188  verticesTotal.insert(verticesTotal.end(), lineVertices.begin(), lineVertices.end());
189  verticesTotal.insert(verticesTotal.end(), circleVertices.begin(), circleVertices.end());
190  verticesTotal.insert(verticesTotal.end(), rectangleVertices.begin(), rectangleVertices.end());
191 
192  // upload data
193  glBufferData(GL_ARRAY_BUFFER, verticesTotal.size() * sizeof(Vertex), verticesTotal.data(), GL_DYNAMIC_DRAW);
194  }
195 
197  m_shader.use();
198 
200 
202 
203  GLint offset = 0;
204 
205  // Draw lines
206  // Draw two vertices, then step the data forward two vertices for each line.
207  for(unsigned int i = 0; i < m_lines.size(); i++) {
208  glLineWidth(m_lines[i].thickness);
209  glDrawArrays(GL_LINES, offset, (GLsizei)2);
210  offset += 2;
211  }
212 
213  // Draw circles
214  for(unsigned int i = 0; i < m_circles.size(); i++) {
215  glDrawArrays(GL_TRIANGLE_FAN, offset, (GLsizei)CIRCLE_LINES);
216  offset += CIRCLE_LINES;
217  }
218 
219  // Draw rectangles
220  for(unsigned int i = 0; i < m_rectangles.size(); i++) {
221  glDrawArrays(GL_TRIANGLE_STRIP, offset, (GLsizei)4);
222  offset += 4;
223  }
224 
225  // Unbind and exit!
227 
228  m_shader.unuse();
229  }
230 
231 } // namespace BARE2D
BARE2D::DebugRenderer::drawRectangle
virtual void drawRectangle(glm::vec4 destRect, float lineThickness, Colour fillColour=Colour(255, 255, 255, 255))
Draws a rectangle.
Definition: DebugRenderer.cpp:108
BARE2D::Vertex
Just holds vertex data for convenience.
Definition: Vertex.hpp:39
BARE2D::ShaderProgram::use
void use()
Activates this shader program for the renderer to use.
Definition: ShaderProgram.cpp:147
BARE2D
Definition: App.cpp:13
BARE2D::VAO::unbind
void unbind()
Unbinds the vao.
Definition: VAO.cpp:53
BARE2D::ShaderProgram::unuse
void unuse()
Deactivates this shader program.
Definition: ShaderProgram.cpp:160
BARE2D::Renderer::m_vertexArrayObject
VAO m_vertexArrayObject
Definition: Renderer.hpp:66
vertShaderSource
const char * vertShaderSource
Definition: DebugRenderer.cpp:16
BARE2D::Circle::origin
glm::vec2 origin
Definition: DebugRenderer.hpp:24
BARE2D::Rectangle::destRect
glm::vec4 destRect
Definition: DebugRenderer.hpp:31
DebugRenderer.hpp
A renderer specifically made for debugging purposes. Contains its own shaders.
BARE2D::Rectangle::construct
void construct(glm::vec4 &posSize, float &lineThick, Colour &fillCol)
Definition: DebugRenderer.cpp:59
BARE2D::DebugRenderer::~DebugRenderer
virtual ~DebugRenderer()
Definition: DebugRenderer.cpp:71
BARE2D::Line::point1
glm::vec2 point1
Definition: DebugRenderer.hpp:16
BARE2D::Line::point0
glm::vec2 point0
Definition: DebugRenderer.hpp:15
BARE2D::Renderer::link
virtual void link(std::initializer_list< std::string > attributes)
Definition: Renderer.cpp:85
BARE2D::DebugRenderer::m_lines
std::vector< Line > m_lines
Definition: DebugRenderer.hpp:86
BARE2D::Rectangle::lines
Line lines[2]
Definition: DebugRenderer.hpp:32
BARE2D::Circle::construct
void construct(glm::vec2 &p0, float &lineThick, float &radius, Colour &fillCol)
Definition: DebugRenderer.cpp:37
fragShaderSource
const char * fragShaderSource
Definition: DebugRenderer.cpp:7
GLContextManager.hpp
BARE2D::DebugRenderer::drawCircle
virtual void drawCircle(glm::vec2 centre, float lineThickness, float radius, Colour fillColour=Colour(255, 255, 255, 255))
Draws a circle.
Definition: DebugRenderer.cpp:103
BARE2D::VAO::bind
void bind()
Binds this vertex array object.
Definition: VAO.cpp:48
BARE2D::Renderer::begin
virtual void begin()
Clears the necessary vectors, etc. to prepare for draw() calls, etc.
Definition: Renderer.cpp:13
BARE2D::DebugRenderer::drawLine
virtual void drawLine(glm::vec2 point0, glm::vec2 point1, float thickness, Colour colour)
Draws a line.
Definition: DebugRenderer.cpp:98
BARE2D::Renderer::m_shader
ShaderProgram m_shader
Definition: Renderer.hpp:65
BARE2D::DebugRenderer::init
virtual void init() override
Initializes with very rudimentary shaders (hardcoded), adds vertex attributes.
Definition: DebugRenderer.cpp:74
BARE2D::DebugRenderer::DebugRenderer
DebugRenderer()
Definition: DebugRenderer.cpp:68
BARE2D::GLContext::setActiveTexture
void setActiveTexture(GLenum texture)
Sets the active texture "slot". This can be GL_TEXTURE0 to GL_TEXTURE8 (I think. Check the literature...
Definition: GLContextManager.cpp:13
BARE2D::Line::thickness
float thickness
Definition: DebugRenderer.hpp:17
BARE2D::Line::colour
Colour colour
Definition: DebugRenderer.hpp:18
BARE2D::DebugRenderer::render
virtual void render() override
Actually renders the contents to the screen!
Definition: DebugRenderer.cpp:196
BARE2D::VAO::bindVBO
void bindVBO()
Binds the VAO's VBO.
Definition: VAO.cpp:57
CIRCLE_LINES
#define CIRCLE_LINES
Definition: DebugRenderer.cpp:5
BARE2D::DebugRenderer::m_rectangles
std::vector< Rectangle > m_rectangles
Definition: DebugRenderer.hpp:88
BARE2D::GLContextManager::getContext
static GLContext * getContext()
Definition: GLContextManager.cpp:44
BARE2D::DebugRenderer::begin
virtual void begin() override
Clears the necessary vectors, etc. to prepare for draw() calls, etc.
Definition: DebugRenderer.cpp:90
BARE2D::VAO::addVertexAttribute
void addVertexAttribute(GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void *data)
Wrapper for glVertexAttribPointer - Adds an attribute to the VBO - each vertex data slot will gain so...
Definition: VAO.cpp:66
BARE2D::Colour
An RGBA 8-bit colour value.
Definition: Vertex.hpp:20
BARE2D::ShaderProgram::compileShadersFromSource
void compileShadersFromSource(const char *vertexSource, const char *fragmentSource)
Similar to compileShaders, this just compiles the shaders.
Definition: ShaderProgram.cpp:38
BARE2D::DebugRenderer::createRenderBatches
virtual void createRenderBatches() override
Constructs all of the render batches from data given by, say, draw() calls.
Definition: DebugRenderer.cpp:113
BARE2D::Line::construct
void construct(glm::vec2 p0, glm::vec2 p1, float &thick, Colour &col)
Definition: DebugRenderer.cpp:30
BARE2D::Circle::lines
std::vector< Line > lines
Definition: DebugRenderer.hpp:25
BARE2D::Line
Definition: DebugRenderer.hpp:12
BARE2D::DebugRenderer::m_circles
std::vector< Circle > m_circles
Definition: DebugRenderer.hpp:87
BARE2D::Renderer::init
virtual void init()
Initializes all necessary bits of the renderer.
Definition: Renderer.cpp:25