I'm following the example in the spine-cpp runtime documentation and I'm not sure if I have the code translated correctly to C++ as I get a crash when I try to render the object. Would someone be able to do a quick scan and see if I misunderstood something? I'm using the Spineboy example in the trial version to see if I can get the animation to match. I'm also using stb image library for loading the atlas .png file but in the code I don't see what I would do with the texture variable that's just an unsigned char* as OpenGL already binds the entire atlas file to the texture unit.
m_animationState->update(delta_sec);
m_animationState->apply(*m_skeleton);
m_skeleton->updateWorldTransform();
struct Vertex
{
float x;
float y;
float r;
float g;
float b;
float a;
float u;
float v;
};
for (int i = 0; i < m_skeleton->getSlots().size(); i++)
{
spine::Slot* slot = m_skeleton->getDrawOrder()[i];
spine::Attachment* attachment = slot->getAttachment();
if (!attachment)
{
continue;
}
spine::Color skeletonColor = m_skeleton->getColor();
spine::Color slotColor = slot->getColor();
spine::Color tint(skeletonColor.r * slotColor.r, skeletonColor.g * slotColor.g, skeletonColor.b * slotColor.b, skeletonColor.a * slotColor.a);
unsigned char* texture = nullptr;
std::vector<Vertex> vertices;
unsigned int* indices = nullptr;
unsigned int quadIndices[] = { 0, 1, 2, 2, 3, 0 };
int indexCount = 6;
if (attachment->getRTTI().isExactly(spine::RegionAttachment::rtti))
{
spine::RegionAttachment* regionAttachment = (spine::RegionAttachment*)(attachment);
vertices.assign(4, Vertex());
regionAttachment->computeWorldVertices(slot->getBone(), &vertices.data()->x, 0, sizeof(Vertex));
indices = quadIndices;
for (int j = 0, k = 0; j < vertices.size(); j++, k += 2)
{
Vertex& vertex = vertices[j];
vertex.r = tint.r;
vertex.g = tint.g;
vertex.b = tint.b;
vertex.a = tint.a;
vertex.u = regionAttachment->getUVs()[k];
vertex.v = regionAttachment->getUVs()[k + 1];
}
spine::AtlasRegion* atlasRegion = (spine::AtlasRegion*)regionAttachment->getRendererObject();
texture = (unsigned char*)atlasRegion->page->getRendererObject();
}
else if (attachment->getRTTI().isExactly(spine::MeshAttachment::rtti))
{
spine::MeshAttachment* meshAttachment = (spine::MeshAttachment*)attachment;
vertices.assign(meshAttachment->getWorldVerticesLength() / 2, Vertex());
meshAttachment->computeWorldVertices(*slot, 0, meshAttachment->getWorldVerticesLength() / 2, &vertices.data()->x, 0, sizeof(Vertex));
indexCount = meshAttachment->getTriangles().size();
indices = new unsigned int[indexCount];
for (int i = 0; i < indexCount; i++)
{
indices[i] = static_cast<unsigned int>(meshAttachment->getTriangles()[i]);
}
for (int j = 0, k = 0; j < vertices.size(); j++, k += 2)
{
Vertex& vertex = vertices[j];
vertex.r = tint.r;
vertex.g = tint.g;
vertex.b = tint.b;
vertex.a = tint.a;
vertex.u = meshAttachment->getUVs()[k];
vertex.v = meshAttachment->getUVs()[k + 1];
}
spine::AtlasRegion* atlasRegion = (spine::AtlasRegion*)meshAttachment->getRendererObject();
texture = (unsigned char*)atlasRegion->page->getRendererObject();
}
else
{
continue;
}
glUniform1i(glGetUniformLocation(shaderProgram, "diffuseTexture"), 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_texture);
glBindVertexArray(m_vao);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), vertices.data(), GL_STATIC_DRAW);
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0);
}