charcoal/OpenGLEngine/Batch.h

119 lines
2.8 KiB
C
Raw Normal View History

#pragma once
#include "stdafx.h"
#include <cstddef>
#include <vector>
#include "Exception.h"
2018-09-12 21:03:46 +00:00
namespace charcoal
{
template <typename VertexType, typename IndexType, int element_buffer_count, typename Renderable = Renderable<VertexType, IndexType> >
2018-09-12 21:03:46 +00:00
class Batch
{
2018-09-12 21:03:46 +00:00
public:
Batch(
const Renderable* renderable,
2018-09-14 22:09:43 +00:00
int element_render_count
2018-09-12 21:03:46 +00:00
)
: m_p_renderable(renderable), m_element_render_count(element_render_count), m_element_buffers(element_buffer_count)
{
2018-09-12 21:03:46 +00:00
if (std::is_same<unsigned int, IndexType>::value)
{
m_gl_index_type = GL_UNSIGNED_INT;
}
else if (std::is_same<unsigned short, IndexType>::value)
{
m_gl_index_type = GL_UNSIGNED_SHORT;
}
else if (std::is_same<unsigned char, IndexType>::value)
{
m_gl_index_type = GL_UNSIGNED_BYTE;
}
else
{
throw EXCEPTION("Invalid Batch IndexType");
}
glGenVertexArrays(1, &m_vao);
glGenBuffers(1, &m_vertex_vbo);
glGenBuffers(1, &m_index_vbo);
glGenBuffers(element_buffer_count, m_element_buffers.data());
}
2018-09-12 21:03:46 +00:00
virtual ~Batch()
{
glDeleteVertexArrays(1, &m_vao);
glDeleteBuffers(1, &m_vertex_vbo);
glDeleteBuffers(1, &m_index_vbo);
2018-09-12 21:03:46 +00:00
glDeleteBuffers(element_buffer_count, m_element_buffers.data());
}
2018-09-12 21:03:46 +00:00
void init()
{
glBindBuffer(GL_ARRAY_BUFFER, m_vertex_vbo);
glBufferData(GL_ARRAY_BUFFER, m_p_renderable->get_mesh()->vertex_count * sizeof(VertexType), m_p_renderable->get_mesh()->vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, NULL);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_index_vbo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_p_renderable->get_mesh()->index_count * sizeof(IndexType), m_p_renderable->get_mesh()->indices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, NULL);
2018-09-12 21:03:46 +00:00
setup_element_buffers();
2018-09-12 21:03:46 +00:00
glBindVertexArray(m_vao); // Possibly want to move these three lines into the init function
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_index_vbo);
2018-09-12 21:03:46 +00:00
setup_vao();
2018-09-12 21:03:46 +00:00
glBindVertexArray(NULL);
}
2018-09-12 21:03:46 +00:00
void prerender()
{
update_element_buffers();
}
2018-09-12 21:03:46 +00:00
void render() const
{
glBindVertexArray(m_vao);
glDrawElementsInstanced(
m_p_renderable->get_draw_mode(),
m_p_renderable->get_mesh()->index_count,
m_gl_index_type,
0,
m_element_render_count
);
glBindVertexArray(NULL);
}
2018-09-14 22:09:43 +00:00
void set_element_render_count(int element_render_count)
2018-09-12 21:03:46 +00:00
{
m_element_render_count = element_render_count;
}
2018-09-12 21:03:46 +00:00
const Renderable* get_renderable() const { return m_p_renderable; }
2018-09-14 22:09:43 +00:00
int get_element_render_count() const { return m_element_render_count; }
2018-09-12 21:03:46 +00:00
protected:
virtual void setup_element_buffers() {}
2018-09-12 21:03:46 +00:00
virtual void setup_vao() = 0;
2018-09-12 21:03:46 +00:00
virtual void update_element_buffers() {}
2018-09-12 21:03:46 +00:00
const Renderable* m_p_renderable;
2018-09-14 22:09:43 +00:00
int m_element_render_count;
2018-09-12 21:03:46 +00:00
GLuint m_vertex_vbo;
std::vector<GLuint> m_element_buffers;
2018-09-12 21:03:46 +00:00
private:
GLuint m_vao;
GLuint m_index_vbo;
GLenum m_gl_index_type;
};
}