refactor to new model

This commit is contained in:
yangxiao 2014-08-18 11:13:08 +08:00
parent 1e961e00b7
commit 79a55286ce
7 changed files with 46 additions and 316 deletions

View File

@ -48,44 +48,6 @@ MeshSkin::~MeshSkin()
CC_SAFE_RELEASE(_skeleton);
}
MeshSkin* MeshSkin::create(Skeleton3D* skeleton, const std::string& filename, const std::string& name)
{
//load skin here;
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(filename);
std::string key = fullPath + "#" + name;
const auto skindata = MeshSkinDataCache::getInstance()->getMeshSkinData(key);
if (skindata)
{
auto skin = new MeshSkin();
skin->_skeleton = skeleton;
skeleton->retain();
skin->initFromSkinData(*skindata);
skin->autorelease();
return skin;
}
else
{
auto instance = Bundle3D::getInstance();
bool ret = instance->load(fullPath);
if (ret)
{
SkinData data;
if (instance->loadSkinData(name, &data))
{
auto skin = new MeshSkin();
skin->_skeleton = skeleton;
skeleton->retain();
skin->initFromSkinData(data);
skin->autorelease();
MeshSkinDataCache::getInstance()->addMeshSkinData(key, data);
return skin;
}
}
}
return nullptr;
}
MeshSkin* MeshSkin::create(Skeleton3D* skeleton, const std::vector<std::string>& boneNames, const std::vector<Mat4>& invBindPose)
{
auto skin = new MeshSkin();
@ -103,39 +65,9 @@ MeshSkin* MeshSkin::create(Skeleton3D* skeleton, const std::vector<std::string>&
return skin;
}
bool MeshSkin::initFromSkinData(const SkinData& skindata)
{
ssize_t i = 0;
for (; i < skindata.skinBoneNames.size(); i++) {
auto bone = Bone3D::create(skindata.skinBoneNames[i]);
bone->_invBindPose = skindata.inverseBindPoseMatrices[i];
bone->setOriPose(skindata.skinBoneOriginMatrices[i]);
_invBindPoses.push_back(skindata.inverseBindPoseMatrices[i]);
addSkinBone(bone);
}
for (i = 0; i < skindata.nodeBoneNames.size(); i++) {
auto bone = Bone3D::create(skindata.nodeBoneNames[i]);
bone->setOriPose(skindata.nodeBoneOriginMatrices[i]);
addNodeBone(bone);
}
for (auto it : skindata.boneChild) {
auto parent = getBoneByIndex(it.first);
for (auto childIt : it.second) {
auto child = getBoneByIndex(childIt);
child->_parent = parent;
parent->_children.pushBack(child);
}
}
setRootBone(getBoneByIndex(skindata.rootBoneIndex));
_rootBone->resetPose();
return true;
}
ssize_t MeshSkin::getBoneCount() const
{
return _skinBones.size() + _nodeBones.size();
return _skinBones.size();
}
//get bone
@ -143,9 +75,6 @@ Bone3D* MeshSkin::getBoneByIndex(unsigned int index) const
{
if (index < _skinBones.size())
return _skinBones.at(index);
index -= _skinBones.size();
if (index < _nodeBones.size())
return _nodeBones.at(index);
return nullptr;
}
@ -156,25 +85,9 @@ Bone3D* MeshSkin::getBoneByName(const std::string& id) const
if (it->getName() == id)
return it;
}
//search from node bones
for (const auto& it : _nodeBones) {
if (it->getName() == id )
return it;
}
return nullptr;
}
Bone3D* MeshSkin::getRootBone() const
{
return _rootBone;
}
void MeshSkin::setRootBone(Bone3D* joint)
{
CC_SAFE_RETAIN(joint);
CC_SAFE_RELEASE(_rootBone);
_rootBone = joint;
}
int MeshSkin::getBoneIndex(Bone3D* bone) const
{
int i = 0;
@ -182,19 +95,13 @@ int MeshSkin::getBoneIndex(Bone3D* bone) const
if (_skinBones.at(i) == bone)
return i;
}
int index = 0;
for (; index < _nodeBones.size(); index++, i++) {
if (_nodeBones.at(index) == bone)
return i;
}
return -1;
}
//compute matrix palette used by gpu skin
Vec4* MeshSkin::getMatrixPalette()
{
updateBoneMatrix();
if (_matrixPalette == nullptr)
{
_matrixPalette = new Vec4[_skinBones.size() * PALETTE_ROWS];
@ -217,17 +124,9 @@ ssize_t MeshSkin::getMatrixPaletteSize() const
return _skinBones.size() * PALETTE_ROWS;
}
//refresh bone world matrix
void MeshSkin::updateBoneMatrix()
{
// _rootBone->setWorldMatDirty(true);
// _rootBone->updateWorldMat();
}
void MeshSkin::removeAllBones()
{
_skinBones.clear();
_nodeBones.clear();
CC_SAFE_DELETE_ARRAY(_matrixPalette);
CC_SAFE_RELEASE(_rootBone);
}
@ -237,11 +136,6 @@ void MeshSkin::addSkinBone(Bone3D* bone)
_skinBones.pushBack(bone);
}
void MeshSkin::addNodeBone(Bone3D* bone)
{
_nodeBones.pushBack(bone);
}
////////////////////////////////////////////////////////////////////////
MeshSkinDataCache* MeshSkinDataCache::_cacheInstance = nullptr;

View File

@ -61,10 +61,6 @@ public:
Bone3D* getBoneByIndex(unsigned int index) const;
Bone3D* getBoneByName(const std::string& id) const;
/**get & set root bone*/
Bone3D* getRootBone() const;
void setRootBone(Bone3D* bone);
/**get bone index*/
int getBoneIndex(Bone3D* bone) const;
@ -74,32 +70,22 @@ public:
/**getSkinBoneCount() * 3*/
ssize_t getMatrixPaletteSize() const;
/**refresh bone world matrix*/
void updateBoneMatrix();
CC_CONSTRUCTOR_ACCESS:
MeshSkin();
~MeshSkin();
/**init from skin data*/
bool initFromSkinData(const SkinData& skindata);
/**remove all bones*/
void removeAllBones();
/**add skin bone*/
void addSkinBone(Bone3D* bone);
/**add Node bone*/
void addNodeBone(Bone3D* bone);
protected:
Vector<Bone3D*> _skinBones; // bones with skin
std::vector<Mat4> _invBindPoses; //inverse bind pose of bone
Vector<Bone3D*> _nodeBones; //bones without skin, only used to compute transform of children
Bone3D* _rootBone;
Skeleton3D* _skeleton; //skeleton the skin refered

View File

@ -249,7 +249,6 @@ void Bone3D::updateLocalMat()
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Skeleton3D::Skeleton3D()
: _rootBone(nullptr)
{
}
@ -259,41 +258,6 @@ Skeleton3D::~Skeleton3D()
removeAllBones();
}
//create a new meshskin if do not want to share meshskin
Skeleton3D* Skeleton3D::create(const std::string& filename, const std::string& name)
{
//load skin here;
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(filename);
std::string key(fullPath + "#" + name);
const auto skeletondata = Skeleton3DDataCache::getInstance()->getSkeletonData(key);
if (skeletondata)
{
auto skeleton = new Skeleton3D();
skeleton->initFromSkeletonData(*skeletondata);
skeleton->autorelease();
return skeleton;
}
else
{
auto instance = Bundle3D::getInstance();
bool ret = instance->load(fullPath);
if (ret)
{
Skeleton3DData data;
if (instance->loadSkeletonData(name, &data))
{
auto skeleton = new Skeleton3D();
skeleton->initFromSkeletonData(data);
skeleton->autorelease();
Skeleton3DDataCache::getInstance()->addSkeletonData(key, data);
return skeleton;
}
}
}
return nullptr;
}
Skeleton3D* Skeleton3D::create(const std::vector<NodeData*>& skeletondata)
{
auto skeleton = new Skeleton3D();
@ -306,31 +270,6 @@ Skeleton3D* Skeleton3D::create(const std::vector<NodeData*>& skeletondata)
return skeleton;
}
bool Skeleton3D::initFromSkeletonData(const Skeleton3DData& skeletondata)
{
ssize_t i = 0;
for (; i < skeletondata.boneNames.size(); i++) {
auto bone = Bone3D::create(skeletondata.boneNames[i]);
bone->setOriPose(skeletondata.boneOriginMatrices[i]);
addBone(bone);
}
for (auto it : skeletondata.boneChild) {
auto parent = getBoneByIndex(it.first);
for (auto childIt : it.second) {
auto child = getBoneByIndex(childIt);
child->_parent = parent;
parent->_children.pushBack(child);
}
}
setRootBone(getBoneByIndex(skeletondata.rootBoneIndex));
if (_rootBone)
_rootBone->resetPose();
return true;
}
ssize_t Skeleton3D::getBoneCount() const
{
return _bones.size();
@ -355,17 +294,6 @@ Bone3D* Skeleton3D::getBoneByName(const std::string& id) const
return nullptr;
}
Bone3D* Skeleton3D::getRootBone() const
{
return _rootBone;
}
void Skeleton3D::setRootBone(Bone3D* joint)
{
CC_SAFE_RETAIN(joint);
CC_SAFE_RELEASE(_rootBone);
_rootBone = joint;
}
ssize_t Skeleton3D::getRootCount() const
{
return _rootBones.size();
@ -401,7 +329,6 @@ void Skeleton3D::updateBoneMatrix()
void Skeleton3D::removeAllBones()
{
_bones.clear();
CC_SAFE_RELEASE(_rootBone);
_rootBones.clear();
}

View File

@ -187,9 +187,6 @@ class CC_DLL Skeleton3D: public Ref
{
public:
/**create a new meshskin if do not want to share Skeleton3D*/
static Skeleton3D* create(const std::string& filename, const std::string& name);
static Skeleton3D* create(const std::vector<NodeData*>& skeletondata);
/**get total bone count*/
@ -200,8 +197,6 @@ public:
Bone3D* getBoneByName(const std::string& id) const;
/**get & set root bone*/
Bone3D* getRootBone() const;
void setRootBone(Bone3D* bone);
ssize_t getRootCount() const;
Bone3D* getRootBone(int index) const;
@ -217,9 +212,6 @@ CC_CONSTRUCTOR_ACCESS:
~Skeleton3D();
/**init from skeleton data*/
bool initFromSkeletonData(const Skeleton3DData& skeletondata);
/**remove all bones*/
void removeAllBones();
@ -233,7 +225,6 @@ protected:
Vector<Bone3D*> _bones; // bones
Bone3D* _rootBone;
Vector<Bone3D*> _rootBones;
};

View File

@ -75,35 +75,7 @@ Sprite3D* Sprite3D::create(const std::string &modelPath, const std::string &text
bool Sprite3D::loadFromCache(const std::string& path)
{
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(path);
//find from the cache
std::string key = fullPath + "#";
auto mesh = MeshCache::getInstance()->getMesh(key);
if (mesh)
{
_mesh = mesh;
_mesh->retain();
char str[20];
for (int i = 0; i < (int)_mesh->getSubMeshCount(); i++) {
sprintf(str, "submesh%d", i);
std::string submeshkey = key + std::string(str);
auto tex = Sprite3DMaterialCache::getInstance()->getSprite3DMaterial(submeshkey);
auto submeshstate = SubMeshState::create();
submeshstate->setTexture(tex);
_subMeshStates.pushBack(submeshstate);
}
_skeleton = Skeleton3D::create(fullPath, "");
CC_SAFE_RETAIN(_skeleton);
_skin = MeshSkin::create(_skeleton, fullPath, "");
CC_SAFE_RETAIN(_skin);
genGLProgramState();
return true;
}
//FIX ME, TODO
return false;
}
@ -196,53 +168,12 @@ bool Sprite3D::loadFromC3x_0_3(const std::string& path)
}
bool Sprite3D::loadFromC3x(const std::string& path)
{
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(path);
std::string key = fullPath + "#";
//load from .c3b or .c3t
auto bundle = Bundle3D::getInstance();
if (!bundle->load(fullPath))
return false;
MeshData meshdata;
bool ret = bundle->loadMeshData("", &meshdata);
if (!ret)
{
return false;
}
_mesh = Mesh::create(meshdata.vertex, meshdata.vertexSizeInFloat, meshdata.subMeshIndices, meshdata.attribs);
CC_SAFE_RETAIN(_mesh);
//add mesh to cache
MeshCache::getInstance()->addMesh(key, _mesh);
_skeleton = Skeleton3D::create(fullPath, "");
CC_SAFE_RETAIN(_skeleton);
_skin = MeshSkin::create(_skeleton, fullPath, "");
CC_SAFE_RETAIN(_skin);
MaterialData materialdata;
ret = bundle->loadMaterialData("", &materialdata);
if (ret)
{
std::vector<std::string> texpaths;
texpaths.resize(_mesh->getSubMeshCount(), "");
for (auto& it : materialdata.texturePaths)
{
texpaths[it.first] = it.second;
}
genMaterials(key, texpaths);
}
genGLProgramState();
//will move loadFromC3x_0_3 here
return true;
}
Sprite3D::Sprite3D()
: _mesh(nullptr)
, _skin(nullptr)
, _skeleton(nullptr)
: _skeleton(nullptr)
, _blend(BlendFunc::ALPHA_NON_PREMULTIPLIED)
{
}
@ -251,8 +182,6 @@ Sprite3D::~Sprite3D()
{
_subMeshStates.clear();
_meshes.clear();
CC_SAFE_RELEASE_NULL(_mesh);
CC_SAFE_RELEASE_NULL(_skin);
CC_SAFE_RELEASE_NULL(_skeleton);
removeAllAttachNode();
}
@ -260,8 +189,6 @@ Sprite3D::~Sprite3D()
bool Sprite3D::initWithFile(const std::string &path)
{
_subMeshStates.clear();
CC_SAFE_RELEASE_NULL(_mesh);
CC_SAFE_RELEASE_NULL(_skin);
CC_SAFE_RELEASE_NULL(_skeleton);
if (loadFromCache(path))
@ -417,27 +344,6 @@ SubMesh* Sprite3D::getSubMesh(const std::string& subMeshId) const
return nullptr;
}
void Sprite3D::genMaterials(const std::string& keyprefix, const std::vector<std::string>& texpaths)
{
_subMeshStates.clear();
char str[20];
auto cache = Director::getInstance()->getTextureCache();
int index = 0;
for (auto& it : texpaths) {
auto tex = cache->addImage(it);
auto subMeshState = SubMeshState::create();
subMeshState->setTexture(tex);
_subMeshStates.pushBack(subMeshState);
// //add to cache
// sprintf(str, "submesh%d", index);
// std::string submeshkey = keyprefix + std::string(str);
// Sprite3DMaterialCache::getInstance()->addSprite3DMaterial(submeshkey, tex);
index++;
}
}
void Sprite3D::setTexture(const std::string& texFile)
{
auto tex = Director::getInstance()->getTextureCache()->addImage(texFile);
@ -454,9 +360,9 @@ AttachNode* Sprite3D::getAttachNode(const std::string& boneName)
if (it != _attachments.end())
return it->second;
if (_skin)
if (_skeleton)
{
auto bone = _skin->getBoneByName(boneName);
auto bone = _skeleton->getBoneByName(boneName);
auto attachNode = AttachNode::create(bone);
addChild(attachNode);
_attachments[boneName] = attachNode;
@ -543,12 +449,12 @@ AABB Sprite3D::getAABB() const
else
{
Mat4 transform(nodeToWorldTransform);
_aabb = _mesh->getOriginAABB();
_aabb = _originalAABB;
if (getSkin() && getSkin()->getRootBone())
{
transform = nodeToWorldTransform * getSkin()->getRootBone()->getWorldMat();
}
// if (getSkin() && getSkin()->getRootBone())
// {
// transform = nodeToWorldTransform * getSkin()->getRootBone()->getWorldMat();
// }
_aabb.transform(transform);
_nodeToWorldTransform = nodeToWorldTransform;
@ -578,4 +484,29 @@ void Sprite3D::setCullFaceEnabled(bool enable)
}
}
SubMeshState* Sprite3D::getSubMeshState(int index) const
{
CCASSERT(index < _subMeshStates.size(), "invald index");
return _subMeshStates.at(index);
}
/**get SubMeshState by Name */
SubMeshState* Sprite3D::getSubMeshStateByName(const std::string& name) const
{
for (const auto& it : _subMeshStates) {
if (it->getName() == name)
return it;
}
return nullptr;
}
MeshSkin* Sprite3D::getSkin() const
{
for (const auto& it : _subMeshStates) {
if (it->getSkin())
return it->getSkin();
}
return nullptr;
}
NS_CC_END

View File

@ -63,12 +63,15 @@ public:
/**get SubMeshState by index*/
SubMeshState* getSubMeshState(int index) const;
/**get SubMeshState by Name */
SubMeshState* getSubMeshStateByName(const std::string& name) const;
/**get mesh*/
Mesh* getMesh() const { return _mesh; }
Mesh* getMesh() const { return _meshes.at(0); }
/**get skin*/
MeshSkin* getSkin() const { return _skin; }
CC_DEPRECATED_ATTRIBUTE MeshSkin* getSkin() const;
Skeleton3D* getSkeleton() const { return _skeleton; }
@ -128,16 +131,13 @@ CC_CONSTRUCTOR_ACCESS:
/**generate default GLProgramState*/
void genGLProgramState();
/**generate materials, and add them to cache, keyprefix is used as key prefix when added to cache*/
void genMaterials(const std::string& keyprefix, const std::vector<std::string>& texpaths);
void createNode(NodeData* nodedata, Node* root, const MaterialDatas& matrialdatas);
/**get SubMesh by Id*/
SubMesh* getSubMesh(const std::string& subMeshId) const;
protected:
Mesh* _mesh;//mesh
MeshSkin* _skin;//skin
Skeleton3D* _skeleton; //skeleton
std::vector<MeshCommand> _meshCommands; //render command each for one submesh
@ -152,6 +152,7 @@ protected:
Vector<Mesh*> _meshes;
mutable AABB _aabb; // cache current aabb
AABB _originalAABB; // original aabb
mutable Mat4 _nodeToWorldTransform; // cache the matrix
};

View File

@ -1275,7 +1275,7 @@ void Sprite3DMirrorTest::addNewSpriteWithCoords(Vec2 p)
std::string fileName = "Sprite3DTest/orc.c3t";
auto sprite = Sprite3D::create(fileName);
sprite->setScale(6);
sprite->setRotation3D(Vec3(90,0,0));
sprite->setRotation3D(Vec3(0,180,0));
addChild(sprite);
sprite->setPosition( Vec2( p.x - 80, p.y) );