Merge branch 'v3' of https://github.com/super626/cocos2d-x into merge-bundle

This commit is contained in:
lvlong 2014-08-16 16:58:02 +08:00
commit 24bb827ef4
9 changed files with 105 additions and 69 deletions

View File

@ -90,12 +90,12 @@ Animate3D* Animate3D::reverse() const
void Animate3D::startWithTarget(Node *target)
{
Sprite3D* sprite = dynamic_cast<Sprite3D*>(target);
CCASSERT(sprite && sprite->getSkin() && _animation, "Animate3D apply to Sprite3D only");
CCASSERT(sprite && sprite->getSkeleton() && _animation, "Animate3D apply to Sprite3D only");
ActionInterval::startWithTarget(target);
_boneCurves.clear();
auto skin = sprite->getSkin();
auto skin = sprite->getSkeleton();
for (unsigned int i = 0; i < skin->getBoneCount(); i++) {
auto bone = skin->getBoneByIndex(i);
auto curve = _animation->getBoneCurveByName(bone->getName());

View File

@ -75,6 +75,7 @@ static const char* BONES = "bones";
static const char* MATERIALS = "materials";
static const char* ANIMATIONS = "animations";
static const char* TRANSFORM = "transform";
static const char* OLDTRANSFORM = "tansform";
static const char* MATERIALDATA_MATERIAL = "material";
static const char* MATERIALDATA_MATERIALS = "materials";
@ -100,7 +101,7 @@ void getChildMap(std::map<int, std::vector<int> >& map, SkinData* skinData, cons
// get transform matrix
Mat4 transform;
const rapidjson::Value& parent_tranform = val[TRANSFORM];
const rapidjson::Value& parent_tranform = val[OLDTRANSFORM];
for (rapidjson::SizeType j = 0; j < parent_tranform.Size(); j++)
{
transform.m[j] = parent_tranform[j].GetDouble();
@ -371,7 +372,7 @@ bool Bundle3D::loadNodes(NodeDatas& nodedatas)
if (_version == "1.2" || _version == "0.2")
{
SkinData skinData;
loadSkinDataJson(&skinData);
loadSkinData("", &skinData);
auto nodeDatas = new NodeData*[skinData.skinBoneNames.size() + skinData.nodeBoneNames.size()];
int index = 0;
size_t i;

View File

@ -206,7 +206,9 @@ Mesh* Mesh::create(const MeshData& meshdata)
{
for (int i = 0; i < (int)mesh->getSubMeshCount(); i++) {
auto submesh = mesh->getSubMesh(i);
if (meshdata.subMeshIds.size())
submesh->_id = meshdata.subMeshIds[i];
submesh->_mesh = mesh;
}
}
mesh->autorelease();

View File

@ -220,8 +220,8 @@ ssize_t MeshSkin::getMatrixPaletteSize() const
//refresh bone world matrix
void MeshSkin::updateBoneMatrix()
{
_rootBone->setWorldMatDirty(true);
_rootBone->updateWorldMat();
// _rootBone->setWorldMatDirty(true);
// _rootBone->updateWorldMat();
}
void MeshSkin::removeAllBones()

View File

@ -298,14 +298,22 @@ Skeleton3D* Skeleton3D::create(const std::vector<NodeData*>& skeletondata)
{
auto skeleton = new Skeleton3D();
for (const auto& it : skeletondata) {
auto bone = createBone3D(*it);
auto bone = skeleton->createBone3D(*it);
skeleton->_rootBones.pushBack(bone);
bone->release();
skeleton->printBone(bone);
}
skeleton->autorelease();
return skeleton;
}
void Skeleton3D::printBone(Bone3D* bone)
{
CCLOG("%s", bone->getName().c_str());
for (auto it : bone->_children) {
printBone(it);
}
}
bool Skeleton3D::initFromSkeletonData(const Skeleton3DData& skeletondata)
{
ssize_t i = 0;
@ -390,8 +398,12 @@ int Skeleton3D::getBoneIndex(Bone3D* bone) const
//refresh bone world matrix
void Skeleton3D::updateBoneMatrix()
{
_rootBone->setWorldMatDirty(true);
_rootBone->updateWorldMat();
//_rootBone->setWorldMatDirty(true);
//_rootBone->updateWorldMat();
for (const auto& it : _rootBones) {
it->setWorldMatDirty(true);
it->updateWorldMat();
}
}
void Skeleton3D::removeAllBones()
@ -412,8 +424,10 @@ Bone3D* Skeleton3D::createBone3D(const NodeData& nodedata)
for (const auto& it : nodedata.children) {
auto child = createBone3D(*it);
bone->addChildBone(child);
child->_parent = bone;
child->release();
}
_bones.pushBack(bone);
return bone;
}

View File

@ -227,7 +227,9 @@ CC_CONSTRUCTOR_ACCESS:
void addBone(Bone3D* bone);
/** create Bone3D from NodeData */
static Bone3D* createBone3D(const NodeData& nodedata);
Bone3D* createBone3D(const NodeData& nodedata);
void printBone(Bone3D* bone);
protected:

View File

@ -291,34 +291,46 @@ bool Sprite3D::initWithFile(const std::string &path)
void Sprite3D::genGLProgramState()
{
auto programstate = GLProgramState::getOrCreateWithGLProgram(getDefaultGLProgram(_mesh->hasVertexAttrib(GLProgram::VERTEX_ATTRIB_TEX_COORD)));
if (_meshes.size() == 0)
return;
//all the mesh should have the same attributes, FIX ME
auto mesh = _meshes.at(0);
auto programstate = GLProgramState::getOrCreateWithGLProgram(getDefaultGLProgram(mesh->hasVertexAttrib(GLProgram::VERTEX_ATTRIB_TEX_COORD)));
long offset = 0;
auto attributeCount = _mesh->getMeshVertexAttribCount();
auto attributeCount = mesh->getMeshVertexAttribCount();
for (auto k = 0; k < attributeCount; k++) {
auto meshattribute = _mesh->getMeshVertexAttribute(k);
auto meshattribute = mesh->getMeshVertexAttribute(k);
programstate->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib],
meshattribute.size,
meshattribute.type,
GL_FALSE,
_mesh->getVertexSizeInBytes(),
mesh->getVertexSizeInBytes(),
(GLvoid*)offset);
offset += meshattribute.attribSizeBytes;
}
setGLProgramState(programstate);
auto count = _mesh->getSubMeshCount();
auto count = mesh->getSubMeshCount();
_meshCommands.resize(count);
for (int i = 0; i < count; i++) {
auto tex = _subMeshStates.at(i)->getTexture();
GLuint texID = tex ? tex->getName() : 0;
_meshCommands[i].genMaterialID(texID, programstate, _mesh, _blend);
_meshCommands[i].genMaterialID(texID, programstate, _meshes.at(i), _blend);
}
}
GLProgram* Sprite3D::getDefaultGLProgram(bool textured)
{
bool hasSkin = _skin && _mesh->hasVertexAttrib(GLProgram::VERTEX_ATTRIB_BLEND_INDEX)
&& _mesh->hasVertexAttrib(GLProgram::VERTEX_ATTRIB_BLEND_WEIGHT);
if (_meshes.size() == 0)
return nullptr;
//all the mesh should have the same attributes, FIX ME
auto mesh = _meshes.at(0);
bool hasSkin = mesh->hasVertexAttrib(GLProgram::VERTEX_ATTRIB_BLEND_INDEX)
&& mesh->hasVertexAttrib(GLProgram::VERTEX_ATTRIB_BLEND_WEIGHT);
if(textured)
{
@ -348,14 +360,15 @@ void Sprite3D::createNode(NodeData* nodedata, Node* root, const MaterialDatas& m
if (modelNodeData->matrialId == "" && matrialdatas.materials.size())
{
NTextureData::Usage type = NTextureData::Usage::Diffuse;
const NTextureData* textureData = matrialdatas.materials[0].getTextureData(type);
const NTextureData* textureData = matrialdatas.materials[0].getTextureData(NTextureData::Usage::Diffuse);
subMeshState->setTexture(textureData->filename);
}
else
{
const NMaterialData* materialData=matrialdatas.getMaterialData(modelNodeData->matrialId);
if(materialData)
{
NTextureData::Usage type = NTextureData::Usage::Diffuse;
const NTextureData* textureData = materialData->getTextureData(type);
const NTextureData* textureData = materialData->getTextureData(NTextureData::Usage::Diffuse);
if(textureData)
{
auto tex = Director::getInstance()->getTextureCache()->addImage(textureData->filename);
@ -374,6 +387,7 @@ void Sprite3D::createNode(NodeData* nodedata, Node* root, const MaterialDatas& m
}
}
}
}
else
{
node= Node::create();

View File

@ -70,6 +70,8 @@ public:
/**get skin*/
MeshSkin* getSkin() const { return _skin; }
Skeleton3D* getSkeleton() const { return _skeleton; }
/**get AttachNode by bone name, return nullptr if not exist*/
AttachNode* getAttachNode(const std::string& boneName);

View File

@ -46,6 +46,7 @@ static int sceneIdx = -1;
static std::function<Layer*()> createFunctions[] =
{
CL(Sprite3DMirrorTest),
CL(Sprite3DBasicTest),
CL(Sprite3DHitTest),
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WP8) && (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT)
@ -1271,17 +1272,17 @@ std::string Sprite3DMirrorTest::subtitle() const
void Sprite3DMirrorTest::addNewSpriteWithCoords(Vec2 p)
{
std::string fileName = "Sprite3DTest/orc.c3b";
std::string fileName = "Sprite3DTest/orc.c3t";
auto sprite = Sprite3D::create(fileName);
sprite->setScale(5);
sprite->setRotation3D(Vec3(0,180,0));
sprite->setScale(6);
sprite->setRotation3D(Vec3(90,0,0));
addChild(sprite);
sprite->setPosition( Vec2( p.x - 80, p.y) );
//test attach
auto sp = Sprite3D::create("Sprite3DTest/axe.c3b");
sprite->getAttachNode("Bip001 R Hand")->addChild(sp);
// //test attach
// auto sp = Sprite3D::create("Sprite3DTest/axe.c3b");
// sprite->getAttachNode("Bip001 R Hand")->addChild(sp);
//
auto animation = Animation3D::create(fileName);
if (animation)
{
@ -1292,25 +1293,25 @@ void Sprite3DMirrorTest::addNewSpriteWithCoords(Vec2 p)
_sprite = sprite;
_hasWeapon = true;
//create mirror Sprite3D
sprite = Sprite3D::create(fileName);
sprite->setScale(5);
sprite->setScaleX(-5);
sprite->setCullFace(GL_FRONT);
sprite->setRotation3D(Vec3(0,180,0));
addChild(sprite);
sprite->setPosition( Vec2( p.x + 80, p.y) );
//test attach
sp = Sprite3D::create("Sprite3DTest/axe.c3b");
sprite->getAttachNode("Bip001 R Hand")->addChild(sp);
animation = Animation3D::create(fileName);
if (animation)
{
auto animate = Animate3D::create(animation);
sprite->runAction(RepeatForever::create(animate));
}
_mirrorSprite = sprite;
// //create mirror Sprite3D
// sprite = Sprite3D::create(fileName);
// sprite->setScale(5);
// sprite->setScaleX(-5);
// sprite->setCullFace(GL_FRONT);
// sprite->setRotation3D(Vec3(0,180,0));
// addChild(sprite);
// sprite->setPosition( Vec2( p.x + 80, p.y) );
//
// //test attach
// sp = Sprite3D::create("Sprite3DTest/axe.c3b");
// sprite->getAttachNode("Bip001 R Hand")->addChild(sp);
//
// animation = Animation3D::create(fileName);
// if (animation)
// {
// auto animate = Animate3D::create(animation);
//
// sprite->runAction(RepeatForever::create(animate));
// }
// _mirrorSprite = sprite;
}