Merge pull request #42 from lvlonggame/v3

modify bundle3D
This commit is contained in:
XiaoYang 2014-06-25 12:49:40 +08:00
commit 3b06c8e398
6 changed files with 133 additions and 108 deletions

View File

@ -29,6 +29,8 @@
#include "renderer/CCGLProgram.h"
#include "CCBundleReader.h"
#include "base/CCData.h"
#include "json/document.h"
#define BUNDLE_TYPE_SCENE 1
#define BUNDLE_TYPE_NODE 2
@ -154,10 +156,17 @@ void Bundle3D::destroyInstance()
CC_SAFE_DELETE(_instance);
}
void Bundle3D::clearBuffer()
void Bundle3D::clear()
{
CC_SAFE_DELETE_ARRAY(_jsonBuffer);
CC_SAFE_DELETE(_binaryBuffer);
if (_isBinary)
{
CC_SAFE_DELETE(_binaryBuffer);
CC_SAFE_DELETE_ARRAY(_references);
}
else
{
CC_SAFE_DELETE_ARRAY(_jsonBuffer);
}
}
bool Bundle3D::load(const std::string& path)
@ -240,16 +249,19 @@ bool Bundle3D::loadAnimationData(const std::string& id, Animation3DData* animati
bool Bundle3D::loadJson(const std::string& path)
{
clear();
Data data = FileUtils::getInstance()->getDataFromFile(path);
ssize_t size = data.getSize();
CC_SAFE_DELETE_ARRAY(_jsonBuffer);
// json need null-terminated string.
_jsonBuffer = new char[size + 1];
memcpy(_jsonBuffer, data.getBytes(), size);
_jsonBuffer[size] = '\0';
if (_document.ParseInsitu<0>(_jsonBuffer).HasParseError())
if (_jsonReader.ParseInsitu<0>(_jsonBuffer).HasParseError())
{
assert(0);
CC_SAFE_DELETE_ARRAY(_jsonBuffer);
clear();
return false;
}
return true;
@ -259,8 +271,8 @@ bool Bundle3D::loadMeshDataJson(MeshData* meshdata)
{
meshdata->resetData();
assert(_document.HasMember(MESHDATA_MESH));
const rapidjson::Value& mash_data_array = _document[MESHDATA_MESH];
assert(_jsonReader.HasMember(MESHDATA_MESH));
const rapidjson::Value& mash_data_array = _jsonReader[MESHDATA_MESH];
assert(mash_data_array.IsArray());
const rapidjson::Value& mash_data_val = mash_data_array[(rapidjson::SizeType)0];
@ -309,11 +321,11 @@ bool Bundle3D::loadMeshDataJson(MeshData* meshdata)
bool Bundle3D::loadSkinDataJson(SkinData* skindata)
{
if (!_document.HasMember(SKINDATA_SKIN )) return false;
if (!_jsonReader.HasMember(SKINDATA_SKIN )) return false;
skindata->resetData();
const rapidjson::Value& skin_data_array = _document[SKINDATA_SKIN ];
const rapidjson::Value& skin_data_array = _jsonReader[SKINDATA_SKIN ];
assert(skin_data_array.IsArray());
const rapidjson::Value& skin_data_array_val_0 = skin_data_array[(rapidjson::SizeType)0];
@ -349,10 +361,10 @@ bool Bundle3D::loadSkinDataJson(SkinData* skindata)
bool Bundle3D::loadMaterialDataJson(MaterialData* materialdata)
{
if (!_document.HasMember(MATERIALDATA_MATERIAL))
if (!_jsonReader.HasMember(MATERIALDATA_MATERIAL))
return false;
const rapidjson::Value& material_data_array = _document[MATERIALDATA_MATERIAL];
const rapidjson::Value& material_data_array = _jsonReader[MATERIALDATA_MATERIAL];
const rapidjson::Value& material_data_array_0 = material_data_array[(rapidjson::SizeType)0];
@ -367,13 +379,13 @@ bool Bundle3D::loadMaterialDataJson(MaterialData* materialdata)
bool Bundle3D::loadAnimationDataJson(Animation3DData* animationdata)
{
if (!_document.HasMember(ANIMATIONDATA_ANIMATION)) return false;
if (!_jsonReader.HasMember(ANIMATIONDATA_ANIMATION)) return false;
animationdata->_rotationKeys.clear();
animationdata->_scaleKeys.clear();
animationdata->_translationKeys.clear();
const rapidjson::Value& animation_data_array = _document[ANIMATIONDATA_ANIMATION];
const rapidjson::Value& animation_data_array = _jsonReader[ANIMATIONDATA_ANIMATION];
if (animation_data_array.Size()==0) return false;
const rapidjson::Value& animation_data_array_val_0 = animation_data_array[(rapidjson::SizeType)0];
@ -426,40 +438,46 @@ bool Bundle3D::loadAnimationDataJson(Animation3DData* animationdata)
bool Bundle3D::loadBinary(const std::string& path)
{
clear();
// get file data
CC_SAFE_DELETE(_binaryBuffer);
_binaryBuffer = new Data();
*_binaryBuffer = FileUtils::getInstance()->getDataFromFile(path);
if (_binaryBuffer->isNull())
{
clear();
CCLOGINFO(false, "Failed to read file: %s", path.c_str());
return false;
}
// Create bundle reader
CC_SAFE_DELETE(_bundleReader);
_bundleReader = BundleReader::create((char*)_binaryBuffer->getBytes(), _binaryBuffer->getSize());
//CC_SAFE_DELETE(_bundleReader);
_binaryReader.init( (char*)_binaryBuffer->getBytes(), _binaryBuffer->getSize() );
// Read identifier info
char identifier[] = { 'C', '3', 'B', '\0'};
char sig[4];
if (_bundleReader->read(sig, 1, 4) != 4 || memcmp(sig, identifier, 4) != 0)
if (_binaryReader.read(sig, 1, 4) != 4 || memcmp(sig, identifier, 4) != 0)
{
clear();
CCLOGINFO(false, "Invalid identifier: %s", path.c_str());
return false;
}
// Read version
unsigned char ver[2];
if (_bundleReader->read(ver, 1, 2) != 2 || ver[0] != 0 || ver[1] != 1)
if (_binaryReader.read(ver, 1, 2) != 2 || ver[0] != 0 || ver[1] != 1)
{
clear();
CCLOGINFO(false, "Unsupported version: (%d, %d)", ver[0], ver[1]);
return false;
}
// Read ref table size
if (_bundleReader->read(&_referenceCount, 4, 1) != 1)
if (_binaryReader.read(&_referenceCount, 4, 1) != 1)
{
clear();
CCLOGINFO("Failed to read ref table size '%s'.", path.c_str());
return false;
}
@ -469,10 +487,11 @@ bool Bundle3D::loadBinary(const std::string& path)
_references = new Reference[_referenceCount];
for (size_t i = 0; i < _referenceCount; ++i)
{
if ((_references[i].id = _bundleReader->readString()).empty() ||
_bundleReader->read(&_references[i].type, 4, 1) != 1 ||
_bundleReader->read(&_references[i].offset, 4, 1) != 1)
if ((_references[i].id = _binaryReader.readString()).empty() ||
_binaryReader.read(&_references[i].type, 4, 1) != 1 ||
_binaryReader.read(&_references[i].offset, 4, 1) != 1)
{
clear();
CCLOGINFO("Failed to read ref number %d for bundle '%s'.", i, path.c_str());
CC_SAFE_DELETE_ARRAY(_references);
return false;
@ -490,7 +509,7 @@ bool Bundle3D::loadMeshDataBinary(MeshData* meshdata)
meshdata->resetData();
// read mesh data
if (_bundleReader->read(&meshdata->attribCount, 4, 1) != 1 || meshdata->attribCount < 1)
if (_binaryReader.read(&meshdata->attribCount, 4, 1) != 1 || meshdata->attribCount < 1)
{
CCLOGINFO("Failed to read meshdata: attribCount '%s'.", _path.c_str());
return false;
@ -500,7 +519,7 @@ bool Bundle3D::loadMeshDataBinary(MeshData* meshdata)
for (ssize_t i = 0; i < meshdata->attribCount; i++)
{
unsigned int vUsage, vSize;
if (_bundleReader->read(&vUsage, 4, 1) != 1 || _bundleReader->read(&vSize, 4, 1) != 1)
if (_binaryReader.read(&vUsage, 4, 1) != 1 || _binaryReader.read(&vSize, 4, 1) != 1)
{
CCLOGINFO("Failed to read meshdata: usage or size '%s'.", _path.c_str());
return false;
@ -513,14 +532,14 @@ bool Bundle3D::loadMeshDataBinary(MeshData* meshdata)
}
// Read vertex data
if (_bundleReader->read(&meshdata->vertexSizeInFloat, 4, 1) != 1 || meshdata->vertexSizeInFloat == 0)
if (_binaryReader.read(&meshdata->vertexSizeInFloat, 4, 1) != 1 || meshdata->vertexSizeInFloat == 0)
{
CCLOGINFO("Failed to read meshdata: vertexSizeInFloat '%s'.", _path.c_str());
return false;
}
meshdata->vertex.resize(meshdata->vertexSizeInFloat);
if (_bundleReader->read(&meshdata->vertex[0], 4, meshdata->vertexSizeInFloat) != meshdata->vertexSizeInFloat)
if (_binaryReader.read(&meshdata->vertex[0], 4, meshdata->vertexSizeInFloat) != meshdata->vertexSizeInFloat)
{
CCLOGINFO("Failed to read meshdata: vertex element '%s'.", _path.c_str());
return false;
@ -528,12 +547,12 @@ bool Bundle3D::loadMeshDataBinary(MeshData* meshdata)
// Read index data
size_t meshPartCount = 1;
//_bundleReader->read(&meshPartCount, 4, 1);
//_binaryReader.read(&meshPartCount, 4, 1);
for (size_t i = 0; i < meshPartCount; ++i)
{
size_t nIndexCount;
if (_bundleReader->read(&nIndexCount, 4, 1) != 1)
if (_binaryReader.read(&nIndexCount, 4, 1) != 1)
{
CCLOGINFO("Failed to read meshdata: nIndexCount '%s'.", _path.c_str());
return false;
@ -541,7 +560,7 @@ bool Bundle3D::loadMeshDataBinary(MeshData* meshdata)
meshdata->numIndex = nIndexCount;
meshdata->indices.resize(meshdata->numIndex);
if (_bundleReader->read(&meshdata->indices[0], 2, meshdata->numIndex) != nIndexCount)
if (_binaryReader.read(&meshdata->indices[0], 2, meshdata->numIndex) != nIndexCount)
{
CCLOGINFO("Failed to read meshdata: indices '%s'.", _path.c_str());
return false;
@ -560,7 +579,7 @@ bool Bundle3D::loadSkinDataBinary(SkinData* skindata)
// transform
float bindShape[16];
if (!_bundleReader->readMatrix(bindShape))
if (!_binaryReader.readMatrix(bindShape))
{
CCLOGINFO("Failed to read SkinData: bindShape matrix '%s'.", _path.c_str());
return false;
@ -568,7 +587,7 @@ bool Bundle3D::loadSkinDataBinary(SkinData* skindata)
// bone count
unsigned int boneNum;
if (!_bundleReader->read(&boneNum))
if (!_binaryReader.read(&boneNum))
{
CCLOGINFO("Failed to read SkinData: boneNum '%s'.", _path.c_str());
return false;
@ -578,8 +597,8 @@ bool Bundle3D::loadSkinDataBinary(SkinData* skindata)
float bindpos[16];
for (unsigned int i = 0; i < boneNum; i++)
{
skindata->skinBoneNames.push_back(_bundleReader->readString());
if (!_bundleReader->readMatrix(bindpos))
skindata->skinBoneNames.push_back(_binaryReader.readString());
if (!_binaryReader.readMatrix(bindpos))
{
CCLOGINFO("Failed to load SkinData: bindpos '%s'.", _path.c_str());
return nullptr;
@ -587,16 +606,18 @@ bool Bundle3D::loadSkinDataBinary(SkinData* skindata)
skindata->inverseBindPoseMatrices.push_back(bindpos);
}
skindata->skinBoneOriginMatrices.resize(boneNum);
// bind shape
_bundleReader->readMatrix(bindShape);
_binaryReader.readMatrix(bindShape);
// read parent and child relationship map
float transform[16];
unsigned int linkNum;
_bundleReader->read(&linkNum);
_binaryReader.read(&linkNum);
for (unsigned int i = 0; i < linkNum; ++i)
{
std::string id = _bundleReader->readString();
std::string id = _binaryReader.readString();
int index = skindata->getSkinBoneNameIndex(id);
if(index < 0)
{
@ -606,22 +627,22 @@ bool Bundle3D::loadSkinDataBinary(SkinData* skindata)
}
else
{
skindata->skinBoneOriginMatrices.push_back(transform);
skindata->skinBoneOriginMatrices[index] = transform;
}
// set root bone index
if (skindata->rootBoneIndex < 0)
skindata->rootBoneIndex = index;
std::string parentid = _bundleReader->readString();
std::string parentid = _binaryReader.readString();
if (!_bundleReader->readMatrix(transform))
if (!_binaryReader.readMatrix(transform))
{
CCLOGINFO("Failed to load SkinData: transform '%s'.", _path.c_str());
return nullptr;
}
int parentIndex = skindata->getBoneNameIndex(parentid);
int parentIndex = skindata->getSkinBoneNameIndex(parentid);
if(parentIndex < 0)
{
skindata->addNodeBoneNames(parentid);
@ -630,7 +651,7 @@ bool Bundle3D::loadSkinDataBinary(SkinData* skindata)
}
else
{
skindata->skinBoneOriginMatrices.push_back(transform);
skindata->skinBoneOriginMatrices[parentIndex] = transform;
}
skindata->boneChild[parentIndex].push_back(index);
@ -645,7 +666,7 @@ bool Bundle3D::loadMaterialDataBinary(MaterialData* materialdata)
if (!seekToFirstType(BUNDLE_TYPE_MATERIAL))
return false;
std::string texturePath = _bundleReader->readString();
std::string texturePath = _binaryReader.readString();
if (texturePath.empty())
{
CCLOGINFO("Failed to read Materialdata: texturePath is empty '%s'.", _path.c_str());
@ -665,16 +686,16 @@ bool Bundle3D::loadAnimationDataBinary(Animation3DData* animationdata)
animationdata->_scaleKeys.clear();
animationdata->_translationKeys.clear();
_bundleReader->readString();
_binaryReader.readString();
if (!_bundleReader->read(&animationdata->_totalTime))
if (!_binaryReader.read(&animationdata->_totalTime))
{
CCLOGINFO("Failed to read AnimationData: totalTime '%s'.", _path.c_str());
return false;
}
size_t animNum;
if (!_bundleReader->read(&animNum))
if (!_binaryReader.read(&animNum))
{
CCLOGINFO("Failed to read AnimationData: animNum '%s'.", _path.c_str());
return false;
@ -682,9 +703,9 @@ bool Bundle3D::loadAnimationDataBinary(Animation3DData* animationdata)
for (size_t i = 0; i < animNum; ++i)
{
std::string boneName = _bundleReader->readString();
std::string boneName = _binaryReader.readString();
size_t keyframeNum;
if (!_bundleReader->read(&keyframeNum))
if (!_binaryReader.read(&keyframeNum))
{
CCLOGINFO("Failed to read AnimationData: keyframeNum '%s'.", _path.c_str());
return false;
@ -693,14 +714,14 @@ bool Bundle3D::loadAnimationDataBinary(Animation3DData* animationdata)
for (size_t j = 0; j < keyframeNum; ++j)
{
float keytime;
if (!_bundleReader->read(&keytime))
if (!_binaryReader.read(&keytime))
{
CCLOGINFO("Failed to read AnimationData: keytime '%s'.", _path.c_str());
return false;
}
Quaternion rotate;
if (_bundleReader->read(&rotate, 4, 4) != 4)
if (_binaryReader.read(&rotate, 4, 4) != 4)
{
CCLOGINFO("Failed to read AnimationData: rotate '%s'.", _path.c_str());
return false;
@ -708,7 +729,7 @@ bool Bundle3D::loadAnimationDataBinary(Animation3DData* animationdata)
animationdata->_rotationKeys[boneName].push_back(Animation3DData::QuatKey(keytime, rotate));
Vec3 scale;
if (_bundleReader->read(&scale, 4, 3) != 3)
if (_binaryReader.read(&scale, 4, 3) != 3)
{
CCLOGINFO("Failed to read AnimationData: scale '%s'.", _path.c_str());
return false;
@ -716,7 +737,7 @@ bool Bundle3D::loadAnimationDataBinary(Animation3DData* animationdata)
animationdata->_scaleKeys[boneName].push_back(Animation3DData::Vec3Key(keytime, scale));
Vec3 position;
if (_bundleReader->read(&position, 4, 3) != 3)
if (_binaryReader.read(&position, 4, 3) != 3)
{
CCLOGINFO("Failed to read AnimationData: position '%s'.", _path.c_str());
return false;
@ -803,7 +824,7 @@ Reference* Bundle3D::seekToFirstType(unsigned int type)
if (ref->type == type)
{
// Found a match
if (_bundleReader->seek(ref->offset, SEEK_SET) == false)
if (_binaryReader.seek(ref->offset, SEEK_SET) == false)
{
CCLOGINFO("Failed to seek to object '%s' in bundle '%s'.", ref->id.c_str(), _path.c_str());
return nullptr;
@ -817,21 +838,18 @@ Reference* Bundle3D::seekToFirstType(unsigned int type)
Bundle3D::Bundle3D()
:_isBinary(false),
_modelRelativePath(""),
_jsonBuffer(nullptr),
_path(""),
_jsonBuffer(nullptr),
_binaryBuffer(nullptr),
_referenceCount(0),
_bundleReader(nullptr),
_references(nullptr),
_binaryBuffer(nullptr)
_references(nullptr)
{
}
Bundle3D::~Bundle3D()
{
CC_SAFE_DELETE_ARRAY(_jsonBuffer);
CC_SAFE_DELETE_ARRAY(_bundleReader);
CC_SAFE_DELETE_ARRAY(_references);
CC_SAFE_DELETE(_binaryBuffer);
clear();
}
NS_CC_END

View File

@ -34,9 +34,9 @@
#include "base/ccTypes.h"
#include "json/document.h"
#include "CCBundleReader.h"
NS_CC_BEGIN
class BundleReader;
class Animation3D;
class Data;
@ -54,7 +54,7 @@ public:
static void destroyInstance();
void clearBuffer();
void clear();
/**
* load a file. You must load a file first, then call loadMeshData, loadSkinData, and so on
@ -164,15 +164,17 @@ protected:
static Bundle3D* _instance;
std::string _modelRelativePath;
char* _jsonBuffer;
std::string _path;
rapidjson::Document _document;
BundleReader* _bundleReader;
// for json reading
char* _jsonBuffer;
rapidjson::Document _jsonReader;
// for binary reading
Data* _binaryBuffer;
BundleReader _binaryReader;
size_t _referenceCount;
Reference* _references;
Data* _binaryBuffer;
bool _isBinary;
};

View File

@ -3,26 +3,40 @@
NS_CC_BEGIN
BundleReader::~BundleReader()
BundleReader::BundleReader()
{
close();
m_buffer = NULL;
m_position = 0;
m_length = 0;
};
BundleReader* BundleReader::create(char* lpbuffer, unsigned int length)
BundleReader::~BundleReader()
{
if (lpbuffer)
{
BundleReader* stream = new BundleReader();
stream->init(lpbuffer, length);
return stream;
}
return NULL;
//close();
};
void BundleReader::init(char* lpbuffer, unsigned int length)
{
m_position = 0;
m_buffer = lpbuffer;
m_length = length;
}
void BundleReader::close()
{
CC_SAFE_DELETE(m_buffer);
}
//BundleReader* BundleReader::create(char* lpbuffer, unsigned int length)
//{
// if (lpbuffer)
// {
// BundleReader* stream = new BundleReader();
// stream->init(lpbuffer, length);
// return stream;
// }
// return NULL;
//}
//void BundleReader::close()
//{
// CC_SAFE_DELETE(m_buffer);
//}
size_t BundleReader::read(void* ptr, size_t size, size_t count)
{
@ -160,18 +174,4 @@ bool BundleReader::readMatrix(float* m)
return (read(m, sizeof(float), 16) == 16);
}
BundleReader::BundleReader()
{
m_buffer = NULL;
m_position = 0;
m_length = 0;
};
void BundleReader::init(char* lpbuffer, unsigned int length)
{
m_position = 0;
m_buffer = lpbuffer;
m_length = length;
}
}

View File

@ -41,20 +41,33 @@ class BundleReader: public cocos2d::Ref
{
public:
/**
* Destructor
* Structor
*/
BundleReader();
/**
* inicial
*/
~BundleReader();
/**
* initialise
* @param lpbuffer The data buffer pointer
* @param length The data buffer size
*/
void init(char* lpbuffer, unsigned int length);
/** creates an BundleReader with lpbuffer and length
* @param lpbuffer The pointer to the file data
* @param length The size for lpbuffer in bytes
*/
static BundleReader* create(char* lpbuffer, unsigned int length);
//static BundleReader* create(char* lpbuffer, unsigned int length);
/**
* Close and delete buffer
*/
void close();
//void close();
/**
* Reads an array of elements.
@ -110,10 +123,6 @@ public:
std::string readString();
bool readMatrix(float* m);
private:
BundleReader();
void init(char* lpbuffer, unsigned int length);
private:
long int m_position;
size_t m_length;

View File

@ -54,10 +54,6 @@ void Bone::setOriPose(const Mat4& m)
void Bone::resetPose()
{
_local =_oriPose;
// if (_parent)
// _world = _parent->_world * _oriPose;
// else
// _world = _oriPose;
for (auto it : _children) {
it->resetPose();

View File

@ -541,10 +541,10 @@ std::string Sprite3DWithSkinTest::subtitle() const
void Sprite3DWithSkinTest::addNewSpriteWithCoords(Vec2 p)
{
std::string fileName = "Sprite3DTest/girl.c3t";
std::string fileName = "Sprite3DTest/girl.c3b";
auto sprite = Sprite3D::create(fileName);
addChild(sprite);
//sprite->setRotation3D(Vec3(0.f, 90.f, 0.f));
sprite->setRotation3D(Vec3(-90.f, 0.f, 0.f));
sprite->setPosition( Vec2( p.x, p.y) );
auto animation = Animation3D::getOrCreate(fileName);