axmol/cocos/3d/CCBundle3D.cpp

473 lines
16 KiB
C++
Raw Normal View History

2014-06-05 16:36:01 +08:00
/****************************************************************************
Copyright (c) 2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
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.
****************************************************************************/
#include "CCBundle3D.h"
2014-06-09 18:37:58 +08:00
#include "CCAnimationCurve.h"
2014-06-05 16:36:01 +08:00
#include "CCAnimation3D.h"
#include "CCSprite3D.h"
#include "CCMeshSkin.h"
#include "base/ccMacros.h"
#include "platform/CCFileUtils.h"
#include "CCBundle3DData.h"
2014-06-05 16:36:01 +08:00
NS_CC_BEGIN
2014-06-13 19:26: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
{
if (!skinData)
return;
2014-06-12 10:01:54 +08:00
2014-06-13 19:26:46 +08:00
if (!val.HasMember("children"))
return;
std::string parent_name = val["id"].GetString();
int parent_name_index = skinData->getBoneNameIndex(parent_name);
2014-06-15 22:45:56 +08:00
// const rapidjson::Value& bind_pos = val["bind_shape"];
// Mat4 mat_bind_pos;
// for (rapidjson::SizeType j = 0; j < bind_pos.Size(); j++)
// {
// mat_bind_pos.m[j] = bind_pos[j].GetDouble();
// }
//
// skinData->inverseBindPoseMatrices[parent_name_index] = (mat_bind_pos);
2014-06-13 19:26:46 +08:00
const rapidjson::Value& children = val["children"];
for (rapidjson::SizeType i = 0; i < children.Size(); i++)
{
const rapidjson::Value& child = children[i];
std::string child_name = child["id"].GetString();
2014-06-12 10:01:54 +08:00
2014-06-13 19:26:46 +08:00
int child_name_index = skinData->getBoneNameIndex(child_name);
if (child_name_index >= 0)
{
map[parent_name_index].push_back(child_name_index);
2014-06-12 10:01:54 +08:00
2014-06-13 19:26:46 +08:00
getChildMap(map, skinData, child);
2014-06-12 10:01:54 +08:00
}
}
}
2014-06-13 19:26:46 +08:00
void getChildMapT(std::map<std::string, std::vector<std::string> >& map, const SkinData* skinData, const rapidjson::Value& val)
{
if (!skinData)
return;
if (!val.HasMember("children"))
return;
std::string parent_name = val["id"].GetString();
const rapidjson::Value& children = val["children"];
for (rapidjson::SizeType i = 0; i < children.Size(); i++)
{
const rapidjson::Value& child = children[i];
std::string child_name = child["id"].GetString();
map[parent_name].push_back(child_name);
getChildMapT(map, skinData, child);
}
}
2014-06-05 16:36:01 +08:00
Bundle3D* Bundle3D::_instance = nullptr;
2014-06-06 19:12:08 +08:00
2014-06-05 16:36:01 +08:00
Bundle3D* Bundle3D::getInstance()
{
if (_instance == nullptr)
_instance = new Bundle3D();
return _instance;
}
void Bundle3D::purgeBundle3D()
{
CC_SAFE_DELETE(_instance);
}
bool Bundle3D::load(const std::string& path)
{
2014-06-12 10:01:54 +08:00
std::string strFileString = FileUtils::getInstance()->getStringFromFile(path);
ssize_t size = strFileString.length();
CC_SAFE_DELETE_ARRAY(_documentBuffer);
_documentBuffer = new char[size + 1];
memcpy(_documentBuffer, strFileString.c_str(), size);
_documentBuffer[size] = '\0';
if (document.ParseInsitu<0>(_documentBuffer).HasParseError())
2014-06-13 19:26:46 +08:00
{
assert(0);
2014-06-12 10:01:54 +08:00
return false;
2014-06-13 19:26:46 +08:00
}
2014-06-05 16:36:01 +08:00
return true;
}
2014-06-05 18:21:25 +08:00
/**
* load mesh data from bundle
* @param id The ID of the mesh, load the first Mesh in the bundle if it is empty
*/
bool Bundle3D::loadMeshData(const std::string& id, MeshData* meshdata)
{
2014-06-06 19:12:08 +08:00
meshdata->resetData();
2014-06-09 18:37:58 +08:00
2014-06-12 10:01:54 +08:00
const rapidjson::Value& mash_data_val_array = document["mesh_data"];
assert(mash_data_val_array.IsArray());
const rapidjson::Value& mash_data_val = mash_data_val_array[(rapidjson::SizeType)0];
assert(mash_data_val.IsObject());
assert(mash_data_val.HasMember("body"));
const rapidjson::Value& mesh_data_body_array = mash_data_val["body"];
assert(mesh_data_body_array.IsArray());
const rapidjson::Value& mesh_data_body_val = mesh_data_body_array[(rapidjson::SizeType)0];
// vertex_size
assert(mesh_data_body_val.HasMember("vertex_size"));
meshdata->vertexSizeInFloat = mesh_data_body_val["vertex_size"].GetInt();
// vertices
meshdata->vertex = new float[meshdata->vertexSizeInFloat];
const rapidjson::Value& mesh_data_body_vertex_val = mesh_data_body_val["vertices"];
for (rapidjson::SizeType i = 0; i < mesh_data_body_vertex_val.Size(); i++)
meshdata->vertex[i] = mesh_data_body_vertex_val[i].GetDouble();
// index_number
meshdata->numIndex = mesh_data_body_val["index_number"].GetUint();
// indices
meshdata->indices = new unsigned short[meshdata->numIndex];
const rapidjson::Value& mesh_data_body_indices_val = mesh_data_body_val["indices"];
for (rapidjson::SizeType i = 0; i < mesh_data_body_indices_val.Size(); i++)
meshdata->indices[i] = (unsigned short)mesh_data_body_indices_val[i].GetUint();
// mesh_vertex_attribute
2014-06-13 19:26:46 +08:00
const rapidjson::Value& mesh_vertex_attribute = mash_data_val["mesh_vertex_attribute"];
2014-06-12 10:01:54 +08:00
meshdata->attribCount = mesh_vertex_attribute.Size();
meshdata->attribs = new MeshVertexAttrib[meshdata->attribCount];
for (rapidjson::SizeType i = 0; i < mesh_vertex_attribute.Size(); i++)
{
const rapidjson::Value& mesh_vertex_attribute_val = mesh_vertex_attribute[i];
meshdata->attribs[i].size = mesh_vertex_attribute_val["size"].GetUint();
meshdata->attribs[i].attribSizeBytes = meshdata->attribs[i].size * parseGLTypeSize(mesh_vertex_attribute_val["type"].GetString());
2014-06-12 10:01:54 +08:00
meshdata->attribs[i].type = parseGLType(mesh_vertex_attribute_val["type"].GetString());
//assignGLTypeByString(meshdata->attribs[i].type, mesh_vertex_attribute_val["type"].GetString());
2014-06-12 10:01:54 +08:00
meshdata->attribs[i].vertexAttrib = parseGLProgramAttribute(mesh_vertex_attribute_val["vertex_attribute"].GetString());
}
2014-06-05 18:21:25 +08:00
return true;
}
//
/**
* load skin data from bundle
* @param id The ID of the skin, load the first Skin in the bundle if it is empty
*/
bool Bundle3D::loadSkinData(const std::string& id, SkinData* skindata)
{
2014-06-09 18:37:58 +08:00
skindata->resetData();
2014-06-12 10:01:54 +08:00
if (!document.HasMember("skin_data")) return false;
const rapidjson::Value& skin_data_array = document["skin_data"];
assert(skin_data_array.IsArray());
2014-06-09 18:37:58 +08:00
2014-06-12 10:01:54 +08:00
const rapidjson::Value& skin_data_array_val_0 = skin_data_array[(rapidjson::SizeType)0];
const rapidjson::Value& skin_data_bind_shape = skin_data_array_val_0["bind_shape"];
assert(skin_data_bind_shape.Size() == 16);
for (rapidjson::SizeType i = 0; i < skin_data_bind_shape.Size(); i++)
{
skindata->bindShape.m[i] = skin_data_bind_shape[i].GetDouble();
}
2014-06-15 22:45:56 +08:00
float m[] = {-0.682038f, -0.035225f, 0.730468f, 0.000000f, 0.731315f, -0.035225f, 0.681130f, 0.000000f, 0.001738f, 0.998758f, 0.049786f, 0.000000f, -0.882000f, 78.798103f, -0.868362f, 1.000000f};
memcpy(skindata->bindShape.m, m, sizeof(m));
//skindata->bindShape.setIdentity();
2014-06-12 10:01:54 +08:00
const rapidjson::Value& skin_data_bones = skin_data_array_val_0["bones"];
for (rapidjson::SizeType i = 0; i < skin_data_bones.Size(); i++)
{
const rapidjson::Value& skin_data_bone = skin_data_bones[i];
std::string name = skin_data_bone["node"].GetString();
skindata->boneNames.push_back(name);
2014-06-15 22:45:56 +08:00
const rapidjson::Value& bind_pos = skin_data_bone["bind_pos"];
2014-06-12 10:01:54 +08:00
Mat4 mat_bind_pos;
for (rapidjson::SizeType j = 0; j < bind_pos.Size(); j++)
{
mat_bind_pos.m[j] = bind_pos[j].GetDouble();
}
2014-06-15 22:45:56 +08:00
skindata->inverseBindPoseMatrices.push_back(mat_bind_pos.getInversed());
2014-06-12 10:01:54 +08:00
}
const rapidjson::Value& skin_data_array_val_1 = skin_data_array[1];
const rapidjson::Value& bone_val_0 = skin_data_array_val_1["children"][(rapidjson::SizeType)0];
2014-06-13 19:26:46 +08:00
skindata->rootBoneIndex = skindata->getBoneNameIndex(bone_val_0["id"].GetString());
2014-06-15 22:45:56 +08:00
//skindata->inverseBindPoseMatrices.resize(skindata->boneNames.size(), Mat4::IDENTITY);
2014-06-13 19:26:46 +08:00
getChildMap(skindata->boneChild, skindata, bone_val_0);
2014-06-12 10:01:54 +08:00
2014-06-13 19:26:46 +08:00
std::map<std::string, std::vector<std::string> > map_child;
getChildMapT(map_child, skindata, bone_val_0);
2014-06-15 22:45:56 +08:00
for (auto it : skindata->boneNames) {
CCLOG("bonename: %s", it.c_str());
}
CCLOG("root: %s", skindata->boneNames[skindata->rootBoneIndex].c_str());
for (auto itr : skindata->boneChild) {
CCLOG("parent: %s", skindata->boneNames[itr.first].c_str());
for (auto itr2 : itr.second) {
CCLOG("%s", skindata->boneNames[(itr2)].c_str());
}
CCLOG("////////");
}
2014-06-05 18:21:25 +08:00
return true;
}
/**
* load material data from bundle
* @param id The ID of the material, load the first Material in the bundle if it is empty
*/
bool Bundle3D::loadMaterialData(const std::string& id, MaterialData* materialdata)
{
2014-06-13 19:26:46 +08:00
if (!document.HasMember("material_data"))
return false;
2014-06-12 10:01:54 +08:00
const rapidjson::Value& material_data_array_val = document["material_data"];
2014-06-13 19:26:46 +08:00
if (!material_data_array_val.IsArray())
return false;
2014-06-12 10:01:54 +08:00
const rapidjson::Value& material_data_array_val_0 = material_data_array_val[(rapidjson::SizeType)0];
2014-06-13 19:26:46 +08:00
if (!material_data_array_val_0.HasMember("file_name"))
return false;
materialdata->texturePath = material_data_array_val_0["file_name"].GetString();
2014-06-05 18:21:25 +08:00
return true;
}
/**
* load material data from bundle
* @param id The ID of the animation, load the first animation in the bundle if it is empty
*/
bool Bundle3D::loadAnimationData(const std::string& id, Animation3DData* animationdata)
{
2014-06-12 14:03:33 +08:00
animationdata->_rotationKeys.clear();
animationdata->_scaleKeys.clear();
animationdata->_translationKeys.clear();
2014-06-12 10:01:54 +08:00
2014-06-13 19:26:46 +08:00
assert(document.HasMember("animation_3d_data"));
if (!document.HasMember("animation_3d_data")) return false;
2014-06-13 19:26:46 +08:00
animationdata->_totalTime = 3;
2014-06-12 10:01:54 +08:00
2014-06-13 19:26:46 +08:00
const rapidjson::Value& animation_data_array_val = document["animation_3d_data"];
2014-06-12 10:01:54 +08:00
const rapidjson::Value& animation_data_array_val_0 = animation_data_array_val[(rapidjson::SizeType)0];
const rapidjson::Value& bones = animation_data_array_val_0["bones"];
2014-06-13 19:26:46 +08:00
rapidjson::SizeType bone_size = bones.Size();
for (rapidjson::SizeType i = 0; i < bone_size/*bones.Size()*/; i++)
2014-06-12 10:01:54 +08:00
{
const rapidjson::Value& bone = bones[i];
2014-06-13 19:26:46 +08:00
std::string bone_name = bone["boneId"].GetString();
2014-06-12 10:01:54 +08:00
if ( bone.HasMember("keyframes"))
{
const rapidjson::Value& bone_keyframes = bone["keyframes"];
2014-06-13 19:26:46 +08:00
rapidjson::SizeType keyframe_size = bone_keyframes.Size();
2014-06-12 10:01:54 +08:00
for (rapidjson::SizeType j = 0; j < bone_keyframes.Size(); j++)
{
const rapidjson::Value& bone_keyframe = bone_keyframes[j];
2014-06-13 19:26:46 +08:00
if ( bone_keyframe.HasMember("translation"))
{
const rapidjson::Value& bone_keyframe_translation = bone_keyframe["translation"];
float keytime = bone_keyframe["keytime"].GetDouble() / 1200.f;
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
}
if ( bone_keyframe.HasMember("rotation"))
{
2014-06-13 19:26:46 +08:00
const rapidjson::Value& bone_keyframe_rotation = bone_keyframe["rotation"];
float keytime = bone_keyframe["keytime"].GetDouble() / 1200.f;
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
}
if ( bone_keyframe.HasMember("scale"))
{
2014-06-13 19:26:46 +08:00
const rapidjson::Value& bone_keyframe_scale = bone_keyframe["scale"];
float keytime = bone_keyframe["keytime"].GetDouble() / 1200.f;
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-06-15 22:45:56 +08:00
animationdata->_totalTime = 3;
CCLOG("translation:////////////////");
//animationdata->_translationKeys.erase(animationdata->_translationKeys.begin(), animationdata->_translationKeys.end());
for (auto itr: animationdata->_translationKeys)
{
CCLOG("%s", itr.first.c_str());
auto& keys = itr.second;
if (keys.size())
{
float maxtime = keys[keys.size() - 1]._time;
for (auto it : keys) {
it._time /= maxtime;
}
}
}
CCLOG("rotation:////////////////");
//animationdata->_rotationKeys.erase(animationdata->_rotationKeys.begin(), animationdata->_rotationKeys.end());
for (auto itr: animationdata->_rotationKeys)
{
CCLOG("%s", itr.first.c_str());
auto& keys = itr.second;
if (keys.size())
{
float maxtime = keys[keys.size() - 1]._time;
for (auto it : keys) {
it._time /= maxtime;
}
}
}
CCLOG("scale:////////////////");
for (auto itr: animationdata->_scaleKeys)
{
CCLOG("%s", itr.first.c_str());
auto& keys = itr.second;
if (keys.size())
{
float maxtime = keys[keys.size() - 1]._time;
for (auto it : keys) {
it._time /= maxtime;
}
}
}
2014-06-05 18:21:25 +08:00
return true;
}
//void Bundle3D::assignGLTypeByString(GLenum& type, std::string str)
//{
// if (str == "GL_FLOAT")
// {
// type = GL_FLOAT;
// }
// else if (str == "GL_UNSIGNED_INT")
// {
// type = GL_UNSIGNED_INT;
// }
// else
// {
// assert(0);
// }
//}
GLenum Bundle3D::parseGLType(const std::string& str)
2014-06-12 10:01:54 +08:00
{
if (str == "GL_FLOAT")
{
return GL_FLOAT;
2014-06-12 10:01:54 +08:00
}
else if (str == "GL_UNSIGNED_INT")
{
return GL_UNSIGNED_INT;
2014-06-12 10:01:54 +08:00
}
else
{
assert(0);
return 0;
2014-06-12 10:01:54 +08:00
}
}
unsigned int Bundle3D::parseGLTypeSize(const std::string& str)
2014-06-12 10:01:54 +08:00
{
if (str == "GL_FLOAT")
{
return sizeof(float);
2014-06-12 10:01:54 +08:00
}
else if (str == "GL_UNSIGNED_INT")
{
return sizeof(unsigned int);
2014-06-12 10:01:54 +08:00
}
else
{
assert(0);
return -1;
}
}
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;
}
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
{
assert(0);
return -1;
}
}
2014-06-05 16:36:01 +08:00
Bundle3D::Bundle3D()
2014-06-12 10:01:54 +08:00
:_isBinary(false),_documentBuffer(NULL)
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-12 10:01:54 +08:00
CC_SAFE_DELETE_ARRAY(_documentBuffer);
2014-06-05 16:36:01 +08:00
}
NS_CC_END