BARE2D
Renderer.cpp
Go to the documentation of this file.
1 #include "Renderer.hpp"
2 
3 #include "GLContextManager.hpp"
4 
5 namespace BARE2D {
6 
8  }
9 
11  }
12 
13  void Renderer::begin() {
14  // Clears renderbatches to make space for new ones!
15  m_batches.clear();
16 
17  m_shader.use();
18  }
19 
20  void Renderer::end() {
21  // Nothing special really needs to be done here.
22  m_shader.unuse();
23  }
24 
25  void Renderer::init() {
26  // Set depth range so that we can actually write depth values.
27  glDepthRange(0.0f, 1.0f);
28 
30 
31  // We must have an activated shader to find/set uniforms in it.
32  m_shader.use();
33 
34  // Set the uniforms (in)
35  initUniforms();
36 
37  m_shader.unuse();
38  }
39 
41  }
42 
45  m_shader.destroy();
46  }
47 
49  // Ensure that we're actually using the shader for the glDrawArrays call!
50  m_shader.use();
51 
52  // Make sure we're in the regular rendering texture (arbitrarily set to texture0, just for consistency)
54  glContext->setActiveTexture(GL_TEXTURE0);
55 
56  // First we must bind the vertex array object and vertex buffer object
58 
59  preRender();
60 
61  // Create RenderBatches (uploading them to the bound texture, so that we can actually draw them next.
63 
64  // Now we can render each renderbatch, uploading their texture data respectively.
65  for(unsigned int i = 0; i < m_batches.size(); i++) {
66  // Bind the texture information to the texture "slot"
67  // 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
68  glContext->bindTexture(GL_TEXTURE_2D, m_batches[i].textureID);
69 
70  // Upload the data
71  glDrawArrays(GL_TRIANGLES, (GLint)m_batches[i].offset, (GLsizei)m_batches[i].numVertices);
72  }
73 
74  // Unbind for safety!
76 
77  // Release the shader
78  m_shader.unuse();
79  }
80 
82  return &m_shader;
83  }
84 
85  void Renderer::link(std::initializer_list<std::string> attributes) {
86  // Set the locations of all possible output variables (all the colour attachments)
87  // This must be done before linking!!!
88  GLint maxDrawBuffers = 0;
89  glGetIntegerv(GL_MAX_DRAW_BUFFERS, &maxDrawBuffers);
90  for(int i = 0; i < maxDrawBuffers; i++) {
91  m_shader.bindFragOutputLocation("colour" + std::to_string(i), i);
92  }
93 
94  // Now actually link the shader.
95  m_shader.linkShaders(attributes);
96  }
97 
98 } // namespace BARE2D
BARE2D::VAO::destroy
void destroy()
Releases necessary memory.
Definition: VAO.cpp:33
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
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::Renderer::render
virtual void render()
Actually renders the contents to the screen!
Definition: Renderer.cpp:48
BARE2D::VAO::init
void init()
Initializes the necessary components, combining the VBO and VAO.
Definition: VAO.cpp:14
BARE2D::Renderer::~Renderer
virtual ~Renderer()
Definition: Renderer.cpp:10
BARE2D::Renderer::link
virtual void link(std::initializer_list< std::string > attributes)
Definition: Renderer.cpp:85
Renderer.hpp
BARE2D::Renderer::Renderer
Renderer()
Definition: Renderer.cpp:7
BARE2D::Renderer::getShader
ShaderProgram * getShader()
Definition: Renderer.cpp:81
BARE2D::ShaderProgram::bindFragOutputLocation
void bindFragOutputLocation(std::string outputVariableName, unsigned int location)
A call to glBindFragDataLocation() - Causes an output variable of the fragment shader to output to a ...
Definition: ShaderProgram.cpp:141
GLContextManager.hpp
BARE2D::Renderer::preRender
virtual void preRender()
Does stuff inside of the render function, within the shader's use.
Definition: Renderer.hpp:56
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::Renderer::destroy
virtual void destroy()
Frees all necessary memory.
Definition: Renderer.cpp:43
BARE2D::GLContext
Definition: GLContextManager.hpp:7
BARE2D::ShaderProgram::destroy
void destroy()
Releases all bound objects and deletes any allocated stuff.
Definition: ShaderProgram.cpp:172
BARE2D::Renderer::m_shader
ShaderProgram m_shader
Definition: Renderer.hpp:65
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
The ShaderProgram is a GLSL program which combines two shaders - the vertex shader and the fragment s...
Definition: ShaderProgram.hpp:14
BARE2D::ShaderProgram::linkShaders
void linkShaders(std::initializer_list< std::string > attributes)
Links the compiled shaders together to create a coherent shader program.
Definition: ShaderProgram.cpp:75
BARE2D::GLContextManager::getContext
static GLContext * getContext()
Definition: GLContextManager.cpp:44
BARE2D::Renderer::initUniforms
virtual void initUniforms()
Initializes all uniforms, such as colour attachments, depth attachments, etc.
Definition: Renderer.cpp:40
BARE2D::Renderer::m_batches
std::vector< RenderBatch > m_batches
Definition: Renderer.hpp:67
BARE2D::Renderer::init
virtual void init()
Initializes all necessary bits of the renderer.
Definition: Renderer.cpp:25
BARE2D::Renderer::end
virtual void end()
Creates the renderbatches, does necessary stuff before render() call.
Definition: Renderer.cpp:20
BARE2D::Renderer::createRenderBatches
virtual void createRenderBatches()=0
Constructs all of the render batches from data given by, say, draw() calls.