BARE2D
BumpyRenderer.cpp
Go to the documentation of this file.
1 #include "BumpyRenderer.hpp"
2 
3 #include "GLContextManager.hpp"
4 
5 namespace BARE2D {
6 
7  BumpyGlyph::BumpyGlyph(glm::vec4& destRect, glm::vec4& uvRect, GLuint& Texture, GLuint& Bumpmap, float& Depth, Colour& colour, float& angle, glm::vec2& COR)
8  : Glyph(destRect, uvRect, Texture, Depth, colour, angle, COR), bumpmap(Bumpmap) {
9 
10  }
11 
12  BumpyRenderer::BumpyRenderer(std::string& fragShader, std::string& vertShader, unsigned int perspectiveWidth, unsigned int perspectiveHeight)
13  : BasicRenderer(fragShader, vertShader, perspectiveWidth, perspectiveHeight) {
14  }
15 
17  }
18 
20  GLint textureUniform = 0;
21  m_shader.setUniform("textureSampler", &textureUniform);
22 
23  GLint bumpUniform = 1;
24  m_shader.setUniform("bumpmapSampler", &bumpUniform);
25  }
26 
28  // We need to define a texture sampler for textures AND bumpmaps!
29  m_camera->update();
30 
31  glm::mat4 projectionMatrix = m_camera->getCameraMatrix();
32  m_shader.setUniformMatrix<glm::mat4>("projectionMatrix", GL_FALSE, &projectionMatrix);
33  }
34 
36  // Ensure that we're actually using the shader for the glDrawArrays call!
37  m_shader.use();
38 
39  // Make sure we're in the regular rendering texture (arbitrarily set to texture0, just for consistency)
41  glContext->setActiveTexture(GL_TEXTURE0);
42 
43  // First we must bind the vertex array object and vertex buffer object
45 
46  preRender();
47 
48  // Create RenderBatches (uploading them to the bound texture, so that we can actually draw them next.
50 
51  // Now we can render each renderbatch, uploading their texture data respectively.
52  for(unsigned int i = 0; i < m_batches.size(); i++) {
53  // Bind the texture information to the texture "slot"
54  // This method of binding the textures decreases speed slightly for completely random textures, but if we are rendering a lot of the same texture, this is similar (if not identical) to instance rendering.
55 
56  glContext->setActiveTexture(GL_TEXTURE1);
57  glContext->bindTexture(GL_TEXTURE_2D, m_batchBumpmaps[i]);
58 
59  glContext->setActiveTexture(GL_TEXTURE0);
60  glContext->bindTexture(GL_TEXTURE_2D, m_batches[i].textureID);
61 
62  // Upload the data
63  glDrawArrays(GL_TRIANGLES, (GLint)m_batches[i].offset, (GLsizei)m_batches[i].numVertices);
64  }
65 
66  // Unbind for safety!
68 
69  // Release the shader
70  m_shader.unuse();
71  }
72 
73  void BumpyRenderer::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)*/) {
74  throwFatalError(BAREError::UNINITIALIZED_FUNCTION, "BumpyRenderer drawing without bumpmap texture! Fix your program!");
75  }
76 
77  void BumpyRenderer::draw(glm::vec4 destRect, glm::vec4 uvRect, GLuint texture, GLuint bumpmap, float depth, Colour colour/* = Colour(255, 255, 255, 255)*/, float angle/* = 0.0f*/, glm::vec2 COR/* = glm::vec2(0.5f)*/) {
78  // Make sure it's actually in the scene.
79  if(!m_camera->isRectInViewspace(destRect))
80  return;
81 
82  // Just add the glyph (as a bumpy glyph!)
83  m_glyphs.push_back(new BumpyGlyph(destRect, uvRect, texture, bumpmap, depth, colour, angle, COR));
84  }
85 
87  // Create all the render batches (based on the draw()'n glyphs) for rendering
88  // THey're going to end up as vertices
89  std::vector<Vertex> vertices;
90 
91  // We already know that the glyphs represent 6 vertices by design.
92  vertices.resize(m_glyphs.size() * 6);
93 
94  // Check if we even have anything to draw
95  if(m_glyphs.size() == 0) {
96  return; // Don't need to do much.
97  }
98 
99  int offset = 0;
100  int vertex = 0;
101 
102  // Make way for new bumpmaps.
103  m_batchBumpmaps.clear();
104 
105  // 'Draw' two triangles from the 6 vertices.
106  m_batches.emplace_back(offset, 6, m_glyphs[0]->texture);
107  vertices[vertex++] = m_glyphs[0]->topLeft;
108  vertices[vertex++] = m_glyphs[0]->bottomLeft;
109  vertices[vertex++] = m_glyphs[0]->bottomRight;
110  vertices[vertex++] = m_glyphs[0]->bottomRight;
111  vertices[vertex++] = m_glyphs[0]->topRight;
112  vertices[vertex++] = m_glyphs[0]->topLeft;
113  m_batchBumpmaps.push_back(static_cast<BumpyGlyph*>(m_glyphs[0])->bumpmap);
114 
115  // Set the offset appropriately for 6 vertices of data.
116  offset += 6;
117 
118  // Add the rest of the glyphs
119  for(unsigned int glyph = 1; glyph < m_glyphs.size(); glyph++) {
120  // Check if this can just be part of the current batch (instancing essentially)
121  // They must have both the same texture AND the same bumpmap for this to work properly.
122  if((m_glyphs[glyph]->texture != m_glyphs[glyph - 1]->texture) || (static_cast<BumpyGlyph*>(m_glyphs[glyph])->bumpmap != static_cast<BumpyGlyph*>(m_glyphs[glyph - 1])->bumpmap)) {
123  // It can't be part of the same batch, so create a new one
124  m_batches.emplace_back(offset, 6, m_glyphs[glyph]->texture);
125  // Add the corresponding bumpmap to the batch bumpmaps vector.
126  m_batchBumpmaps.push_back(static_cast<BumpyGlyph*>(m_glyphs[glyph])->bumpmap);
127  } else {
128  // If its part of the current batch, just increase numVertices. It'll reuse the texture, but nothing more.
129  m_batches.back().numVertices += 6;
130  }
131  // 'Draw' two triangles.
132  vertices[vertex++] = m_glyphs[glyph]->topLeft;
133  vertices[vertex++] = m_glyphs[glyph]->bottomLeft;
134  vertices[vertex++] = m_glyphs[glyph]->bottomRight;
135  vertices[vertex++] = m_glyphs[glyph]->bottomRight;
136  vertices[vertex++] = m_glyphs[glyph]->topRight;
137  vertices[vertex++] = m_glyphs[glyph]->topLeft;
138 
139  // Push data offset forward 6 vertices.
140  offset += 6;
141  }
142 
143  // Done with the glyphs, we can clear em
144  m_glyphs.clear();
145 
146  // Now that render batches are created, we can upload the information to OpenGL
147 
148  // Bind the VBO
150 
151  // Orphan the buffer
152  glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), nullptr, GL_DYNAMIC_DRAW);
153 
154  // Actually upload the data to the buffer.
155  glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size() * sizeof(Vertex), vertices.data());
156 
157  // Unbind the VBO again for safety
159  }
160 
161 }
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::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::BumpyRenderer::initUniforms
virtual void initUniforms() override
Initializes all uniforms, such as colour attachments, depth attachments, etc.
Definition: BumpyRenderer.cpp:19
BARE2D::Renderer::m_vertexArrayObject
VAO m_vertexArrayObject
Definition: Renderer.hpp:66
BARE2D::GLContext::bindTexture
void bindTexture(GLenum target, GLenum texture)
Binds a texture to target in the currently active texture slot.
Definition: GLContextManager.cpp:22
BARE2D::BumpyRenderer::createRenderBatches
virtual void createRenderBatches() override
Constructs all of the render batches from data given by, say, draw() calls.
Definition: BumpyRenderer.cpp:86
BARE2D::BumpyRenderer::preRender
virtual void preRender() override
Does stuff inside of the render function, within the shader's use.
Definition: BumpyRenderer.cpp:27
BARE2D::Texture
The texture struct holds very basic stuff - the filepath, width, height, and ID,.
Definition: Texture.hpp:13
BARE2D::BumpyRenderer::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: BumpyRenderer.cpp:73
BARE2D::BasicRenderer::m_glyphs
std::vector< Glyph * > m_glyphs
Definition: BasicRenderer.hpp:44
BARE2D::BumpyRenderer::m_batchBumpmaps
std::vector< GLuint > m_batchBumpmaps
Definition: BumpyRenderer.hpp:42
BARE2D::BasicRenderer::m_camera
std::shared_ptr< Camera2D > m_camera
Definition: BasicRenderer.hpp:46
BARE2D::BAREError::UNINITIALIZED_FUNCTION
@ UNINITIALIZED_FUNCTION
BumpyRenderer.hpp
GLContextManager.hpp
BARE2D::VAO::bind
void bind()
Binds this vertex array object.
Definition: VAO.cpp:48
BARE2D::VAO::unbindVBO
void unbindVBO()
Unbinds this VAO's VBO.
Definition: VAO.cpp:62
BARE2D::GLContext
Definition: GLContextManager.hpp:7
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::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::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::BumpyRenderer::~BumpyRenderer
~BumpyRenderer()
Definition: BumpyRenderer.cpp:16
BARE2D::BumpyRenderer::BumpyRenderer
BumpyRenderer(std::string &fragShader, std::string &vertShader, unsigned int perspectiveWidth=2, unsigned int perspectiveHeight=2)
Definition: BumpyRenderer.cpp:12
BARE2D::BasicRenderer
Definition: BasicRenderer.hpp:18
BARE2D::BumpyRenderer::render
virtual void render() override
Actually renders the contents to the screen!
Definition: BumpyRenderer.cpp:35
BARE2D::VAO::bindVBO
void bindVBO()
Binds the VAO's VBO.
Definition: VAO.cpp:57
BARE2D::BumpyGlyph::BumpyGlyph
BumpyGlyph(glm::vec4 &destRect, glm::vec4 &uvRect, GLuint &Texture, GLuint &Bumpmap, float &Depth, Colour &colour, float &angle, glm::vec2 &COR)
Definition: BumpyRenderer.cpp:7
BARE2D::BumpyGlyph
This is a glyph, but with bumpmap (for lighting etc.).
Definition: BumpyRenderer.hpp:11
BARE2D::GLContextManager::getContext
static GLContext * getContext()
Definition: GLContextManager.cpp:44
BARE2D::Renderer::m_batches
std::vector< RenderBatch > m_batches
Definition: Renderer.hpp:67
BARE2D::Colour
An RGBA 8-bit colour value.
Definition: Vertex.hpp:20
BARE2D::throwFatalError
void throwFatalError(BAREError err, std::string message)
Throws an error (fatal). Also calls displayErrors and exits the program.
Definition: BAREErrors.cpp:178