Skip to content

Commit

Permalink
Updated gui vertex format
Browse files Browse the repository at this point in the history
  • Loading branch information
JCash committed Oct 12, 2023
1 parent 3720a14 commit 4e7eed9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
21 changes: 13 additions & 8 deletions defold-spine/commonsrc/vertices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace dmSpine
{

static inline void addVertex(dmSpine::SpineVertex* vertex, float x, float y, float u, float v, float r, float g, float b, float a)
static inline void addVertex(dmSpine::SpineVertex* vertex, float x, float y, float u, float v, float r, float g, float b, float a, float page_index)
{
vertex->x = x;
vertex->y = y;
Expand All @@ -23,6 +23,7 @@ static inline void addVertex(dmSpine::SpineVertex* vertex, float x, float y, flo
vertex->g = g;
vertex->b = b;
vertex->a = a;
vertex->page_index = 0;
}

template <typename T>
Expand Down Expand Up @@ -215,6 +216,8 @@ uint32_t GenerateVertexData(dmArray<SpineVertex>& vertex_buffer, const spSkeleto
float colorB = tintB * regionAttachment->color.b;
float colorA = tintA * regionAttachment->color.a;

float page_index = 0;

// Computed the world vertices positions for the 4 vertices that make up
// the rectangular region attachment. This assumes the world transform of the
// bone to which the slot (and hence attachment) is attached has been calculated
Expand All @@ -223,13 +226,13 @@ uint32_t GenerateVertexData(dmArray<SpineVertex>& vertex_buffer, const spSkeleto

// Create 2 triangles, with 3 vertices each from the region's
// world vertex positions and its UV coordinates (in the range [0-1]).
addVertex(&vertex_buffer[vindex++], scratch[0], scratch[1], uvs[0], uvs[1], colorR, colorG, colorB, colorA);
addVertex(&vertex_buffer[vindex++], scratch[2], scratch[3], uvs[2], uvs[3], colorR, colorG, colorB, colorA);
addVertex(&vertex_buffer[vindex++], scratch[4], scratch[5], uvs[4], uvs[5], colorR, colorG, colorB, colorA);
addVertex(&vertex_buffer[vindex++], scratch[0], scratch[1], uvs[0], uvs[1], colorR, colorG, colorB, colorA, page_index);
addVertex(&vertex_buffer[vindex++], scratch[2], scratch[3], uvs[2], uvs[3], colorR, colorG, colorB, colorA, page_index);
addVertex(&vertex_buffer[vindex++], scratch[4], scratch[5], uvs[4], uvs[5], colorR, colorG, colorB, colorA, page_index);

addVertex(&vertex_buffer[vindex++], scratch[4], scratch[5], uvs[4], uvs[5], colorR, colorG, colorB, colorA);
addVertex(&vertex_buffer[vindex++], scratch[6], scratch[7], uvs[6], uvs[7], colorR, colorG, colorB, colorA);
addVertex(&vertex_buffer[vindex++], scratch[0], scratch[1], uvs[0], uvs[1], colorR, colorG, colorB, colorA);
addVertex(&vertex_buffer[vindex++], scratch[4], scratch[5], uvs[4], uvs[5], colorR, colorG, colorB, colorA, page_index);
addVertex(&vertex_buffer[vindex++], scratch[6], scratch[7], uvs[6], uvs[7], colorR, colorG, colorB, colorA, page_index);
addVertex(&vertex_buffer[vindex++], scratch[0], scratch[1], uvs[0], uvs[1], colorR, colorG, colorB, colorA, page_index);
}
else if (attachment->type == SP_ATTACHMENT_MESH)
{
Expand Down Expand Up @@ -257,13 +260,15 @@ uint32_t GenerateVertexData(dmArray<SpineVertex>& vertex_buffer, const spSkeleto
float colorB = tintB * mesh->color.b;
float colorA = tintA * mesh->color.a;

float page_index = 0;

const float* uvs = mesh->uvs;
int tri_count = mesh->trianglesCount;
for (int t = 0; t < tri_count; ++t)
{
int index = mesh->triangles[t] << 1;

addVertex(&vertex_buffer[vindex++], scratch[index], scratch[index + 1], uvs[index], uvs[index + 1], colorR, colorG, colorB, colorA);
addVertex(&vertex_buffer[vindex++], scratch[index], scratch[index + 1], uvs[index], uvs[index + 1], colorR, colorG, colorB, colorA, page_index);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions defold-spine/include/common/vertices.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ struct SpineVertex
float x, y, z;
float u, v;
float r, g, b, a;
float page_index;
};

struct SpineModelBounds
Expand Down
1 change: 1 addition & 0 deletions defold-spine/src/comp_spine_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ namespace dmSpine
dmGraphics::AddVertexStream(stream_declaration, "position", 3, dmGraphics::TYPE_FLOAT, false);
dmGraphics::AddVertexStream(stream_declaration, "texcoord0", 2, dmGraphics::TYPE_FLOAT, true);
dmGraphics::AddVertexStream(stream_declaration, "color", 4, dmGraphics::TYPE_FLOAT, true);
dmGraphics::AddVertexStream(stream_declaration, "page_index", 1, dmGraphics::TYPE_FLOAT, false);

world->m_VertexDeclaration = dmGraphics::NewVertexDeclaration(context->m_GraphicsContext, stream_declaration);
world->m_VertexBuffer = dmGraphics::NewVertexBuffer(context->m_GraphicsContext, 0, 0x0, dmGraphics::BUFFER_USAGE_DYNAMIC_DRAW);
Expand Down
6 changes: 6 additions & 0 deletions defold-spine/src/gui_node_spine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,12 @@ static void GuiGetVertices(const dmGameSystem::CustomNodeCtx* nodectx, uint32_t
{
InternalGuiNode* node = (InternalGuiNode*)(nodectx->m_NodeData);

if (sizeof(dmSpine::SpineVertex) != struct_size)
{
dmLogOnceError("Size of SpineVertex is %u, but sizeof of gui BoxVertex is %u. Skipping GUI rendering\n", (uint32_t)sizeof(dmSpine::SpineVertex), struct_size);
return;
}

//TODO: Verify the vertex declaration
// In theory, we can check the vertex format to see which components to output
// We currently know it's xyz-uv-rgba
Expand Down

0 comments on commit 4e7eed9

Please sign in to comment.