diff --git a/source/gs-vertexbuffer.cpp b/source/gs-vertexbuffer.cpp index 91026e13..4ac35309 100644 --- a/source/gs-vertexbuffer.cpp +++ b/source/gs-vertexbuffer.cpp @@ -70,16 +70,19 @@ gs::vertex_buffer::~vertex_buffer() } } -// cppcheck-suppress uninitMemberVar -gs::vertex_buffer::vertex_buffer() : vertex_buffer(MAXIMUM_VERTICES) {} +gs::vertex_buffer::vertex_buffer() : vertex_buffer(MAXIMUM_VERTICES, MAXIMUM_UVW_LAYERS) {} -gs::vertex_buffer::vertex_buffer(uint32_t maximumVertices) - : m_size(maximumVertices), m_capacity(maximumVertices), m_layers(MAXIMUM_UVW_LAYERS), m_positions(nullptr), - m_normals(nullptr), m_tangents(nullptr), m_colors(nullptr), m_vertexbufferdata(nullptr), m_vertexbuffer(nullptr), - m_layerdata(nullptr) +gs::vertex_buffer::vertex_buffer(uint32_t vertices) : vertex_buffer(vertices, MAXIMUM_UVW_LAYERS) {} + +gs::vertex_buffer::vertex_buffer(uint32_t vertices, uint8_t uvlayers) + : m_size(vertices), m_capacity(vertices), m_layers(uvlayers), m_positions(nullptr), m_normals(nullptr), + m_tangents(nullptr), m_colors(nullptr), m_vertexbufferdata(nullptr), m_vertexbuffer(nullptr), m_layerdata(nullptr) { - if (maximumVertices > MAXIMUM_VERTICES) { - throw std::out_of_range("maximumVertices out of range"); + if (vertices > MAXIMUM_VERTICES) { + throw std::out_of_range("vertices out of range"); + } + if (uvlayers > MAXIMUM_UVW_LAYERS) { + throw std::out_of_range("uvlayers out of range"); } // Allocate memory for data. @@ -99,12 +102,16 @@ gs::vertex_buffer::vertex_buffer(uint32_t maximumVertices) std::memset(m_colors, 0, sizeof(uint32_t) * m_capacity); m_vertexbufferdata->num_tex = m_layers; - m_vertexbufferdata->tvarray = m_layerdata = - (gs_tvertarray*)util::malloc_aligned(16, sizeof(gs_tvertarray) * m_layers); - for (size_t n = 0; n < MAXIMUM_UVW_LAYERS; n++) { - m_layerdata[n].array = m_uvs[n] = (vec4*)util::malloc_aligned(16, sizeof(vec4) * m_capacity); - m_layerdata[n].width = 4; - std::memset(m_uvs[n], 0, sizeof(vec4) * m_capacity); + if (m_layers > 0) { + m_vertexbufferdata->tvarray = m_layerdata = + (gs_tvertarray*)util::malloc_aligned(16, sizeof(gs_tvertarray) * m_layers); + for (size_t n = 0; n < MAXIMUM_UVW_LAYERS; n++) { + m_layerdata[n].array = m_uvs[n] = (vec4*)util::malloc_aligned(16, sizeof(vec4) * m_capacity); + m_layerdata[n].width = 4; + std::memset(m_uvs[n], 0, sizeof(vec4) * m_capacity); + } + } else { + m_vertexbufferdata->tvarray = nullptr; } // Allocate GPU diff --git a/source/gs-vertexbuffer.h b/source/gs-vertexbuffer.h index 0c6313c6..27652207 100644 --- a/source/gs-vertexbuffer.h +++ b/source/gs-vertexbuffer.h @@ -45,9 +45,17 @@ namespace gs { /*! * \brief Create a Vertex Buffer with a specific number of Vertices. * - * \param maximumVertices Maximum amount of vertices to store. + * \param vertices Number of vertices to store. */ - vertex_buffer(uint32_t maximumVertices); + vertex_buffer(uint32_t vertices); + + /*! + * \brief Create a Vertex Buffer with a specific number of Vertices and uv layers. + * + * \param vertices Number of vertices to store. + * \param layers Number of uv layers to store. + */ + vertex_buffer(uint32_t vertices, uint8_t layers); /*! * \brief Create a copy of a Vertex Buffer