2014-06-05 16:36:01 +08:00
|
|
|
/****************************************************************************
|
2014-08-18 17:31:04 +08:00
|
|
|
Copyright (c) 2014 Chukong Technologies Inc.
|
2014-06-05 16:36:01 +08:00
|
|
|
|
2014-08-18 17:31:04 +08:00
|
|
|
http://www.cocos2d-x.org
|
2014-06-05 16:36:01 +08:00
|
|
|
|
2014-08-18 17:31:04 +08:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
2014-06-05 16:36:01 +08:00
|
|
|
|
2014-08-18 17:31:04 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2014-06-05 16:36:01 +08:00
|
|
|
|
2014-08-18 17:31:04 +08:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2014-06-05 16:36:01 +08:00
|
|
|
|
2014-06-17 10:49:52 +08:00
|
|
|
#include "3d/CCBundle3D.h"
|
2014-08-17 22:49:53 +08:00
|
|
|
#include "3d/CCObjLoader.h"
|
2014-06-05 16:36:01 +08:00
|
|
|
|
|
|
|
#include "base/ccMacros.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
2014-06-17 15:31:45 +08:00
|
|
|
#include "renderer/CCGLProgram.h"
|
2014-06-19 15:43:02 +08:00
|
|
|
#include "CCBundleReader.h"
|
|
|
|
#include "base/CCData.h"
|
2014-06-25 09:13:24 +08:00
|
|
|
#include "json/document.h"
|
|
|
|
|
2014-06-19 15:43:02 +08:00
|
|
|
#define BUNDLE_TYPE_SCENE 1
|
|
|
|
#define BUNDLE_TYPE_NODE 2
|
|
|
|
#define BUNDLE_TYPE_ANIMATIONS 3
|
|
|
|
#define BUNDLE_TYPE_ANIMATION 4
|
|
|
|
#define BUNDLE_TYPE_ANIMATION_CHANNEL 5
|
|
|
|
#define BUNDLE_TYPE_MODEL 10
|
|
|
|
#define BUNDLE_TYPE_MATERIAL 16
|
|
|
|
#define BUNDLE_TYPE_EFFECT 18
|
|
|
|
#define BUNDLE_TYPE_CAMERA 32
|
|
|
|
#define BUNDLE_TYPE_LIGHT 33
|
|
|
|
#define BUNDLE_TYPE_MESH 34
|
|
|
|
#define BUNDLE_TYPE_MESHPART 35
|
|
|
|
#define BUNDLE_TYPE_MESHSKIN 36
|
2014-06-12 16:28:36 +08:00
|
|
|
|
2014-07-29 10:49:06 +08:00
|
|
|
static const char* VERSION = "version";
|
2014-06-20 22:38:09 +08:00
|
|
|
static const char* ID = "id";
|
2014-08-20 10:24:46 +08:00
|
|
|
static const char* DEFAULTPART = "body";
|
|
|
|
static const char* VERTEXSIZE = "vertexsize";
|
|
|
|
static const char* VERTEX = "vertex";
|
|
|
|
static const char* VERTICES = "vertices";
|
|
|
|
static const char* INDEXNUM = "indexnum";
|
|
|
|
static const char* INDICES = "indices";
|
|
|
|
static const char* SUBMESH = "submesh";
|
|
|
|
static const char* ATTRIBUTES = "attributes";
|
2014-08-21 11:23:31 +08:00
|
|
|
static const char* ATTRIBUTESIZE = "size";
|
2014-08-20 10:24:46 +08:00
|
|
|
static const char* TYPE = "type";
|
|
|
|
static const char* ATTRIBUTE = "attribute";
|
|
|
|
static const char* SKIN = "skin";
|
|
|
|
static const char* BINDSHAPE = "bindshape";
|
2014-08-16 11:12:26 +08:00
|
|
|
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";
|
2014-08-21 10:38:33 +08:00
|
|
|
static const char* SKELETON = "skeleton";
|
2014-08-16 11:12:26 +08:00
|
|
|
static const char* MATERIALS = "materials";
|
|
|
|
static const char* ANIMATIONS = "animations";
|
|
|
|
static const char* TRANSFORM = "transform";
|
2014-08-16 16:56:04 +08:00
|
|
|
static const char* OLDTRANSFORM = "tansform";
|
2014-08-18 20:22:18 +08:00
|
|
|
static const char* ANIMATION = "animation";
|
2014-08-20 10:24:46 +08:00
|
|
|
static const char* MATERIAL = "material";
|
|
|
|
static const char* BASE = "base";
|
|
|
|
static const char* FILENAME = "filename";
|
|
|
|
static const char* TEXTURES = "textures";
|
|
|
|
static const char* LENGTH = "length";
|
|
|
|
static const char* BONEID = "boneId";
|
|
|
|
static const char* KEYFRAMES = "keyframes";
|
|
|
|
static const char* TRANSLATION = "translation";
|
|
|
|
static const char* ROTATION = "rotation";
|
|
|
|
static const char* SCALE = "scale";
|
|
|
|
static const char* KEYTIME = "keytime";
|
2014-06-20 22:38:09 +08:00
|
|
|
|
2014-06-05 16:36:01 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2014-08-20 10:24:46 +08:00
|
|
|
void getChildMap(std::map<int, std::vector<int> >& map, SkinData* skinData, const rapidjson::Value& val)
|
2014-06-12 10:01:54 +08:00
|
|
|
{
|
2014-06-12 15:53:41 +08:00
|
|
|
if (!skinData)
|
|
|
|
return;
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-06-24 14:10:03 +08:00
|
|
|
// get transform matrix
|
|
|
|
Mat4 transform;
|
2014-08-16 16:56:04 +08:00
|
|
|
const rapidjson::Value& parent_tranform = val[OLDTRANSFORM];
|
2014-06-24 14:10:03 +08:00
|
|
|
for (rapidjson::SizeType j = 0; j < parent_tranform.Size(); j++)
|
|
|
|
{
|
|
|
|
transform.m[j] = parent_tranform[j].GetDouble();
|
|
|
|
}
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-06-24 14:10:03 +08:00
|
|
|
// set origin matrices
|
2014-06-20 22:38:09 +08:00
|
|
|
std::string parent_name = val[ID].GetString();
|
2014-08-20 10:24:46 +08:00
|
|
|
int parent_name_index = skinData->getSkinBoneNameIndex(parent_name);
|
2014-06-24 14:10:03 +08:00
|
|
|
if (parent_name_index < 0)
|
|
|
|
{
|
|
|
|
skinData->addNodeBoneNames(parent_name);
|
|
|
|
skinData->nodeBoneOriginMatrices.push_back(transform);
|
2014-06-27 09:32:09 +08:00
|
|
|
parent_name_index = skinData->getBoneNameIndex(parent_name);
|
2014-06-24 14:10:03 +08:00
|
|
|
}
|
2014-06-24 23:33:30 +08:00
|
|
|
else if (parent_name_index < skinData->skinBoneNames.size())
|
2014-06-24 14:10:03 +08:00
|
|
|
{
|
2014-06-24 23:33:30 +08:00
|
|
|
skinData->skinBoneOriginMatrices[parent_name_index] = (transform);
|
2014-06-24 14:10:03 +08:00
|
|
|
}
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-06-24 14:10:03 +08:00
|
|
|
// set root bone index
|
|
|
|
if(skinData->rootBoneIndex < 0)
|
|
|
|
skinData->rootBoneIndex = parent_name_index;
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-08-16 11:12:26 +08:00
|
|
|
if (!val.HasMember(CHILDREN))
|
2014-06-24 23:33:30 +08:00
|
|
|
return;
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-08-16 11:12:26 +08:00
|
|
|
const rapidjson::Value& children = val[CHILDREN];
|
2014-06-13 19:26:46 +08:00
|
|
|
for (rapidjson::SizeType i = 0; i < children.Size(); i++)
|
|
|
|
{
|
2014-06-24 14:10:03 +08:00
|
|
|
// get child bone name
|
2014-06-13 19:26:46 +08:00
|
|
|
const rapidjson::Value& child = children[i];
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-06-20 22:38:09 +08:00
|
|
|
std::string child_name = child[ID].GetString();
|
2014-06-24 14:10:03 +08:00
|
|
|
int child_name_index = skinData->getSkinBoneNameIndex(child_name);
|
|
|
|
if (child_name_index < 0)
|
2014-06-13 19:26:46 +08:00
|
|
|
{
|
2014-06-24 14:10:03 +08:00
|
|
|
skinData->addNodeBoneNames(child_name);
|
|
|
|
child_name_index = skinData->getBoneNameIndex(child_name);
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-06-12 10:01:54 +08:00
|
|
|
}
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-06-24 14:10:03 +08:00
|
|
|
map[parent_name_index].push_back(child_name_index);
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-06-24 14:10:03 +08:00
|
|
|
getChildMap(map, skinData, child);
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-06-12 10:01:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-05 16:36:01 +08:00
|
|
|
Bundle3D* Bundle3D::_instance = nullptr;
|
2014-06-06 19:12:08 +08:00
|
|
|
|
2014-07-30 18:32:34 +08:00
|
|
|
void Bundle3D::setBundleInstance(Bundle3D* bundleInstance)
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_instance);
|
|
|
|
_instance = bundleInstance;
|
|
|
|
}
|
|
|
|
|
2014-06-05 16:36:01 +08:00
|
|
|
Bundle3D* Bundle3D::getInstance()
|
|
|
|
{
|
|
|
|
if (_instance == nullptr)
|
2014-08-28 07:31:57 +08:00
|
|
|
_instance = new (std::nothrow) Bundle3D();
|
2014-06-05 16:36:01 +08:00
|
|
|
return _instance;
|
|
|
|
}
|
|
|
|
|
2014-06-17 19:18:56 +08:00
|
|
|
void Bundle3D::destroyInstance()
|
2014-06-05 16:36:01 +08:00
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_instance);
|
|
|
|
}
|
|
|
|
|
2014-06-25 09:13:24 +08:00
|
|
|
void Bundle3D::clear()
|
2014-06-05 16:36:01 +08:00
|
|
|
{
|
2014-06-25 09:13:24 +08:00
|
|
|
if (_isBinary)
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_binaryBuffer);
|
|
|
|
CC_SAFE_DELETE_ARRAY(_references);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE_ARRAY(_jsonBuffer);
|
|
|
|
}
|
2014-06-20 15:30:08 +08:00
|
|
|
}
|
2014-06-19 15:43:02 +08:00
|
|
|
|
2014-06-20 15:30:08 +08:00
|
|
|
bool Bundle3D::load(const std::string& path)
|
|
|
|
{
|
2014-06-17 13:59:03 +08:00
|
|
|
if (_path == path)
|
|
|
|
return true;
|
2014-06-12 10:01:54 +08:00
|
|
|
|
2014-06-20 15:30:08 +08:00
|
|
|
getModelRelativePath(path);
|
|
|
|
|
|
|
|
bool ret = false;
|
|
|
|
std::string ext = path.substr(path.length() - 4, 4);
|
|
|
|
std::transform(ext.begin(), ext.end(), ext.begin(), tolower);
|
|
|
|
if (ext == ".c3t")
|
|
|
|
{
|
|
|
|
_isBinary = false;
|
|
|
|
ret = loadJson(path);
|
|
|
|
}
|
|
|
|
else if (ext == ".c3b")
|
|
|
|
{
|
|
|
|
_isBinary = true;
|
|
|
|
ret = loadBinary(path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-29 11:56:33 +08:00
|
|
|
CCLOG("warning: %s is invalid file formate", path.c_str());
|
2014-06-20 15:30:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ret?(_path = path):(_path = "");
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-06-20 15:30:08 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2014-08-17 22:49:53 +08:00
|
|
|
|
|
|
|
bool Bundle3D::loadObj(MeshDatas& meshdatas, MaterialDatas& materialdatas, NodeDatas& nodedatas, const std::string& fullPath, const char* mtl_basepath)
|
|
|
|
{
|
|
|
|
meshdatas.resetData();
|
|
|
|
materialdatas.resetData();
|
|
|
|
nodedatas.resetData();
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-08-17 22:49:53 +08:00
|
|
|
ObjLoader::shapes_t shapes;
|
|
|
|
auto ret = ObjLoader::LoadObj(shapes, fullPath.c_str(), mtl_basepath);
|
|
|
|
if (ret.empty())
|
|
|
|
{
|
|
|
|
//fill data
|
2014-08-28 07:31:57 +08:00
|
|
|
MeshData* meshdata = new (std::nothrow) MeshData();
|
2014-08-17 22:49:53 +08:00
|
|
|
MeshVertexAttrib attrib;
|
|
|
|
attrib.size = 3;
|
|
|
|
attrib.type = GL_FLOAT;
|
|
|
|
if (shapes.positions.size())
|
|
|
|
{
|
|
|
|
attrib.vertexAttrib = GLProgram::VERTEX_ATTRIB_POSITION;
|
|
|
|
attrib.attribSizeBytes = attrib.size * sizeof(float);
|
|
|
|
meshdata->attribs.push_back(attrib);
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-08-17 22:49:53 +08:00
|
|
|
}
|
|
|
|
bool hasnormal = false, hastex = false;
|
|
|
|
if (shapes.normals.size())
|
|
|
|
{
|
|
|
|
hasnormal = true;
|
|
|
|
attrib.vertexAttrib = GLProgram::VERTEX_ATTRIB_NORMAL;
|
|
|
|
attrib.attribSizeBytes = attrib.size * sizeof(float);;
|
|
|
|
meshdata->attribs.push_back(attrib);
|
|
|
|
}
|
|
|
|
if (shapes.texcoords.size())
|
|
|
|
{
|
|
|
|
hastex = true;
|
|
|
|
attrib.size = 2;
|
|
|
|
attrib.vertexAttrib = GLProgram::VERTEX_ATTRIB_TEX_COORD;
|
|
|
|
attrib.attribSizeBytes = attrib.size * sizeof(float);
|
|
|
|
meshdata->attribs.push_back(attrib);
|
|
|
|
}
|
|
|
|
auto vertexNum = shapes.positions.size() / 3;
|
|
|
|
for(auto i = 0; i < vertexNum; i++)
|
|
|
|
{
|
|
|
|
meshdata->vertex.push_back(shapes.positions[i * 3]);
|
|
|
|
meshdata->vertex.push_back(shapes.positions[i * 3 + 1]);
|
|
|
|
meshdata->vertex.push_back(shapes.positions[i * 3 + 2]);
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-08-17 22:49:53 +08:00
|
|
|
if (hasnormal)
|
|
|
|
{
|
|
|
|
meshdata->vertex.push_back(shapes.normals[i * 3]);
|
|
|
|
meshdata->vertex.push_back(shapes.normals[i * 3 + 1]);
|
|
|
|
meshdata->vertex.push_back(shapes.normals[i * 3 + 2]);
|
|
|
|
}
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-08-17 22:49:53 +08:00
|
|
|
if (hastex)
|
|
|
|
{
|
|
|
|
meshdata->vertex.push_back(shapes.texcoords[i * 2]);
|
|
|
|
meshdata->vertex.push_back(shapes.texcoords[i * 2 + 1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
meshdatas.meshDatas.push_back(meshdata);
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-08-17 22:49:53 +08:00
|
|
|
NMaterialData materialdata;
|
|
|
|
int i = 0;
|
|
|
|
char str[20];
|
|
|
|
std::string dir = "";
|
|
|
|
auto last = fullPath.rfind("/");
|
|
|
|
if (last != -1)
|
|
|
|
dir = fullPath.substr(0, last + 1);
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-08-17 22:49:53 +08:00
|
|
|
for (const auto& it : shapes.shapes)
|
|
|
|
{
|
|
|
|
NTextureData tex;
|
2014-12-23 15:52:45 +08:00
|
|
|
tex.filename = it.material.diffuse_texname.empty() ? it.material.diffuse_texname : dir + it.material.diffuse_texname;
|
2014-08-17 22:49:53 +08:00
|
|
|
tex.type = NTextureData::Usage::Diffuse;
|
2014-08-18 11:42:41 +08:00
|
|
|
tex.wrapS = GL_CLAMP_TO_EDGE;
|
|
|
|
tex.wrapT = GL_CLAMP_TO_EDGE;
|
2014-08-17 22:49:53 +08:00
|
|
|
|
|
|
|
sprintf(str, "%d", i++);
|
|
|
|
materialdata.textures.push_back(tex);
|
|
|
|
materialdata.id = str;
|
|
|
|
materialdatas.materials.push_back(materialdata);
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-08-17 22:49:53 +08:00
|
|
|
meshdata->subMeshIndices.push_back(it.mesh.indices);
|
2014-11-19 17:58:40 +08:00
|
|
|
meshdata->subMeshAABB.push_back(calculateAABB(meshdata->vertex, meshdata->getPerVertexSize(), it.mesh.indices));
|
2014-08-17 22:49:53 +08:00
|
|
|
meshdata->subMeshIds.push_back(str);
|
2014-08-28 07:31:57 +08:00
|
|
|
auto node = new (std::nothrow) NodeData();
|
|
|
|
auto modelnode = new (std::nothrow) ModelData();
|
2014-08-17 22:49:53 +08:00
|
|
|
modelnode->matrialId = str;
|
|
|
|
modelnode->subMeshId = str;
|
2014-08-20 19:19:49 +08:00
|
|
|
node->id = it.name;
|
2014-08-20 11:40:04 +08:00
|
|
|
node->modelNodeDatas.push_back(modelnode);
|
|
|
|
nodedatas.nodes.push_back(node);
|
2014-08-17 22:49:53 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2014-08-29 11:56:33 +08:00
|
|
|
CCLOG("warning: load %s file error: %s", fullPath.c_str(), ret.c_str());
|
2014-08-17 22:49:53 +08:00
|
|
|
return false;
|
|
|
|
}
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-06-20 15:30:08 +08:00
|
|
|
bool Bundle3D::loadSkinData(const std::string& id, SkinData* skindata)
|
|
|
|
{
|
2014-07-29 10:49:06 +08:00
|
|
|
skindata->resetData();
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-06-20 15:30:08 +08:00
|
|
|
if (_isBinary)
|
|
|
|
{
|
|
|
|
return loadSkinDataBinary(skindata);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return loadSkinDataJson(skindata);
|
|
|
|
}
|
|
|
|
}
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-06-20 15:30:08 +08:00
|
|
|
bool Bundle3D::loadAnimationData(const std::string& id, Animation3DData* animationdata)
|
|
|
|
{
|
2014-07-29 10:49:06 +08:00
|
|
|
animationdata->resetData();
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-06-20 15:30:08 +08:00
|
|
|
if (_isBinary)
|
|
|
|
{
|
2014-10-20 19:22:00 +08:00
|
|
|
return loadAnimationDataBinary(id,animationdata);
|
2014-06-20 15:30:08 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-10-20 19:22:00 +08:00
|
|
|
return loadAnimationDataJson(id,animationdata);
|
2014-06-20 15:30:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-15 13:03:53 +08:00
|
|
|
//since 3.3, to support reskin
|
|
|
|
bool Bundle3D::loadMeshDatas(MeshDatas& meshdatas)
|
|
|
|
{
|
2014-08-15 18:27:28 +08:00
|
|
|
meshdatas.resetData();
|
2014-08-15 15:32:15 +08:00
|
|
|
if (_isBinary)
|
|
|
|
{
|
2014-10-21 18:44:10 +08:00
|
|
|
if (_version == "0.1" || _version == "0.2")
|
2014-08-18 17:31:04 +08:00
|
|
|
{
|
|
|
|
return loadMeshDatasBinary_0_1(meshdatas);
|
|
|
|
}
|
2014-08-29 11:59:30 +08:00
|
|
|
else
|
2014-08-18 17:31:04 +08:00
|
|
|
{
|
|
|
|
return loadMeshDatasBinary(meshdatas);
|
|
|
|
}
|
2014-08-15 15:32:15 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-10-21 18:44:10 +08:00
|
|
|
if (_version == "1.2" || _version == "0.2")
|
2014-08-16 14:14:37 +08:00
|
|
|
{
|
|
|
|
return loadMeshDataJson_0_1(meshdatas);
|
|
|
|
}
|
2014-08-29 11:59:30 +08:00
|
|
|
else
|
2014-08-16 14:14:37 +08:00
|
|
|
{
|
|
|
|
return loadMeshDatasJson(meshdatas);
|
|
|
|
}
|
2014-08-15 15:32:15 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool Bundle3D::loadMeshDatasBinary(MeshDatas& meshdatas)
|
|
|
|
{
|
2014-08-18 17:31:04 +08:00
|
|
|
if (!seekToFirstType(BUNDLE_TYPE_MESH))
|
|
|
|
return false;
|
|
|
|
unsigned int meshSize = 0;
|
|
|
|
if (_binaryReader.read(&meshSize, 4, 1) != 1)
|
|
|
|
{
|
2014-08-29 11:56:33 +08:00
|
|
|
CCLOG("warning: Failed to read meshdata: attribCount '%s'.", _path.c_str());
|
2014-08-18 17:31:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for(int i = 0; i < meshSize ; i++ )
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
MeshData* meshData = new (std::nothrow) MeshData();
|
2014-08-19 14:03:41 +08:00
|
|
|
unsigned int attribSize=0;
|
2014-08-18 17:31:04 +08:00
|
|
|
// read mesh data
|
2014-08-19 14:03:41 +08:00
|
|
|
if (_binaryReader.read(&attribSize, 4, 1) != 1 || attribSize < 1)
|
2014-08-18 17:31:04 +08:00
|
|
|
{
|
2014-08-29 11:56:33 +08:00
|
|
|
CCLOG("warning: Failed to read meshdata: attribCount '%s'.", _path.c_str());
|
2014-08-18 17:31:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
2014-08-19 14:03:41 +08:00
|
|
|
meshData->attribCount = attribSize;
|
2014-08-18 17:31:04 +08:00
|
|
|
meshData->attribs.resize(meshData->attribCount);
|
2014-08-19 14:03:41 +08:00
|
|
|
for (ssize_t j = 0; j < meshData->attribCount; j++)
|
2014-08-18 17:31:04 +08:00
|
|
|
{
|
2014-08-19 14:03:41 +08:00
|
|
|
std::string attribute="";
|
|
|
|
unsigned int vSize;
|
|
|
|
if (_binaryReader.read(&vSize, 4, 1) != 1)
|
2014-08-18 17:31:04 +08:00
|
|
|
{
|
2014-08-29 11:56:33 +08:00
|
|
|
CCLOG("warning: Failed to read meshdata: usage or size '%s'.", _path.c_str());
|
2014-08-18 17:31:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
2014-08-19 14:03:41 +08:00
|
|
|
std::string type = _binaryReader.readString();
|
|
|
|
attribute=_binaryReader.readString();
|
|
|
|
meshData->attribs[j].size = vSize;
|
|
|
|
meshData->attribs[j].attribSizeBytes = meshData->attribs[j].size * 4;
|
|
|
|
meshData->attribs[j].type = parseGLType(type);
|
|
|
|
meshData->attribs[j].vertexAttrib = parseGLProgramAttribute(attribute);
|
2014-08-18 17:31:04 +08:00
|
|
|
}
|
|
|
|
unsigned int vertexSizeInFloat = 0;
|
|
|
|
// Read vertex data
|
|
|
|
if (_binaryReader.read(&vertexSizeInFloat, 4, 1) != 1 || vertexSizeInFloat == 0)
|
|
|
|
{
|
2014-08-29 11:56:33 +08:00
|
|
|
CCLOG("warning: Failed to read meshdata: vertexSizeInFloat '%s'.", _path.c_str());
|
2014-08-18 17:31:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
meshData->vertex.resize(vertexSizeInFloat);
|
|
|
|
if (_binaryReader.read(&meshData->vertex[0], 4, vertexSizeInFloat) != vertexSizeInFloat)
|
|
|
|
{
|
2014-08-29 11:56:33 +08:00
|
|
|
CCLOG("warning: Failed to read meshdata: vertex element '%s'.", _path.c_str());
|
2014-08-18 17:31:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read index data
|
|
|
|
unsigned int meshPartCount = 1;
|
|
|
|
_binaryReader.read(&meshPartCount, 4, 1);
|
|
|
|
|
2014-08-19 14:03:41 +08:00
|
|
|
for (unsigned int k = 0; k < meshPartCount; ++k)
|
2014-08-18 17:31:04 +08:00
|
|
|
{
|
|
|
|
std::vector<unsigned short> indexArray;
|
|
|
|
std:: string meshPartid = _binaryReader.readString();
|
|
|
|
meshData->subMeshIds.push_back(meshPartid);
|
|
|
|
unsigned int nIndexCount;
|
|
|
|
if (_binaryReader.read(&nIndexCount, 4, 1) != 1)
|
|
|
|
{
|
2014-08-29 11:56:33 +08:00
|
|
|
CCLOG("warning: Failed to read meshdata: nIndexCount '%s'.", _path.c_str());
|
2014-08-18 17:31:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
indexArray.resize(nIndexCount);
|
|
|
|
if (_binaryReader.read(&indexArray[0], 2, nIndexCount) != nIndexCount)
|
|
|
|
{
|
2014-08-29 11:56:33 +08:00
|
|
|
CCLOG("warning: Failed to read meshdata: indices '%s'.", _path.c_str());
|
2014-08-18 17:31:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
meshData->subMeshIndices.push_back(indexArray);
|
2014-08-20 10:24:46 +08:00
|
|
|
meshData->numIndex = (int)meshData->subMeshIndices.size();
|
2014-11-19 17:58:40 +08:00
|
|
|
meshData->subMeshAABB.push_back(calculateAABB(meshData->vertex, meshData->getPerVertexSize(), indexArray));
|
2014-08-18 17:31:04 +08:00
|
|
|
}
|
|
|
|
meshdatas.meshDatas.push_back(meshData);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool Bundle3D::loadMeshDatasBinary_0_1(MeshDatas& meshdatas)
|
|
|
|
{
|
|
|
|
if (!seekToFirstType(BUNDLE_TYPE_MESH))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
meshdatas.resetData();
|
|
|
|
|
2014-08-28 07:31:57 +08:00
|
|
|
MeshData* meshdata = new (std::nothrow) MeshData();
|
2014-08-18 17:31:04 +08:00
|
|
|
|
|
|
|
// read mesh data
|
|
|
|
unsigned int attribSize=0;
|
|
|
|
if (_binaryReader.read(&attribSize, 4, 1) != 1 || attribSize < 1)
|
|
|
|
{
|
2014-08-29 11:56:33 +08:00
|
|
|
CCLOG("warning: Failed to read meshdata: attribCount '%s'.", _path.c_str());
|
2014-08-18 17:31:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
2014-08-19 14:03:41 +08:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
VERTEX_ATTRIB_POSITION,
|
|
|
|
VERTEX_ATTRIB_COLOR,
|
|
|
|
VERTEX_ATTRIB_TEX_COORD,
|
|
|
|
VERTEX_ATTRIB_NORMAL,
|
|
|
|
VERTEX_ATTRIB_BLEND_WEIGHT,
|
|
|
|
VERTEX_ATTRIB_BLEND_INDEX,
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-08-19 14:03:41 +08:00
|
|
|
VERTEX_ATTRIB_MAX,
|
|
|
|
|
|
|
|
// backward compatibility
|
|
|
|
VERTEX_ATTRIB_TEX_COORDS = VERTEX_ATTRIB_TEX_COORD,
|
|
|
|
};
|
2014-08-18 17:31:04 +08:00
|
|
|
for (ssize_t i = 0; i < attribSize; i++)
|
|
|
|
{
|
|
|
|
unsigned int vUsage, vSize;
|
|
|
|
if (_binaryReader.read(&vUsage, 4, 1) != 1 || _binaryReader.read(&vSize, 4, 1) != 1)
|
|
|
|
{
|
2014-08-29 11:56:33 +08:00
|
|
|
CCLOG("warning: Failed to read meshdata: usage or size '%s'.", _path.c_str());
|
2014-08-18 17:31:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
MeshVertexAttrib meshVertexAttribute;
|
|
|
|
meshVertexAttribute.size = vSize;
|
|
|
|
meshVertexAttribute.attribSizeBytes = vSize * 4;
|
|
|
|
meshVertexAttribute.type = GL_FLOAT;
|
2014-08-19 14:03:41 +08:00
|
|
|
if(vUsage == VERTEX_ATTRIB_NORMAL)
|
|
|
|
{
|
|
|
|
vUsage= GLProgram::VERTEX_ATTRIB_NORMAL;
|
|
|
|
}
|
|
|
|
else if(vUsage == VERTEX_ATTRIB_BLEND_WEIGHT)
|
|
|
|
{
|
|
|
|
vUsage= GLProgram::VERTEX_ATTRIB_BLEND_WEIGHT;
|
|
|
|
}
|
|
|
|
else if(vUsage == VERTEX_ATTRIB_BLEND_INDEX)
|
|
|
|
{
|
|
|
|
vUsage= GLProgram::VERTEX_ATTRIB_BLEND_INDEX;
|
|
|
|
}
|
|
|
|
else if(vUsage == VERTEX_ATTRIB_POSITION)
|
|
|
|
{
|
|
|
|
vUsage= GLProgram::VERTEX_ATTRIB_POSITION;
|
|
|
|
}
|
|
|
|
else if(vUsage == VERTEX_ATTRIB_TEX_COORD)
|
|
|
|
{
|
|
|
|
vUsage= GLProgram::VERTEX_ATTRIB_TEX_COORD;
|
|
|
|
}
|
2014-08-18 17:31:04 +08:00
|
|
|
meshVertexAttribute.vertexAttrib = vUsage;
|
|
|
|
|
|
|
|
meshdata->attribs.push_back(meshVertexAttribute);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read vertex data
|
|
|
|
if (_binaryReader.read(&meshdata->vertexSizeInFloat, 4, 1) != 1 || meshdata->vertexSizeInFloat == 0)
|
|
|
|
{
|
2014-08-29 11:56:33 +08:00
|
|
|
CCLOG("warning: Failed to read meshdata: vertexSizeInFloat '%s'.", _path.c_str());
|
2014-08-18 17:31:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
meshdata->vertex.resize(meshdata->vertexSizeInFloat);
|
|
|
|
if (_binaryReader.read(&meshdata->vertex[0], 4, meshdata->vertexSizeInFloat) != meshdata->vertexSizeInFloat)
|
|
|
|
{
|
2014-08-29 11:56:33 +08:00
|
|
|
CCLOG("warning: Failed to read meshdata: vertex element '%s'.", _path.c_str());
|
2014-08-18 17:31:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read index data
|
|
|
|
unsigned int meshPartCount = 1;
|
|
|
|
for (unsigned int i = 0; i < meshPartCount; ++i)
|
|
|
|
{
|
|
|
|
unsigned int nIndexCount;
|
|
|
|
if (_binaryReader.read(&nIndexCount, 4, 1) != 1)
|
|
|
|
{
|
2014-08-29 11:56:33 +08:00
|
|
|
CCLOG("warning: Failed to read meshdata: nIndexCount '%s'.", _path.c_str());
|
2014-08-18 17:31:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<unsigned short> indices;
|
|
|
|
indices.resize(nIndexCount);
|
|
|
|
if (_binaryReader.read(&indices[0], 2, nIndexCount) != nIndexCount)
|
|
|
|
{
|
2014-08-29 11:56:33 +08:00
|
|
|
CCLOG("warning: Failed to read meshdata: indices '%s'.", _path.c_str());
|
2014-08-18 17:31:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
meshdata->subMeshIndices.push_back(indices);
|
2014-11-19 17:58:40 +08:00
|
|
|
meshdata->subMeshAABB.push_back(calculateAABB(meshdata->vertex, meshdata->getPerVertexSize(), indices));
|
2014-08-18 17:31:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
meshdatas.meshDatas.push_back(meshdata);
|
2014-08-15 13:03:53 +08:00
|
|
|
return true;
|
|
|
|
}
|
2014-08-18 17:31:04 +08:00
|
|
|
|
|
|
|
bool Bundle3D::loadMeshDatasBinary_0_2(MeshDatas& meshdatas)
|
|
|
|
{
|
|
|
|
if (!seekToFirstType(BUNDLE_TYPE_MESH))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
meshdatas.resetData();
|
|
|
|
|
2014-08-28 07:31:57 +08:00
|
|
|
MeshData* meshdata = new (std::nothrow) MeshData();
|
2014-08-18 17:31:04 +08:00
|
|
|
|
|
|
|
// read mesh data
|
|
|
|
unsigned int attribSize=0;
|
|
|
|
if (_binaryReader.read(&attribSize, 4, 1) != 1 || attribSize < 1)
|
|
|
|
{
|
2014-08-29 11:56:33 +08:00
|
|
|
CCLOG("warning: Failed to read meshdata: attribCount '%s'.", _path.c_str());
|
2014-08-18 17:31:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
2014-08-19 14:03:41 +08:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
VERTEX_ATTRIB_POSITION,
|
|
|
|
VERTEX_ATTRIB_COLOR,
|
|
|
|
VERTEX_ATTRIB_TEX_COORD,
|
|
|
|
VERTEX_ATTRIB_NORMAL,
|
|
|
|
VERTEX_ATTRIB_BLEND_WEIGHT,
|
|
|
|
VERTEX_ATTRIB_BLEND_INDEX,
|
|
|
|
|
|
|
|
VERTEX_ATTRIB_MAX,
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-08-19 14:03:41 +08:00
|
|
|
// backward compatibility
|
|
|
|
VERTEX_ATTRIB_TEX_COORDS = VERTEX_ATTRIB_TEX_COORD,
|
|
|
|
};
|
2014-08-18 17:31:04 +08:00
|
|
|
for (ssize_t i = 0; i < attribSize; i++)
|
|
|
|
{
|
|
|
|
unsigned int vUsage, vSize;
|
|
|
|
if (_binaryReader.read(&vUsage, 4, 1) != 1 || _binaryReader.read(&vSize, 4, 1) != 1)
|
|
|
|
{
|
2014-08-29 11:56:33 +08:00
|
|
|
CCLOG("warning: Failed to read meshdata: usage or size '%s'.", _path.c_str());
|
2014-08-18 17:31:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
MeshVertexAttrib meshVertexAttribute;
|
|
|
|
meshVertexAttribute.size = vSize;
|
|
|
|
meshVertexAttribute.attribSizeBytes = vSize * 4;
|
|
|
|
meshVertexAttribute.type = GL_FLOAT;
|
2014-08-19 14:03:41 +08:00
|
|
|
if(vUsage == VERTEX_ATTRIB_NORMAL)
|
|
|
|
{
|
|
|
|
vUsage= GLProgram::VERTEX_ATTRIB_NORMAL;
|
|
|
|
}
|
|
|
|
else if(vUsage == VERTEX_ATTRIB_BLEND_WEIGHT)
|
|
|
|
{
|
|
|
|
vUsage= GLProgram::VERTEX_ATTRIB_BLEND_WEIGHT;
|
|
|
|
}
|
|
|
|
else if(vUsage == VERTEX_ATTRIB_BLEND_INDEX)
|
|
|
|
{
|
|
|
|
vUsage= GLProgram::VERTEX_ATTRIB_BLEND_INDEX;
|
|
|
|
}
|
|
|
|
else if(vUsage == VERTEX_ATTRIB_POSITION)
|
|
|
|
{
|
|
|
|
vUsage= GLProgram::VERTEX_ATTRIB_POSITION;
|
|
|
|
}
|
|
|
|
else if(vUsage == VERTEX_ATTRIB_TEX_COORD)
|
|
|
|
{
|
|
|
|
vUsage= GLProgram::VERTEX_ATTRIB_TEX_COORD;
|
|
|
|
}
|
2014-08-18 17:31:04 +08:00
|
|
|
meshVertexAttribute.vertexAttrib = vUsage;
|
|
|
|
|
|
|
|
meshdata->attribs.push_back(meshVertexAttribute);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read vertex data
|
|
|
|
if (_binaryReader.read(&meshdata->vertexSizeInFloat, 4, 1) != 1 || meshdata->vertexSizeInFloat == 0)
|
|
|
|
{
|
2014-08-29 11:56:33 +08:00
|
|
|
CCLOG("warning: Failed to read meshdata: vertexSizeInFloat '%s'.", _path.c_str());
|
2014-08-18 17:31:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
meshdata->vertex.resize(meshdata->vertexSizeInFloat);
|
|
|
|
if (_binaryReader.read(&meshdata->vertex[0], 4, meshdata->vertexSizeInFloat) != meshdata->vertexSizeInFloat)
|
|
|
|
{
|
2014-08-29 11:56:33 +08:00
|
|
|
CCLOG("warning: Failed to read meshdata: vertex element '%s'.", _path.c_str());
|
2014-08-18 17:31:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read submesh
|
|
|
|
unsigned int submeshCount;
|
|
|
|
if (_binaryReader.read(&submeshCount, 4, 1) != 1)
|
|
|
|
{
|
2014-08-29 11:56:33 +08:00
|
|
|
CCLOG("warning: Failed to read meshdata: submeshCount '%s'.", _path.c_str());
|
2014-08-18 17:31:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < submeshCount; ++i)
|
|
|
|
{
|
|
|
|
unsigned int nIndexCount;
|
|
|
|
if (_binaryReader.read(&nIndexCount, 4, 1) != 1)
|
|
|
|
{
|
2014-08-29 11:56:33 +08:00
|
|
|
CCLOG("warning: Failed to read meshdata: nIndexCount '%s'.", _path.c_str());
|
2014-08-18 17:31:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<unsigned short> indices;
|
|
|
|
indices.resize(nIndexCount);
|
|
|
|
if (_binaryReader.read(&indices[0], 2, nIndexCount) != nIndexCount)
|
|
|
|
{
|
2014-08-29 11:56:33 +08:00
|
|
|
CCLOG("warning: Failed to read meshdata: indices '%s'.", _path.c_str());
|
2014-08-18 17:31:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
meshdata->subMeshIndices.push_back(indices);
|
2014-11-19 17:58:40 +08:00
|
|
|
meshdata->subMeshAABB.push_back(calculateAABB(meshdata->vertex, meshdata->getPerVertexSize(), indices));
|
2014-08-18 17:31:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
meshdatas.meshDatas.push_back(meshdata);
|
2014-08-18 18:56:01 +08:00
|
|
|
|
|
|
|
return true;
|
2014-08-18 17:31:04 +08:00
|
|
|
}
|
2014-08-15 15:32:15 +08:00
|
|
|
bool Bundle3D::loadMeshDatasJson(MeshDatas& meshdatas)
|
|
|
|
{
|
2014-08-16 11:12:26 +08:00
|
|
|
const rapidjson::Value& mesh_data_array = _jsonReader[MESHES];
|
2014-08-15 15:32:15 +08:00
|
|
|
for (rapidjson::SizeType index = 0; index < mesh_data_array.Size(); index++)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
MeshData* meshData = new (std::nothrow) MeshData();
|
2014-08-15 15:32:15 +08:00
|
|
|
const rapidjson::Value& mesh_data = mesh_data_array[index];
|
|
|
|
// mesh_vertex_attribute
|
2014-08-20 10:24:46 +08:00
|
|
|
const rapidjson::Value& mesh_vertex_attribute = mesh_data[ATTRIBUTES];
|
2014-08-15 15:32:15 +08:00
|
|
|
MeshVertexAttrib tempAttrib;
|
|
|
|
meshData->attribCount=mesh_vertex_attribute.Size();
|
|
|
|
meshData->attribs.resize(meshData->attribCount);
|
|
|
|
for (int i = 0; i < mesh_vertex_attribute.Size(); i++)
|
|
|
|
{
|
|
|
|
const rapidjson::Value& mesh_vertex_attribute_val = mesh_vertex_attribute[i];
|
2014-08-15 13:03:53 +08:00
|
|
|
|
2014-08-21 11:23:31 +08:00
|
|
|
int size = mesh_vertex_attribute_val[ATTRIBUTESIZE].GetInt();
|
2014-08-20 10:24:46 +08:00
|
|
|
std::string type = mesh_vertex_attribute_val[TYPE].GetString();
|
|
|
|
std::string attribute = mesh_vertex_attribute_val[ATTRIBUTE].GetString();
|
2014-08-15 15:32:15 +08:00
|
|
|
|
|
|
|
tempAttrib.size = size;
|
|
|
|
tempAttrib.attribSizeBytes = sizeof(float) * size;
|
|
|
|
tempAttrib.type = parseGLType(type);
|
|
|
|
tempAttrib.vertexAttrib = parseGLProgramAttribute(attribute);
|
|
|
|
meshData->attribs[i]=tempAttrib;
|
|
|
|
}
|
|
|
|
// mesh vertices
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2014-08-20 10:24:46 +08:00
|
|
|
const rapidjson::Value& mesh_data_vertex_array = mesh_data[VERTICES];
|
2014-08-15 15:32:15 +08:00
|
|
|
meshData->vertexSizeInFloat=mesh_data_vertex_array.Size();
|
|
|
|
for (rapidjson::SizeType i = 0; i < mesh_data_vertex_array.Size(); i++)
|
|
|
|
{
|
|
|
|
meshData->vertex.push_back(mesh_data_vertex_array[i].GetDouble());
|
|
|
|
}
|
|
|
|
// mesh part
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2014-08-16 11:12:26 +08:00
|
|
|
const rapidjson::Value& mesh_part_array = mesh_data[PARTS];
|
2014-08-15 15:32:15 +08:00
|
|
|
for (rapidjson::SizeType i = 0; i < mesh_part_array.Size(); i++)
|
|
|
|
{
|
|
|
|
std::vector<unsigned short> indexArray;
|
|
|
|
const rapidjson::Value& mesh_part = mesh_part_array[i];
|
|
|
|
meshData->subMeshIds.push_back(mesh_part[ID].GetString());
|
|
|
|
// index_number
|
2014-08-20 10:24:46 +08:00
|
|
|
const rapidjson::Value& indices_val_array = mesh_part[INDICES];
|
2014-08-15 15:32:15 +08:00
|
|
|
for (rapidjson::SizeType j = 0; j < indices_val_array.Size(); j++)
|
|
|
|
indexArray.push_back((unsigned short)indices_val_array[j].GetUint());
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-08-15 15:32:15 +08:00
|
|
|
meshData->subMeshIndices.push_back(indexArray);
|
2014-08-20 10:24:46 +08:00
|
|
|
meshData->numIndex = (int)meshData->subMeshIndices.size();
|
2014-11-19 17:58:40 +08:00
|
|
|
meshData->subMeshAABB.push_back(calculateAABB(meshData->vertex, meshData->getPerVertexSize(), indexArray));
|
2014-08-15 15:32:15 +08:00
|
|
|
}
|
|
|
|
meshdatas.meshDatas.push_back(meshData);
|
|
|
|
}
|
2014-08-16 11:12:26 +08:00
|
|
|
return true;
|
2014-08-15 15:32:15 +08:00
|
|
|
}
|
2014-08-15 13:03:53 +08:00
|
|
|
bool Bundle3D::loadNodes(NodeDatas& nodedatas)
|
|
|
|
{
|
2014-08-18 17:31:04 +08:00
|
|
|
if (_version == "0.1" || _version == "1.2" || _version == "0.2")
|
|
|
|
{
|
|
|
|
SkinData skinData;
|
2014-09-02 17:33:22 +08:00
|
|
|
if (!loadSkinData("", &skinData))
|
|
|
|
{
|
|
|
|
auto node= new (std::nothrow) NodeData();
|
|
|
|
auto modelnode = new (std::nothrow) ModelData();
|
|
|
|
modelnode->matrialId = "";
|
|
|
|
modelnode->subMeshId = "";
|
|
|
|
node->modelNodeDatas.push_back(modelnode);
|
|
|
|
nodedatas.nodes.push_back(node);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-28 07:31:57 +08:00
|
|
|
auto nodeDatas = new (std::nothrow) NodeData*[skinData.skinBoneNames.size() + skinData.nodeBoneNames.size()];
|
2014-08-18 17:31:04 +08:00
|
|
|
int index = 0;
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < skinData.skinBoneNames.size(); i++)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
nodeDatas[index] = new (std::nothrow) NodeData();
|
2014-08-18 17:31:04 +08:00
|
|
|
nodeDatas[index]->id = skinData.skinBoneNames[i];
|
|
|
|
nodeDatas[index]->transform = skinData.skinBoneOriginMatrices[i];
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
for (i = 0; i < skinData.nodeBoneNames.size(); i++)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
nodeDatas[index] = new (std::nothrow) NodeData();
|
2014-08-18 17:31:04 +08:00
|
|
|
nodeDatas[index]->id = skinData.nodeBoneNames[i];
|
|
|
|
nodeDatas[index]->transform = skinData.nodeBoneOriginMatrices[i];
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
for (const auto& it : skinData.boneChild)
|
|
|
|
{
|
|
|
|
const auto& children = it.second;
|
|
|
|
auto parent = nodeDatas[it.first];
|
|
|
|
for (const auto& child : children)
|
|
|
|
{
|
|
|
|
parent->children.push_back(nodeDatas[child]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nodedatas.skeleton.push_back(nodeDatas[skinData.rootBoneIndex]);
|
2014-08-28 07:31:57 +08:00
|
|
|
auto node= new (std::nothrow) NodeData();
|
|
|
|
auto modelnode = new (std::nothrow) ModelData();
|
2014-08-18 17:31:04 +08:00
|
|
|
modelnode->matrialId = "";
|
|
|
|
modelnode->subMeshId = "";
|
|
|
|
modelnode->bones = skinData.skinBoneNames;
|
|
|
|
modelnode->invBindPose = skinData.inverseBindPoseMatrices;
|
2014-08-20 11:40:04 +08:00
|
|
|
node->modelNodeDatas.push_back(modelnode);
|
|
|
|
nodedatas.nodes.push_back(node);
|
2014-08-16 11:12:26 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-18 17:31:04 +08:00
|
|
|
if (_isBinary)
|
2014-08-16 14:44:41 +08:00
|
|
|
{
|
2014-08-18 17:31:04 +08:00
|
|
|
loadNodesBinary(nodedatas);
|
2014-08-16 14:44:41 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-18 17:31:04 +08:00
|
|
|
loadNodesJson(nodedatas);
|
2014-08-16 14:44:41 +08:00
|
|
|
}
|
2014-08-16 11:12:26 +08:00
|
|
|
}
|
2014-08-15 13:03:53 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool Bundle3D::loadMaterials(MaterialDatas& materialdatas)
|
|
|
|
{
|
2014-08-15 17:45:37 +08:00
|
|
|
materialdatas.resetData();
|
2014-08-15 15:32:15 +08:00
|
|
|
if (_isBinary)
|
|
|
|
{
|
2014-08-18 17:31:04 +08:00
|
|
|
if (_version == "0.1")
|
|
|
|
{
|
|
|
|
return loadMaterialsBinary_0_1(materialdatas);
|
|
|
|
}
|
|
|
|
else if (_version == "0.2")
|
|
|
|
{
|
|
|
|
return loadMaterialsBinary_0_2(materialdatas);
|
|
|
|
}
|
2014-08-29 11:59:30 +08:00
|
|
|
else
|
2014-08-18 17:31:04 +08:00
|
|
|
{
|
|
|
|
return loadMaterialsBinary(materialdatas);
|
|
|
|
}
|
2014-08-15 15:32:15 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-16 14:14:37 +08:00
|
|
|
if (_version == "1.2")
|
|
|
|
{
|
2014-08-18 17:31:04 +08:00
|
|
|
return loadMaterialDataJson_0_1(materialdatas);
|
2014-08-16 14:14:37 +08:00
|
|
|
}
|
|
|
|
else if (_version == "0.2")
|
|
|
|
{
|
2014-08-18 17:31:04 +08:00
|
|
|
return loadMaterialDataJson_0_2(materialdatas);
|
2014-08-16 14:14:37 +08:00
|
|
|
}
|
2014-08-29 11:59:30 +08:00
|
|
|
else
|
2014-08-16 14:14:37 +08:00
|
|
|
{
|
|
|
|
return loadMaterialsJson(materialdatas);
|
|
|
|
}
|
2014-08-15 15:32:15 +08:00
|
|
|
}
|
2014-08-15 13:03:53 +08:00
|
|
|
return true;
|
|
|
|
}
|
2014-08-16 11:12:26 +08:00
|
|
|
bool Bundle3D::loadMaterialsBinary(MaterialDatas& materialdatas)
|
2014-08-15 15:32:15 +08:00
|
|
|
{
|
2014-08-18 17:31:04 +08:00
|
|
|
if (!seekToFirstType(BUNDLE_TYPE_MATERIAL))
|
|
|
|
return false;
|
|
|
|
unsigned int materialnum = 1;
|
|
|
|
_binaryReader.read(&materialnum, 4, 1);
|
|
|
|
for (int i = 0; i < materialnum; i++)
|
|
|
|
{
|
|
|
|
NMaterialData materialData;
|
|
|
|
materialData.id = _binaryReader.readString();
|
|
|
|
float data[14];
|
|
|
|
_binaryReader.read(&data,sizeof(float), 14);
|
|
|
|
|
|
|
|
unsigned int textruenum = 1;
|
|
|
|
_binaryReader.read(&textruenum, 4, 1);
|
2014-08-19 14:03:41 +08:00
|
|
|
for(int j = 0; j < textruenum ; j++ )
|
2014-08-18 17:31:04 +08:00
|
|
|
{
|
|
|
|
NTextureData textureData;
|
|
|
|
textureData.id = _binaryReader.readString();
|
|
|
|
if (textureData.id.empty())
|
|
|
|
{
|
2014-08-29 11:56:33 +08:00
|
|
|
CCLOG("warning: Failed to read Materialdata: texturePath is empty '%s'.", textureData.id.c_str());
|
2014-08-18 17:31:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::string texturePath = _binaryReader.readString();
|
|
|
|
if (texturePath.empty())
|
|
|
|
{
|
2014-08-29 11:56:33 +08:00
|
|
|
CCLOG("warning: Failed to read Materialdata: texturePath is empty '%s'.", _path.c_str());
|
2014-08-18 17:31:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-23 15:52:45 +08:00
|
|
|
textureData.filename = texturePath.empty() ? texturePath : _modelPath + texturePath;
|
2014-08-18 17:31:04 +08:00
|
|
|
float uvdata[4];
|
|
|
|
_binaryReader.read(&uvdata,sizeof(float), 4);
|
|
|
|
textureData.type = parseGLTextureType(_binaryReader.readString());
|
|
|
|
textureData.wrapS= parseGLType(_binaryReader.readString());
|
|
|
|
textureData.wrapT= parseGLType(_binaryReader.readString());
|
|
|
|
materialData.textures.push_back(textureData);
|
|
|
|
}
|
|
|
|
materialdatas.materials.push_back(materialData);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool Bundle3D::loadMaterialsBinary_0_1(MaterialDatas& materialdatas)
|
|
|
|
{
|
|
|
|
if (!seekToFirstType(BUNDLE_TYPE_MATERIAL))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
NMaterialData materialData;
|
|
|
|
|
|
|
|
std::string texturePath = _binaryReader.readString();
|
|
|
|
if (texturePath.empty())
|
|
|
|
{
|
2014-08-29 11:56:33 +08:00
|
|
|
CCLOG("warning: Failed to read Materialdata: texturePath is empty '%s'.", _path.c_str());
|
2014-08-18 17:31:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTextureData textureData;
|
2014-12-23 15:52:45 +08:00
|
|
|
textureData.filename = texturePath.empty() ? texturePath : _modelPath + texturePath;
|
2014-08-18 17:31:04 +08:00
|
|
|
textureData.type= NTextureData::Usage::Diffuse;
|
|
|
|
textureData.id="";
|
|
|
|
materialData.textures.push_back(textureData);
|
|
|
|
materialdatas.materials.push_back(materialData);
|
2014-08-18 17:57:14 +08:00
|
|
|
return true;
|
2014-08-18 17:31:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Bundle3D::loadMaterialsBinary_0_2(MaterialDatas& materialdatas)
|
|
|
|
{
|
|
|
|
if (!seekToFirstType(BUNDLE_TYPE_MATERIAL))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
unsigned int materialnum = 1;
|
|
|
|
_binaryReader.read(&materialnum, 4, 1);
|
|
|
|
|
|
|
|
for (int i = 0; i < materialnum; i++)
|
|
|
|
{
|
|
|
|
NMaterialData materialData;
|
|
|
|
|
|
|
|
std::string texturePath = _binaryReader.readString();
|
|
|
|
if (texturePath.empty())
|
|
|
|
{
|
2014-08-29 11:56:33 +08:00
|
|
|
CCLOG("warning: Failed to read Materialdata: texturePath is empty '%s'.", _path.c_str());
|
2014-10-21 18:44:10 +08:00
|
|
|
return true;
|
2014-08-18 17:31:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NTextureData textureData;
|
2014-12-23 15:52:45 +08:00
|
|
|
textureData.filename = texturePath.empty() ? texturePath : _modelPath + texturePath;
|
2014-08-18 17:31:04 +08:00
|
|
|
textureData.type= NTextureData::Usage::Diffuse;
|
|
|
|
textureData.id="";
|
|
|
|
materialData.textures.push_back(textureData);
|
|
|
|
materialdatas.materials.push_back(materialData);
|
|
|
|
}
|
2014-08-18 17:57:14 +08:00
|
|
|
return true;
|
2014-08-15 15:32:15 +08:00
|
|
|
}
|
|
|
|
bool Bundle3D::loadMaterialsJson(MaterialDatas& materialdatas)
|
|
|
|
{
|
2014-08-20 10:24:46 +08:00
|
|
|
if (!_jsonReader.HasMember(MATERIALS))
|
2014-08-15 15:32:15 +08:00
|
|
|
return false;
|
2014-08-20 10:24:46 +08:00
|
|
|
const rapidjson::Value& material_array = _jsonReader[MATERIALS];
|
2014-08-15 15:32:15 +08:00
|
|
|
for (rapidjson::SizeType i = 0; i < material_array.Size(); i++)
|
|
|
|
{
|
2014-08-16 10:41:42 +08:00
|
|
|
NMaterialData materialData;
|
|
|
|
const rapidjson::Value& material_val = material_array[i];
|
|
|
|
materialData.id = material_val[ID].GetString();
|
2014-08-20 10:24:46 +08:00
|
|
|
if (material_val.HasMember(TEXTURES))
|
2014-08-16 10:41:42 +08:00
|
|
|
{
|
2014-08-20 10:24:46 +08:00
|
|
|
const rapidjson::Value& testure_array = material_val[TEXTURES];
|
2014-08-16 10:41:42 +08:00
|
|
|
for (rapidjson::SizeType j = 0; j < testure_array.Size(); j++)
|
|
|
|
{
|
|
|
|
NTextureData textureData;
|
|
|
|
const rapidjson::Value& texture_val = testure_array[j];
|
2014-08-20 10:24:46 +08:00
|
|
|
std::string filename = texture_val[FILENAME].GetString();
|
2014-12-23 15:52:45 +08:00
|
|
|
textureData.filename = filename.empty() ? filename : _modelPath + filename;
|
2014-08-16 10:41:42 +08:00
|
|
|
textureData.type = parseGLTextureType(texture_val["type"].GetString());
|
|
|
|
textureData.wrapS = parseGLType(texture_val["wrapModeU"].GetString());
|
|
|
|
textureData.wrapT = parseGLType(texture_val["wrapModeV"].GetString());
|
|
|
|
materialData.textures.push_back(textureData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
materialdatas.materials.push_back(materialData);
|
2014-08-15 15:32:15 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2014-06-20 15:30:08 +08:00
|
|
|
bool Bundle3D::loadJson(const std::string& path)
|
|
|
|
{
|
2014-06-25 09:13:24 +08:00
|
|
|
clear();
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-06-20 22:38:09 +08:00
|
|
|
Data data = FileUtils::getInstance()->getDataFromFile(path);
|
|
|
|
ssize_t size = data.getSize();
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-06-25 09:13:24 +08:00
|
|
|
// json need null-terminated string.
|
2014-06-20 15:30:08 +08:00
|
|
|
_jsonBuffer = new char[size + 1];
|
2014-06-20 22:38:09 +08:00
|
|
|
memcpy(_jsonBuffer, data.getBytes(), size);
|
2014-06-20 15:30:08 +08:00
|
|
|
_jsonBuffer[size] = '\0';
|
2014-06-25 09:13:24 +08:00
|
|
|
if (_jsonReader.ParseInsitu<0>(_jsonBuffer).HasParseError())
|
2014-06-13 19:26:46 +08:00
|
|
|
{
|
2014-08-21 13:30:51 +08:00
|
|
|
CCASSERT(false, "Parse json failed");
|
2014-08-18 17:31:04 +08:00
|
|
|
clear();
|
|
|
|
return false;
|
2014-06-13 19:26:46 +08:00
|
|
|
}
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-07-29 10:49:06 +08:00
|
|
|
const rapidjson::Value& mash_data_array = _jsonReader[VERSION];
|
2014-08-29 10:43:26 +08:00
|
|
|
if (mash_data_array.IsArray()) // Compatible with the old version
|
|
|
|
_version = "1.2";
|
|
|
|
else
|
|
|
|
_version = mash_data_array.GetString();
|
|
|
|
|
2014-06-05 16:36:01 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-21 18:44:10 +08:00
|
|
|
|
|
|
|
bool Bundle3D::loadBinary(const std::string& path)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
|
|
|
|
// get file data
|
|
|
|
CC_SAFE_DELETE(_binaryBuffer);
|
|
|
|
_binaryBuffer = new (std::nothrow) Data();
|
|
|
|
*_binaryBuffer = FileUtils::getInstance()->getDataFromFile(path);
|
|
|
|
if (_binaryBuffer->isNull())
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
CCLOG("warning: Failed to read file: %s", path.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialise bundle reader
|
|
|
|
_binaryReader.init( (char*)_binaryBuffer->getBytes(), _binaryBuffer->getSize() );
|
|
|
|
|
|
|
|
// Read identifier info
|
|
|
|
char identifier[] = { 'C', '3', 'B', '\0'};
|
|
|
|
char sig[4];
|
|
|
|
if (_binaryReader.read(sig, 1, 4) != 4 || memcmp(sig, identifier, 4) != 0)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
CCLOG("warning: Invalid identifier: %s", path.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read version
|
|
|
|
unsigned char ver[2];
|
|
|
|
if (_binaryReader.read(ver, 1, 2)!= 2){
|
|
|
|
CCLOG("warning: Failed to read version:");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char version[20] = {0};
|
|
|
|
sprintf(version, "%d.%d", ver[0], ver[1]);
|
|
|
|
_version = version;
|
|
|
|
|
|
|
|
// Read ref table size
|
|
|
|
if (_binaryReader.read(&_referenceCount, 4, 1) != 1)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
CCLOG("warning: Failed to read ref table size '%s'.", path.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read all refs
|
|
|
|
CC_SAFE_DELETE_ARRAY(_references);
|
|
|
|
_references = new (std::nothrow) Reference[_referenceCount];
|
|
|
|
for (ssize_t i = 0; i < _referenceCount; ++i)
|
|
|
|
{
|
|
|
|
if ((_references[i].id = _binaryReader.readString()).empty() ||
|
|
|
|
_binaryReader.read(&_references[i].type, 4, 1) != 1 ||
|
|
|
|
_binaryReader.read(&_references[i].offset, 4, 1) != 1)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
CCLOG("warning: Failed to read ref number %d for bundle '%s'.", (int)i, path.c_str());
|
|
|
|
CC_SAFE_DELETE_ARRAY(_references);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-16 14:14:37 +08:00
|
|
|
bool Bundle3D::loadMeshDataJson_0_1(MeshDatas& meshdatas)
|
2014-07-29 10:49:06 +08:00
|
|
|
{
|
2014-08-16 11:12:26 +08:00
|
|
|
const rapidjson::Value& mesh_data_array = _jsonReader[MESH];
|
2014-08-16 14:14:37 +08:00
|
|
|
MeshData* meshdata= new MeshData();
|
2014-07-29 10:49:06 +08:00
|
|
|
const rapidjson::Value& mesh_data_val = mesh_data_array[(rapidjson::SizeType)0];
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-08-20 10:24:46 +08:00
|
|
|
const rapidjson::Value& mesh_data_body_array = mesh_data_val[DEFAULTPART];
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-06-16 19:08:43 +08:00
|
|
|
const rapidjson::Value& mesh_data_body_array_0 = mesh_data_body_array[(rapidjson::SizeType)0];
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-07-29 10:49:06 +08:00
|
|
|
// mesh_vertex_attribute
|
2014-08-20 10:24:46 +08:00
|
|
|
const rapidjson::Value& mesh_vertex_attribute = mesh_data_val[ATTRIBUTES];
|
2014-07-29 10:49:06 +08:00
|
|
|
meshdata->attribCount = mesh_vertex_attribute.Size();
|
|
|
|
meshdata->attribs.resize(meshdata->attribCount);
|
|
|
|
for (rapidjson::SizeType i = 0; i < mesh_vertex_attribute.Size(); i++)
|
|
|
|
{
|
|
|
|
const rapidjson::Value& mesh_vertex_attribute_val = mesh_vertex_attribute[i];
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-08-21 11:23:31 +08:00
|
|
|
meshdata->attribs[i].size = mesh_vertex_attribute_val[ATTRIBUTESIZE].GetUint();
|
2014-07-29 10:49:06 +08:00
|
|
|
meshdata->attribs[i].attribSizeBytes = meshdata->attribs[i].size * 4;
|
2014-08-20 10:24:46 +08:00
|
|
|
meshdata->attribs[i].type = parseGLType(mesh_vertex_attribute_val[TYPE].GetString());
|
|
|
|
meshdata->attribs[i].vertexAttrib = parseGLProgramAttribute(mesh_vertex_attribute_val[ATTRIBUTE].GetString());
|
2014-07-29 10:49:06 +08:00
|
|
|
}
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-06-12 10:01:54 +08:00
|
|
|
// vertices
|
2014-08-20 10:24:46 +08:00
|
|
|
meshdata->vertexSizeInFloat = mesh_data_body_array_0[VERTEXSIZE].GetInt();
|
2014-06-16 00:01:38 +08:00
|
|
|
meshdata->vertex.resize(meshdata->vertexSizeInFloat);
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-08-20 10:24:46 +08:00
|
|
|
const rapidjson::Value& mesh_data_body_vertices = mesh_data_body_array_0[VERTICES];
|
2014-06-16 19:08:43 +08:00
|
|
|
for (rapidjson::SizeType i = 0; i < mesh_data_body_vertices.Size(); i++)
|
|
|
|
meshdata->vertex[i] = mesh_data_body_vertices[i].GetDouble();
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-06-12 10:01:54 +08:00
|
|
|
// index_number
|
2014-08-20 10:24:46 +08:00
|
|
|
unsigned int indexnum = mesh_data_body_array_0[INDEXNUM].GetUint();
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-06-12 10:01:54 +08:00
|
|
|
// indices
|
2014-07-29 10:49:06 +08:00
|
|
|
std::vector<unsigned short> indices;
|
|
|
|
indices.resize(indexnum);
|
|
|
|
|
2014-08-20 10:24:46 +08:00
|
|
|
const rapidjson::Value& indices_val_array = mesh_data_body_array_0[INDICES];
|
2014-07-29 10:49:06 +08:00
|
|
|
for (rapidjson::SizeType i = 0; i < indices_val_array.Size(); i++)
|
|
|
|
indices[i] = (unsigned short)indices_val_array[i].GetUint();
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-07-29 10:49:06 +08:00
|
|
|
meshdata->subMeshIndices.push_back(indices);
|
2014-11-19 17:58:40 +08:00
|
|
|
meshdata->subMeshAABB.push_back(calculateAABB(meshdata->vertex, meshdata->getPerVertexSize(), indices));
|
2014-08-16 14:14:37 +08:00
|
|
|
meshdatas.meshDatas.push_back(meshdata);
|
2014-07-29 10:49:06 +08:00
|
|
|
return true;
|
|
|
|
}
|
2014-06-12 10:01:54 +08:00
|
|
|
|
2014-08-16 14:14:37 +08:00
|
|
|
bool Bundle3D::loadMeshDataJson_0_2(MeshDatas& meshdatas)
|
2014-07-29 10:49:06 +08:00
|
|
|
{
|
2014-08-16 14:44:41 +08:00
|
|
|
MeshData* meshdata= new MeshData();
|
2014-08-16 11:12:26 +08:00
|
|
|
const rapidjson::Value& mesh_array = _jsonReader[MESH];
|
2014-07-29 10:49:06 +08:00
|
|
|
const rapidjson::Value& mesh_array_0 = mesh_array[(rapidjson::SizeType)0];
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-06-12 10:01:54 +08:00
|
|
|
// mesh_vertex_attribute
|
2014-08-20 10:24:46 +08:00
|
|
|
const rapidjson::Value& mesh_vertex_attribute = mesh_array_0[ATTRIBUTES];
|
2014-06-12 10:01:54 +08:00
|
|
|
meshdata->attribCount = mesh_vertex_attribute.Size();
|
2014-06-16 00:01:38 +08:00
|
|
|
meshdata->attribs.resize(meshdata->attribCount);
|
2014-06-12 10:01:54 +08:00
|
|
|
for (rapidjson::SizeType i = 0; i < mesh_vertex_attribute.Size(); i++)
|
|
|
|
{
|
|
|
|
const rapidjson::Value& mesh_vertex_attribute_val = mesh_vertex_attribute[i];
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-08-21 11:23:31 +08:00
|
|
|
meshdata->attribs[i].size = mesh_vertex_attribute_val[ATTRIBUTESIZE].GetUint();
|
2014-06-20 15:30:08 +08:00
|
|
|
meshdata->attribs[i].attribSizeBytes = meshdata->attribs[i].size * 4;
|
2014-08-20 10:24:46 +08:00
|
|
|
meshdata->attribs[i].type = parseGLType(mesh_vertex_attribute_val[TYPE].GetString());
|
|
|
|
meshdata->attribs[i].vertexAttrib = parseGLProgramAttribute(mesh_vertex_attribute_val[ATTRIBUTE].GetString());
|
2014-06-12 10:01:54 +08:00
|
|
|
}
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-07-29 10:49:06 +08:00
|
|
|
// vertices
|
2014-08-20 10:24:46 +08:00
|
|
|
const rapidjson::Value& mesh_data_vertex = mesh_array_0[VERTEX];
|
2014-07-29 10:49:06 +08:00
|
|
|
const rapidjson::Value& mesh_data_vertex_0 = mesh_data_vertex[(rapidjson::SizeType)0];
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-08-20 10:24:46 +08:00
|
|
|
meshdata->vertexSizeInFloat = mesh_data_vertex_0[VERTEXSIZE].GetInt();
|
2014-07-29 10:49:06 +08:00
|
|
|
meshdata->vertex.resize(meshdata->vertexSizeInFloat);
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-08-20 10:24:46 +08:00
|
|
|
const rapidjson::Value& mesh_data_body_vertices = mesh_data_vertex_0[VERTICES];
|
2014-07-29 10:49:06 +08:00
|
|
|
for (rapidjson::SizeType i = 0; i < mesh_data_body_vertices.Size(); i++)
|
|
|
|
meshdata->vertex[i] = mesh_data_body_vertices[i].GetDouble();
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-07-29 10:49:06 +08:00
|
|
|
// submesh
|
2014-08-20 10:24:46 +08:00
|
|
|
const rapidjson::Value& mesh_submesh_array = mesh_array_0[SUBMESH];
|
2014-07-29 10:49:06 +08:00
|
|
|
for (rapidjson::SizeType i = 0; i < mesh_submesh_array.Size(); i++)
|
|
|
|
{
|
|
|
|
const rapidjson::Value& mesh_submesh_val = mesh_submesh_array[i];
|
|
|
|
//std::string id = mesh_submesh_val[ID].GetString();
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-07-29 10:49:06 +08:00
|
|
|
// index_number
|
2014-08-20 10:24:46 +08:00
|
|
|
unsigned int indexnum = mesh_submesh_val[INDEXNUM].GetUint();
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-07-29 10:49:06 +08:00
|
|
|
// indices
|
|
|
|
std::vector<unsigned short> indices;
|
|
|
|
indices.resize(indexnum);
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-08-20 10:24:46 +08:00
|
|
|
const rapidjson::Value& indices_val_array = mesh_submesh_val[INDICES];
|
2014-07-29 10:49:06 +08:00
|
|
|
for (rapidjson::SizeType j = 0; j < indices_val_array.Size(); j++)
|
|
|
|
indices[j] = (unsigned short)indices_val_array[j].GetUint();
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-07-29 10:49:06 +08:00
|
|
|
meshdata->subMeshIndices.push_back(indices);
|
2014-11-19 17:58:40 +08:00
|
|
|
meshdata->subMeshAABB.push_back(calculateAABB(meshdata->vertex, meshdata->getPerVertexSize(), indices));
|
2014-07-29 10:49:06 +08:00
|
|
|
}
|
2014-08-16 14:14:37 +08:00
|
|
|
meshdatas.meshDatas.push_back(meshdata);
|
2014-06-05 18:21:25 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-20 15:30:08 +08:00
|
|
|
bool Bundle3D::loadSkinDataJson(SkinData* skindata)
|
2014-06-05 18:21:25 +08:00
|
|
|
{
|
2014-08-20 10:24:46 +08:00
|
|
|
if (!_jsonReader.HasMember(SKIN )) return false;
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-08-20 10:24:46 +08:00
|
|
|
const rapidjson::Value& skin_data_array = _jsonReader[SKIN ];
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-08-21 13:30:51 +08:00
|
|
|
CCASSERT(skin_data_array.IsArray(), "skin data is not an array");
|
2014-06-12 10:01:54 +08:00
|
|
|
const rapidjson::Value& skin_data_array_val_0 = skin_data_array[(rapidjson::SizeType)0];
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-08-16 11:12:26 +08:00
|
|
|
if (!skin_data_array_val_0.HasMember(BONES))
|
2014-06-16 19:08:43 +08:00
|
|
|
return false;
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-08-16 11:12:26 +08:00
|
|
|
const rapidjson::Value& skin_data_bones = skin_data_array_val_0[BONES];
|
2014-06-12 10:01:54 +08:00
|
|
|
for (rapidjson::SizeType i = 0; i < skin_data_bones.Size(); i++)
|
|
|
|
{
|
|
|
|
const rapidjson::Value& skin_data_bone = skin_data_bones[i];
|
2014-08-16 11:12:26 +08:00
|
|
|
std::string name = skin_data_bone[NODE].GetString();
|
2014-06-24 14:10:03 +08:00
|
|
|
skindata->addSkinBoneNames(name);
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-06-12 10:01:54 +08:00
|
|
|
Mat4 mat_bind_pos;
|
2014-08-20 10:24:46 +08:00
|
|
|
const rapidjson::Value& bind_pos = skin_data_bone[BINDSHAPE];
|
2014-06-12 10:01:54 +08:00
|
|
|
for (rapidjson::SizeType j = 0; j < bind_pos.Size(); j++)
|
|
|
|
{
|
|
|
|
mat_bind_pos.m[j] = bind_pos[j].GetDouble();
|
|
|
|
}
|
2014-06-16 15:38:03 +08:00
|
|
|
skindata->inverseBindPoseMatrices.push_back(mat_bind_pos);
|
2014-06-12 10:01:54 +08:00
|
|
|
}
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-06-24 14:10:03 +08:00
|
|
|
// set root bone infomation
|
2014-06-16 19:08:43 +08:00
|
|
|
const rapidjson::Value& skin_data_1 = skin_data_array[1];
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-06-24 14:10:03 +08:00
|
|
|
// parent and child relationship map
|
2014-06-24 23:33:30 +08:00
|
|
|
skindata->skinBoneOriginMatrices.resize(skindata->skinBoneNames.size());
|
2014-06-24 14:10:03 +08:00
|
|
|
getChildMap(skindata->boneChild, skindata, skin_data_1);
|
2014-06-05 18:21:25 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-21 18:44:10 +08:00
|
|
|
|
|
|
|
bool Bundle3D::loadSkinDataBinary(SkinData* skindata)
|
|
|
|
{
|
|
|
|
if (!seekToFirstType(BUNDLE_TYPE_MESHSKIN))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
std::string boneName = _binaryReader.readString();
|
|
|
|
|
|
|
|
// transform
|
|
|
|
float bindShape[16];
|
|
|
|
if (!_binaryReader.readMatrix(bindShape))
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read SkinData: bindShape matrix '%s'.", _path.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// bone count
|
|
|
|
unsigned int boneNum;
|
|
|
|
if (!_binaryReader.read(&boneNum))
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read SkinData: boneNum '%s'.", _path.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// bone names and bind pos
|
|
|
|
float bindpos[16];
|
|
|
|
for (unsigned int i = 0; i < boneNum; i++)
|
|
|
|
{
|
|
|
|
std::string skinBoneName = _binaryReader.readString();
|
|
|
|
skindata->skinBoneNames.push_back(skinBoneName);
|
|
|
|
if (!_binaryReader.readMatrix(bindpos))
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to load SkinData: bindpos '%s'.", _path.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
skindata->inverseBindPoseMatrices.push_back(bindpos);
|
|
|
|
}
|
|
|
|
|
|
|
|
skindata->skinBoneOriginMatrices.resize(boneNum);
|
|
|
|
|
|
|
|
boneName = _binaryReader.readString();
|
|
|
|
|
|
|
|
// bind shape
|
|
|
|
_binaryReader.readMatrix(bindShape);
|
|
|
|
int rootIndex = skindata->getSkinBoneNameIndex(boneName);
|
|
|
|
if(rootIndex < 0)
|
|
|
|
{
|
|
|
|
skindata->addNodeBoneNames(boneName);
|
|
|
|
rootIndex = skindata->getBoneNameIndex(boneName);
|
|
|
|
skindata->nodeBoneOriginMatrices.push_back(bindShape);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
skindata->skinBoneOriginMatrices[rootIndex] = bindShape;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set root bone index
|
|
|
|
skindata->rootBoneIndex = rootIndex;
|
|
|
|
|
|
|
|
// read parent and child relationship map
|
|
|
|
float transform[16];
|
|
|
|
unsigned int linkNum;
|
|
|
|
_binaryReader.read(&linkNum);
|
|
|
|
for (unsigned int i = 0; i < linkNum; ++i)
|
|
|
|
{
|
|
|
|
std::string id = _binaryReader.readString();
|
|
|
|
int index = skindata->getSkinBoneNameIndex(id);
|
|
|
|
|
|
|
|
|
|
|
|
std::string parentid = _binaryReader.readString();
|
|
|
|
|
|
|
|
if (!_binaryReader.readMatrix(transform))
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to load SkinData: transform '%s'.", _path.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(index < 0)
|
|
|
|
{
|
|
|
|
skindata->addNodeBoneNames(id);
|
|
|
|
index = skindata->getBoneNameIndex(id);
|
|
|
|
skindata->nodeBoneOriginMatrices.push_back(transform);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
skindata->skinBoneOriginMatrices[index] = transform;
|
|
|
|
}
|
|
|
|
|
|
|
|
int parentIndex = skindata->getSkinBoneNameIndex(parentid);
|
|
|
|
if(parentIndex < 0)
|
|
|
|
{
|
|
|
|
skindata->addNodeBoneNames(parentid);
|
|
|
|
parentIndex = skindata->getBoneNameIndex(parentid);
|
|
|
|
}
|
|
|
|
|
|
|
|
skindata->boneChild[parentIndex].push_back(index);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-16 14:14:37 +08:00
|
|
|
bool Bundle3D::loadMaterialDataJson_0_1(MaterialDatas& materialdatas)
|
2014-06-05 18:21:25 +08:00
|
|
|
{
|
2014-08-20 10:24:46 +08:00
|
|
|
if (!_jsonReader.HasMember(MATERIAL))
|
2014-06-13 19:26:46 +08:00
|
|
|
return false;
|
2014-08-16 14:14:37 +08:00
|
|
|
NMaterialData materialData;
|
2014-08-20 10:24:46 +08:00
|
|
|
const rapidjson::Value& material_data_array = _jsonReader[MATERIAL];
|
2014-06-12 10:01:54 +08:00
|
|
|
|
2014-11-27 17:31:22 +08:00
|
|
|
if (material_data_array.Size() > 0)
|
|
|
|
{
|
|
|
|
const rapidjson::Value& material_data_array_0 = material_data_array[(rapidjson::SizeType)0];
|
|
|
|
if (material_data_array_0.HasMember(BASE))
|
|
|
|
{
|
|
|
|
const rapidjson::Value& material_data_base_array = material_data_array_0[BASE];
|
|
|
|
const rapidjson::Value& material_data_base_array_0 = material_data_base_array[(rapidjson::SizeType)0];
|
|
|
|
NTextureData textureData;
|
|
|
|
// set texture
|
2014-12-23 15:52:45 +08:00
|
|
|
std::string filename = material_data_base_array_0[FILENAME].GetString();
|
|
|
|
textureData.filename = filename.empty() ? filename : _modelPath + filename;
|
2014-11-27 17:31:22 +08:00
|
|
|
textureData.type= NTextureData::Usage::Diffuse;
|
|
|
|
textureData.id="";
|
|
|
|
materialData.textures.push_back(textureData);
|
|
|
|
materialdatas.materials.push_back(materialData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-29 10:49:06 +08:00
|
|
|
return true;
|
|
|
|
}
|
2014-06-13 19:26:46 +08:00
|
|
|
|
2014-08-16 14:14:37 +08:00
|
|
|
bool Bundle3D::loadMaterialDataJson_0_2(MaterialDatas& materialdatas)
|
2014-07-29 10:49:06 +08:00
|
|
|
{
|
2014-08-20 10:24:46 +08:00
|
|
|
if (!_jsonReader.HasMember(MATERIAL))
|
2014-07-29 10:49:06 +08:00
|
|
|
return false;
|
2014-08-18 17:31:04 +08:00
|
|
|
NMaterialData materialData;
|
2014-08-20 10:24:46 +08:00
|
|
|
const rapidjson::Value& material_array = _jsonReader[MATERIAL];
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-07-29 10:49:06 +08:00
|
|
|
for (rapidjson::SizeType i = 0; i < material_array.Size(); i++)
|
|
|
|
{
|
2014-08-16 14:14:37 +08:00
|
|
|
NTextureData textureData;
|
2014-07-29 10:49:06 +08:00
|
|
|
const rapidjson::Value& material_val = material_array[i];
|
2014-08-16 14:44:41 +08:00
|
|
|
|
2014-07-29 10:49:06 +08:00
|
|
|
// set texture
|
2014-12-23 15:52:45 +08:00
|
|
|
std::string filename = material_val[TEXTURES].GetString();
|
|
|
|
textureData.filename = filename.empty() ? filename : _modelPath + filename;
|
2014-08-16 14:14:37 +08:00
|
|
|
textureData.type= NTextureData::Usage::Diffuse;
|
|
|
|
textureData.id="";
|
|
|
|
materialData.textures.push_back(textureData);
|
2014-07-29 10:49:06 +08:00
|
|
|
}
|
2014-08-16 14:14:37 +08:00
|
|
|
materialdatas.materials.push_back(materialData);
|
2014-06-05 18:21:25 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-20 19:22:00 +08:00
|
|
|
bool Bundle3D::loadAnimationDataJson(const std::string& id, Animation3DData* animationdata)
|
2014-06-05 18:21:25 +08:00
|
|
|
{
|
2014-08-18 20:22:18 +08:00
|
|
|
std::string anim = "";
|
|
|
|
if (_version == "1.2" || _version == "0.2")
|
|
|
|
anim = ANIMATION;
|
|
|
|
else
|
|
|
|
anim = ANIMATIONS;
|
|
|
|
|
|
|
|
if (!_jsonReader.HasMember(anim.c_str())) return false;
|
2014-10-20 19:22:00 +08:00
|
|
|
int the_index = -1;
|
2014-08-18 20:22:18 +08:00
|
|
|
const rapidjson::Value& animation_data_array = _jsonReader[anim.c_str()];
|
2014-10-20 19:22:00 +08:00
|
|
|
|
2014-06-16 19:08:43 +08:00
|
|
|
if (animation_data_array.Size()==0) return false;
|
2014-06-12 16:28:36 +08:00
|
|
|
|
2014-10-20 19:22:00 +08:00
|
|
|
if(!id.empty())
|
|
|
|
{
|
|
|
|
for(int i=0 ;i<animation_data_array.Size();i++)
|
|
|
|
{
|
|
|
|
if(animation_data_array[i][ID].GetString() ==id )
|
|
|
|
{
|
|
|
|
the_index = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(the_index < 0) return false;
|
|
|
|
}else{
|
|
|
|
the_index = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const rapidjson::Value& animation_data_array_val_0 = animation_data_array[(rapidjson::SizeType)the_index];
|
2014-06-12 10:01:54 +08:00
|
|
|
|
2014-08-20 10:24:46 +08:00
|
|
|
animationdata->_totalTime = animation_data_array_val_0[LENGTH].GetDouble();
|
2014-06-16 15:38:03 +08:00
|
|
|
|
2014-08-16 11:12:26 +08:00
|
|
|
const rapidjson::Value& bones = animation_data_array_val_0[BONES];
|
2014-06-16 19:08:43 +08:00
|
|
|
for (rapidjson::SizeType i = 0; i < bones.Size(); i++)
|
2014-06-12 10:01:54 +08:00
|
|
|
{
|
|
|
|
const rapidjson::Value& bone = bones[i];
|
2014-08-20 10:24:46 +08:00
|
|
|
std::string bone_name = bone[BONEID].GetString();
|
2014-06-12 10:01:54 +08:00
|
|
|
|
2014-08-20 10:24:46 +08:00
|
|
|
if ( bone.HasMember(KEYFRAMES))
|
2014-06-12 10:01:54 +08:00
|
|
|
{
|
2014-08-20 10:24:46 +08:00
|
|
|
const rapidjson::Value& bone_keyframes = bone[KEYFRAMES];
|
2014-06-13 19:26:46 +08:00
|
|
|
rapidjson::SizeType keyframe_size = bone_keyframes.Size();
|
2014-08-25 11:30:34 +08:00
|
|
|
animationdata->_rotationKeys[bone_name].reserve(keyframe_size);
|
|
|
|
animationdata->_scaleKeys[bone_name].reserve(keyframe_size);
|
|
|
|
animationdata->_translationKeys[bone_name].reserve(keyframe_size);
|
|
|
|
|
2014-06-24 14:10:03 +08:00
|
|
|
for (rapidjson::SizeType j = 0; j < keyframe_size; j++)
|
2014-06-12 10:01:54 +08:00
|
|
|
{
|
|
|
|
const rapidjson::Value& bone_keyframe = bone_keyframes[j];
|
2014-06-12 16:28:36 +08:00
|
|
|
|
2014-08-20 10:24:46 +08:00
|
|
|
if ( bone_keyframe.HasMember(TRANSLATION))
|
2014-06-13 19:26:46 +08:00
|
|
|
{
|
2014-08-20 10:24:46 +08:00
|
|
|
const rapidjson::Value& bone_keyframe_translation = bone_keyframe[TRANSLATION];
|
|
|
|
float keytime = bone_keyframe[KEYTIME].GetDouble();
|
2014-06-13 19:26:46 +08:00
|
|
|
Vec3 val = Vec3(bone_keyframe_translation[(rapidjson::SizeType)0].GetDouble(), bone_keyframe_translation[1].GetDouble(), bone_keyframe_translation[2].GetDouble());
|
|
|
|
animationdata->_translationKeys[bone_name].push_back(Animation3DData::Vec3Key(keytime,val));
|
2014-06-12 10:01:54 +08:00
|
|
|
}
|
|
|
|
|
2014-08-20 10:24:46 +08:00
|
|
|
if ( bone_keyframe.HasMember(ROTATION))
|
2014-06-12 10:01:54 +08:00
|
|
|
{
|
2014-08-20 10:24:46 +08:00
|
|
|
const rapidjson::Value& bone_keyframe_rotation = bone_keyframe[ROTATION];
|
|
|
|
float keytime = bone_keyframe[KEYTIME].GetDouble();
|
2014-06-13 19:26:46 +08:00
|
|
|
Quaternion val = Quaternion(bone_keyframe_rotation[(rapidjson::SizeType)0].GetDouble(),bone_keyframe_rotation[1].GetDouble(),bone_keyframe_rotation[2].GetDouble(),bone_keyframe_rotation[3].GetDouble());
|
|
|
|
animationdata->_rotationKeys[bone_name].push_back(Animation3DData::QuatKey(keytime,val));
|
2014-06-12 10:01:54 +08:00
|
|
|
}
|
|
|
|
|
2014-08-20 10:24:46 +08:00
|
|
|
if ( bone_keyframe.HasMember(SCALE))
|
2014-06-12 10:01:54 +08:00
|
|
|
{
|
2014-08-20 10:24:46 +08:00
|
|
|
const rapidjson::Value& bone_keyframe_scale = bone_keyframe[SCALE];
|
|
|
|
float keytime = bone_keyframe[KEYTIME].GetDouble();
|
2014-06-13 19:26:46 +08:00
|
|
|
Vec3 val = Vec3(bone_keyframe_scale[(rapidjson::SizeType)0].GetDouble(), bone_keyframe_scale[1].GetDouble(), bone_keyframe_scale[2].GetDouble());
|
|
|
|
animationdata->_scaleKeys[bone_name].push_back(Animation3DData::Vec3Key(keytime,val));
|
2014-06-12 10:01:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-06-05 18:21:25 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-20 19:22:00 +08:00
|
|
|
bool Bundle3D::loadAnimationDataBinary(const std::string& id, Animation3DData* animationdata)
|
2014-06-12 10:01:54 +08:00
|
|
|
{
|
2014-11-18 16:10:33 +08:00
|
|
|
|
|
|
|
if( _version == "0.1"|| _version == "0.2" || _version == "0.3"|| _version == "0.4")
|
|
|
|
{
|
|
|
|
if (!seekToFirstType(BUNDLE_TYPE_ANIMATIONS))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// if id is not a null string, we need to add a suffix of "animation" for seeding.
|
|
|
|
std::string id_ = id;
|
|
|
|
if(id != "") id_ = id + "animation";
|
|
|
|
|
|
|
|
if (!seekToFirstType(BUNDLE_TYPE_ANIMATIONS, id_))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-21 11:59:03 +08:00
|
|
|
unsigned int animNum = 1;
|
2014-11-19 17:59:35 +08:00
|
|
|
if( _version == "0.3"|| _version == "0.4")
|
2014-08-18 17:31:04 +08:00
|
|
|
{
|
|
|
|
if (!_binaryReader.read(&animNum))
|
|
|
|
{
|
2014-08-29 11:56:33 +08:00
|
|
|
CCLOG("warning: Failed to read AnimationData: animNum '%s'.", _path.c_str());
|
2014-08-18 17:31:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-10-21 11:59:03 +08:00
|
|
|
|
2014-10-21 11:43:28 +08:00
|
|
|
bool has_found =false;
|
2014-10-21 11:59:03 +08:00
|
|
|
for(unsigned int k = 0; k < animNum ; k++ )
|
2014-06-12 10:01:54 +08:00
|
|
|
{
|
2014-10-20 19:22:00 +08:00
|
|
|
animationdata->resetData();
|
|
|
|
std::string animId = _binaryReader.readString();
|
2014-06-20 15:30:08 +08:00
|
|
|
|
2014-10-20 19:22:00 +08:00
|
|
|
if (!_binaryReader.read(&animationdata->_totalTime))
|
2014-06-20 15:30:08 +08:00
|
|
|
{
|
2014-10-20 19:22:00 +08:00
|
|
|
CCLOG("warning: Failed to read AnimationData: totalTime '%s'.", _path.c_str());
|
2014-06-20 15:30:08 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-20 19:22:00 +08:00
|
|
|
unsigned int nodeAnimationNum;
|
|
|
|
if (!_binaryReader.read(&nodeAnimationNum))
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read AnimationData: animNum '%s'.", _path.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (unsigned int i = 0; i < nodeAnimationNum; ++i)
|
2014-06-20 15:30:08 +08:00
|
|
|
{
|
2014-10-20 19:22:00 +08:00
|
|
|
std::string boneName = _binaryReader.readString();
|
|
|
|
unsigned int keyframeNum;
|
|
|
|
if (!_binaryReader.read(&keyframeNum))
|
2014-06-20 15:30:08 +08:00
|
|
|
{
|
2014-10-20 19:22:00 +08:00
|
|
|
CCLOG("warning: Failed to read AnimationData: keyframeNum '%s'.", _path.c_str());
|
2014-06-20 15:30:08 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-20 19:22:00 +08:00
|
|
|
animationdata->_rotationKeys[boneName].reserve(keyframeNum);
|
|
|
|
animationdata->_scaleKeys[boneName].reserve(keyframeNum);
|
|
|
|
animationdata->_translationKeys[boneName].reserve(keyframeNum);
|
|
|
|
|
|
|
|
for (unsigned int j = 0; j < keyframeNum; ++j)
|
2014-06-20 15:30:08 +08:00
|
|
|
{
|
2014-10-20 19:22:00 +08:00
|
|
|
float keytime;
|
|
|
|
if (!_binaryReader.read(&keytime))
|
2014-08-27 18:44:31 +08:00
|
|
|
{
|
2014-10-20 19:22:00 +08:00
|
|
|
CCLOG("warning: Failed to read AnimationData: keytime '%s'.", _path.c_str());
|
2014-08-27 18:44:31 +08:00
|
|
|
return false;
|
|
|
|
}
|
2014-10-20 19:22:00 +08:00
|
|
|
|
|
|
|
// transform flag
|
|
|
|
unsigned char transformFlag(0);
|
2014-11-18 16:10:33 +08:00
|
|
|
if (_version != "0.1" && _version != "0.2" && _version != "0.3")
|
2014-08-27 18:44:31 +08:00
|
|
|
{
|
2014-10-20 19:22:00 +08:00
|
|
|
if (!_binaryReader.read(&transformFlag))
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read AnimationData: transformFlag '%s'.", _path.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
2014-08-27 18:44:31 +08:00
|
|
|
}
|
2014-06-20 15:30:08 +08:00
|
|
|
|
2014-10-20 19:22:00 +08:00
|
|
|
// rotation
|
|
|
|
bool hasRotate = true;
|
2014-11-18 16:10:33 +08:00
|
|
|
if (_version != "0.1" && _version != "0.2" && _version != "0.3")
|
2014-10-20 19:22:00 +08:00
|
|
|
hasRotate = transformFlag & 0x01;
|
|
|
|
|
|
|
|
if (hasRotate)
|
2014-08-27 18:44:31 +08:00
|
|
|
{
|
2014-10-20 19:22:00 +08:00
|
|
|
Quaternion rotate;
|
|
|
|
if (_binaryReader.read(&rotate, 4, 4) != 4)
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read AnimationData: rotate '%s'.", _path.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
animationdata->_rotationKeys[boneName].push_back(Animation3DData::QuatKey(keytime, rotate));
|
2014-08-27 18:44:31 +08:00
|
|
|
}
|
2014-10-20 19:22:00 +08:00
|
|
|
|
|
|
|
// scale
|
|
|
|
bool hasScale = true;
|
2014-11-18 16:10:33 +08:00
|
|
|
if (_version != "0.1" && _version != "0.2" && _version != "0.3")
|
2014-10-20 19:22:00 +08:00
|
|
|
hasScale = (transformFlag >> 1) & 0x01;
|
|
|
|
|
|
|
|
if (hasScale)
|
2014-08-27 18:44:31 +08:00
|
|
|
{
|
2014-10-20 19:22:00 +08:00
|
|
|
Vec3 scale;
|
|
|
|
if (_binaryReader.read(&scale, 4, 3) != 3)
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read AnimationData: scale '%s'.", _path.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
animationdata->_scaleKeys[boneName].push_back(Animation3DData::Vec3Key(keytime, scale));
|
|
|
|
}
|
|
|
|
|
|
|
|
// translation
|
|
|
|
bool hasTranslation = true;
|
2014-11-18 16:10:33 +08:00
|
|
|
if (_version != "0.1" && _version != "0.2" && _version != "0.3")
|
2014-10-20 19:22:00 +08:00
|
|
|
hasTranslation = (transformFlag >> 2) & 0x01;
|
|
|
|
|
|
|
|
if (hasTranslation)
|
|
|
|
{
|
|
|
|
Vec3 position;
|
|
|
|
if (_binaryReader.read(&position, 4, 3) != 3)
|
|
|
|
{
|
|
|
|
CCLOG("warning: Failed to read AnimationData: position '%s'.", _path.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
animationdata->_translationKeys[boneName].push_back(Animation3DData::Vec3Key(keytime, position));
|
2014-08-27 18:44:31 +08:00
|
|
|
}
|
2014-06-20 15:30:08 +08:00
|
|
|
}
|
2014-10-20 19:22:00 +08:00
|
|
|
|
2014-06-20 15:30:08 +08:00
|
|
|
}
|
2014-10-20 19:22:00 +08:00
|
|
|
if( id == animId || id.empty())
|
|
|
|
{
|
2014-10-21 11:43:28 +08:00
|
|
|
has_found = true;
|
2014-10-20 19:22:00 +08:00
|
|
|
break;
|
|
|
|
}
|
2014-06-12 10:01:54 +08:00
|
|
|
}
|
2014-10-21 11:59:03 +08:00
|
|
|
if(!has_found)
|
2014-10-21 11:43:28 +08:00
|
|
|
{
|
2014-10-21 11:59:03 +08:00
|
|
|
animationdata->resetData();
|
2014-10-21 11:43:28 +08:00
|
|
|
return false;
|
|
|
|
}
|
2014-06-20 15:30:08 +08:00
|
|
|
return true;
|
2014-06-12 10:01:54 +08:00
|
|
|
}
|
|
|
|
|
2014-10-20 19:22:00 +08:00
|
|
|
|
2014-08-16 11:12:26 +08:00
|
|
|
bool Bundle3D::loadNodesJson(NodeDatas& nodedatas)
|
|
|
|
{
|
|
|
|
if (!_jsonReader.HasMember(NODES)) return false;
|
|
|
|
const rapidjson::Value& nodes = _jsonReader[NODES];
|
|
|
|
if(!nodes.IsArray()) return false;
|
|
|
|
|
2014-08-16 14:28:15 +08:00
|
|
|
// traverse the nodes again
|
2014-08-16 11:12:26 +08:00
|
|
|
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);
|
|
|
|
|
2014-08-21 10:38:33 +08:00
|
|
|
bool isSkeleton = jnode[SKELETON].GetBool();
|
2014-08-18 17:31:04 +08:00
|
|
|
if (isSkeleton)
|
2014-08-16 11:12:26 +08:00
|
|
|
nodedatas.skeleton.push_back(nodedata);
|
|
|
|
else
|
|
|
|
nodedatas.nodes.push_back(nodedata);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
NodeData* Bundle3D::parseNodesRecursivelyJson(const rapidjson::Value& jvalue)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
NodeData* nodedata = new (std::nothrow) NodeData();;
|
2014-08-16 11:12:26 +08:00
|
|
|
// 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];
|
2014-08-20 11:40:04 +08:00
|
|
|
|
2014-08-16 11:12:26 +08:00
|
|
|
|
|
|
|
for (rapidjson::SizeType i = 0; i < parts.Size(); i++)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
auto modelnodedata = new (std::nothrow) ModelData();;
|
2014-08-16 11:12:26 +08:00
|
|
|
const rapidjson::Value& part = parts[i];
|
2014-08-16 18:19:41 +08:00
|
|
|
modelnodedata->subMeshId = part[MESHPARTID].GetString();
|
|
|
|
modelnodedata->matrialId = part[MATERIALID].GetString();
|
2014-08-16 11:12:26 +08:00
|
|
|
|
2014-08-16 18:19:41 +08:00
|
|
|
if (modelnodedata->subMeshId == "" || modelnodedata->matrialId == "")
|
2014-08-16 11:12:26 +08:00
|
|
|
{
|
2014-08-29 10:43:26 +08:00
|
|
|
CCLOG("warning: Node %s part is missing meshPartId or materialId", nodedata->id.c_str());
|
2014-08-16 11:12:26 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (part.HasMember(BONES))
|
|
|
|
{
|
|
|
|
const rapidjson::Value& bones = part[BONES];
|
|
|
|
|
|
|
|
for (rapidjson::SizeType j = 0; j < bones.Size(); j++)
|
|
|
|
{
|
2014-08-16 19:51:59 +08:00
|
|
|
const rapidjson::Value& bone = bones[j];
|
2014-08-16 11:12:26 +08:00
|
|
|
|
|
|
|
// node
|
|
|
|
if (!bone.HasMember(NODE))
|
|
|
|
{
|
2014-08-29 10:43:26 +08:00
|
|
|
CCLOG("warning: Bone node ID missing");
|
2014-08-16 11:12:26 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
modelnodedata->bones.push_back(bone[NODE].GetString());
|
|
|
|
|
|
|
|
Mat4 invbindpos;
|
2014-08-20 10:24:46 +08:00
|
|
|
const rapidjson::Value& jinvbindpos = bone[TRANSFORM];
|
2014-08-16 11:12:26 +08:00
|
|
|
|
2014-08-20 10:24:46 +08:00
|
|
|
for (rapidjson::SizeType k = 0; k < jinvbindpos.Size(); k++)
|
2014-08-16 11:12:26 +08:00
|
|
|
{
|
2014-08-20 10:24:46 +08:00
|
|
|
invbindpos.m[k] = jinvbindpos[k].GetDouble();
|
2014-08-16 11:12:26 +08:00
|
|
|
}
|
|
|
|
|
2014-08-16 18:19:41 +08:00
|
|
|
//invbindpos.inverse();
|
2014-08-16 11:12:26 +08:00
|
|
|
modelnodedata->invBindPose.push_back(invbindpos);
|
|
|
|
}
|
|
|
|
}
|
2014-08-20 11:40:04 +08:00
|
|
|
nodedata->modelNodeDatas.push_back(modelnodedata);
|
2014-08-16 11:12:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-08-16 14:28:15 +08:00
|
|
|
bool Bundle3D::loadNodesBinary(NodeDatas& nodedatas)
|
|
|
|
{
|
|
|
|
if (!seekToFirstType(BUNDLE_TYPE_NODE))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
unsigned int nodeSize = 0;
|
|
|
|
if (_binaryReader.read(&nodeSize, 4, 1) != 1)
|
|
|
|
{
|
2014-08-29 11:56:33 +08:00
|
|
|
CCLOG("warning: Failed to read nodes");
|
2014-08-16 14:28:15 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// traverse the nodes again
|
|
|
|
for (rapidjson::SizeType i = 0; i < nodeSize; i++)
|
|
|
|
{
|
2014-08-18 17:31:04 +08:00
|
|
|
bool skeleton = false;
|
|
|
|
NodeData* nodedata = parseNodesRecursivelyBinary(skeleton);
|
2014-08-16 14:28:15 +08:00
|
|
|
|
2014-08-18 17:31:04 +08:00
|
|
|
if (skeleton)
|
2014-08-16 14:28:15 +08:00
|
|
|
nodedatas.skeleton.push_back(nodedata);
|
|
|
|
else
|
|
|
|
nodedatas.nodes.push_back(nodedata);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2014-08-18 17:31:04 +08:00
|
|
|
NodeData* Bundle3D::parseNodesRecursivelyBinary(bool& skeleton)
|
2014-08-16 14:28:15 +08:00
|
|
|
{
|
2014-08-18 17:31:04 +08:00
|
|
|
// id
|
|
|
|
std::string id = _binaryReader.readString();
|
2014-08-21 10:38:33 +08:00
|
|
|
// is skeleton
|
|
|
|
bool skeleton_;
|
|
|
|
if (_binaryReader.read(&skeleton_, 1, 1) != 1)
|
2014-08-16 14:28:15 +08:00
|
|
|
{
|
2014-08-29 10:43:26 +08:00
|
|
|
CCLOG("warning: Failed to read is sleleton");
|
2014-08-18 17:52:27 +08:00
|
|
|
return nullptr;
|
2014-08-16 14:28:15 +08:00
|
|
|
}
|
2014-08-21 10:38:33 +08:00
|
|
|
if (skeleton_)
|
|
|
|
skeleton = true;
|
|
|
|
|
2014-08-18 17:31:04 +08:00
|
|
|
// transform
|
|
|
|
Mat4 transform;
|
|
|
|
if (!_binaryReader.readMatrix(transform.m))
|
2014-08-16 14:28:15 +08:00
|
|
|
{
|
2014-08-29 10:43:26 +08:00
|
|
|
CCLOG("warning: Failed to read transform matrix");
|
2014-08-18 17:52:27 +08:00
|
|
|
return nullptr;
|
2014-08-16 14:28:15 +08:00
|
|
|
}
|
2014-08-18 17:31:04 +08:00
|
|
|
// parts
|
|
|
|
unsigned int partsSize = 0;
|
2014-08-16 14:28:15 +08:00
|
|
|
if (_binaryReader.read(&partsSize, 4, 1) != 1)
|
|
|
|
{
|
2014-08-29 10:43:26 +08:00
|
|
|
CCLOG("warning: Failed to read meshdata: attribCount '%s'.", _path.c_str());
|
2014-08-18 17:52:27 +08:00
|
|
|
return nullptr;
|
2014-08-16 14:28:15 +08:00
|
|
|
}
|
|
|
|
|
2014-08-28 07:31:57 +08:00
|
|
|
NodeData* nodedata = new (std::nothrow) NodeData();
|
2014-08-20 11:40:04 +08:00
|
|
|
nodedata->id = id;
|
|
|
|
nodedata->transform = transform;
|
2014-08-16 14:28:15 +08:00
|
|
|
if (partsSize > 0)
|
|
|
|
{
|
2014-08-29 10:43:26 +08:00
|
|
|
for (unsigned int i = 0; i < partsSize; i++)
|
2014-08-16 14:28:15 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
auto modelnodedata = new (std::nothrow) ModelData();
|
2014-08-18 17:31:04 +08:00
|
|
|
modelnodedata->subMeshId = _binaryReader.readString();
|
|
|
|
modelnodedata->matrialId = _binaryReader.readString();
|
|
|
|
|
|
|
|
if (modelnodedata->subMeshId == "" || modelnodedata->matrialId == "")
|
2014-08-16 14:28:15 +08:00
|
|
|
{
|
2014-08-18 17:31:04 +08:00
|
|
|
std::string err = "Node " + nodedata->id + " part is missing meshPartId or materialId";
|
2014-08-29 11:56:33 +08:00
|
|
|
CCLOG("Node %s part is missing meshPartId or materialId", nodedata->id.c_str());
|
2014-08-18 17:31:04 +08:00
|
|
|
return nullptr;
|
2014-08-16 14:28:15 +08:00
|
|
|
}
|
|
|
|
|
2014-08-18 17:31:04 +08:00
|
|
|
// read bone
|
|
|
|
unsigned int bonesSize = 0;
|
|
|
|
if (_binaryReader.read(&bonesSize, 4, 1) != 1)
|
2014-08-16 14:28:15 +08:00
|
|
|
{
|
2014-08-29 11:56:33 +08:00
|
|
|
CCLOG("warning: Failed to read meshdata: attribCount '%s'.", _path.c_str());
|
2014-08-18 17:52:27 +08:00
|
|
|
return nullptr;
|
2014-08-18 17:31:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (bonesSize > 0)
|
|
|
|
{
|
2014-08-29 10:43:26 +08:00
|
|
|
for (unsigned int j = 0; j < bonesSize; j++)
|
2014-08-16 14:28:15 +08:00
|
|
|
{
|
2014-08-18 17:31:04 +08:00
|
|
|
std::string name = _binaryReader.readString();
|
|
|
|
modelnodedata->bones.push_back(name);
|
2014-08-16 14:28:15 +08:00
|
|
|
|
2014-08-18 17:31:04 +08:00
|
|
|
Mat4 invbindpos;
|
|
|
|
if (!_binaryReader.readMatrix(invbindpos.m))
|
|
|
|
{
|
2014-08-18 17:52:27 +08:00
|
|
|
return nullptr;
|
2014-08-18 17:31:04 +08:00
|
|
|
}
|
2014-08-16 14:28:15 +08:00
|
|
|
|
2014-08-18 17:31:04 +08:00
|
|
|
modelnodedata->invBindPose.push_back(invbindpos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unsigned int uvMapping = 0;
|
|
|
|
if (_binaryReader.read(&uvMapping, 4, 1) != 1)
|
|
|
|
{
|
2014-08-29 11:56:33 +08:00
|
|
|
CCLOG("warning: Failed to read nodedata: uvMapping '%s'.", _path.c_str());
|
2014-08-18 17:52:27 +08:00
|
|
|
return nullptr;
|
2014-08-18 17:31:04 +08:00
|
|
|
}
|
2014-08-20 10:24:46 +08:00
|
|
|
for( int j = 0 ;j < uvMapping ; j++ )
|
2014-08-18 17:31:04 +08:00
|
|
|
{
|
|
|
|
unsigned int textureIndexSize=0;
|
|
|
|
if (_binaryReader.read(&textureIndexSize, 4, 1) != 1)
|
|
|
|
{
|
2014-08-29 11:56:33 +08:00
|
|
|
CCLOG("warning: Failed to read meshdata: attribCount '%s'.", _path.c_str());
|
2014-08-18 17:52:27 +08:00
|
|
|
return nullptr;
|
2014-08-18 17:31:04 +08:00
|
|
|
}
|
2014-08-20 10:24:46 +08:00
|
|
|
for(int k =0; k < textureIndexSize ; k++ )
|
2014-08-18 17:31:04 +08:00
|
|
|
{
|
|
|
|
unsigned int index=0;
|
|
|
|
if (_binaryReader.read(&index, 4, 1) != 1)
|
2014-08-16 14:28:15 +08:00
|
|
|
{
|
2014-08-18 17:52:27 +08:00
|
|
|
return nullptr;
|
2014-08-16 14:28:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-20 11:40:04 +08:00
|
|
|
nodedata->modelNodeDatas.push_back(modelnodedata);
|
2014-08-16 14:28:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int childrenSize = 0;
|
|
|
|
if (_binaryReader.read(&childrenSize, 4, 1) != 1)
|
|
|
|
{
|
2014-08-29 11:56:33 +08:00
|
|
|
CCLOG("warning: Failed to read meshdata: attribCount '%s'.", _path.c_str());
|
2014-08-18 17:52:27 +08:00
|
|
|
return nullptr;
|
2014-08-16 14:28:15 +08:00
|
|
|
}
|
|
|
|
if (childrenSize > 0)
|
|
|
|
{
|
2014-08-29 10:43:26 +08:00
|
|
|
for (unsigned int i = 0; i < childrenSize; i++)
|
2014-08-16 14:28:15 +08:00
|
|
|
{
|
2014-08-18 17:31:04 +08:00
|
|
|
NodeData* tempdata = parseNodesRecursivelyBinary(skeleton);
|
|
|
|
nodedata->children.push_back(tempdata);
|
2014-08-16 14:28:15 +08:00
|
|
|
}
|
|
|
|
}
|
2014-08-18 17:31:04 +08:00
|
|
|
return nodedata;
|
2014-08-16 14:28:15 +08:00
|
|
|
}
|
|
|
|
|
2014-06-20 15:30:08 +08:00
|
|
|
GLenum Bundle3D::parseGLType(const std::string& str)
|
2014-06-12 10:01:54 +08:00
|
|
|
{
|
2014-07-07 17:22:46 +08:00
|
|
|
if (str == "GL_BYTE")
|
2014-06-12 10:01:54 +08:00
|
|
|
{
|
2014-07-07 09:16:08 +08:00
|
|
|
return GL_BYTE;
|
|
|
|
}
|
2014-07-07 17:22:46 +08:00
|
|
|
else if(str == "GL_UNSIGNED_BYTE")
|
2014-07-07 09:16:08 +08:00
|
|
|
{
|
|
|
|
return GL_UNSIGNED_BYTE;
|
|
|
|
}
|
2014-07-07 17:22:46 +08:00
|
|
|
else if(str == "GL_SHORT")
|
2014-07-07 09:16:08 +08:00
|
|
|
{
|
|
|
|
return GL_SHORT;
|
|
|
|
}
|
2014-07-07 17:22:46 +08:00
|
|
|
else if(str == "GL_UNSIGNED_SHORT")
|
2014-07-07 09:16:08 +08:00
|
|
|
{
|
|
|
|
return GL_UNSIGNED_SHORT;
|
|
|
|
}
|
2014-07-07 17:22:46 +08:00
|
|
|
else if(str == "GL_INT")
|
2014-07-07 09:16:08 +08:00
|
|
|
{
|
|
|
|
return GL_INT;
|
|
|
|
}
|
2014-07-07 17:22:46 +08:00
|
|
|
else if (str == "GL_UNSIGNED_INT")
|
2014-07-07 09:16:08 +08:00
|
|
|
{
|
|
|
|
return GL_UNSIGNED_INT;
|
2014-06-12 10:01:54 +08:00
|
|
|
}
|
2014-07-07 09:16:08 +08:00
|
|
|
else if (str == "GL_FLOAT")
|
|
|
|
{
|
|
|
|
return GL_FLOAT;
|
|
|
|
}
|
2014-08-16 15:40:00 +08:00
|
|
|
else if (str == "REPEAT")
|
|
|
|
{
|
|
|
|
return GL_REPEAT;
|
|
|
|
}
|
|
|
|
else if (str == "CLAMP")
|
|
|
|
{
|
|
|
|
return GL_CLAMP_TO_EDGE;
|
|
|
|
}
|
2014-06-12 10:01:54 +08:00
|
|
|
else
|
|
|
|
{
|
2014-08-29 10:43:26 +08:00
|
|
|
CCASSERT(false, "Invalid GL type");
|
2014-06-20 15:30:08 +08:00
|
|
|
return 0;
|
2014-06-12 10:01:54 +08:00
|
|
|
}
|
|
|
|
}
|
2014-08-16 10:41:42 +08:00
|
|
|
NTextureData::Usage Bundle3D::parseGLTextureType(const std::string& str)
|
|
|
|
{
|
|
|
|
if (str == "AMBIENT")
|
|
|
|
{
|
|
|
|
return NTextureData::Usage::Ambient;
|
|
|
|
}
|
|
|
|
else if(str == "BUMP")
|
|
|
|
{
|
|
|
|
return NTextureData::Usage::Bump;
|
|
|
|
}
|
|
|
|
else if(str == "DIFFUSE")
|
|
|
|
{
|
|
|
|
return NTextureData::Usage::Diffuse;
|
|
|
|
}
|
|
|
|
else if(str == "EMISSIVE")
|
|
|
|
{
|
|
|
|
return NTextureData::Usage::Emissive;
|
|
|
|
}
|
|
|
|
else if(str == "NONE")
|
|
|
|
{
|
|
|
|
return NTextureData::Usage::None;
|
|
|
|
}
|
|
|
|
else if (str == "NORMAL")
|
|
|
|
{
|
2014-08-18 17:31:04 +08:00
|
|
|
return NTextureData::Usage::Normal;
|
2014-08-16 10:41:42 +08:00
|
|
|
}
|
|
|
|
else if (str == "REFLECTION")
|
|
|
|
{
|
|
|
|
return NTextureData::Usage::Reflection;
|
|
|
|
}
|
|
|
|
else if (str == "SHININESS")
|
|
|
|
{
|
2014-08-18 17:31:04 +08:00
|
|
|
return NTextureData::Usage::Shininess;
|
2014-08-16 10:41:42 +08:00
|
|
|
}
|
|
|
|
else if (str == "SPECULAR")
|
|
|
|
{
|
|
|
|
return NTextureData::Usage::Specular;
|
|
|
|
}
|
|
|
|
else if (str == "TRANSPARENCY")
|
|
|
|
{
|
2014-08-18 17:31:04 +08:00
|
|
|
return NTextureData::Usage::Transparency;
|
2014-08-16 10:41:42 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-21 13:30:51 +08:00
|
|
|
CCASSERT(false, "Wrong Texture type");
|
2014-08-16 10:41:42 +08:00
|
|
|
return NTextureData::Usage::Unknown;
|
|
|
|
}
|
|
|
|
}
|
2014-06-12 10:01:54 +08:00
|
|
|
unsigned int Bundle3D::parseGLProgramAttribute(const std::string& str)
|
|
|
|
{
|
|
|
|
if (str == "VERTEX_ATTRIB_POSITION")
|
|
|
|
{
|
|
|
|
return GLProgram::VERTEX_ATTRIB_POSITION;
|
|
|
|
}
|
|
|
|
else if (str == "VERTEX_ATTRIB_COLOR")
|
|
|
|
{
|
|
|
|
return GLProgram::VERTEX_ATTRIB_COLOR;
|
|
|
|
}
|
|
|
|
else if (str == "VERTEX_ATTRIB_TEX_COORD")
|
|
|
|
{
|
|
|
|
return GLProgram::VERTEX_ATTRIB_TEX_COORD;
|
|
|
|
}
|
2014-08-18 20:22:18 +08:00
|
|
|
else if (str == "VERTEX_ATTRIB_TEX_COORD1")
|
|
|
|
{
|
|
|
|
return GLProgram::VERTEX_ATTRIB_TEX_COORD1;
|
|
|
|
}
|
2014-08-21 18:14:24 +08:00
|
|
|
else if (str == "VERTEX_ATTRIB_TEX_COORD2")
|
|
|
|
{
|
|
|
|
return GLProgram::VERTEX_ATTRIB_TEX_COORD2;
|
|
|
|
}
|
|
|
|
else if (str == "VERTEX_ATTRIB_TEX_COORD3")
|
|
|
|
{
|
|
|
|
return GLProgram::VERTEX_ATTRIB_TEX_COORD3;
|
|
|
|
}
|
2014-10-10 14:06:37 +08:00
|
|
|
//comment out them
|
|
|
|
// else if (str == "VERTEX_ATTRIB_TEX_COORD4")
|
|
|
|
// {
|
|
|
|
// return GLProgram::VERTEX_ATTRIB_TEX_COORD4;
|
|
|
|
// }
|
|
|
|
// else if (str == "VERTEX_ATTRIB_TEX_COORD5")
|
|
|
|
// {
|
|
|
|
// return GLProgram::VERTEX_ATTRIB_TEX_COORD5;
|
|
|
|
// }
|
|
|
|
// else if (str == "VERTEX_ATTRIB_TEX_COORD6")
|
|
|
|
// {
|
|
|
|
// return GLProgram::VERTEX_ATTRIB_TEX_COORD6;
|
|
|
|
// }
|
|
|
|
// else if (str == "VERTEX_ATTRIB_TEX_COORD7")
|
|
|
|
// {
|
|
|
|
// return GLProgram::VERTEX_ATTRIB_TEX_COORD7;
|
|
|
|
// }
|
2014-06-12 10:01:54 +08:00
|
|
|
else if (str == "VERTEX_ATTRIB_NORMAL")
|
|
|
|
{
|
|
|
|
return GLProgram::VERTEX_ATTRIB_NORMAL;
|
|
|
|
}
|
|
|
|
else if (str == "VERTEX_ATTRIB_BLEND_WEIGHT")
|
|
|
|
{
|
|
|
|
return GLProgram::VERTEX_ATTRIB_BLEND_WEIGHT;
|
|
|
|
}
|
|
|
|
else if (str == "VERTEX_ATTRIB_BLEND_INDEX")
|
|
|
|
{
|
|
|
|
return GLProgram::VERTEX_ATTRIB_BLEND_INDEX;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-21 13:30:51 +08:00
|
|
|
CCASSERT(false, "Wrong Attribute type");
|
2014-06-12 10:01:54 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-20 15:30:08 +08:00
|
|
|
void Bundle3D::getModelRelativePath(const std::string& path)
|
2014-06-16 19:08:43 +08:00
|
|
|
{
|
2014-06-30 17:10:07 +08:00
|
|
|
ssize_t index = path.find_last_of('/');
|
2014-06-16 19:08:43 +08:00
|
|
|
std::string fullModelPath;
|
2014-08-16 11:12:26 +08:00
|
|
|
_modelPath = path.substr(0, index + 1);
|
2014-06-16 19:08:43 +08:00
|
|
|
}
|
|
|
|
|
2014-11-18 16:10:33 +08:00
|
|
|
Reference* Bundle3D::seekToFirstType(unsigned int type, const std::string& id)
|
2014-06-19 15:43:02 +08:00
|
|
|
{
|
|
|
|
// for each Reference
|
|
|
|
for (unsigned int i = 0; i < _referenceCount; ++i)
|
|
|
|
{
|
|
|
|
Reference* ref = &_references[i];
|
|
|
|
if (ref->type == type)
|
|
|
|
{
|
2014-11-18 16:10:33 +08:00
|
|
|
// if id is not a null string, we also need to check the Reference's id.
|
|
|
|
if (id != "" && id != ref->id)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-06-19 15:43:02 +08:00
|
|
|
// Found a match
|
2014-06-25 09:13:24 +08:00
|
|
|
if (_binaryReader.seek(ref->offset, SEEK_SET) == false)
|
2014-06-19 15:43:02 +08:00
|
|
|
{
|
2014-08-29 11:56:33 +08:00
|
|
|
CCLOG("warning: Failed to seek to object '%s' in bundle '%s'.", ref->id.c_str(), _path.c_str());
|
2014-06-20 22:38:09 +08:00
|
|
|
return nullptr;
|
2014-06-19 15:43:02 +08:00
|
|
|
}
|
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
}
|
2014-06-20 22:38:09 +08:00
|
|
|
return nullptr;
|
2014-06-19 15:43:02 +08:00
|
|
|
}
|
|
|
|
|
2014-06-05 16:36:01 +08:00
|
|
|
Bundle3D::Bundle3D()
|
2014-10-30 22:25:46 +08:00
|
|
|
: _modelPath(""),
|
2014-08-18 17:31:04 +08:00
|
|
|
_path(""),
|
|
|
|
_version(""),
|
|
|
|
_jsonBuffer(nullptr),
|
|
|
|
_binaryBuffer(nullptr),
|
|
|
|
_referenceCount(0),
|
2014-10-30 22:25:46 +08:00
|
|
|
_references(nullptr),
|
|
|
|
_isBinary(false)
|
2014-06-05 16:36:01 +08:00
|
|
|
{
|
2014-06-12 10:01:54 +08:00
|
|
|
|
2014-06-05 16:36:01 +08:00
|
|
|
}
|
|
|
|
Bundle3D::~Bundle3D()
|
|
|
|
{
|
2014-06-25 09:13:24 +08:00
|
|
|
clear();
|
2014-08-18 17:31:04 +08:00
|
|
|
|
2014-06-05 16:36:01 +08:00
|
|
|
}
|
|
|
|
|
2014-11-19 17:58:40 +08:00
|
|
|
cocos2d::AABB Bundle3D::calculateAABB( const std::vector<float>& vertex, int stride, const std::vector<unsigned short>& index )
|
|
|
|
{
|
|
|
|
AABB aabb;
|
|
|
|
stride /= 4;
|
|
|
|
for(const auto& it : index)
|
|
|
|
{
|
|
|
|
Vec3 point = Vec3(vertex[it * stride ], vertex[ it * stride + 1], vertex[it * stride + 2 ]);
|
|
|
|
aabb.updateMinMax(&point, 1);
|
|
|
|
}
|
|
|
|
return aabb;
|
|
|
|
}
|
|
|
|
|
2014-06-05 16:36:01 +08:00
|
|
|
NS_CC_END
|