Abstracted Batch Element Specification

Now, the batch subclasses define the Element specification and the
elements are not stored in the main Batch class. It is suggested
that the batch subclasses use vectors of the different element
types per requested VBO and offer a function to return a reference
to one of the elements by index. This functionality is implemented
in the current version of MyBatch.

A test for the movement needs to be completed
This commit is contained in:
elipzer 2018-09-07 11:45:32 -04:00
parent 0a9fde252a
commit 05363f94b7
8 changed files with 78 additions and 61 deletions

View File

@ -12,21 +12,17 @@ namespace
struct EmptyElement {};
}
// TODO: Element buffer count as just the number of element types and allow multiple
// element types to be passed via Variadic Templates (see http://www.cplusplus.com/articles/EhvU7k9E/ )
// This is effectively just support for more than one element vbo per rendered instance
template <typename VertexType, typename IndexType, int element_buffer_count = 0, typename ElementType = EmptyElement, typename RenderableType = Renderable<VertexType, IndexType> >
template <typename VertexType, typename IndexType, int element_buffer_count = 0, typename Renderable = Renderable<VertexType, IndexType> >
class Batch
{
public:
typedef GLsizei SizeType;
Batch(
const RenderableType* renderable,
const SizeType& element_count,
const Renderable* renderable,
const SizeType& element_render_count
)
: m_p_renderable(renderable), m_elements(element_count), m_element_render_count(element_render_count), m_element_buffers(element_buffer_count)
: m_p_renderable(renderable), m_element_render_count(element_render_count), m_element_buffers(element_buffer_count)
{
if (std::is_same<unsigned int, IndexType>::value)
{
@ -93,7 +89,7 @@ public:
m_p_renderable->get_mesh()->index_count,
m_gl_index_type,
0,
(GLsizei)m_element_render_count
m_element_render_count
);
glBindVertexArray(NULL);
}
@ -103,14 +99,6 @@ public:
m_element_render_count = element_render_count;
}
ElementType& get_element() { return m_elements[0]; }
ElementType& get_element(const SizeType& index) { return m_elements[index]; }
const ElementType& get_element() const { return m_elements[0]; }
const ElementType& get_element(const SizeType& index) const { return m_elements[index]; }
protected:
virtual void setup_element_buffers() {}
@ -118,8 +106,7 @@ protected:
virtual void update_element_buffers() {}
const RenderableType* m_p_renderable;
std::vector<ElementType> m_elements;
const Renderable* m_p_renderable;
SizeType m_element_render_count;
GLuint m_vertex_vbo;

View File

@ -2,9 +2,11 @@
void MyBatch::setup_element_buffers()
{
glBindBuffer(GL_ARRAY_BUFFER, m_element_buffers[0]);
// TODO: Maybe make the next line use a typedef instead of MyShaderProgram::ColorType
glBufferData(GL_ARRAY_BUFFER, m_elements.size() * sizeof(MyBatchTestShaderProgram::ColorType), NULL, GL_STREAM_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, m_element_buffers[COLOR_VBO_INDEX]);
glBufferData(GL_ARRAY_BUFFER, m_color_elements.size() * sizeof(MyBatchTestShaderProgram::Color), NULL, GL_STREAM_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, m_element_buffers[OFFSET_VBO_INDEX]);
glBufferData(GL_ARRAY_BUFFER, m_offset_elements.size() * sizeof(MyBatchTestShaderProgram::Offset), NULL, GL_STREAM_DRAW);
}
void MyBatch::setup_vao()
@ -12,19 +14,27 @@ void MyBatch::setup_vao()
glBindBuffer(GL_ARRAY_BUFFER, m_vertex_vbo);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glBindBuffer(GL_ARRAY_BUFFER, m_element_buffers[0]);
glBindBuffer(GL_ARRAY_BUFFER, m_element_buffers[COLOR_VBO_INDEX]);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, NULL);
glBindBuffer(GL_ARRAY_BUFFER, m_element_buffers[OFFSET_VBO_INDEX]);
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glVertexAttribDivisor(0, 0); // Only need to send the mesh data once
glVertexAttribDivisor(0, 0); // Only need to send the mesh data once (This should probably be done every time)
glVertexAttribDivisor(1, 1); // Send the color data for each instance drawn
glVertexAttribDivisor(2, 1); // Send the offset data for each instance drawn
}
void MyBatch::update_element_buffers()
{
// 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, m_element_buffers[0]);
glBufferData(GL_ARRAY_BUFFER, m_elements.size() * sizeof(MyBatchTestShaderProgram::ColorType), NULL, GL_STREAM_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, m_elements.size() * sizeof(MyBatchTestShaderProgram::ColorType), m_elements.data());
glBindBuffer(GL_ARRAY_BUFFER, m_element_buffers[COLOR_VBO_INDEX]);
glBufferData(GL_ARRAY_BUFFER, m_color_elements.size() * sizeof(MyBatchTestShaderProgram::Color), NULL, GL_STREAM_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, m_color_elements.size() * sizeof(MyBatchTestShaderProgram::Color), m_color_elements.data());
glBindBuffer(GL_ARRAY_BUFFER, m_element_buffers[OFFSET_VBO_INDEX]);
glBufferData(GL_ARRAY_BUFFER, m_offset_elements.size() * sizeof(MyBatchTestShaderProgram::Offset), NULL, GL_STREAM_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, m_offset_elements.size() * sizeof(MyBatchTestShaderProgram::Offset), m_offset_elements.data());
}

View File

@ -4,14 +4,18 @@
#include "MyBatchTestShaderProgram.h"
class MyBatch : public Batch<MyBatchTestShaderProgram::VertexType, MyBatchTestShaderProgram::IndexType, 1, MyBatchTestShaderProgram::ColorType>
class MyBatch : public Batch<MyBatchTestShaderProgram::Vertex, MyBatchTestShaderProgram::Index, 2>
{
public:
MyBatch(
const MyBatchTestShaderProgram::RenderableType* renderable,
const MyBatchTestShaderProgram::Renderable* renderable,
const SizeType& element_count,
const SizeType& element_render_count
) : Batch(renderable, element_count, element_render_count) {}
) : Batch(renderable, element_render_count), m_color_elements(element_count), m_offset_elements(element_count) {}
MyBatchTestShaderProgram::Color& get_color(const SizeType& index) { return m_color_elements[index]; }
MyBatchTestShaderProgram::Offset& get_offset(const SizeType& index) { return m_offset_elements[index]; }
protected:
void setup_element_buffers() override;
@ -19,4 +23,11 @@ protected:
void setup_vao() override;
void update_element_buffers() override;
private:
const int COLOR_VBO_INDEX = 0;
const int OFFSET_VBO_INDEX = 1;
std::vector<MyBatchTestShaderProgram::Color> m_color_elements;
std::vector<MyBatchTestShaderProgram::Offset> m_offset_elements;
};

View File

@ -8,26 +8,32 @@
class MyBatchTestShaderProgram : public ShaderProgram
{
public:
struct VertexType
struct Vertex
{
VertexType() {}
VertexType(float x, float y, float z)
Vertex() {}
Vertex(float x, float y, float z)
: x(x), y(y), z(z)
{}
float x;
float y;
float z;
};
struct ColorType // Try changing to normalized unsigned chars (0-255) for the example
struct Color // Try changing to normalized unsigned chars (0-255) for the example
{
float r;
float g;
float b;
float a;
float r = 0.0f;
float g = 0.0f;
float b = 0.0f;
float a = 1.0f;
};
typedef unsigned int IndexType;
typedef Renderable<VertexType, IndexType> RenderableType;
typedef Mesh<VertexType, IndexType> MeshType;
struct Offset
{
float x = 0.0f;
float y = 0.0f;
float z = 0.0f;
};
typedef unsigned int Index;
typedef Renderable<Vertex, Index> Renderable;
typedef Mesh<Vertex, Index> Mesh;
MyBatchTestShaderProgram();

View File

@ -4,12 +4,12 @@
#include "MeshFactory.h"
MyObjectOrientedScene::MyObjectOrientedScene()
: m_shape(MeshFactory<MyBatchTestShaderProgram::VertexType, MyBatchTestShaderProgram::IndexType>::gen(
: m_shape(MeshFactory<MyBatchTestShaderProgram::Vertex, MyBatchTestShaderProgram::Index>::gen(
DrawMode::DRAW_TRIANGLES,
MyBatchTestShaderProgram::VertexType(-0.5f, 0.4f, 0.0f),
MyBatchTestShaderProgram::VertexType(0.5f, 0.4f, 0.0f),
MyBatchTestShaderProgram::VertexType(-0.8f, -0.4f, 0.0f),
MyBatchTestShaderProgram::VertexType(0.8f, -0.4f, 0.0f)
MyBatchTestShaderProgram::Vertex(-0.1f, 0.1f, 0.0f),
MyBatchTestShaderProgram::Vertex(0.1f, 0.1f, 0.0f),
MyBatchTestShaderProgram::Vertex(-0.15f, -0.1f, 0.0f),
MyBatchTestShaderProgram::Vertex(0.15f, -0.1f, 0.0f)
), DrawMode::DRAW_TRIANGLES), m_batch(&m_shape, 1, 1)
{
}
@ -37,7 +37,8 @@ void MyObjectOrientedScene::unuse()
void MyObjectOrientedScene::update(float delta_time, clock_t clock)
{
float n;
clock_t c = (clock / 10) % 512;
clock_t c;
c = (clock / 10) % 512;
if (c < 256)
{
n = (float)c / 256;
@ -47,11 +48,13 @@ void MyObjectOrientedScene::update(float delta_time, clock_t clock)
n = (float)(512 - c) / 256;
}
MyBatchTestShaderProgram::ColorType& element = m_batch.get_element();
element.r = n;
element.g = n;
element.b = n;
element.a = 1.0f;
// TODO: Movement Test
MyBatchTestShaderProgram::Color& color = m_batch.get_color(0);
color.r = n;
color.g = n;
color.b = n;
color.a = 1.0f;
// TODO: Make a prerender function or move this to render
m_batch.prerender();

View File

@ -22,6 +22,6 @@ public:
void render() override;
private:
MyBatch m_batch;
MyBatchTestShaderProgram::RenderableType m_shape;
MyBatchTestShaderProgram::Renderable m_shape;
MyBatchTestShaderProgram m_shader_program;
};

View File

@ -8,10 +8,10 @@
class MyShaderProgram : public ShaderProgram
{
public:
struct VertexType
struct Vertex
{
VertexType() {}
VertexType(float x, float y, float z)
Vertex() {}
Vertex(float x, float y, float z)
: x(x), y(y), z(z)
{
}
@ -19,16 +19,16 @@ public:
float y;
float z;
};
struct ColorType // Try changing to normalized unsigned chars (0-255) for the example
struct Color // Try changing to normalized unsigned chars (0-255) for the example
{
float r;
float g;
float b;
float a;
};
typedef unsigned int IndexType;
typedef Renderable<VertexType, IndexType> RenderableType;
typedef Mesh<VertexType, IndexType> MeshType;
typedef unsigned int Index;
typedef Renderable<Vertex, Index> Renderable;
typedef Mesh<Vertex, Index> Mesh;
MyShaderProgram();

View File

@ -79,7 +79,7 @@
<Filter>Source Files\Example\Scenes</Filter>
</ClCompile>
<ClCompile Include="MyBatchTestShaderProgram.cpp">
<Filter>Source Files</Filter>
<Filter>Source Files\Example</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
@ -156,7 +156,7 @@
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="MyBatchTestShaderProgram.h">
<Filter>Header Files</Filter>
<Filter>Header Files\Example</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>