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 {}; struct EmptyElement {};
} }
// TODO: Element buffer count as just the number of element types and allow multiple template <typename VertexType, typename IndexType, int element_buffer_count = 0, typename Renderable = Renderable<VertexType, IndexType> >
// 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> >
class Batch class Batch
{ {
public: public:
typedef GLsizei SizeType; typedef GLsizei SizeType;
Batch( Batch(
const RenderableType* renderable, const Renderable* renderable,
const SizeType& element_count,
const SizeType& element_render_count 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) if (std::is_same<unsigned int, IndexType>::value)
{ {
@ -93,7 +89,7 @@ public:
m_p_renderable->get_mesh()->index_count, m_p_renderable->get_mesh()->index_count,
m_gl_index_type, m_gl_index_type,
0, 0,
(GLsizei)m_element_render_count m_element_render_count
); );
glBindVertexArray(NULL); glBindVertexArray(NULL);
} }
@ -103,14 +99,6 @@ public:
m_element_render_count = element_render_count; 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: protected:
virtual void setup_element_buffers() {} virtual void setup_element_buffers() {}
@ -118,8 +106,7 @@ protected:
virtual void update_element_buffers() {} virtual void update_element_buffers() {}
const RenderableType* m_p_renderable; const Renderable* m_p_renderable;
std::vector<ElementType> m_elements;
SizeType m_element_render_count; SizeType m_element_render_count;
GLuint m_vertex_vbo; GLuint m_vertex_vbo;

View File

@ -2,9 +2,11 @@
void MyBatch::setup_element_buffers() void MyBatch::setup_element_buffers()
{ {
glBindBuffer(GL_ARRAY_BUFFER, m_element_buffers[0]); glBindBuffer(GL_ARRAY_BUFFER, m_element_buffers[COLOR_VBO_INDEX]);
// TODO: Maybe make the next line use a typedef instead of MyShaderProgram::ColorType glBufferData(GL_ARRAY_BUFFER, m_color_elements.size() * sizeof(MyBatchTestShaderProgram::Color), NULL, GL_STREAM_DRAW);
glBufferData(GL_ARRAY_BUFFER, m_elements.size() * sizeof(MyBatchTestShaderProgram::ColorType), 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() void MyBatch::setup_vao()
@ -12,19 +14,27 @@ void MyBatch::setup_vao()
glBindBuffer(GL_ARRAY_BUFFER, m_vertex_vbo); glBindBuffer(GL_ARRAY_BUFFER, m_vertex_vbo);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL); 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); glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, NULL); 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(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() 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. // 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]); glBindBuffer(GL_ARRAY_BUFFER, m_element_buffers[COLOR_VBO_INDEX]);
glBufferData(GL_ARRAY_BUFFER, m_elements.size() * sizeof(MyBatchTestShaderProgram::ColorType), NULL, GL_STREAM_DRAW); glBufferData(GL_ARRAY_BUFFER, m_color_elements.size() * sizeof(MyBatchTestShaderProgram::Color), NULL, GL_STREAM_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, m_elements.size() * sizeof(MyBatchTestShaderProgram::ColorType), m_elements.data()); 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" #include "MyBatchTestShaderProgram.h"
class MyBatch : public Batch<MyBatchTestShaderProgram::VertexType, MyBatchTestShaderProgram::IndexType, 1, MyBatchTestShaderProgram::ColorType> class MyBatch : public Batch<MyBatchTestShaderProgram::Vertex, MyBatchTestShaderProgram::Index, 2>
{ {
public: public:
MyBatch( MyBatch(
const MyBatchTestShaderProgram::RenderableType* renderable, const MyBatchTestShaderProgram::Renderable* renderable,
const SizeType& element_count, const SizeType& element_count,
const SizeType& element_render_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: protected:
void setup_element_buffers() override; void setup_element_buffers() override;
@ -19,4 +23,11 @@ protected:
void setup_vao() override; void setup_vao() override;
void update_element_buffers() 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 class MyBatchTestShaderProgram : public ShaderProgram
{ {
public: public:
struct VertexType struct Vertex
{ {
VertexType() {} Vertex() {}
VertexType(float x, float y, float z) Vertex(float x, float y, float z)
: x(x), y(y), z(z) : x(x), y(y), z(z)
{} {}
float x; float x;
float y; float y;
float z; 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 r = 0.0f;
float g; float g = 0.0f;
float b; float b = 0.0f;
float a; float a = 1.0f;
}; };
typedef unsigned int IndexType; struct Offset
typedef Renderable<VertexType, IndexType> RenderableType; {
typedef Mesh<VertexType, IndexType> MeshType; 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(); MyBatchTestShaderProgram();

View File

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

View File

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

View File

@ -8,10 +8,10 @@
class MyShaderProgram : public ShaderProgram class MyShaderProgram : public ShaderProgram
{ {
public: public:
struct VertexType struct Vertex
{ {
VertexType() {} Vertex() {}
VertexType(float x, float y, float z) Vertex(float x, float y, float z)
: x(x), y(y), z(z) : x(x), y(y), z(z)
{ {
} }
@ -19,16 +19,16 @@ public:
float y; float y;
float z; 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 r;
float g; float g;
float b; float b;
float a; float a;
}; };
typedef unsigned int IndexType; typedef unsigned int Index;
typedef Renderable<VertexType, IndexType> RenderableType; typedef Renderable<Vertex, Index> Renderable;
typedef Mesh<VertexType, IndexType> MeshType; typedef Mesh<Vertex, Index> Mesh;
MyShaderProgram(); MyShaderProgram();

View File

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