merge bundle

This commit is contained in:
lvlong 2014-08-16 11:12:26 +08:00
parent 3ab7c6b952
commit f8f416cd3c
2 changed files with 269 additions and 38 deletions

View File

@ -48,9 +48,6 @@
static const char* VERSION = "version"; static const char* VERSION = "version";
static const char* ID = "id"; static const char* ID = "id";
static const char* MESHDATA_MESH = "mesh";
static const char* MESHDATA_MESHES = "meshes";
static const char* MESHDATA_PARTS ="parts";
static const char* MESHDATA_DEFAULTPART = "body"; static const char* MESHDATA_DEFAULTPART = "body";
static const char* MESHDATA_VERTEXSIZE = "vertexsize"; static const char* MESHDATA_VERTEXSIZE = "vertexsize";
static const char* MESHDATA_VERTEX = "vertex"; static const char* MESHDATA_VERTEX = "vertex";
@ -64,11 +61,20 @@ static const char* MESHDATA_TYPE = "type";
static const char* MESHDATA_ATTRIBUTE = "attribute"; static const char* MESHDATA_ATTRIBUTE = "attribute";
static const char* SKINDATA_SKIN = "skin"; static const char* SKINDATA_SKIN = "skin";
static const char* SKINDATA_BONES = "bones";
static const char* SKINDATA_NODE = "node";
static const char* SKINDATA_BINDSHAPE = "bindshape"; static const char* SKINDATA_BINDSHAPE = "bindshape";
static const char* SKINDATA_CHILDREN = "children";
static const char* SKINDATA_TRANSFORM = "tansform"; static const char* MESH = "mesh";
static const char* MESHES = "meshes";
static const char* MESHPARTID = "meshpartid";
static const char* MATERIALID = "materialid";
static const char* NODE = "node";
static const char* NODES = "nodes";
static const char* CHILDREN = "children";
static const char* PARTS = "parts";
static const char* BONES = "bones";
static const char* MATERIALS = "materials";
static const char* ANIMATIONS = "animations";
static const char* TRANSFORM = "transform";
static const char* MATERIALDATA_MATERIAL = "material"; static const char* MATERIALDATA_MATERIAL = "material";
static const char* MATERIALDATA_MATERIALS = "materials"; static const char* MATERIALDATA_MATERIALS = "materials";
@ -78,7 +84,6 @@ static const char* MATERIALDATA_TEXTURES = "textures";
static const char* ANIMATIONDATA_ANIMATION = "animation"; static const char* ANIMATIONDATA_ANIMATION = "animation";
static const char* ANIMATIONDATA_LENGTH = "length"; static const char* ANIMATIONDATA_LENGTH = "length";
static const char* ANIMATIONDATA_BONES = "bones";
static const char* ANIMATIONDATA_BONEID = "boneId"; static const char* ANIMATIONDATA_BONEID = "boneId";
static const char* ANIMATIONDATA_KEYFRAMES = "keyframes"; static const char* ANIMATIONDATA_KEYFRAMES = "keyframes";
static const char* ANIMATIONDATA_TRANSLATION = "translation"; static const char* ANIMATIONDATA_TRANSLATION = "translation";
@ -95,7 +100,7 @@ void getChildMap(std::map<int, std::vector<int> >& map, SkinData* skinData, cons
// get transform matrix // get transform matrix
Mat4 transform; Mat4 transform;
const rapidjson::Value& parent_tranform = val[SKINDATA_TRANSFORM]; const rapidjson::Value& parent_tranform = val[TRANSFORM];
for (rapidjson::SizeType j = 0; j < parent_tranform.Size(); j++) for (rapidjson::SizeType j = 0; j < parent_tranform.Size(); j++)
{ {
transform.m[j] = parent_tranform[j].GetDouble(); transform.m[j] = parent_tranform[j].GetDouble();
@ -119,10 +124,10 @@ void getChildMap(std::map<int, std::vector<int> >& map, SkinData* skinData, cons
if(skinData->rootBoneIndex < 0) if(skinData->rootBoneIndex < 0)
skinData->rootBoneIndex = parent_name_index; skinData->rootBoneIndex = parent_name_index;
if (!val.HasMember(SKINDATA_CHILDREN)) if (!val.HasMember(CHILDREN))
return; return;
const rapidjson::Value& children = val[SKINDATA_CHILDREN]; const rapidjson::Value& children = val[CHILDREN];
for (rapidjson::SizeType i = 0; i < children.Size(); i++) for (rapidjson::SizeType i = 0; i < children.Size(); i++)
{ {
// get child bone name // get child bone name
@ -291,7 +296,7 @@ bool Bundle3D::loadMeshDatasBinary(MeshDatas& meshdatas)
} }
bool Bundle3D::loadMeshDatasJson(MeshDatas& meshdatas) bool Bundle3D::loadMeshDatasJson(MeshDatas& meshdatas)
{ {
const rapidjson::Value& mesh_data_array = _jsonReader[MESHDATA_MESHES]; const rapidjson::Value& mesh_data_array = _jsonReader[MESHES];
for (rapidjson::SizeType index = 0; index < mesh_data_array.Size(); index++) for (rapidjson::SizeType index = 0; index < mesh_data_array.Size(); index++)
{ {
MeshData* meshData = new MeshData(); MeshData* meshData = new MeshData();
@ -325,7 +330,7 @@ bool Bundle3D::loadMeshDatasJson(MeshDatas& meshdatas)
} }
// mesh part // mesh part
//////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////
const rapidjson::Value& mesh_part_array = mesh_data[MESHDATA_PARTS]; const rapidjson::Value& mesh_part_array = mesh_data[PARTS];
for (rapidjson::SizeType i = 0; i < mesh_part_array.Size(); i++) for (rapidjson::SizeType i = 0; i < mesh_part_array.Size(); i++)
{ {
std::vector<unsigned short> indexArray; std::vector<unsigned short> indexArray;
@ -345,6 +350,15 @@ bool Bundle3D::loadMeshDatasJson(MeshDatas& meshdatas)
} }
bool Bundle3D::loadNodes(NodeDatas& nodedatas) bool Bundle3D::loadNodes(NodeDatas& nodedatas)
{ {
if (_isBinary)
{
// TODO
return false;
}
else
{
return loadNodesJson(nodedatas);
}
return true; return true;
} }
@ -385,7 +399,7 @@ bool Bundle3D::loadMaterialsJson(MaterialDatas& materialdatas)
// NTextureData textureData; // NTextureData textureData;
// const rapidjson::Value& texture_val = testure_array[j]; // const rapidjson::Value& texture_val = testure_array[j];
// std::string filename = texture_val[MATERIALDATA_FILENAME].GetString(); // std::string filename = texture_val[MATERIALDATA_FILENAME].GetString();
// textureData.filename = _modelRelativePath + filename; // textureData.filename = _modelPath + filename;
// textureData.wrapS = parseGLType(texture_val["wrapModeU"].GetString()); // textureData.wrapS = parseGLType(texture_val["wrapModeU"].GetString());
// textureData.wrapT = parseGLType(texture_val["wrapModeV"].GetString()); // textureData.wrapT = parseGLType(texture_val["wrapModeV"].GetString());
// materialData.textures.push_back(textureData); // materialData.textures.push_back(textureData);
@ -438,7 +452,7 @@ bool Bundle3D::loadMeshDataJson(MeshData* meshdata)
bool Bundle3D::loadMeshDataJson_0_1(MeshData* meshdata) bool Bundle3D::loadMeshDataJson_0_1(MeshData* meshdata)
{ {
const rapidjson::Value& mesh_data_array = _jsonReader[MESHDATA_MESH]; const rapidjson::Value& mesh_data_array = _jsonReader[MESH];
const rapidjson::Value& mesh_data_val = mesh_data_array[(rapidjson::SizeType)0]; const rapidjson::Value& mesh_data_val = mesh_data_array[(rapidjson::SizeType)0];
@ -486,7 +500,7 @@ bool Bundle3D::loadMeshDataJson_0_1(MeshData* meshdata)
bool Bundle3D::loadMeshDataJson_0_2(MeshData* meshdata) bool Bundle3D::loadMeshDataJson_0_2(MeshData* meshdata)
{ {
const rapidjson::Value& mesh_array = _jsonReader[MESHDATA_MESH]; const rapidjson::Value& mesh_array = _jsonReader[MESH];
const rapidjson::Value& mesh_array_0 = mesh_array[(rapidjson::SizeType)0]; const rapidjson::Value& mesh_array_0 = mesh_array[(rapidjson::SizeType)0];
@ -547,14 +561,14 @@ bool Bundle3D::loadSkinDataJson(SkinData* skindata)
assert(skin_data_array.IsArray()); assert(skin_data_array.IsArray());
const rapidjson::Value& skin_data_array_val_0 = skin_data_array[(rapidjson::SizeType)0]; const rapidjson::Value& skin_data_array_val_0 = skin_data_array[(rapidjson::SizeType)0];
if (!skin_data_array_val_0.HasMember(SKINDATA_BONES)) if (!skin_data_array_val_0.HasMember(BONES))
return false; return false;
const rapidjson::Value& skin_data_bones = skin_data_array_val_0[SKINDATA_BONES]; const rapidjson::Value& skin_data_bones = skin_data_array_val_0[BONES];
for (rapidjson::SizeType i = 0; i < skin_data_bones.Size(); i++) for (rapidjson::SizeType i = 0; i < skin_data_bones.Size(); i++)
{ {
const rapidjson::Value& skin_data_bone = skin_data_bones[i]; const rapidjson::Value& skin_data_bone = skin_data_bones[i];
std::string name = skin_data_bone[SKINDATA_NODE].GetString(); std::string name = skin_data_bone[NODE].GetString();
skindata->addSkinBoneNames(name); skindata->addSkinBoneNames(name);
Mat4 mat_bind_pos; Mat4 mat_bind_pos;
@ -607,7 +621,7 @@ bool Bundle3D::loadMaterialDataJson_0_1(MaterialData* materialdata)
const rapidjson::Value& material_data_base_array_0 = material_data_base_array[(rapidjson::SizeType)0]; const rapidjson::Value& material_data_base_array_0 = material_data_base_array[(rapidjson::SizeType)0];
// set texture // set texture
materialdata->texturePaths[0] =_modelRelativePath + material_data_base_array_0[MATERIALDATA_FILENAME].GetString(); materialdata->texturePaths[0] = _modelPath + material_data_base_array_0[MATERIALDATA_FILENAME].GetString();
return true; return true;
} }
@ -625,7 +639,7 @@ bool Bundle3D::loadMaterialDataJson_0_2(MaterialData* materialdata)
//std::string id = material_val[ID].GetString(); //std::string id = material_val[ID].GetString();
// set texture // set texture
materialdata->texturePaths[i] = _modelRelativePath + material_val[MATERIALDATA_TEXTURES].GetString(); materialdata->texturePaths[i] = _modelPath + material_val[MATERIALDATA_TEXTURES].GetString();
} }
return true; return true;
@ -642,7 +656,7 @@ bool Bundle3D::loadAnimationDataJson(Animation3DData* animationdata)
animationdata->_totalTime = animation_data_array_val_0[ANIMATIONDATA_LENGTH].GetDouble(); animationdata->_totalTime = animation_data_array_val_0[ANIMATIONDATA_LENGTH].GetDouble();
const rapidjson::Value& bones = animation_data_array_val_0[ANIMATIONDATA_BONES]; const rapidjson::Value& bones = animation_data_array_val_0[BONES];
for (rapidjson::SizeType i = 0; i < bones.Size(); i++) for (rapidjson::SizeType i = 0; i < bones.Size(); i++)
{ {
const rapidjson::Value& bone = bones[i]; const rapidjson::Value& bone = bones[i];
@ -1034,7 +1048,7 @@ bool Bundle3D::loadMaterialDataBinary(MaterialData* materialdata)
return false; return false;
} }
std::string path = _modelRelativePath + texturePath; std::string path = _modelPath + texturePath;
materialdata->texturePaths[i] = path; materialdata->texturePaths[i] = path;
} }
@ -1108,6 +1122,213 @@ bool Bundle3D::loadAnimationDataBinary(Animation3DData* animationdata)
return true; return true;
} }
bool Bundle3D::loadNodesJson(NodeDatas& nodedatas)
{
if (!_jsonReader.HasMember(NODES)) return false;
const rapidjson::Value& nodes = _jsonReader[NODES];
if(!nodes.IsArray()) return false;
loadBoneNamesJson(nodes);
for (rapidjson::SizeType i = 0; i < nodes.Size(); i++)
{
const rapidjson::Value& jnode = nodes[i];
std::string id = jnode[ID].GetString();
NodeData* nodedata = parseNodesRecursivelyJson(jnode);
bool isSkeleton;
isSkeleton = checkIsSkeletonJson(jnode);
if (isSkeleton)
nodedatas.skeleton.push_back(nodedata);
else
nodedatas.nodes.push_back(nodedata);
}
return true;
}
void Bundle3D::loadBoneNamesJson(const rapidjson::Value& jnodes)
{
for (rapidjson::SizeType i = 0; i < jnodes.Size(); i++)
{
parseBoneNameRecursivelyJson(jnodes[i]);
}
}
void Bundle3D::parseBoneNameRecursivelyJson(const rapidjson::Value& jnode)
{
std::string id = jnode[ID].GetString();
if (jnode.HasMember(PARTS))
{
const rapidjson::Value& parts = jnode[PARTS];
for (rapidjson::SizeType i = 0; i < parts.Size(); i++)
{
const rapidjson::Value& part = parts[i];
if (part.HasMember(BONES))
{
const rapidjson::Value& bones = part[BONES];
for (rapidjson::SizeType j = 0; j < bones.Size(); j++)
{
const rapidjson::Value& bone = bones[j];
std::string bonename = bone[NODE].GetString();
if (!checkIsBone(bonename))
{
_bonenames.push_back(bonename);
}
}
}
}
}
if (jnode.HasMember(CHILDREN))
{
const rapidjson::Value& children = jnode[CHILDREN];
for (rapidjson::SizeType i = 0; i < children.Size(); i++)
{
parseBoneNameRecursivelyJson(children[i]);
}
}
}
bool Bundle3D::checkIsBone(const std::string& name)
{
std::list<std::string>::iterator iter = std::find(_bonenames.begin(), _bonenames.end(), name);
bool ret = (iter != _bonenames.end()) ? true : false;
return ret;
}
bool Bundle3D::checkIsSkeletonJson(const rapidjson::Value& jnode)
{
bool ret = false;
if (jnode.HasMember(CHILDREN))
{
const rapidjson::Value& children = jnode[CHILDREN];
for (rapidjson::SizeType i = 0; i < children.Size(); i++)
{
ret = checkIsSkeletonRecursivelyJson(children[i]);
if (ret) break;
}
}
else
{
ret = checkIsSkeletonRecursivelyJson(jnode);
}
return ret;
}
bool Bundle3D::checkIsSkeletonRecursivelyJson(const rapidjson::Value& jnode)
{
std::string id = jnode[ID].GetString();
if (checkIsBone(id))
return true;
if (jnode.HasMember(CHILDREN))
{
const rapidjson::Value& children = jnode[CHILDREN];
for (rapidjson::SizeType i = 0; i < children.Size(); i++)
{
checkIsSkeletonRecursivelyJson(children[i]);
}
}
return false;
}
NodeData* Bundle3D::parseNodesRecursivelyJson(const rapidjson::Value& jvalue)
{
NodeData* nodedata;
if (jvalue.HasMember(PARTS))
nodedata = new ModelNodeData();
else
nodedata = new NodeData();
// id
nodedata->id = jvalue[ID].GetString();
// transform
Mat4 tranform;
const rapidjson::Value& jtransform = jvalue[TRANSFORM];
for (rapidjson::SizeType j = 0; j < jtransform.Size(); j++)
{
tranform.m[j] = jtransform[j].GetDouble();
}
nodedata->transform = tranform;
// parts
if (jvalue.HasMember(PARTS))
{
const rapidjson::Value& parts = jvalue[PARTS];
ModelNodeData* modelnodedata = dynamic_cast<ModelNodeData*>(nodedata);
for (rapidjson::SizeType i = 0; i < parts.Size(); i++)
{
const rapidjson::Value& part = parts[i];
std::string meshPartId = part[MESHPARTID].GetString();
std::string materialId = part[MATERIALID].GetString();
if (meshPartId == "" || materialId == "")
{
std::string err = "Node " + nodedata->id + " part is missing meshPartId or materialId";
CCASSERT(false, err.c_str());
return nullptr;
}
if (part.HasMember(BONES))
{
const rapidjson::Value& bones = part[BONES];
for (rapidjson::SizeType j = 0; j < bones.Size(); j++)
{
const rapidjson::Value& bone = bones[i];
// node
if (!bone.HasMember(NODE))
{
CCASSERT(false, "Bone node ID missing");
return nullptr;
}
modelnodedata->bones.push_back(bone[NODE].GetString());
Mat4 invbindpos;
const rapidjson::Value& jtransform = jvalue[TRANSFORM];
for (rapidjson::SizeType j = 0; j < jtransform.Size(); j++)
{
invbindpos.m[j] = jtransform[j].GetDouble();
}
modelnodedata->invBindPose.push_back(invbindpos);
}
}
}
}
if (jvalue.HasMember(CHILDREN))
{
const rapidjson::Value& children = jvalue[CHILDREN];
for (rapidjson::SizeType i = 0; i < children.Size(); i++)
{
const rapidjson::Value& child = children[i];
NodeData* tempdata = parseNodesRecursivelyJson(child);
nodedata->children.push_back(tempdata);
}
}
return nodedata;
}
GLenum Bundle3D::parseGLType(const std::string& str) GLenum Bundle3D::parseGLType(const std::string& str)
{ {
if (str == "GL_BYTE") if (str == "GL_BYTE")
@ -1182,17 +1403,7 @@ void Bundle3D::getModelRelativePath(const std::string& path)
{ {
ssize_t index = path.find_last_of('/'); ssize_t index = path.find_last_of('/');
std::string fullModelPath; std::string fullModelPath;
fullModelPath = path.substr(0, index + 1); _modelPath = path.substr(0, index + 1);
auto list = FileUtils::getInstance()->getSearchPaths();
for( const auto &item : list )
{
if ( fullModelPath.find(item) != std::string::npos )
{
_modelRelativePath = fullModelPath.substr(item.length(), fullModelPath.length() + 1);
break;
}
}
} }
Reference* Bundle3D::seekToFirstType(unsigned int type) Reference* Bundle3D::seekToFirstType(unsigned int type)
@ -1217,7 +1428,7 @@ Reference* Bundle3D::seekToFirstType(unsigned int type)
Bundle3D::Bundle3D() Bundle3D::Bundle3D()
:_isBinary(false), :_isBinary(false),
_modelRelativePath(""), _modelPath(""),
_path(""), _path(""),
_version(""), _version(""),
_jsonBuffer(nullptr), _jsonBuffer(nullptr),

View File

@ -26,6 +26,7 @@
#define __CCBUNDLE3D_H__ #define __CCBUNDLE3D_H__
#include <map> #include <map>
#include <list>
#include "3d/CCBundle3DData.h" #include "3d/CCBundle3DData.h"
@ -153,6 +154,23 @@ protected:
*/ */
bool loadAnimationDataBinary(Animation3DData* animationdata); bool loadAnimationDataBinary(Animation3DData* animationdata);
bool checkIsBone(const std::string& name);
/**
* load nodes of json
*/
bool loadNodesJson(NodeDatas& nodedatas);
void loadBoneNamesJson(const rapidjson::Value& jnodes);
void parseBoneNameRecursivelyJson(const rapidjson::Value& jnode);
bool checkIsSkeletonJson(const rapidjson::Value& jnode);
bool checkIsSkeletonRecursivelyJson(const rapidjson::Value& jnode);
NodeData* parseNodesRecursivelyJson(const rapidjson::Value& jvalue);
/**
* load nodes of Binary
*/
/** /**
* get define data type * get define data type
* @param str The type in string * @param str The type in string
@ -186,7 +204,7 @@ protected:
static Bundle3D* _instance; static Bundle3D* _instance;
std::string _modelRelativePath; std::string _modelPath;
std::string _path; std::string _path;
std::string _version;// the c3b or c3t version std::string _version;// the c3b or c3t version
@ -202,6 +220,8 @@ protected:
Reference* _references; Reference* _references;
bool _isBinary; bool _isBinary;
std::list<std::string> _bonenames;
}; };
NS_CC_END NS_CC_END