From 162ce9f5aa89c7484b7cc2acc1331288f9e086de Mon Sep 17 00:00:00 2001 From: badlogic Date: Wed, 30 May 2018 18:31:36 +0200 Subject: [PATCH] Fixed meshes. --- spine-c/spine-c/src/spine/SkeletonBinary.c | 4 +- .../example/Classes/BatchingExample.cpp | 2 +- spine-cocos2dx/src/spine/SkeletonRenderer.cpp | 47 +++---------------- spine-cocos2dx/src/spine/spine-cocos2dx.cpp | 42 +++++++++++++++++ spine-cocos2dx/src/spine/spine-cocos2dx.h | 7 +++ .../include/spine/AtlasAttachmentLoader.h | 2 + .../include/spine/AttachmentLoader.h | 3 ++ .../src/spine/AtlasAttachmentLoader.cpp | 4 ++ .../spine-cpp/src/spine/SkeletonBinary.cpp | 8 +++- .../spine-cpp/src/spine/SkeletonJson.cpp | 8 +++- 10 files changed, 83 insertions(+), 44 deletions(-) diff --git a/spine-c/spine-c/src/spine/SkeletonBinary.c b/spine-c/spine-c/src/spine/SkeletonBinary.c index 0893650c9..11d270454 100644 --- a/spine-c/spine-c/src/spine/SkeletonBinary.c +++ b/spine-c/spine-c/src/spine/SkeletonBinary.c @@ -779,6 +779,7 @@ spAttachment* spSkeletonBinary_readAttachment(spSkeletonBinary* self, _dataInput } if (nonessential) readInt(input); /* Skip color. */ if (freeName) FREE(name); + spAttachmentLoader_configureAttachment(self->attachmentLoader, attachment); return attachment; } case SP_ATTACHMENT_POINT: { @@ -791,6 +792,7 @@ spAttachment* spSkeletonBinary_readAttachment(spSkeletonBinary* self, _dataInput if (nonessential) { readColor(input, &point->color.r, &point->color.g, &point->color.b, &point->color.a); } + spAttachmentLoader_configureAttachment(self->attachmentLoader, attachment); return attachment; } case SP_ATTACHMENT_CLIPPING: { @@ -925,7 +927,7 @@ spSkeletonData* spSkeletonBinary_readSkeletonData (spSkeletonBinary* self, const /* TODO Avoid copying of slotName */ spSlotData* slotData = spSlotData_create(i, slotName, boneData); FREE(slotName); - readColor(input, &slotData->color.r, &slotData->color.g, &slotData->color.b, &slotData->color.a); + readColor(input, &slotData->color.r, &slotData->color.g, &slotData->color.b, &slotData->color.a); a = readByte(input); r = readByte(input); g = readByte(input); diff --git a/spine-cocos2dx/example/Classes/BatchingExample.cpp b/spine-cocos2dx/example/Classes/BatchingExample.cpp index 1221d1d26..ce5d0be7b 100644 --- a/spine-cocos2dx/example/Classes/BatchingExample.cpp +++ b/spine-cocos2dx/example/Classes/BatchingExample.cpp @@ -52,7 +52,7 @@ bool BatchingExample::init () { // This attachment loader configures attachments with data needed for cocos2d-x rendering. // Do not dispose the attachment loader until the skeleton data is disposed! - _attachmentLoader = new (__FILE__, __LINE__) AtlasAttachmentLoader(_atlas); + _attachmentLoader = new (__FILE__, __LINE__) Cocos2dAtlasAttachmentLoader(_atlas); // Load the skeleton data. SkeletonJson* json = new (__FILE__, __LINE__) SkeletonJson(_attachmentLoader); diff --git a/spine-cocos2dx/src/spine/SkeletonRenderer.cpp b/spine-cocos2dx/src/spine/SkeletonRenderer.cpp index 158431027..872ca93cd 100644 --- a/spine-cocos2dx/src/spine/SkeletonRenderer.cpp +++ b/spine-cocos2dx/src/spine/SkeletonRenderer.cpp @@ -189,7 +189,7 @@ void SkeletonRenderer::initWithData (SkeletonData* skeletonData, bool ownsSkelet void SkeletonRenderer::initWithJsonFile (const std::string& skeletonDataFile, Atlas* atlas, float scale) { _atlas = atlas; - _attachmentLoader = new (__FILE__, __LINE__) AtlasAttachmentLoader(_atlas); + _attachmentLoader = new (__FILE__, __LINE__) Cocos2dAtlasAttachmentLoader(_atlas); SkeletonJson* json = new (__FILE__, __LINE__) SkeletonJson(_attachmentLoader); json->setScale(scale); @@ -207,7 +207,7 @@ void SkeletonRenderer::initWithJsonFile (const std::string& skeletonDataFile, co _atlas = new (__FILE__, __LINE__) Atlas(atlasFile.c_str(), &textureLoader); CCASSERT(_atlas, "Error reading atlas file."); - _attachmentLoader = new (__FILE__, __LINE__) AtlasAttachmentLoader(_atlas); + _attachmentLoader = new (__FILE__, __LINE__) Cocos2dAtlasAttachmentLoader(_atlas); SkeletonJson* json = new (__FILE__, __LINE__) SkeletonJson(_attachmentLoader); json->setScale(scale); @@ -224,7 +224,7 @@ void SkeletonRenderer::initWithJsonFile (const std::string& skeletonDataFile, co void SkeletonRenderer::initWithBinaryFile (const std::string& skeletonDataFile, Atlas* atlas, float scale) { _atlas = atlas; - _attachmentLoader = new (__FILE__, __LINE__) AtlasAttachmentLoader(_atlas); + _attachmentLoader = new (__FILE__, __LINE__) Cocos2dAtlasAttachmentLoader(_atlas); SkeletonBinary* binary = new (__FILE__, __LINE__) SkeletonBinary(_attachmentLoader); binary->setScale(scale); @@ -241,7 +241,7 @@ void SkeletonRenderer::initWithBinaryFile (const std::string& skeletonDataFile, _atlas = new (__FILE__, __LINE__) Atlas(atlasFile.c_str(), &textureLoader); CCASSERT(_atlas, "Error reading atlas file."); - _attachmentLoader = new (__FILE__, __LINE__) AtlasAttachmentLoader(_atlas); + _attachmentLoader = new (__FILE__, __LINE__) Cocos2dAtlasAttachmentLoader(_atlas); SkeletonBinary* binary = new (__FILE__, __LINE__) SkeletonBinary(_attachmentLoader); binary->setScale(scale); @@ -260,37 +260,6 @@ void SkeletonRenderer::update (float deltaTime) { if (_ownsSkeleton) _skeleton->update(deltaTime * _timeScale); } -static void deleteAttachmentVertices (void* vertices) { - delete (AttachmentVertices *) vertices; -} - -static unsigned short quadTriangles[6] = {0, 1, 2, 2, 3, 0}; - -static void setAttachmentVertices(RegionAttachment* attachment) { - if (!attachment->getRendererObject()) { - AtlasRegion* region = (AtlasRegion*)attachment->getRendererObject(); - AttachmentVertices* attachmentVertices = new AttachmentVertices((Texture2D*)region->page->getRendererObject(), 4, quadTriangles, 6); - V3F_C4B_T2F* vertices = attachmentVertices->_triangles->verts; - for (int i = 0, ii = 0; i < 4; ++i, ii += 2) { - vertices[i].texCoords.u = attachment->getUVs()[ii]; - vertices[i].texCoords.v = attachment->getUVs()[ii + 1]; - } - attachment->setRendererObject(attachmentVertices, deleteAttachmentVertices); - } -} - -static void setAttachmentVertices(MeshAttachment* attachment) { - AtlasRegion* region = (AtlasRegion*)attachment->getRendererObject(); - AttachmentVertices* attachmentVertices = new AttachmentVertices((Texture2D*)region->page->getRendererObject(), - attachment->getWorldVerticesLength() >> 1, attachment->getTriangles().buffer(), attachment->getTriangles().size()); - V3F_C4B_T2F* vertices = attachmentVertices->_triangles->verts; - for (int i = 0, ii = 0, nn = attachment->getWorldVerticesLength(); ii < nn; ++i, ii += 2) { - vertices[i].texCoords.u = attachment->getUVs()[ii]; - vertices[i].texCoords.v = attachment->getUVs()[ii + 1]; - } - attachment->setRendererObject(attachmentVertices, deleteAttachmentVertices); -} - void SkeletonRenderer::draw (Renderer* renderer, const Mat4& transform, uint32_t transformFlags) { SkeletonBatch* batch = SkeletonBatch::getInstance(); SkeletonTwoColorBatch* twoColorBatch = SkeletonTwoColorBatch::getInstance(); @@ -336,7 +305,6 @@ void SkeletonRenderer::draw (Renderer* renderer, const Mat4& transform, uint32_t if (slot->getAttachment()->getRTTI().isExactly(RegionAttachment::rtti)) { RegionAttachment* attachment = (RegionAttachment*)slot->getAttachment(); - setAttachmentVertices(attachment); attachmentVertices = (AttachmentVertices*)attachment->getRendererObject(); if (!isTwoColorTint) { @@ -363,8 +331,7 @@ void SkeletonRenderer::draw (Renderer* renderer, const Mat4& transform, uint32_t color.a = attachment->getColor().a; } else if (slot->getAttachment()->getRTTI().isExactly(MeshAttachment::rtti)) { - MeshAttachment* attachment = (MeshAttachment*)slot->getAttachment(); - setAttachmentVertices(attachment); + MeshAttachment* attachment = (MeshAttachment*)slot->getAttachment(); attachmentVertices = (AttachmentVertices*)attachment->getRendererObject(); if (!isTwoColorTint) { @@ -373,7 +340,7 @@ void SkeletonRenderer::draw (Renderer* renderer, const Mat4& transform, uint32_t triangles.verts = batch->allocateVertices(attachmentVertices->_triangles->vertCount); triangles.vertCount = attachmentVertices->_triangles->vertCount; memcpy(triangles.verts, attachmentVertices->_triangles->verts, sizeof(cocos2d::V3F_C4B_T2F) * attachmentVertices->_triangles->vertCount); - attachment->computeWorldVertices(*slot, 0, triangles.vertCount * sizeof(cocos2d::V3F_C4B_T2F) / 4, (float*)triangles.verts, 0, 6); + attachment->computeWorldVertices(*slot, 0, attachment->getWorldVerticesLength(), (float*)triangles.verts, 0, 6); } else { trianglesTwoColor.indices = attachmentVertices->_triangles->indices; trianglesTwoColor.indexCount = attachmentVertices->_triangles->indexCount; @@ -382,7 +349,7 @@ void SkeletonRenderer::draw (Renderer* renderer, const Mat4& transform, uint32_t for (int i = 0; i < trianglesTwoColor.vertCount; i++) { trianglesTwoColor.verts[i].texCoords = attachmentVertices->_triangles->verts[i].texCoords; } - attachment->computeWorldVertices(*slot, 0, trianglesTwoColor.vertCount * sizeof(V3F_C4B_C4B_T2F) / 4, (float*)trianglesTwoColor.verts, 0, 7); + attachment->computeWorldVertices(*slot, 0, attachment->getWorldVerticesLength(), (float*)trianglesTwoColor.verts, 0, 7); } color.r = attachment->getColor().r; diff --git a/spine-cocos2dx/src/spine/spine-cocos2dx.cpp b/spine-cocos2dx/src/spine/spine-cocos2dx.cpp index 93f59f429..2d52e0c09 100644 --- a/spine-cocos2dx/src/spine/spine-cocos2dx.cpp +++ b/spine-cocos2dx/src/spine/spine-cocos2dx.cpp @@ -35,6 +35,48 @@ USING_NS_CC; using namespace spine; +static void deleteAttachmentVertices (void* vertices) { + delete (AttachmentVertices *) vertices; +} + +static unsigned short quadTriangles[6] = {0, 1, 2, 2, 3, 0}; + +static void setAttachmentVertices(RegionAttachment* attachment) { + AtlasRegion* region = (AtlasRegion*)attachment->getRendererObject(); + AttachmentVertices* attachmentVertices = new AttachmentVertices((Texture2D*)region->page->getRendererObject(), 4, quadTriangles, 6); + V3F_C4B_T2F* vertices = attachmentVertices->_triangles->verts; + for (int i = 0, ii = 0; i < 4; ++i, ii += 2) { + vertices[i].texCoords.u = attachment->getUVs()[ii]; + vertices[i].texCoords.v = attachment->getUVs()[ii + 1]; + } + attachment->setRendererObject(attachmentVertices, deleteAttachmentVertices); +} + +static void setAttachmentVertices(MeshAttachment* attachment) { + AtlasRegion* region = (AtlasRegion*)attachment->getRendererObject(); + AttachmentVertices* attachmentVertices = new AttachmentVertices((Texture2D*)region->page->getRendererObject(), + attachment->getWorldVerticesLength() >> 1, attachment->getTriangles().buffer(), attachment->getTriangles().size()); + V3F_C4B_T2F* vertices = attachmentVertices->_triangles->verts; + for (int i = 0, ii = 0, nn = attachment->getWorldVerticesLength(); ii < nn; ++i, ii += 2) { + vertices[i].texCoords.u = attachment->getUVs()[ii]; + vertices[i].texCoords.v = attachment->getUVs()[ii + 1]; + } + attachment->setRendererObject(attachmentVertices, deleteAttachmentVertices); +} + +Cocos2dAtlasAttachmentLoader::Cocos2dAtlasAttachmentLoader(Atlas* atlas): AtlasAttachmentLoader(atlas) { +} + +Cocos2dAtlasAttachmentLoader::~Cocos2dAtlasAttachmentLoader() { } + +void Cocos2dAtlasAttachmentLoader::configureAttachment(Attachment* attachment) { + if (attachment->getRTTI().isExactly(RegionAttachment::rtti)) { + setAttachmentVertices((RegionAttachment*)attachment); + } else if (attachment->getRTTI().isExactly(MeshAttachment::rtti)) { + setAttachmentVertices((MeshAttachment*)attachment); + } +} + GLuint wrap (TextureWrap wrap) { return wrap == TextureWrap_ClampToEdge ? GL_CLAMP_TO_EDGE : GL_REPEAT; } diff --git a/spine-cocos2dx/src/spine/spine-cocos2dx.h b/spine-cocos2dx/src/spine/spine-cocos2dx.h index 0b03cf4af..c5704d5d7 100644 --- a/spine-cocos2dx/src/spine/spine-cocos2dx.h +++ b/spine-cocos2dx/src/spine/spine-cocos2dx.h @@ -39,6 +39,13 @@ #include namespace spine { + class Cocos2dAtlasAttachmentLoader: public AtlasAttachmentLoader { + public: + Cocos2dAtlasAttachmentLoader(Atlas* atlas); + virtual ~Cocos2dAtlasAttachmentLoader(); + virtual void configureAttachment(Attachment* attachment); + }; + class Cocos2dTextureLoader: public TextureLoader { public: Cocos2dTextureLoader(); diff --git a/spine-cpp/spine-cpp/include/spine/AtlasAttachmentLoader.h b/spine-cpp/spine-cpp/include/spine/AtlasAttachmentLoader.h index 0a3f7d364..414fdbc1f 100644 --- a/spine-cpp/spine-cpp/include/spine/AtlasAttachmentLoader.h +++ b/spine-cpp/spine-cpp/include/spine/AtlasAttachmentLoader.h @@ -61,6 +61,8 @@ namespace spine { virtual PointAttachment* newPointAttachment(Skin& skin, const String& name); virtual ClippingAttachment* newClippingAttachment(Skin& skin, const String& name); + + virtual void configureAttachment(Attachment* attachment); AtlasRegion* findRegion(const String& name); diff --git a/spine-cpp/spine-cpp/include/spine/AttachmentLoader.h b/spine-cpp/spine-cpp/include/spine/AttachmentLoader.h index be75836c7..d098e70d9 100644 --- a/spine-cpp/spine-cpp/include/spine/AttachmentLoader.h +++ b/spine-cpp/spine-cpp/include/spine/AttachmentLoader.h @@ -37,6 +37,7 @@ namespace spine { class Skin; + class Attachment; class RegionAttachment; class MeshAttachment; class BoundingBoxAttachment; @@ -67,6 +68,8 @@ namespace spine { virtual PointAttachment* newPointAttachment(Skin& skin, const String& name) = 0; virtual ClippingAttachment* newClippingAttachment(Skin& skin, const String& name) = 0; + + virtual void configureAttachment(Attachment* attachment) = 0; }; } diff --git a/spine-cpp/spine-cpp/src/spine/AtlasAttachmentLoader.cpp b/spine-cpp/spine-cpp/src/spine/AtlasAttachmentLoader.cpp index 04fafba51..cd2fa0e75 100644 --- a/spine-cpp/spine-cpp/src/spine/AtlasAttachmentLoader.cpp +++ b/spine-cpp/spine-cpp/src/spine/AtlasAttachmentLoader.cpp @@ -115,6 +115,10 @@ ClippingAttachment *AtlasAttachmentLoader::newClippingAttachment(Skin &skin, con return new(__FILE__, __LINE__) ClippingAttachment(name); } +void AtlasAttachmentLoader::configureAttachment(Attachment* attachment) { + SP_UNUSED(attachment); +} + AtlasRegion *AtlasAttachmentLoader::findRegion(const String &name) { return _atlas->findRegion(name); } diff --git a/spine-cpp/spine-cpp/src/spine/SkeletonBinary.cpp b/spine-cpp/spine-cpp/src/spine/SkeletonBinary.cpp index aa133b361..09ebf5f7e 100644 --- a/spine-cpp/spine-cpp/src/spine/SkeletonBinary.cpp +++ b/spine-cpp/spine-cpp/src/spine/SkeletonBinary.cpp @@ -294,6 +294,7 @@ SkeletonData *SkeletonBinary::readSkeletonData(const unsigned char *binary, cons } linkedMesh->_mesh->setParentMesh(static_cast(parent)); linkedMesh->_mesh->updateUVs(); + _attachmentLoader->configureAttachment(linkedMesh->_mesh); } ContainerUtil::cleanUpVectorOfPointers(_linkedMeshes); _linkedMeshes.clear(); @@ -469,6 +470,7 @@ Attachment *SkeletonBinary::readAttachment(DataInput *input, Skin *skin, int slo region->_height = readFloat(input) * _scale; readColor(input, region->getColor()); region->updateOffset(); + _attachmentLoader->configureAttachment(region); return region; } case AttachmentType_Boundingbox: { @@ -479,6 +481,7 @@ Attachment *SkeletonBinary::readAttachment(DataInput *input, Skin *skin, int slo /* Skip color. */ readInt(input); } + _attachmentLoader->configureAttachment(box); return box; } case AttachmentType_Mesh: { @@ -504,6 +507,7 @@ Attachment *SkeletonBinary::readAttachment(DataInput *input, Skin *skin, int slo mesh->_width = 0; mesh->_height = 0; } + _attachmentLoader->configureAttachment(mesh); return mesh; } case AttachmentType_Linkedmesh: { @@ -541,6 +545,7 @@ Attachment *SkeletonBinary::readAttachment(DataInput *input, Skin *skin, int slo /* Skip color. */ readInt(input); } + _attachmentLoader->configureAttachment(path); return path; } case AttachmentType_Point: { @@ -553,7 +558,7 @@ Attachment *SkeletonBinary::readAttachment(DataInput *input, Skin *skin, int slo /* Skip color. */ readInt(input); } - + _attachmentLoader->configureAttachment(point); return point; } case AttachmentType_Clipping: { @@ -566,6 +571,7 @@ Attachment *SkeletonBinary::readAttachment(DataInput *input, Skin *skin, int slo /* Skip color. */ readInt(input); } + _attachmentLoader->configureAttachment(clip); return clip; } } diff --git a/spine-cpp/spine-cpp/src/spine/SkeletonJson.cpp b/spine-cpp/spine-cpp/src/spine/SkeletonJson.cpp index 062a1473a..1e6d05f8c 100644 --- a/spine-cpp/spine-cpp/src/spine/SkeletonJson.cpp +++ b/spine-cpp/spine-cpp/src/spine/SkeletonJson.cpp @@ -495,7 +495,7 @@ SkeletonData *SkeletonJson::readSkeletonData(const char *json) { } region->updateOffset(); - + _attachmentLoader->configureAttachment(region); break; } case AttachmentType_Mesh: @@ -548,6 +548,7 @@ SkeletonData *SkeletonJson::readSkeletonData(const char *json) { mesh->_edges[ii] = entry->_valueInt; } } + _attachmentLoader->configureAttachment(mesh); } else { mesh->_inheritDeform = Json::getInt(attachmentMap, "deform", 1) ? true : false; LinkedMesh *linkedMesh = new(__FILE__, __LINE__) LinkedMesh(mesh, @@ -567,6 +568,7 @@ SkeletonData *SkeletonJson::readSkeletonData(const char *json) { int vertexCount = Json::getInt(attachmentMap, "vertexCount", 0) << 1; readVertices(attachmentMap, box, vertexCount); + _attachmentLoader->configureAttachment(attachment); break; } case AttachmentType_Path: { @@ -587,6 +589,7 @@ SkeletonData *SkeletonJson::readSkeletonData(const char *json) { for (curves = curves->_child, ii = 0; curves; curves = curves->_next, ++ii) { pathAttatchment->_lengths[ii] = curves->_valueFloat * _scale; } + _attachmentLoader->configureAttachment(attachment); break; } case AttachmentType_Point: { @@ -597,6 +600,7 @@ SkeletonData *SkeletonJson::readSkeletonData(const char *json) { point->_x = Json::getFloat(attachmentMap, "x", 0) * _scale; point->_y = Json::getFloat(attachmentMap, "y", 0) * _scale; point->_rotation = Json::getFloat(attachmentMap, "rotation", 0); + _attachmentLoader->configureAttachment(attachment); break; } case AttachmentType_Clipping: { @@ -612,6 +616,7 @@ SkeletonData *SkeletonJson::readSkeletonData(const char *json) { } vertexCount = Json::getInt(attachmentMap, "vertexCount", 0) << 1; readVertices(attachmentMap, clip, vertexCount); + _attachmentLoader->configureAttachment(attachment); break; } } @@ -640,6 +645,7 @@ SkeletonData *SkeletonJson::readSkeletonData(const char *json) { } linkedMesh->_mesh->setParentMesh(static_cast(parent)); linkedMesh->_mesh->updateUVs(); + _attachmentLoader->configureAttachment(linkedMesh->_mesh); } ContainerUtil::cleanUpVectorOfPointers(_linkedMeshes); _linkedMeshes.clear();