modify 3dloader, you can run animation.

This commit is contained in:
lvlong 2014-06-12 18:26:42 +08:00
parent 1ca69d2689
commit 14f4b6430f
5 changed files with 62 additions and 60 deletions

View File

@ -36,13 +36,14 @@
NS_CC_BEGIN NS_CC_BEGIN
void getChildMap(const SkinData* skinData, std::map<int, std::vector<int> >& map, const rapidjson::Value& val, int index) void getChildMap(const SkinData* skinData, std::map<int, std::vector<int> >& map, const rapidjson::Value& val)
{ {
if (!skinData) if (!skinData)
return; return;
if (val.HasMember("children")) if (val.HasMember("children"))
{ {
int parent_name_index = skinData->getBoneNameIndex(val["id"].GetString());
const rapidjson::Value& children = val["children"]; const rapidjson::Value& children = val["children"];
for (rapidjson::SizeType i = 0; i < children.Size(); i++) for (rapidjson::SizeType i = 0; i < children.Size(); i++)
{ {
@ -51,11 +52,11 @@ void getChildMap(const SkinData* skinData, std::map<int, std::vector<int> >& map
int child_name_index = skinData->getBoneNameIndex(child_name); int child_name_index = skinData->getBoneNameIndex(child_name);
if (child_name_index < 0) if (child_name_index < 0)
break; continue;
map[index].push_back(child_name_index); map[parent_name_index].push_back(child_name_index);
getChildMap(skinData, map, child, child_name_index); getChildMap(skinData, map, child);
} }
} }
} }
@ -98,40 +99,40 @@ bool Bundle3D::load(const std::string& path)
bool Bundle3D::loadMeshData(const std::string& id, MeshData* meshdata) bool Bundle3D::loadMeshData(const std::string& id, MeshData* meshdata)
{ {
meshdata->resetData(); meshdata->resetData();
//meshdata->vertexSizeInFloat = 13 * 4; /*meshdata->vertexSizeInFloat = 13 * 4;
//meshdata->vertex = new float[meshdata->vertexSizeInFloat]; meshdata->vertex = new float[meshdata->vertexSizeInFloat];
//float vert[] = {0.f,50.f,0.f,0.f,0.f,0.f,0.f,0.f,0.f,1.f,0.f,0.f,0.f, float vert[] = {0.f,50.f,0.f,0.f,0.f,0.f,0.f,0.f,0.f,1.f,0.f,0.f,0.f,
// 0.f,0.f,50.f,1.f,1.f,0.f,0.f,0.f,0.f,1.f,0.f,0.f,0.f, 0.f,0.f,50.f,1.f,1.f,0.f,0.f,0.f,0.f,1.f,0.f,0.f,0.f,
// 50.f,0.f,0.f,1.f,1.f,0.f,0.f,0.f,0.f,1.f,0.f,0.f,0.f, 50.f,0.f,0.f,1.f,1.f,0.f,0.f,0.f,0.f,1.f,0.f,0.f,0.f,
// -50.f,0.f,0.f,1.f,1.f,0.f,0.f,0.f,0.f,1.f,0.f,0.f,0.f}; -50.f,0.f,0.f,1.f,1.f,0.f,0.f,0.f,0.f,1.f,0.f,0.f,0.f};
//memcpy(meshdata->vertex, vert, meshdata->vertexSizeInFloat * sizeof(float)); memcpy(meshdata->vertex, vert, meshdata->vertexSizeInFloat * sizeof(float));
//meshdata->numIndex = 4 * 3; meshdata->numIndex = 4 * 3;
//meshdata->indices = new unsigned short[meshdata->numIndex]; meshdata->indices = new unsigned short[meshdata->numIndex];
//unsigned short index[] = {0,1,2, 0,3,1, 0,2,3, 3,2,1}; unsigned short index[] = {0,1,2, 0,3,1, 0,2,3, 3,2,1};
//memcpy(meshdata->indices, index, meshdata->numIndex * sizeof(unsigned short)); memcpy(meshdata->indices, index, meshdata->numIndex * sizeof(unsigned short));
//meshdata->attribCount = 4; meshdata->attribCount = 4;
//meshdata->attribs = new MeshVertexAttrib[meshdata->attribCount]; meshdata->attribs = new MeshVertexAttrib[meshdata->attribCount];
//meshdata->attribs[0].attribSizeBytes = 3 * sizeof(float); meshdata->attribs[0].attribSizeBytes = 3 * sizeof(float);
//meshdata->attribs[0].size = 3; meshdata->attribs[0].size = 3;
//meshdata->attribs[0].type = GL_FLOAT; meshdata->attribs[0].type = GL_FLOAT;
//meshdata->attribs[0].vertexAttrib = GLProgram::VERTEX_ATTRIB_POSITION; meshdata->attribs[0].vertexAttrib = GLProgram::VERTEX_ATTRIB_POSITION;
//meshdata->attribs[1].attribSizeBytes = 2 * sizeof(float); meshdata->attribs[1].attribSizeBytes = 2 * sizeof(float);
//meshdata->attribs[1].size = 2; meshdata->attribs[1].size = 2;
//meshdata->attribs[1].type = GL_FLOAT; meshdata->attribs[1].type = GL_FLOAT;
//meshdata->attribs[1].vertexAttrib = GLProgram::VERTEX_ATTRIB_TEX_COORD; meshdata->attribs[1].vertexAttrib = GLProgram::VERTEX_ATTRIB_TEX_COORD;
//meshdata->attribs[2].attribSizeBytes = 4 * sizeof(float); meshdata->attribs[2].attribSizeBytes = 4 * sizeof(float);
//meshdata->attribs[2].size = 4; meshdata->attribs[2].size = 4;
//meshdata->attribs[2].type = GL_FLOAT; meshdata->attribs[2].type = GL_FLOAT;
//meshdata->attribs[2].vertexAttrib = GLProgram::VERTEX_ATTRIB_BLEND_INDEX; meshdata->attribs[2].vertexAttrib = GLProgram::VERTEX_ATTRIB_BLEND_INDEX;
//meshdata->attribs[3].attribSizeBytes = 4 * sizeof(float); meshdata->attribs[3].attribSizeBytes = 4 * sizeof(float);
//meshdata->attribs[3].size = 4; meshdata->attribs[3].size = 4;
//meshdata->attribs[3].type = GL_FLOAT; meshdata->attribs[3].type = GL_FLOAT;
//meshdata->attribs[3].vertexAttrib = GLProgram::VERTEX_ATTRIB_BLEND_WEIGHT; meshdata->attribs[3].vertexAttrib = GLProgram::VERTEX_ATTRIB_BLEND_WEIGHT;*/
const rapidjson::Value& mash_data_val_array = document["mesh_data"]; const rapidjson::Value& mash_data_val_array = document["mesh_data"];
assert(mash_data_val_array.IsArray()); assert(mash_data_val_array.IsArray());
@ -177,7 +178,7 @@ bool Bundle3D::loadMeshData(const std::string& id, MeshData* meshdata)
const rapidjson::Value& mesh_vertex_attribute_val = mesh_vertex_attribute[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].size = mesh_vertex_attribute_val["size"].GetUint();
meshdata->attribs[i].attribSizeBytes = meshdata->attribs[1].size * parseGLTypeSize(mesh_vertex_attribute_val["type"].GetString()); meshdata->attribs[i].attribSizeBytes = meshdata->attribs[i].size * parseGLTypeSize(mesh_vertex_attribute_val["type"].GetString());
meshdata->attribs[i].type = parseGLType(mesh_vertex_attribute_val["type"].GetString()); meshdata->attribs[i].type = parseGLType(mesh_vertex_attribute_val["type"].GetString());
//assignGLTypeByString(meshdata->attribs[i].type, mesh_vertex_attribute_val["type"].GetString()); //assignGLTypeByString(meshdata->attribs[i].type, mesh_vertex_attribute_val["type"].GetString());
meshdata->attribs[i].vertexAttrib = parseGLProgramAttribute(mesh_vertex_attribute_val["vertex_attribute"].GetString()); meshdata->attribs[i].vertexAttrib = parseGLProgramAttribute(mesh_vertex_attribute_val["vertex_attribute"].GetString());
@ -198,16 +199,16 @@ bool Bundle3D::loadSkinData(const std::string& id, SkinData* skindata)
skindata->inverseBindPoseMatrices.push_back(Mat4::IDENTITY); skindata->inverseBindPoseMatrices.push_back(Mat4::IDENTITY);
skindata->rootBoneIndex = 0;*/ skindata->rootBoneIndex = 0;*/
assert(document.HasMember("skin_data")); //assert(document.HasMember("skin_data"));
if (!document.HasMember("skin_data")) return false; if (!document.HasMember("skin_data")) return false;
const rapidjson::Value& skin_data_array = document["skin_data"]; const rapidjson::Value& skin_data_array = document["skin_data"];
assert(skin_data_array.IsArray()); assert(skin_data_array.IsArray());
const rapidjson::Value& skin_data_array_val_0 = skin_data_array[(rapidjson::SizeType)0]; const rapidjson::Value& skin_data_array_val_0 = skin_data_array[(rapidjson::SizeType)0];
skindata->boneNames.push_back("root"); //skindata->boneNames.push_back("root");
skindata->inverseBindPoseMatrices.push_back(Mat4::IDENTITY); //skindata->inverseBindPoseMatrices.push_back(Mat4::IDENTITY);
skindata->rootBoneIndex = 0; skindata->rootBoneIndex = 1;
const rapidjson::Value& skin_data_bind_shape = skin_data_array_val_0["bind_shape"]; const rapidjson::Value& skin_data_bind_shape = skin_data_array_val_0["bind_shape"];
assert(skin_data_bind_shape.Size() == 16); assert(skin_data_bind_shape.Size() == 16);
@ -232,7 +233,8 @@ bool Bundle3D::loadSkinData(const std::string& id, SkinData* skindata)
} }
const rapidjson::Value& skin_data_array_val_1 = skin_data_array[1]; const rapidjson::Value& skin_data_array_val_1 = skin_data_array[1];
getChildMap(skindata, skindata->boneChild, skin_data_array_val_1, 0); const rapidjson::Value& bone_val_0 = skin_data_array_val_1["children"][(rapidjson::SizeType)0];
getChildMap(skindata, skindata->boneChild, /*skin_data_array_val_1*/bone_val_0);
return true; return true;
} }
@ -244,7 +246,7 @@ bool Bundle3D::loadSkinData(const std::string& id, SkinData* skindata)
*/ */
bool Bundle3D::loadMaterialData(const std::string& id, MaterialData* materialdata) bool Bundle3D::loadMaterialData(const std::string& id, MaterialData* materialdata)
{ {
assert(document.HasMember("material_data")); //assert(document.HasMember("material_data"));
if (!document.HasMember("material_data")) return false; if (!document.HasMember("material_data")) return false;
const rapidjson::Value& material_data_array_val = document["material_data"]; const rapidjson::Value& material_data_array_val = document["material_data"];
@ -264,7 +266,7 @@ bool Bundle3D::loadAnimationData(const std::string& id, Animation3DData* animati
animationdata->_rotationKeys.clear(); animationdata->_rotationKeys.clear();
animationdata->_scaleKeys.clear(); animationdata->_scaleKeys.clear();
animationdata->_translationKeys.clear(); animationdata->_translationKeys.clear();
assert(document.HasMember("animation_data")); //assert(document.HasMember("animation_data"));
if (!document.HasMember("animation_data")) return false; if (!document.HasMember("animation_data")) return false;
animationdata->_totalTime = 3; animationdata->_totalTime = 3;

View File

@ -297,7 +297,7 @@ void Sprite3D::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
_mesh->getIndexCount(), _mesh->getIndexCount(),
transform); transform);
_meshCommand.setCullFaceEnabled(false); _meshCommand.setCullFaceEnabled(true);
_meshCommand.setDepthTestEnabled(true); _meshCommand.setDepthTestEnabled(true);
if (_skin) if (_skin)
{ {

View File

@ -542,11 +542,11 @@ std::string Sprite3DWithSkinTest::subtitle() const
void Sprite3DWithSkinTest::addNewSpriteWithCoords(Vec2 p) void Sprite3DWithSkinTest::addNewSpriteWithCoords(Vec2 p)
{ {
auto sprite = Sprite3D::create("Sprite3DTest/cube_anim.c3t"); auto sprite = Sprite3D::create("Sprite3DTest/cube_anim.c3t");
sprite->setScale(1.f); sprite->setScale(10.f);
sprite->setTexture("Sprite3DTest/boss.png"); //sprite->setTexture("Sprite3DTest/boss.png");
addChild(sprite); addChild(sprite);
/*sprite->setPosition( Vec2( p.x, p.y) ); sprite->setPosition( Vec2( p.x, p.y) );
auto animation = Animation3D::getOrCreate("Sprite3DTest/cube_anim.c3t"); auto animation = Animation3D::getOrCreate("Sprite3DTest/cube_anim.c3t");
auto animate = Animate3D::create(animation); auto animate = Animate3D::create(animation);
@ -560,7 +560,7 @@ void Sprite3DWithSkinTest::addNewSpriteWithCoords(Vec2 p)
animate->setSpeed(animate->getSpeed() * (std::rand() % 10)); animate->setSpeed(animate->getSpeed() * (std::rand() % 10));
} }
sprite->runAction(RepeatForever::create(animate));*/ sprite->runAction(RepeatForever::create(animate));
} }
void Sprite3DWithSkinTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* event) void Sprite3DWithSkinTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)

View File

@ -82,7 +82,7 @@
], ],
"material_data": [ "material_data": [
{ {
"filename": "Sprite3DTest/boss.png" "filename": "Sprite3DTest/checkboard.png"
} }
], ],
"skin_data": [ "skin_data": [
@ -133,23 +133,23 @@
"value": [ 0.000000, -0.000000, -0.002285, 0.999997] "value": [ 0.000000, -0.000000, -0.002285, 0.999997]
}, },
{ {
"keytime": 33.333332, "keytime": 0.333332,
"value": [ 0.000000, -0.000000, -0.002285, 0.999997] "value": [ 0.000000, -0.000000, -0.002285, 0.999997]
}, },
{ {
"keytime": 66.666664, "keytime": 0.666664,
"value": [ 0.000000, -0.000000, 0.089457, 0.995991] "value": [ 0.000000, -0.000000, 0.089457, 0.995991]
}, },
{ {
"keytime": 100.000000, "keytime": 0.800000,
"value": [ 0.000000, -0.000000, 0.184131, 0.982902] "value": [ 0.000000, -0.000000, 0.184131, 0.982902]
}, },
{ {
"keytime": 133.333328, "keytime": 0.933328,
"value": [ 0.000000, -0.000000, 0.274421, 0.961610] "value": [ 0.000000, -0.000000, 0.274421, 0.961610]
}, },
{ {
"keytime": 166.000000, "keytime": 1.000000,
"value": [ 0.000000, -0.000000, 0.362349, 0.932043] "value": [ 0.000000, -0.000000, 0.362349, 0.932043]
} }
] ]

View File

@ -39,12 +39,12 @@
{ {
"size":4, "size":4,
"type":"GL_FLOAT", "type":"GL_FLOAT",
"vertex_attribute":"VERTEX_ATTRIB_BLEND_WEIGHT" "vertex_attribute":"VERTEX_ATTRIB_BLEND_INDEX"
}, },
{ {
"size":4, "size":4,
"type":"GL_FLOAT", "type":"GL_FLOAT",
"vertex_attribute":"VERTEX_ATTRIB_BLEND_INDEX" "vertex_attribute":"VERTEX_ATTRIB_BLEND_WEIGHT"
} }
] ]
} }