charcoal/OpenGLEngine/PoseableBatch.h
2018-09-14 18:09:43 -04:00

76 lines
3.1 KiB
C++

#pragma once
#include "Batch.h"
#include "BuiltinBatch.h"
#include "Renderable.h"
#include "Poseable.h"
namespace charcoal
{
namespace builtin
{
template <typename VertexType, typename IndexType, typename Renderable = Renderable<VertexType, IndexType> >
class PoseableBatch : public builtin::Batch<VertexType, IndexType, 1, Renderable>
{
public:
// TODO: Figure out how to get rid of this typename garbage. If that it figured out, m_element_buffers should get fixed.
PoseableBatch(
Renderable* renderable,
int element_count
) : PoseableBatch(renderable, element_count, element_count)
{}
PoseableBatch(
Renderable* renderable,
int element_count,
int element_render_count
) : builtin::Batch<VertexType, IndexType, 1, Renderable>(renderable, element_render_count), m_pose_elements(element_count)
{}
virtual ~PoseableBatch() {}
Poseable& get_pose(int index) { return m_pose_elements[index]; }
const Poseable& get_pose(int index) const { return m_pose_elements[index]; }
protected:
void setup_element_buffers() override
{
glBindBuffer(GL_ARRAY_BUFFER, charcoal::Batch<VertexType, IndexType, 1, Renderable>::m_element_buffers[0]);
glBufferData(GL_ARRAY_BUFFER, m_pose_elements.size() * sizeof(Poseable), NULL, GL_STREAM_DRAW);
}
void setup_vao() override
{
glBindBuffer(GL_ARRAY_BUFFER, charcoal::Batch<VertexType, IndexType, 1, Renderable>::m_vertex_vbo);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(VertexType), NULL);
glBindBuffer(GL_ARRAY_BUFFER, charcoal::Batch<VertexType, IndexType, 1, Renderable>::m_element_buffers[0]);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glEnableVertexAttribArray(3);
glEnableVertexAttribArray(4);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(0 * sizeof(vec4)));
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(1 * sizeof(vec4)));
glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(2 * sizeof(vec4)));
glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(3 * sizeof(vec4)));
glVertexAttribDivisor(0, 0); // Send the mesh data once
glVertexAttribDivisor(1, 1); // Send the offset data for each instance drawn
glVertexAttribDivisor(2, 1); // Send the offset data for each instance drawn
glVertexAttribDivisor(3, 1); // Send the offset data for each instance drawn
glVertexAttribDivisor(4, 1); // Send the offset data for each instance drawn
}
void update_element_buffers() override
{
// TODO: There are probably better ways to do this. Should check with the old engine to see what I did there.
glBindBuffer(GL_ARRAY_BUFFER, charcoal::Batch<VertexType, IndexType, 1, Renderable>::m_element_buffers[0]);
glBufferData(GL_ARRAY_BUFFER, m_pose_elements.size() * sizeof(Poseable), NULL, GL_STREAM_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, m_pose_elements.size() * sizeof(Poseable), m_pose_elements.data());
}
private:
std::vector<Poseable> m_pose_elements;
};
}
}