fixed mesh missing default stride calculation

This commit is contained in:
Noel Berry 2020-08-27 19:11:49 -07:00
parent 4c0531a3f3
commit 5a6d2c3de7
5 changed files with 66 additions and 10 deletions

View File

@ -22,6 +22,7 @@ add_library(blah
public/blah/graphics/texture.h public/blah/graphics/texture.h
public/blah/graphics/framebuffer.h public/blah/graphics/framebuffer.h
public/blah/graphics/shader.h public/blah/graphics/shader.h
public/blah/graphics/mesh.cpp
public/blah/graphics/mesh.h public/blah/graphics/mesh.h
public/blah/graphics/material.h public/blah/graphics/material.h
public/blah/graphics/material.cpp public/blah/graphics/material.cpp

View File

@ -1028,7 +1028,7 @@ namespace Blah
return m_id; 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); gl.BindVertexArray(m_id);
{ {
@ -1040,7 +1040,7 @@ namespace Blah
gl.BindVertexArray(0); 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); gl.BindVertexArray(m_id);
{ {

View File

@ -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);
}

View File

@ -11,10 +11,10 @@ namespace Blah
virtual ~Mesh() = default; virtual ~Mesh() = default;
// Sets the Vertex Format of the Mesh // 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 // 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 // Uploads the given index buffer to the Mesh
virtual void index_data(const void* indices, int64_t count) = 0; virtual void index_data(const void* indices, int64_t count) = 0;
@ -41,6 +41,10 @@ namespace Blah
// Destroys the given Mesh // Destroys the given Mesh
virtual void dispose() = 0; 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<Mesh> MeshRef; typedef std::shared_ptr<Mesh> MeshRef;