BARE2D
TexturelessRenderer.cpp
Go to the documentation of this file.
2 
3 #include "GLContextManager.hpp"
4 
5 namespace BARE2D {
6 
7  TexturelessRenderer::TexturelessRenderer(std::string& fragShader,
8  std::string& vertShader,
9  unsigned int perspectiveWidth,
10  unsigned int perspectiveHeight) :
11  Renderer(),
12  m_fragShader(fragShader), m_vertShader(vertShader) {
13  m_camera = std::make_shared<Camera2D>();
14  m_camera->init(perspectiveWidth, perspectiveHeight);
15  }
16 
18  }
19 
22 
23  link({"vertexPosition", "vertexColour", "vertexUV"});
24 
25  Renderer::init(); // Initializes the VAO, so we can add attributes.
26 
27  m_vertexArrayObject.addVertexAttribute(3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, position));
29  GL_UNSIGNED_BYTE,
30  GL_TRUE,
31  sizeof(Vertex),
32  (void*)offsetof(Vertex, colour));
33  m_vertexArrayObject.addVertexAttribute(2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, uv));
34 
35  // Create our blank texture
36  glGenTextures(1, &m_texture);
37  // Now bind the texture to its slot
39 
40  // all white
41  unsigned char* data = new unsigned char[4];
42  data[0] = 255;
43  data[1] = 255;
44  data[2] = 255;
45  data[3] = 255;
46 
47  glTexImage2D(GL_TEXTURE_2D,
48  0,
49  GL_RGBA,
50  1, // x size
51  1, // y size
52  0,
53  GL_RGBA,
54  GL_UNSIGNED_BYTE,
55  &data[0]);
56 
57  // OpenGL copies data, we can delete
58  delete[] data;
59 
60  // Give the texture some more basic settings.
61  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
62  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
63  float backColour[] = {0.0f, 0.0f, 0.0f, 0.0f}; // Fully transparent
64  glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, backColour);
65  // So we don't get any real interpolation to the corners.
66  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
67  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
68 
69  // Now that its attached, we can unbind the texture
70  GLContextManager::getContext()->bindTexture(GL_TEXTURE_2D, 0);
71  }
72 
73  void TexturelessRenderer::setCamera(std::shared_ptr<Camera2D> camera) {
74  m_camera = camera;
75  }
76 
77  std::shared_ptr<Camera2D> TexturelessRenderer::getCamera() {
78  return m_camera;
79  }
80 
82  m_camera->update();
83 
84  glm::mat4 projectionMatrix = m_camera->getCameraMatrix();
85  m_shader.setUniformMatrix<glm::mat4>("projectionMatrix", GL_FALSE, &projectionMatrix);
86  }
87 
88  void TexturelessRenderer::draw(glm::vec4 destRect,
89  float depth,
90  Colour colour /*255, 255, 255, 255*/,
91  float angle /* = 0.0f*/,
92  glm::vec2 COR /*=glm::vec2(0.5f)*/) {
93  // Make sure it's actually in the scene.
94  if(!m_camera->isRectInViewspace(destRect))
95  return;
96 
97  // At this point we can just scale the size (the position should be translated in the shader) and draw it
98 
99  glm::vec4 uv(0.0f, 0.0f, 1.0f, 1.0f);
100 
101  // Just add the glyph
102  m_glyphs.push_back(new Glyph(destRect, uv, m_texture, depth, colour, angle, COR));
103  }
104 
106  // Create all the render batches (based on the draw()'n glyphs) for rendering
107  // THey're going to end up as vertices
108  std::vector<Vertex> vertices;
109 
110  // We already know that the glyphs represent 6 vertices by design.
111  vertices.resize(m_glyphs.size() * 6);
112 
113  // Check if we even have anything to draw
114  if(m_glyphs.size() == 0) {
115  return; // Don't need to do much.
116  }
117 
118  int offset = 0;
119  int vertex = 0;
120 
121  // 'Draw' two triangles from the 6 vertices.
122  m_batches.emplace_back(offset, 6, m_texture);
123  vertices[vertex++] = m_glyphs[0]->topLeft;
124  vertices[vertex++] = m_glyphs[0]->bottomLeft;
125  vertices[vertex++] = m_glyphs[0]->bottomRight;
126  vertices[vertex++] = m_glyphs[0]->bottomRight;
127  vertices[vertex++] = m_glyphs[0]->topRight;
128  vertices[vertex++] = m_glyphs[0]->topLeft;
129 
130  // Set the offset appropriately for 6 vertices of data.
131  offset += 6;
132 
133  // Add the rest of the glyphs
134  for(unsigned int glyph = 1; glyph < m_glyphs.size(); glyph++) {
135  // We have no different textures, as we are rendering just blank empty squares.
136  m_batches.back().numVertices += 6;
137 
138  // 'Draw' two triangles.
139  vertices[vertex++] = m_glyphs[glyph]->topLeft;
140  vertices[vertex++] = m_glyphs[glyph]->bottomLeft;
141  vertices[vertex++] = m_glyphs[glyph]->bottomRight;
142  vertices[vertex++] = m_glyphs[glyph]->bottomRight;
143  vertices[vertex++] = m_glyphs[glyph]->topRight;
144  vertices[vertex++] = m_glyphs[glyph]->topLeft;
145 
146  // Push data offset forward 6 vertices.
147  offset += 6;
148  }
149 
150  // Done with the glyphs, we can clear em
151  m_glyphs.clear();
152 
153  // Now that render batches are created, we can upload the information to OpenGL
154 
155  // Bind the VBO
157 
158  // Orphan the buffer
159  glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), nullptr, GL_DYNAMIC_DRAW);
160 
161  // Actually upload the data to the buffer.
162  glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size() * sizeof(Vertex), vertices.data());
163 
164  // Unbind the VBO again for safety
166  }
167 
168 } // 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::TexturelessRenderer::m_texture
GLuint m_texture
Definition: TexturelessRenderer.hpp:54
BARE2D
Definition: App.cpp:13
BARE2D::Renderer::m_vertexArrayObject
VAO m_vertexArrayObject
Definition: Renderer.hpp:66
BARE2D::TexturelessRenderer::init
virtual void init() override
Initializes all necessary bits of the renderer.
Definition: TexturelessRenderer.cpp:20
BARE2D::GLContext::bindTexture
void bindTexture(GLenum target, GLenum texture)
Binds a texture to target in the currently active texture slot.
Definition: GLContextManager.cpp:22
TexturelessRenderer.hpp
A renderer made to simply run a shader with some passed glyphs. VAOs vertexPosition (vec3),...
BARE2D::TexturelessRenderer::m_fragShader
std::string m_fragShader
Definition: TexturelessRenderer.hpp:50
BARE2D::TexturelessRenderer::m_glyphs
std::vector< Glyph * > m_glyphs
Definition: TexturelessRenderer.hpp:52
BARE2D::TexturelessRenderer::~TexturelessRenderer
virtual ~TexturelessRenderer()
Definition: TexturelessRenderer.cpp:17
BARE2D::TexturelessRenderer::getCamera
std::shared_ptr< Camera2D > getCamera()
Definition: TexturelessRenderer.cpp:77
BARE2D::Renderer::link
virtual void link(std::initializer_list< std::string > attributes)
Definition: Renderer.cpp:85
BARE2D::TexturelessRenderer::createRenderBatches
virtual void createRenderBatches() override
Constructs all of the render batches from data given by, say, draw() calls.
Definition: TexturelessRenderer.cpp:105
BARE2D::TexturelessRenderer::setCamera
virtual void setCamera(std::shared_ptr< Camera2D > camera)
Sets the camera to a given pointer. This means that the renderer takes ownership.
Definition: TexturelessRenderer.cpp:73
BARE2D::ShaderProgram::compileShaders
void compileShaders(const char *vertexShaderPath, const char *fragmentShaderPath)
Compiles the shaders. Does not link them.
Definition: ShaderProgram.cpp:23
GLContextManager.hpp
BARE2D::TexturelessRenderer::draw
virtual void draw(glm::vec4 destRect, float depth, Colour colour=Colour(255, 255, 255, 255), float angle=0.0f, glm::vec2 COR=glm::vec2(0.5f))
Definition: TexturelessRenderer.cpp:88
BARE2D::TexturelessRenderer::m_vertShader
std::string m_vertShader
Definition: TexturelessRenderer.hpp:50
BARE2D::VAO::unbindVBO
void unbindVBO()
Unbinds this VAO's VBO.
Definition: VAO.cpp:62
BARE2D::TexturelessRenderer::m_camera
std::shared_ptr< Camera2D > m_camera
Definition: TexturelessRenderer.hpp:56
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::TexturelessRenderer::preRender
virtual void preRender() override
Does stuff inside of the render function, within the shader's use.
Definition: TexturelessRenderer.cpp:81
BARE2D::VAO::bindVBO
void bindVBO()
Binds the VAO's VBO.
Definition: VAO.cpp:57
BARE2D::TexturelessRenderer::TexturelessRenderer
TexturelessRenderer(std::string &fragShader, std::string &vertShader, unsigned int perspectiveWidth=2, unsigned int perspectiveHeight=2)
Definition: TexturelessRenderer.cpp:7
BARE2D::GLContextManager::getContext
static GLContext * getContext()
Definition: GLContextManager.cpp:44
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::Renderer::init
virtual void init()
Initializes all necessary bits of the renderer.
Definition: Renderer.cpp:25