From 05363f94b740049a6af6b93ce48da5410cb47a4f Mon Sep 17 00:00:00 2001 From: elipzer Date: Fri, 7 Sep 2018 11:45:32 -0400 Subject: [PATCH] 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 --- OpenGLEngine/Batch.h | 23 ++++--------------- OpenGLEngine/MyBatch.cpp | 26 ++++++++++++++------- OpenGLEngine/MyBatch.h | 17 +++++++++++--- OpenGLEngine/MyBatchTestShaderProgram.h | 28 ++++++++++++++--------- OpenGLEngine/MyObjectOrientedScene.cpp | 25 +++++++++++--------- OpenGLEngine/MyObjectOrientedScene.h | 2 +- OpenGLEngine/MyShaderProgram.h | 14 ++++++------ OpenGLEngine/OpenGLEngine.vcxproj.filters | 4 ++-- 8 files changed, 78 insertions(+), 61 deletions(-) diff --git a/OpenGLEngine/Batch.h b/OpenGLEngine/Batch.h index 273ab37..a7ecd5d 100644 --- a/OpenGLEngine/Batch.h +++ b/OpenGLEngine/Batch.h @@ -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 > +template > 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::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 m_elements; + const Renderable* m_p_renderable; SizeType m_element_render_count; GLuint m_vertex_vbo; diff --git a/OpenGLEngine/MyBatch.cpp b/OpenGLEngine/MyBatch.cpp index b05d8e8..795dfce 100644 --- a/OpenGLEngine/MyBatch.cpp +++ b/OpenGLEngine/MyBatch.cpp @@ -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()); } diff --git a/OpenGLEngine/MyBatch.h b/OpenGLEngine/MyBatch.h index cdec6ed..3805823 100644 --- a/OpenGLEngine/MyBatch.h +++ b/OpenGLEngine/MyBatch.h @@ -4,14 +4,18 @@ #include "MyBatchTestShaderProgram.h" -class MyBatch : public Batch +class MyBatch : public Batch { 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 m_color_elements; + std::vector m_offset_elements; }; \ No newline at end of file diff --git a/OpenGLEngine/MyBatchTestShaderProgram.h b/OpenGLEngine/MyBatchTestShaderProgram.h index bf2431a..d9b6bd3 100644 --- a/OpenGLEngine/MyBatchTestShaderProgram.h +++ b/OpenGLEngine/MyBatchTestShaderProgram.h @@ -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 RenderableType; - typedef Mesh MeshType; + struct Offset + { + float x = 0.0f; + float y = 0.0f; + float z = 0.0f; + }; + typedef unsigned int Index; + typedef Renderable Renderable; + typedef Mesh Mesh; MyBatchTestShaderProgram(); diff --git a/OpenGLEngine/MyObjectOrientedScene.cpp b/OpenGLEngine/MyObjectOrientedScene.cpp index a6a8389..63799b0 100644 --- a/OpenGLEngine/MyObjectOrientedScene.cpp +++ b/OpenGLEngine/MyObjectOrientedScene.cpp @@ -4,12 +4,12 @@ #include "MeshFactory.h" MyObjectOrientedScene::MyObjectOrientedScene() - : m_shape(MeshFactory::gen( + : m_shape(MeshFactory::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(); diff --git a/OpenGLEngine/MyObjectOrientedScene.h b/OpenGLEngine/MyObjectOrientedScene.h index ff1cf31..e484c03 100644 --- a/OpenGLEngine/MyObjectOrientedScene.h +++ b/OpenGLEngine/MyObjectOrientedScene.h @@ -22,6 +22,6 @@ public: void render() override; private: MyBatch m_batch; - MyBatchTestShaderProgram::RenderableType m_shape; + MyBatchTestShaderProgram::Renderable m_shape; MyBatchTestShaderProgram m_shader_program; }; \ No newline at end of file diff --git a/OpenGLEngine/MyShaderProgram.h b/OpenGLEngine/MyShaderProgram.h index 25f1d82..54b4a7e 100644 --- a/OpenGLEngine/MyShaderProgram.h +++ b/OpenGLEngine/MyShaderProgram.h @@ -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 RenderableType; - typedef Mesh MeshType; + typedef unsigned int Index; + typedef Renderable Renderable; + typedef Mesh Mesh; MyShaderProgram(); diff --git a/OpenGLEngine/OpenGLEngine.vcxproj.filters b/OpenGLEngine/OpenGLEngine.vcxproj.filters index 7e2c63e..a801be0 100644 --- a/OpenGLEngine/OpenGLEngine.vcxproj.filters +++ b/OpenGLEngine/OpenGLEngine.vcxproj.filters @@ -79,7 +79,7 @@ Source Files\Example\Scenes - Source Files + Source Files\Example @@ -156,7 +156,7 @@ Header Files - Header Files + Header Files\Example