From 0c95fe1fbddc045856bf780c0980acd4135dce1c Mon Sep 17 00:00:00 2001 From: yangxiao Date: Fri, 6 Jun 2014 19:12:08 +0800 Subject: [PATCH] animation test --- cocos/3d/CCBundle3D.cpp | 50 ++++++++++++++++++- cocos/3d/CCBundle3D.h | 3 ++ cocos/3d/CCMeshSkin.h | 4 +- cocos/3d/CCSprite3D.cpp | 13 +++-- cocos/renderer/ccShader_3D_ColorTex.frag | 1 + .../Classes/Sprite3DTest/Sprite3DTest.cpp | 41 ++++++++++++++- .../Classes/Sprite3DTest/Sprite3DTest.h | 13 +++++ 7 files changed, 117 insertions(+), 8 deletions(-) diff --git a/cocos/3d/CCBundle3D.cpp b/cocos/3d/CCBundle3D.cpp index 7e6234bd11..d7fa75b355 100644 --- a/cocos/3d/CCBundle3D.cpp +++ b/cocos/3d/CCBundle3D.cpp @@ -34,6 +34,30 @@ NS_CC_BEGIN Bundle3D* Bundle3D::_instance = nullptr; +Bundle3D::MeshData::MeshData() +: vertex(nullptr) +, vertexSizeInFloat(0) +, indices(nullptr) +, numIndex(0) +, attribs(nullptr) +, attribCount(0) +{ + +} +Bundle3D::MeshData::~MeshData() +{ + resetData(); +} +void Bundle3D::MeshData::resetData() +{ + CC_SAFE_DELETE_ARRAY(vertex); + CC_SAFE_DELETE_ARRAY(indices); + CC_SAFE_DELETE_ARRAY(attribs); + vertexSizeInFloat = 0; + numIndex = 0; + attribCount = 0; +} + Bundle3D* Bundle3D::getInstance() { if (_instance == nullptr) @@ -57,7 +81,30 @@ bool Bundle3D::load(const std::string& path) */ bool Bundle3D::loadMeshData(const std::string& id, MeshData* meshdata) { - //meshdata->vertex; + meshdata->resetData(); + meshdata->vertexSizeInFloat = 5 * 4; + meshdata->vertex = new float[meshdata->vertexSizeInFloat]; + float vert[] = {0.f,50.f,0.f,0.f,0.f, 0.f,0.f,50.f,1.f,1.f, 50.f,0.f,0.f,1.f,1.f, -50.f,0.f,0.f,1.f,1.f}; + memcpy(meshdata->vertex, vert, meshdata->vertexSizeInFloat * sizeof(float)); + + //meshdata->numIndex = 4 * 3; + meshdata->numIndex = 3; + meshdata->indices = new unsigned short[meshdata->numIndex]; + unsigned short index[] = {0,1,2};//{0,1,2, 0,3,1, 0,2,3, 3,2,1}; + memcpy(meshdata->indices, index, meshdata->numIndex * sizeof(unsigned short)); + + meshdata->attribCount = 2; + meshdata->attribs = new MeshVertexAttrib[meshdata->attribCount]; + meshdata->attribs[0].attribSizeBytes = 3 * sizeof(float); + meshdata->attribs[0].size = 3; + meshdata->attribs[0].type = GL_FLOAT; + meshdata->attribs[0].vertexAttrib = GLProgram::VERTEX_ATTRIB_POSITION; + + meshdata->attribs[1].attribSizeBytes = 2 * sizeof(float); + meshdata->attribs[1].size = 2; + meshdata->attribs[1].type = GL_FLOAT; + meshdata->attribs[1].vertexAttrib = GLProgram::VERTEX_ATTRIB_TEX_COORD; + return true; } @@ -78,6 +125,7 @@ bool Bundle3D::loadSkinData(const std::string& id, SkinData* skindata) */ bool Bundle3D::loadMaterialData(const std::string& id, MaterialData* materialdata) { + materialdata->texturePath = "Sprite3DTest/boss.png"; return true; } diff --git a/cocos/3d/CCBundle3D.h b/cocos/3d/CCBundle3D.h index 0f8e991bb5..50d20790fe 100644 --- a/cocos/3d/CCBundle3D.h +++ b/cocos/3d/CCBundle3D.h @@ -48,6 +48,9 @@ public: int numIndex; MeshVertexAttrib* attribs; int attribCount; + void resetData(); + MeshData(); + ~MeshData(); }; struct SkinData { diff --git a/cocos/3d/CCMeshSkin.h b/cocos/3d/CCMeshSkin.h index b82f6d4e11..32d5b46bfa 100644 --- a/cocos/3d/CCMeshSkin.h +++ b/cocos/3d/CCMeshSkin.h @@ -152,10 +152,10 @@ protected: class MeshSkin: public Ref { public: - MeshSkin* getOrCreate(const std::string& filename, const std::string& name); + static MeshSkin* getOrCreate(const std::string& filename, const std::string& name); //create a new meshskin if do not want to share meshskin - MeshSkin* create(const std::string& filename, const std::string& name); + static MeshSkin* create(const std::string& filename, const std::string& name); //get & set bind shape matrix const Mat4& getBindShape() const; diff --git a/cocos/3d/CCSprite3D.cpp b/cocos/3d/CCSprite3D.cpp index 7cf5ec53ba..dcbae7ed15 100644 --- a/cocos/3d/CCSprite3D.cpp +++ b/cocos/3d/CCSprite3D.cpp @@ -153,14 +153,19 @@ bool Sprite3D::loadFromC3x(const std::string& path) } _mesh = Mesh::create(meshdata.vertex, meshdata.vertexSizeInFloat, meshdata.indices, meshdata.numIndex, meshdata.attribs, meshdata.attribCount); - _mesh->retain(); + CC_SAFE_RETAIN(_mesh); - Bundle3D::SkinData skindata; - ret = bundle->loadSkinData("", &skindata); +// _skin = MeshSkin::create(fullPath, ""); +// CC_SAFE_RETAIN(_skin); Bundle3D::MaterialData materialdata; ret = bundle->loadMaterialData("", &materialdata); + if (ret) + { + setTexture(materialdata.texturePath); + } + genGLProgramState(); return true; } @@ -292,7 +297,7 @@ void Sprite3D::draw(Renderer *renderer, const Mat4 &transform, bool transformUpd _mesh->getIndexCount(), transform); - _meshCommand.setCullFaceEnabled(true); + //_meshCommand.setCullFaceEnabled(true); _meshCommand.setDepthTestEnabled(true); if (_skin) { diff --git a/cocos/renderer/ccShader_3D_ColorTex.frag b/cocos/renderer/ccShader_3D_ColorTex.frag index d7465c9013..3e8c1ffa2c 100644 --- a/cocos/renderer/ccShader_3D_ColorTex.frag +++ b/cocos/renderer/ccShader_3D_ColorTex.frag @@ -11,5 +11,6 @@ uniform vec4 u_color; void main(void) { gl_FragColor = texture2D(CC_Texture0, TextureCoordOut) * u_color; + gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); } ); diff --git a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp index 8076d2a9e8..370089246e 100644 --- a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp +++ b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp @@ -41,7 +41,8 @@ static int sceneIdx = -1; static std::function createFunctions[] = { CL(Sprite3DBasicTest), - CL(Sprite3DEffectTest) + CL(Sprite3DEffectTest), + CL(Sprite3DWithSkinTest) }; #define MAX_LAYER (sizeof(createFunctions) / sizeof(createFunctions[0])) @@ -502,3 +503,41 @@ void Sprite3DEffectTest::onTouchesEnded(const std::vector& touches, Even addNewSpriteWithCoords( location ); } } + +Sprite3DWithSkinTest::Sprite3DWithSkinTest() +{ + auto listener = EventListenerTouchAllAtOnce::create(); + listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DWithSkinTest::onTouchesEnded, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); + + auto s = Director::getInstance()->getWinSize(); + addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) ); +} +std::string Sprite3DWithSkinTest::title() const +{ + return "Testing Sprite3D"; +} +std::string Sprite3DWithSkinTest::subtitle() const +{ + return "Sprite3D from .c3t"; +} + +void Sprite3DWithSkinTest::addNewSpriteWithCoords(Vec2 p) +{ + auto sprite = Sprite3D::create("Sprite3DTest/XXX.c3t"); + sprite->setScale(8.f); + sprite->setTexture("Sprite3DTest/boss.png"); + addChild(sprite); + + sprite->setPosition( Vec2( p.x, p.y) ); +} + +void Sprite3DWithSkinTest::onTouchesEnded(const std::vector& touches, Event* event) +{ + for (auto touch: touches) + { + auto location = touch->getLocation(); + + addNewSpriteWithCoords( location ); + } +} diff --git a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h index 843bd0af8a..bc89dfd671 100644 --- a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h +++ b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h @@ -135,6 +135,19 @@ public: void onTouchesEnded(const std::vector& touches, Event* event); }; +class Sprite3DWithSkinTest : public Sprite3DTestDemo +{ +public: + CREATE_FUNC(Sprite3DWithSkinTest); + Sprite3DWithSkinTest(); + virtual std::string title() const override; + virtual std::string subtitle() const override; + + void addNewSpriteWithCoords(Vec2 p); + + void onTouchesEnded(const std::vector& touches, Event* event); +}; + class Sprite3DTestScene : public TestScene { public: