This commit is contained in:
yangxiao 2014-08-15 18:27:03 +08:00
commit 9ab48bd71e
3 changed files with 92 additions and 7 deletions

View File

@ -274,6 +274,7 @@ bool Bundle3D::loadSkeletonData(const std::string& id, Skeleton3DData* skeletond
//since 3.3, to support reskin //since 3.3, to support reskin
bool Bundle3D::loadMeshDatas(MeshDatas& meshdatas) bool Bundle3D::loadMeshDatas(MeshDatas& meshdatas)
{ {
meshdatas.resetData();
if (_isBinary) if (_isBinary)
{ {
return loadMeshDatasBinary(meshdatas); return loadMeshDatasBinary(meshdatas);
@ -349,7 +350,7 @@ bool Bundle3D::loadNodes(NodeDatas& nodedatas)
bool Bundle3D::loadMaterials(MaterialDatas& materialdatas) bool Bundle3D::loadMaterials(MaterialDatas& materialdatas)
{ {
materialdatas.resetData();
if (_isBinary) if (_isBinary)
{ {
return loadMaterialsBinary(materialdatas); return loadMaterialsBinary(materialdatas);

View File

@ -62,7 +62,6 @@ Sprite3D* Sprite3D::create(const std::string &modelPath)
CC_SAFE_DELETE(sprite); CC_SAFE_DELETE(sprite);
return nullptr; return nullptr;
} }
Sprite3D* Sprite3D::create(const std::string &modelPath, const std::string &texturePath) Sprite3D* Sprite3D::create(const std::string &modelPath, const std::string &texturePath)
{ {
auto sprite = create(modelPath); auto sprite = create(modelPath);
@ -152,7 +151,58 @@ bool Sprite3D::loadFromObj(const std::string& path)
return true; return true;
} }
bool Sprite3D::loadFromC3x_0_3(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;
MeshDatas meshdatas;
bool ret = bundle->loadMeshDatas(meshdatas);
if (!ret)
{
return false;
}
for( int i = 0 ; i < meshdatas.meshDatas.size() ; i++ )
{
MeshData* meshData= meshdatas.meshDatas[i];
if(meshData)
{
Mesh* mesh = Mesh::create(meshData->vertex,meshData->vertexSizeInFloat, meshData->subMeshIndices,meshData->attribs);
_meshes.push_back(mesh);
}
}
// 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);
MaterialDatas materialdatas;
ret = bundle->loadMaterials(materialdatas);
if (ret)
{
}
NodeDatas nodeDatas;
bundle->loadNodes(nodeDatas);
for(int i = 0; i < nodeDatas.nodes.size(); i++ )
{
NodeData* nodeData= nodeDatas.nodes[i];
if(nodeData)
{
createNode(nodeData,this);
}
}
_skeleton = Skeleton3D::create(nodeDatas.skeleton);
CC_SAFE_RETAIN(_skeleton);
genGLProgramState();
return true;
}
bool Sprite3D::loadFromC3x(const std::string& path) bool Sprite3D::loadFromC3x(const std::string& path)
{ {
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(path); std::string fullPath = FileUtils::getInstance()->fullPathForFilename(path);
@ -192,7 +242,8 @@ bool Sprite3D::loadFromC3x(const std::string& path)
} }
genMaterials(key, texpaths); genMaterials(key, texpaths);
} }
NodeDatas nodeDatas;
bundle->loadNodes(nodeDatas);
genGLProgramState(); genGLProgramState();
return true; return true;
@ -284,7 +335,39 @@ GLProgram* Sprite3D::getDefaultGLProgram(bool textured)
return GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_3D_POSITION); return GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_3D_POSITION);
} }
} }
void Sprite3D::createNode(NodeData* nodedata,Node* root)
{
Node* node=nullptr;
ModelNodeData* modelNodeData=nodedata->asModelNodeData();
if(modelNodeData)
{
auto subMeshState = SubMeshState::create();
if(subMeshState)
{
_subMeshStates.pushBack(subMeshState);
}
}
else
{
node= Node::create();
if(node)
{
node->setAdditionalTransform(&nodedata->transform);
if(root)
{
root->addChild(node);
}
}
}
for(int i = 0; i < nodedata->children.size(); i++ )
{
NodeData* childData = nodedata->children[i];
if(childData)
{
createNode(childData,node);
}
}
}
void Sprite3D::genMaterials(const std::string& keyprefix, const std::vector<std::string>& texpaths) void Sprite3D::genMaterials(const std::string& keyprefix, const std::vector<std::string>& texpaths)
{ {
_subMeshStates.clear(); _subMeshStates.clear();
@ -316,7 +399,6 @@ void Sprite3D::setTexture(Texture2D* texture)
{ {
_subMeshStates.at(0)->setTexture(texture); _subMeshStates.at(0)->setTexture(texture);
} }
AttachNode* Sprite3D::getAttachNode(const std::string& boneName) AttachNode* Sprite3D::getAttachNode(const std::string& boneName)
{ {
auto it = _attachments.find(boneName); auto it = _attachments.find(boneName);

View File

@ -44,13 +44,14 @@ class MeshSkin;
class AttachNode; class AttachNode;
class SubMeshState; class SubMeshState;
class Skeleton3D; class Skeleton3D;
struct NodeData;
/** Sprite3D: A sprite can be loaded from 3D model files, .obj, .c3t, .c3b, then can be drawed as sprite */ /** Sprite3D: A sprite can be loaded from 3D model files, .obj, .c3t, .c3b, then can be drawed as sprite */
class CC_DLL Sprite3D : public Node, public BlendProtocol class CC_DLL Sprite3D : public Node, public BlendProtocol
{ {
public: public:
/** creates a Sprite3D*/ /** creates a Sprite3D*/
static Sprite3D* create(const std::string &modelPath); static Sprite3D* create(const std::string &modelPath);
static Sprite3D* create();
// creates a Sprite3D. It only supports one texture, and overrides the internal texture with 'texturePath' // creates a Sprite3D. It only supports one texture, and overrides the internal texture with 'texturePath'
static Sprite3D* create(const std::string &modelPath, const std::string &texturePath); static Sprite3D* create(const std::string &modelPath, const std::string &texturePath);
@ -114,6 +115,7 @@ CC_CONSTRUCTOR_ACCESS:
/**load from .c3b or .c3t*/ /**load from .c3b or .c3t*/
bool loadFromC3x(const std::string& path); bool loadFromC3x(const std::string& path);
bool loadFromC3x_0_3(const std::string& path);
/**draw*/ /**draw*/
virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override; virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override;
@ -126,7 +128,7 @@ CC_CONSTRUCTOR_ACCESS:
/**generate materials, and add them to cache, keyprefix is used as key prefix when added to cache*/ /**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 genMaterials(const std::string& keyprefix, const std::vector<std::string>& texpaths);
void createNode(NodeData* nodedata,Node* root);
protected: protected:
Mesh* _mesh;//mesh Mesh* _mesh;//mesh
MeshSkin* _skin;//skin MeshSkin* _skin;//skin