From 5a6d2c3de7352887aacb05d1c6a310ed69962384 Mon Sep 17 00:00:00 2001 From: Noel Berry Date: Thu, 27 Aug 2020 19:11:49 -0700 Subject: [PATCH] fixed mesh missing default stride calculation --- CMakeLists.txt | 1 + private/blah/internal/graphics_opengl.cpp | 4 +- private/blah/internal/platform_sdl2.cpp | 12 +++--- public/blah/graphics/mesh.cpp | 51 +++++++++++++++++++++++ public/blah/graphics/mesh.h | 8 +++- 5 files changed, 66 insertions(+), 10 deletions(-) create mode 100644 public/blah/graphics/mesh.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 77e8dd8..744b09d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,7 @@ add_library(blah public/blah/graphics/texture.h public/blah/graphics/framebuffer.h public/blah/graphics/shader.h + public/blah/graphics/mesh.cpp public/blah/graphics/mesh.h public/blah/graphics/material.h public/blah/graphics/material.cpp diff --git a/private/blah/internal/graphics_opengl.cpp b/private/blah/internal/graphics_opengl.cpp index cac2070..dccec3d 100644 --- a/private/blah/internal/graphics_opengl.cpp +++ b/private/blah/internal/graphics_opengl.cpp @@ -1028,7 +1028,7 @@ namespace Blah return m_id; } - virtual void vertex_format(const VertexAttribute* attributes, int attribute_count, int stride = -1) override + virtual void vertex_format_internal(const VertexAttribute* attributes, int attribute_count, int stride = -1) override { gl.BindVertexArray(m_id); { @@ -1040,7 +1040,7 @@ namespace Blah gl.BindVertexArray(0); } - virtual void instance_format(const VertexAttribute* attributes, int attribute_count, int stride = -1) override + virtual void instance_format_internal(const VertexAttribute* attributes, int attribute_count, int stride = -1) override { gl.BindVertexArray(m_id); { diff --git a/private/blah/internal/platform_sdl2.cpp b/private/blah/internal/platform_sdl2.cpp index e2d3da2..1fb5444 100644 --- a/private/blah/internal/platform_sdl2.cpp +++ b/private/blah/internal/platform_sdl2.cpp @@ -29,12 +29,12 @@ using namespace Internal; namespace { - SDL_Window* window = nullptr; - SDL_Joystick* joysticks[BLAH_MAX_CONTROLLERS]; - SDL_GameController* gamepads[BLAH_MAX_CONTROLLERS]; - char* basePath = nullptr; - char* userPath = nullptr; - bool displayed = false; + SDL_Window* window = nullptr; + SDL_Joystick* joysticks[BLAH_MAX_CONTROLLERS]; + SDL_GameController* gamepads[BLAH_MAX_CONTROLLERS]; + char* basePath = nullptr; + char* userPath = nullptr; + bool displayed = false; void sdl_log(void* userdata, int category, SDL_LogPriority priority, const char* message) { diff --git a/public/blah/graphics/mesh.cpp b/public/blah/graphics/mesh.cpp new file mode 100644 index 0000000..54d2674 --- /dev/null +++ b/public/blah/graphics/mesh.cpp @@ -0,0 +1,51 @@ +#include "mesh.h" + +using namespace Blah; + +void Mesh::vertex_format(const VertexAttribute* attributes, int attribute_count, int stride) +{ + if (stride < 0) + { + stride = 0; + + for (int n = 0; n < attribute_count; n++) + { + const VertexAttribute* attrib = (attributes + n); + + if (attrib->type == VertexAttributeType::Byte) + stride += attrib->components * 1; + else if (attrib->type == VertexAttributeType::Short) + stride += attrib->components * 2; + else if (attrib->type == VertexAttributeType::Int) + stride += attrib->components * 4; + else if (attrib->type == VertexAttributeType::Float) + stride += attrib->components * 4; + } + } + + vertex_format_internal(attributes, attribute_count, stride); +} + +void Mesh::instance_format(const VertexAttribute* attributes, int attribute_count, int stride) +{ + if (stride < 0) + { + stride = 0; + + for (int n = 0; n < attribute_count; n++) + { + const VertexAttribute* attrib = (attributes + n); + + if (attrib->type == VertexAttributeType::Byte) + stride += attrib->components * 1; + else if (attrib->type == VertexAttributeType::Short) + stride += attrib->components * 2; + else if (attrib->type == VertexAttributeType::Int) + stride += attrib->components * 4; + else if (attrib->type == VertexAttributeType::Float) + stride += attrib->components * 4; + } + } + + instance_format_internal(attributes, attribute_count, stride); +} diff --git a/public/blah/graphics/mesh.h b/public/blah/graphics/mesh.h index 9c76fb9..e8296b1 100644 --- a/public/blah/graphics/mesh.h +++ b/public/blah/graphics/mesh.h @@ -11,10 +11,10 @@ namespace Blah virtual ~Mesh() = default; // Sets the Vertex Format of the Mesh - virtual void vertex_format(const VertexAttribute* attributes, int attribute_count, int stride = -1) = 0; + void vertex_format(const VertexAttribute* attributes, int attribute_count, int stride = -1); // Sets the Instance Format of the Mesh - virtual void instance_format(const VertexAttribute* attributes, int attribute_count, int stride = -1) = 0; + void instance_format(const VertexAttribute* attributes, int attribute_count, int stride = -1); // Uploads the given index buffer to the Mesh virtual void index_data(const void* indices, int64_t count) = 0; @@ -41,6 +41,10 @@ namespace Blah // Destroys the given Mesh virtual void dispose() = 0; + + protected: + virtual void vertex_format_internal(const VertexAttribute* attributes, int count, int stride) = 0; + virtual void instance_format_internal(const VertexAttribute* attributes, int count, int stride) = 0; }; typedef std::shared_ptr MeshRef;