BARE2D
BasicRenderer.cpp
Go to the documentation of this file.
1 #include "BasicRenderer.hpp"
2 
3 namespace BARE2D {
4 
5 BasicRenderer::BasicRenderer(std::string& fragShader,
6  std::string& vertShader,
7  unsigned int perspectiveWidth,
8  unsigned int perspectiveHeight)
9  : Renderer()
10  , m_fragShader(fragShader)
11  , m_vertShader(vertShader)
12 {
13  m_camera = std::make_shared<Camera2D>();
14  m_camera->init(perspectiveWidth, perspectiveHeight);
15 }
16 
18 {}
19 
21 {
23 
24  link({"vertexPosition", "vertexColour", "vertexUV"});
25 
26  Renderer::init(); // Initializes the VAO, so we can add attributes.
27 
28  m_vertexArrayObject.addVertexAttribute(3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, position));
29  m_vertexArrayObject.addVertexAttribute(4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex),
30  (void*)offsetof(Vertex, colour));
31  m_vertexArrayObject.addVertexAttribute(2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, uv));
32 }
33 
34 void BasicRenderer::setCamera(std::shared_ptr<Camera2D> camera)
35 {
36  m_camera = camera;
37 }
38 
39 std::shared_ptr<Camera2D> BasicRenderer::getCamera()
40 {
41  return m_camera;
42 }
43 
45 {
46  // We also need to define a texture sampler for textures!
47 
48  m_camera->update();
49 
50  GLint textureUniform = 0;
51  m_shader.setUniform("textureSampler", &textureUniform);
52  glm::mat4 projectionMatrix = m_camera->getCameraMatrix();
53  m_shader.setUniformMatrix<glm::mat4>("projectionMatrix", GL_FALSE, &projectionMatrix);
54 }
55 
56 void BasicRenderer::draw(glm::vec4 destRect,
57  glm::vec4 uvRect,
58  GLuint texture,
59  float depth,
60  Colour colour /*255, 255, 255, 255*/,
61  float angle /* = 0.0f*/,
62  glm::vec2 COR /*=glm::vec2(0.5f)*/)
63 {
64  // Make sure it's actually in the scene.
65  if(!m_camera->isRectInViewspace(destRect))
66  return;
67 
68  // At this point we can just scale the size (the position should be translated in the shader) and draw it
69 
70  // Just add the glyph
71  m_glyphs.push_back(new Glyph(destRect, uvRect, texture, depth, colour, angle, COR));
72 }
73 
75 {
76  // Create all the render batches (based on the draw()'n glyphs) for rendering
77  // THey're going to end up as vertices
78  std::vector<Vertex> vertices;
79 
80  // We already know that the glyphs represent 6 vertices by design.
81  vertices.resize(m_glyphs.size() * 6);
82 
83  // Check if we even have anything to draw
84  if(m_glyphs.size() == 0)
85  {
86  return; // Don't need to do much.
87  }
88 
89  int offset = 0;
90  int vertex = 0;
91 
92  // 'Draw' two triangles from the 6 vertices.
93  m_batches.emplace_back(offset, 6, m_glyphs[0]->texture);
94  vertices[vertex++] = m_glyphs[0]->topLeft;
95  vertices[vertex++] = m_glyphs[0]->bottomLeft;
96  vertices[vertex++] = m_glyphs[0]->bottomRight;
97  vertices[vertex++] = m_glyphs[0]->bottomRight;
98  vertices[vertex++] = m_glyphs[0]->topRight;
99  vertices[vertex++] = m_glyphs[0]->topLeft;
100 
101  // Set the offset appropriately for 6 vertices of data.
102  offset += 6;
103 
104  // Add the rest of the glyphs
105  for(unsigned int glyph = 1; glyph < m_glyphs.size(); glyph++)
106  {
107  // Check if this can just be part of the current batch (instancing essentially)
108  if(m_glyphs[glyph]->texture != m_glyphs[glyph - 1]->texture)
109  {
110  // It can't be part of the same batch, so create a new one
111  m_batches.emplace_back(offset, 6, m_glyphs[glyph]->texture);
112  } else
113  {
114  // If its part of the current batch, just increase numVertices. It'll reuse the texture, but nothing
115  // more.
116  m_batches.back().numVertices += 6;
117  }
118  // 'Draw' two triangles.
119  vertices[vertex++] = m_glyphs[glyph]->topLeft;
120  vertices[vertex++] = m_glyphs[glyph]->bottomLeft;
121  vertices[vertex++] = m_glyphs[glyph]->bottomRight;
122  vertices[vertex++] = m_glyphs[glyph]->bottomRight;
123  vertices[vertex++] = m_glyphs[glyph]->topRight;
124  vertices[vertex++] = m_glyphs[glyph]->topLeft;
125 
126  // Push data offset forward 6 vertices.
127  offset += 6;
128  }
129 
130  // Done with the glyphs, we can clear em
131  m_glyphs.clear();
132 
133  // Now that render batches are created, we can upload the information to OpenGL
134 
135  // Bind the VBO
137 
138  // Orphan the buffer
139  glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), nullptr, GL_DYNAMIC_DRAW);
140 
141  // Actually upload the data to the buffer.
142  glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size() * sizeof(Vertex), vertices.data());
143 
144  // Unbind the VBO again for safety
146 }
147 
148 } // namespace BARE2D
BARE2D::ShaderProgram::setUniformMatrix
void setUniformMatrix(const std::string uniform, bool transpose, T *data, unsigned int num=1)
Similar to setUniform.
BARE2D::Vertex
Just holds vertex data for convenience.
Definition: Vertex.hpp:39
BARE2D::Renderer
The renderer class holds some shader program, manages some VBO, some render batch(es),...
Definition: Renderer.hpp:15
BARE2D::BasicRenderer::m_fragShader
std::string m_fragShader
Definition: BasicRenderer.hpp:42
BARE2D
Definition: App.cpp:13
BasicRenderer.hpp
A simple renderer. Has the GLSL VAOs vertexPosition (vec3), vertexColour (vec4), and vertexUV (vec2)....
BARE2D::Renderer::m_vertexArrayObject
VAO m_vertexArrayObject
Definition: Renderer.hpp:66
BARE2D::BasicRenderer::m_vertShader
std::string m_vertShader
Definition: BasicRenderer.hpp:42
BARE2D::BasicRenderer::~BasicRenderer
virtual ~BasicRenderer()
Definition: BasicRenderer.cpp:17
BARE2D::BasicRenderer::m_glyphs
std::vector< Glyph * > m_glyphs
Definition: BasicRenderer.hpp:44
BARE2D::Renderer::link
virtual void link(std::initializer_list< std::string > attributes)
Definition: Renderer.cpp:85
BARE2D::BasicRenderer::m_camera
std::shared_ptr< Camera2D > m_camera
Definition: BasicRenderer.hpp:46
BARE2D::ShaderProgram::compileShaders
void compileShaders(const char *vertexShaderPath, const char *fragmentShaderPath)
Compiles the shaders. Does not link them.
Definition: ShaderProgram.cpp:23
BARE2D::BasicRenderer::setCamera
virtual void setCamera(std::shared_ptr< Camera2D > camera)
Sets the camera to a given pointer. This means that the renderer takes ownership.
Definition: BasicRenderer.cpp:34
BARE2D::BasicRenderer::BasicRenderer
BasicRenderer(std::string &fragShader, std::string &vertShader, unsigned int perspectiveWidth=2, unsigned int perspectiveHeight=2)
Definition: BasicRenderer.cpp:5
BARE2D::VAO::unbindVBO
void unbindVBO()
Unbinds this VAO's VBO.
Definition: VAO.cpp:62
BARE2D::BasicRenderer::createRenderBatches
virtual void createRenderBatches() override
Constructs all of the render batches from data given by, say, draw() calls.
Definition: BasicRenderer.cpp:74
BARE2D::Renderer::m_shader
ShaderProgram m_shader
Definition: Renderer.hpp:65
BARE2D::Glyph
The glyph represents a renderbatch's primitive data, which is created from each draw call in the basi...
Definition: Vertex.hpp:87
BARE2D::ShaderProgram::setUniform
void setUniform(const std::string uniform, T *data, unsigned int num=1)
A generalized wrapper to find and set a uniform for this shader.
BARE2D::BasicRenderer::preRender
virtual void preRender() override
Does stuff inside of the render function, within the shader's use.
Definition: BasicRenderer.cpp:44
BARE2D::BasicRenderer::getCamera
std::shared_ptr< Camera2D > getCamera()
Definition: BasicRenderer.cpp:39
BARE2D::VAO::bindVBO
void bindVBO()
Binds the VAO's VBO.
Definition: VAO.cpp:57
BARE2D::Renderer::m_batches
std::vector< RenderBatch > m_batches
Definition: Renderer.hpp:67
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::BasicRenderer::init
virtual void init() override
Initializes all necessary bits of the renderer.
Definition: BasicRenderer.cpp:20
BARE2D::BasicRenderer::draw
virtual void draw(glm::vec4 destRect, glm::vec4 uvRect, GLuint texture, float depth, Colour colour=Colour(255, 255, 255, 255), float angle=0.0f, glm::vec2 COR=glm::vec2(0.5f))
Definition: BasicRenderer.cpp:56
BARE2D::Renderer::init
virtual void init()
Initializes all necessary bits of the renderer.
Definition: Renderer.cpp:25