2014-05-19 05:49:16 +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 "3d/CCSprite3D.h"
|
|
|
|
#include "3d/CCObjLoader.h"
|
2014-06-04 18:17:09 +08:00
|
|
|
#include "3d/CCMeshSkin.h"
|
2014-06-05 16:36:01 +08:00
|
|
|
#include "3d/CCBundle3D.h"
|
2014-06-16 20:58:13 +08:00
|
|
|
#include "3d/CCSprite3DMaterial.h"
|
2014-07-29 10:49:06 +08:00
|
|
|
#include "3d/CCAttachNode.h"
|
2014-08-29 15:39:52 +08:00
|
|
|
#include "3d/CCMesh.h"
|
2014-05-19 05:49:16 +08:00
|
|
|
|
|
|
|
#include "base/CCDirector.h"
|
2014-09-10 08:17:07 +08:00
|
|
|
#include "platform/CCPlatformMacros.h"
|
2014-05-19 05:49:16 +08:00
|
|
|
#include "base/ccMacros.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
#include "renderer/CCTextureCache.h"
|
|
|
|
#include "renderer/CCRenderer.h"
|
|
|
|
#include "renderer/CCGLProgramState.h"
|
|
|
|
#include "renderer/CCGLProgramCache.h"
|
|
|
|
|
|
|
|
#include "deprecated/CCString.h" // For StringUtils::format
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2014-08-21 18:14:24 +08:00
|
|
|
std::string s_attributeNames[] = {GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::ATTRIBUTE_NAME_TEX_COORD1, GLProgram::ATTRIBUTE_NAME_TEX_COORD2,GLProgram::ATTRIBUTE_NAME_TEX_COORD3,GLProgram::ATTRIBUTE_NAME_TEX_COORD4,GLProgram::ATTRIBUTE_NAME_TEX_COORD5,GLProgram::ATTRIBUTE_NAME_TEX_COORD6,GLProgram::ATTRIBUTE_NAME_TEX_COORD7,GLProgram::ATTRIBUTE_NAME_NORMAL, GLProgram::ATTRIBUTE_NAME_BLEND_WEIGHT, GLProgram::ATTRIBUTE_NAME_BLEND_INDEX};
|
2014-05-19 05:49:16 +08:00
|
|
|
|
|
|
|
Sprite3D* Sprite3D::create(const std::string &modelPath)
|
|
|
|
{
|
|
|
|
if (modelPath.length() < 4)
|
|
|
|
CCASSERT(false, "improper name specified when creating Sprite3D");
|
|
|
|
|
2014-08-28 07:31:57 +08:00
|
|
|
auto sprite = new (std::nothrow) Sprite3D();
|
2014-05-19 05:49:16 +08:00
|
|
|
if (sprite && sprite->initWithFile(modelPath))
|
|
|
|
{
|
2014-09-09 17:47:34 +08:00
|
|
|
sprite->_contentSize = sprite->getBoundingBox().size;
|
2014-05-19 05:49:16 +08:00
|
|
|
sprite->autorelease();
|
|
|
|
return sprite;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(sprite);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
Sprite3D* Sprite3D::create(const std::string &modelPath, const std::string &texturePath)
|
|
|
|
{
|
|
|
|
auto sprite = create(modelPath);
|
|
|
|
if (sprite)
|
|
|
|
{
|
|
|
|
sprite->setTexture(texturePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sprite;
|
|
|
|
}
|
|
|
|
|
2014-07-29 10:49:06 +08:00
|
|
|
bool Sprite3D::loadFromCache(const std::string& path)
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
2014-08-19 11:56:09 +08:00
|
|
|
auto spritedata = Sprite3DCache::getInstance()->getSpriteData(path);
|
|
|
|
if (spritedata)
|
|
|
|
{
|
2014-08-22 13:25:26 +08:00
|
|
|
for (auto it : spritedata->meshVertexDatas) {
|
|
|
|
_meshVertexDatas.pushBack(it);
|
2014-08-19 11:56:09 +08:00
|
|
|
}
|
|
|
|
_skeleton = Skeleton3D::create(spritedata->nodedatas->skeleton);
|
|
|
|
CC_SAFE_RETAIN(_skeleton);
|
|
|
|
|
|
|
|
for(const auto& it : spritedata->nodedatas->nodes)
|
|
|
|
{
|
|
|
|
if(it)
|
|
|
|
{
|
|
|
|
createNode(it, this, *(spritedata->materialdatas), spritedata->nodedatas->nodes.size() == 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-17 13:42:08 +08:00
|
|
|
for (ssize_t i = 0; i < _meshes.size(); i++) {
|
|
|
|
_meshes.at(i)->setGLProgramState(spritedata->glProgramStates.at(i));
|
|
|
|
}
|
2014-08-19 11:56:09 +08:00
|
|
|
return true;
|
|
|
|
}
|
2014-06-16 20:58:13 +08:00
|
|
|
|
2014-07-29 10:49:06 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//.mtl file should at the same directory with the same name if exist
|
|
|
|
bool Sprite3D::loadFromObj(const std::string& path)
|
|
|
|
{
|
|
|
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(path);
|
|
|
|
|
2014-08-17 22:49:53 +08:00
|
|
|
MeshDatas meshdatas;
|
2014-08-28 07:31:57 +08:00
|
|
|
MaterialDatas* materialdatas = new (std::nothrow) MaterialDatas();
|
|
|
|
NodeDatas* nodeDatas = new (std::nothrow) NodeDatas();
|
2014-08-19 11:56:09 +08:00
|
|
|
bool ret = Bundle3D::loadObj(meshdatas, *materialdatas, *nodeDatas, fullPath);
|
|
|
|
if (ret && initFrom(*nodeDatas, meshdatas, *materialdatas))
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
2014-08-19 11:56:09 +08:00
|
|
|
//add to cache
|
2014-08-28 07:31:57 +08:00
|
|
|
auto data = new (std::nothrow) Sprite3DCache::Sprite3DData();
|
2014-08-19 11:56:09 +08:00
|
|
|
data->materialdatas = materialdatas;
|
|
|
|
data->nodedatas = nodeDatas;
|
2014-08-22 13:25:26 +08:00
|
|
|
data->meshVertexDatas = _meshVertexDatas;
|
2014-09-17 13:42:08 +08:00
|
|
|
for (const auto mesh : _meshes) {
|
|
|
|
data->glProgramStates.pushBack(mesh->getGLProgramState());
|
|
|
|
}
|
|
|
|
|
2014-08-19 11:56:09 +08:00
|
|
|
Sprite3DCache::getInstance()->addSprite3DData(path, data);
|
|
|
|
return true;
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
2014-08-19 11:56:09 +08:00
|
|
|
delete materialdatas;
|
|
|
|
delete nodeDatas;
|
2014-07-29 10:49:06 +08:00
|
|
|
|
2014-08-18 14:16:34 +08:00
|
|
|
return false;
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
2014-08-18 14:16:34 +08:00
|
|
|
bool Sprite3D::loadFromC3x(const std::string& path)
|
2014-08-15 17:46:21 +08:00
|
|
|
{
|
|
|
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(path);
|
|
|
|
|
|
|
|
//load from .c3b or .c3t
|
|
|
|
auto bundle = Bundle3D::getInstance();
|
|
|
|
if (!bundle->load(fullPath))
|
|
|
|
return false;
|
2014-08-16 14:16:17 +08:00
|
|
|
|
2014-08-15 17:46:21 +08:00
|
|
|
MeshDatas meshdatas;
|
2014-08-28 07:31:57 +08:00
|
|
|
MaterialDatas* materialdatas = new (std::nothrow) MaterialDatas();
|
|
|
|
NodeDatas* nodeDatas = new (std::nothrow) NodeDatas();
|
2014-08-18 14:16:34 +08:00
|
|
|
if (bundle->loadMeshDatas(meshdatas)
|
2014-08-19 11:56:09 +08:00
|
|
|
&& bundle->loadMaterials(*materialdatas)
|
|
|
|
&& bundle->loadNodes(*nodeDatas)
|
|
|
|
&& initFrom(*nodeDatas, meshdatas, *materialdatas))
|
2014-08-15 17:46:21 +08:00
|
|
|
{
|
2014-08-19 11:56:09 +08:00
|
|
|
//add to cache
|
2014-08-28 07:31:57 +08:00
|
|
|
auto data = new (std::nothrow) Sprite3DCache::Sprite3DData();
|
2014-08-19 11:56:09 +08:00
|
|
|
data->materialdatas = materialdatas;
|
|
|
|
data->nodedatas = nodeDatas;
|
2014-08-22 13:25:26 +08:00
|
|
|
data->meshVertexDatas = _meshVertexDatas;
|
2014-09-17 13:42:08 +08:00
|
|
|
for (const auto mesh : _meshes) {
|
|
|
|
data->glProgramStates.pushBack(mesh->getGLProgramState());
|
|
|
|
}
|
2014-08-19 11:56:09 +08:00
|
|
|
Sprite3DCache::getInstance()->addSprite3DData(path, data);
|
|
|
|
return true;
|
2014-08-15 17:46:21 +08:00
|
|
|
}
|
2014-08-15 20:52:45 +08:00
|
|
|
|
2014-08-19 11:56:09 +08:00
|
|
|
delete materialdatas;
|
|
|
|
delete nodeDatas;
|
|
|
|
|
2014-08-18 14:16:34 +08:00
|
|
|
return false;
|
2014-06-04 18:17:09 +08:00
|
|
|
}
|
|
|
|
|
2014-05-19 05:49:16 +08:00
|
|
|
Sprite3D::Sprite3D()
|
2014-08-18 11:13:08 +08:00
|
|
|
: _skeleton(nullptr)
|
2014-05-19 05:49:16 +08:00
|
|
|
, _blend(BlendFunc::ALPHA_NON_PREMULTIPLIED)
|
2014-08-18 17:02:13 +08:00
|
|
|
, _aabbDirty(true)
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Sprite3D::~Sprite3D()
|
|
|
|
{
|
2014-08-15 20:52:45 +08:00
|
|
|
_meshes.clear();
|
2014-08-22 13:25:26 +08:00
|
|
|
_meshVertexDatas.clear();
|
2014-07-30 18:32:34 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(_skeleton);
|
2014-07-29 10:49:06 +08:00
|
|
|
removeAllAttachNode();
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Sprite3D::initWithFile(const std::string &path)
|
|
|
|
{
|
2014-08-18 14:16:34 +08:00
|
|
|
_meshes.clear();
|
2014-08-22 13:25:26 +08:00
|
|
|
_meshVertexDatas.clear();
|
2014-07-30 18:32:34 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(_skeleton);
|
2014-08-18 14:16:34 +08:00
|
|
|
removeAllAttachNode();
|
2014-07-29 10:49:06 +08:00
|
|
|
|
|
|
|
if (loadFromCache(path))
|
|
|
|
return true;
|
2014-05-19 05:49:16 +08:00
|
|
|
|
2014-06-16 20:58:13 +08:00
|
|
|
//load from file
|
|
|
|
std::string ext = path.substr(path.length() - 4, 4);
|
|
|
|
std::transform(ext.begin(), ext.end(), ext.begin(), tolower);
|
|
|
|
|
|
|
|
if (ext == ".obj")
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
2014-06-16 20:58:13 +08:00
|
|
|
return loadFromObj(path);
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
2014-06-16 20:58:13 +08:00
|
|
|
else if (ext == ".c3b" || ext == ".c3t")
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
2014-08-18 14:16:34 +08:00
|
|
|
return loadFromC3x(path);
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
2014-06-16 20:58:13 +08:00
|
|
|
|
|
|
|
return false;
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
|
|
|
|
2014-08-18 14:16:34 +08:00
|
|
|
bool Sprite3D::initFrom(const NodeDatas& nodeDatas, const MeshDatas& meshdatas, const MaterialDatas& materialdatas)
|
|
|
|
{
|
|
|
|
for(const auto& it : meshdatas.meshDatas)
|
|
|
|
{
|
|
|
|
if(it)
|
|
|
|
{
|
2014-08-22 13:25:26 +08:00
|
|
|
// Mesh* mesh = Mesh::create(*it);
|
|
|
|
// _meshes.pushBack(mesh);
|
|
|
|
auto meshvertex = MeshVertexData::create(*it);
|
|
|
|
_meshVertexDatas.pushBack(meshvertex);
|
2014-08-18 14:16:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_skeleton = Skeleton3D::create(nodeDatas.skeleton);
|
|
|
|
CC_SAFE_RETAIN(_skeleton);
|
|
|
|
|
|
|
|
for(const auto& it : nodeDatas.nodes)
|
|
|
|
{
|
|
|
|
if(it)
|
|
|
|
{
|
|
|
|
createNode(it, this, materialdatas, nodeDatas.nodes.size() == 1);
|
|
|
|
}
|
|
|
|
}
|
2014-08-21 11:23:31 +08:00
|
|
|
for(const auto& it : nodeDatas.skeleton)
|
|
|
|
{
|
|
|
|
if(it)
|
|
|
|
{
|
|
|
|
createAttachSprite3DNode(it,materialdatas);
|
|
|
|
}
|
|
|
|
}
|
2014-08-18 14:16:34 +08:00
|
|
|
genGLProgramState();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2014-08-21 11:23:31 +08:00
|
|
|
Sprite3D* Sprite3D::createSprite3DNode(NodeData* nodedata,ModelData* modeldata,const MaterialDatas& matrialdatas)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
auto sprite = new (std::nothrow) Sprite3D();
|
2014-08-21 11:23:31 +08:00
|
|
|
if (sprite)
|
|
|
|
{
|
2014-09-03 15:55:11 +08:00
|
|
|
sprite->setName(nodedata->id);
|
2014-08-22 13:25:26 +08:00
|
|
|
auto mesh = Mesh::create(nodedata->id, getMeshIndexData(modeldata->subMeshId));
|
2014-08-21 11:23:31 +08:00
|
|
|
if (modeldata->matrialId == "" && matrialdatas.materials.size())
|
|
|
|
{
|
|
|
|
const NTextureData* textureData = matrialdatas.materials[0].getTextureData(NTextureData::Usage::Diffuse);
|
2014-08-22 13:25:26 +08:00
|
|
|
mesh->setTexture(textureData->filename);
|
2014-08-21 11:23:31 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const NMaterialData* materialData=matrialdatas.getMaterialData(modeldata->matrialId);
|
|
|
|
if(materialData)
|
|
|
|
{
|
|
|
|
const NTextureData* textureData = materialData->getTextureData(NTextureData::Usage::Diffuse);
|
|
|
|
if(textureData)
|
|
|
|
{
|
|
|
|
auto tex = Director::getInstance()->getTextureCache()->addImage(textureData->filename);
|
|
|
|
if(tex)
|
|
|
|
{
|
|
|
|
Texture2D::TexParams texParams;
|
|
|
|
texParams.minFilter = GL_LINEAR;
|
|
|
|
texParams.magFilter = GL_LINEAR;
|
|
|
|
texParams.wrapS = textureData->wrapS;
|
|
|
|
texParams.wrapT = textureData->wrapT;
|
|
|
|
tex->setTexParameters(texParams);
|
2014-08-22 13:25:26 +08:00
|
|
|
mesh->setTexture(tex);
|
2014-09-12 17:57:54 +08:00
|
|
|
mesh->_isTransparent = (materialData->getTextureData(NTextureData::Usage::Transparency) != nullptr);
|
2014-08-21 11:23:31 +08:00
|
|
|
}
|
2014-08-18 14:16:34 +08:00
|
|
|
|
2014-08-21 11:23:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sprite->setAdditionalTransform(&nodedata->transform);
|
2014-08-22 13:25:26 +08:00
|
|
|
sprite->addMesh(mesh);
|
2014-08-21 11:23:31 +08:00
|
|
|
sprite->autorelease();
|
|
|
|
sprite->genGLProgramState();
|
|
|
|
}
|
|
|
|
return sprite;
|
|
|
|
}
|
|
|
|
void Sprite3D::createAttachSprite3DNode(NodeData* nodedata,const MaterialDatas& matrialdatas)
|
|
|
|
{
|
|
|
|
for(const auto& it : nodedata->modelNodeDatas)
|
|
|
|
{
|
|
|
|
if(it && getAttachNode(nodedata->id))
|
|
|
|
{
|
|
|
|
auto sprite = createSprite3DNode(nodedata,it,matrialdatas);
|
|
|
|
if (sprite)
|
|
|
|
{
|
|
|
|
getAttachNode(nodedata->id)->addChild(sprite);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(const auto& it : nodedata->children)
|
|
|
|
{
|
|
|
|
createAttachSprite3DNode(it,matrialdatas);
|
|
|
|
}
|
|
|
|
}
|
2014-05-19 05:49:16 +08:00
|
|
|
void Sprite3D::genGLProgramState()
|
|
|
|
{
|
2014-08-22 13:25:26 +08:00
|
|
|
std::unordered_map<const MeshVertexData*, GLProgramState*> glProgramestates;
|
|
|
|
for(auto& mesh : _meshVertexDatas)
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
2014-08-20 18:20:19 +08:00
|
|
|
bool textured = mesh->hasVertexAttrib(GLProgram::VERTEX_ATTRIB_TEX_COORD);
|
|
|
|
bool hasSkin = mesh->hasVertexAttrib(GLProgram::VERTEX_ATTRIB_BLEND_INDEX)
|
|
|
|
&& mesh->hasVertexAttrib(GLProgram::VERTEX_ATTRIB_BLEND_WEIGHT);
|
|
|
|
|
|
|
|
GLProgram* glProgram = nullptr;
|
|
|
|
if(textured)
|
|
|
|
{
|
|
|
|
if (hasSkin)
|
|
|
|
glProgram = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_3D_SKINPOSITION_TEXTURE);
|
|
|
|
else
|
|
|
|
glProgram = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_3D_POSITION_TEXTURE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glProgram = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_3D_POSITION);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto programstate = GLProgramState::create(glProgram);
|
|
|
|
long offset = 0;
|
|
|
|
auto attributeCount = mesh->getMeshVertexAttribCount();
|
|
|
|
for (auto k = 0; k < attributeCount; k++) {
|
2014-08-22 13:25:26 +08:00
|
|
|
auto meshattribute = mesh->getMeshVertexAttrib(k);
|
2014-08-20 18:20:19 +08:00
|
|
|
programstate->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib],
|
|
|
|
meshattribute.size,
|
|
|
|
meshattribute.type,
|
|
|
|
GL_FALSE,
|
2014-08-22 13:25:26 +08:00
|
|
|
mesh->getVertexBuffer()->getSizePerVertex(),
|
2014-08-20 18:20:19 +08:00
|
|
|
(GLvoid*)offset);
|
|
|
|
offset += meshattribute.attribSizeBytes;
|
|
|
|
}
|
2014-06-05 16:36:01 +08:00
|
|
|
|
2014-08-20 18:20:19 +08:00
|
|
|
glProgramestates[mesh] = programstate;
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
2014-08-20 18:20:19 +08:00
|
|
|
|
2014-08-22 13:25:26 +08:00
|
|
|
for (auto& it : _meshes) {
|
|
|
|
auto glProgramState = glProgramestates[it->getMeshIndexData()->getMeshVertexData()];
|
|
|
|
it->setGLProgramState(glProgramState);
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
|
|
|
}
|
2014-08-20 18:20:19 +08:00
|
|
|
|
2014-08-18 14:16:34 +08:00
|
|
|
void Sprite3D::createNode(NodeData* nodedata, Node* root, const MaterialDatas& matrialdatas, bool singleSprite)
|
2014-08-15 17:46:21 +08:00
|
|
|
{
|
|
|
|
Node* node=nullptr;
|
2014-08-20 11:40:04 +08:00
|
|
|
for(const auto& it : nodedata->modelNodeDatas)
|
2014-08-15 17:46:21 +08:00
|
|
|
{
|
2014-08-20 11:40:04 +08:00
|
|
|
if(it)
|
2014-08-15 17:46:21 +08:00
|
|
|
{
|
2014-08-20 11:40:04 +08:00
|
|
|
if(it->bones.size() > 0 || singleSprite)
|
2014-08-17 22:49:53 +08:00
|
|
|
{
|
2014-08-22 13:25:26 +08:00
|
|
|
auto mesh = Mesh::create(nodedata->id, getMeshIndexData(it->subMeshId));
|
|
|
|
if(mesh)
|
2014-08-18 10:32:23 +08:00
|
|
|
{
|
2014-08-22 13:25:26 +08:00
|
|
|
_meshes.pushBack(mesh);
|
2014-08-20 11:40:04 +08:00
|
|
|
if (_skeleton && it->bones.size())
|
|
|
|
{
|
|
|
|
auto skin = MeshSkin::create(_skeleton, it->bones, it->invBindPose);
|
2014-08-22 13:25:26 +08:00
|
|
|
mesh->setSkin(skin);
|
2014-08-20 11:40:04 +08:00
|
|
|
}
|
2014-08-22 13:25:26 +08:00
|
|
|
mesh->_visibleChanged = std::bind(&Sprite3D::onAABBDirty, this);
|
2014-08-18 10:32:23 +08:00
|
|
|
|
2014-08-20 11:40:04 +08:00
|
|
|
if (it->matrialId == "" && matrialdatas.materials.size())
|
2014-08-18 10:32:23 +08:00
|
|
|
{
|
2014-08-20 11:40:04 +08:00
|
|
|
const NTextureData* textureData = matrialdatas.materials[0].getTextureData(NTextureData::Usage::Diffuse);
|
2014-08-22 13:25:26 +08:00
|
|
|
mesh->setTexture(textureData->filename);
|
2014-08-20 11:40:04 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const NMaterialData* materialData=matrialdatas.getMaterialData(it->matrialId);
|
|
|
|
if(materialData)
|
2014-08-18 10:32:23 +08:00
|
|
|
{
|
2014-08-20 11:40:04 +08:00
|
|
|
const NTextureData* textureData = materialData->getTextureData(NTextureData::Usage::Diffuse);
|
|
|
|
if(textureData)
|
2014-08-18 10:32:23 +08:00
|
|
|
{
|
2014-08-20 11:40:04 +08:00
|
|
|
auto tex = Director::getInstance()->getTextureCache()->addImage(textureData->filename);
|
|
|
|
if(tex)
|
|
|
|
{
|
|
|
|
Texture2D::TexParams texParams;
|
|
|
|
texParams.minFilter = GL_LINEAR;
|
|
|
|
texParams.magFilter = GL_LINEAR;
|
|
|
|
texParams.wrapS = textureData->wrapS;
|
|
|
|
texParams.wrapT = textureData->wrapT;
|
|
|
|
tex->setTexParameters(texParams);
|
2014-08-22 13:25:26 +08:00
|
|
|
mesh->setTexture(tex);
|
2014-09-12 17:57:54 +08:00
|
|
|
mesh->_isTransparent = (materialData->getTextureData(NTextureData::Usage::Transparency) != nullptr);
|
2014-08-20 11:40:04 +08:00
|
|
|
}
|
2014-08-18 10:32:23 +08:00
|
|
|
|
2014-08-20 11:40:04 +08:00
|
|
|
}
|
2014-08-18 10:32:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-16 14:49:38 +08:00
|
|
|
}
|
2014-08-20 11:40:04 +08:00
|
|
|
else
|
2014-08-16 10:41:42 +08:00
|
|
|
{
|
2014-08-21 11:23:31 +08:00
|
|
|
auto sprite = createSprite3DNode(nodedata,it,matrialdatas);
|
2014-08-20 11:40:04 +08:00
|
|
|
if (sprite)
|
2014-08-18 10:32:23 +08:00
|
|
|
{
|
2014-08-20 11:40:04 +08:00
|
|
|
if(root)
|
|
|
|
{
|
|
|
|
root->addChild(sprite);
|
|
|
|
}
|
2014-08-16 16:56:04 +08:00
|
|
|
}
|
2014-08-20 11:40:04 +08:00
|
|
|
node=sprite;
|
|
|
|
}
|
2014-08-15 17:46:21 +08:00
|
|
|
}
|
|
|
|
}
|
2014-08-20 11:40:04 +08:00
|
|
|
if(nodedata->modelNodeDatas.size() ==0 )
|
2014-08-15 17:46:21 +08:00
|
|
|
{
|
|
|
|
node= Node::create();
|
|
|
|
if(node)
|
|
|
|
{
|
2014-09-03 11:43:42 +08:00
|
|
|
node->setName(nodedata->id);
|
2014-08-15 17:46:21 +08:00
|
|
|
node->setAdditionalTransform(&nodedata->transform);
|
2014-08-15 18:20:37 +08:00
|
|
|
if(root)
|
|
|
|
{
|
|
|
|
root->addChild(node);
|
|
|
|
}
|
2014-08-15 17:46:21 +08:00
|
|
|
}
|
|
|
|
}
|
2014-08-21 11:23:31 +08:00
|
|
|
for(const auto& it : nodedata->children)
|
2014-08-15 17:46:21 +08:00
|
|
|
{
|
2014-08-21 11:23:31 +08:00
|
|
|
createNode(it,node, matrialdatas, singleSprite);
|
2014-08-15 17:46:21 +08:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 20:52:45 +08:00
|
|
|
|
2014-08-22 13:25:26 +08:00
|
|
|
MeshIndexData* Sprite3D::getMeshIndexData(const std::string& indexId) const
|
2014-08-15 20:52:45 +08:00
|
|
|
{
|
2014-08-22 13:25:26 +08:00
|
|
|
for (auto it : _meshVertexDatas) {
|
|
|
|
auto index = it->getMeshIndexDataById(indexId);
|
|
|
|
if (index)
|
|
|
|
return index;
|
2014-08-15 20:52:45 +08:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-08-22 13:25:26 +08:00
|
|
|
void Sprite3D::addMesh(Mesh* mesh)
|
2014-08-18 10:32:23 +08:00
|
|
|
{
|
2014-08-22 13:25:26 +08:00
|
|
|
auto meshVertex = mesh->getMeshIndexData()->_vertexData;
|
|
|
|
_meshVertexDatas.pushBack(meshVertex);
|
|
|
|
_meshes.pushBack(mesh);
|
2014-08-18 10:32:23 +08:00
|
|
|
}
|
2014-07-29 10:49:06 +08:00
|
|
|
|
2014-05-19 05:49:16 +08:00
|
|
|
void Sprite3D::setTexture(const std::string& texFile)
|
|
|
|
{
|
|
|
|
auto tex = Director::getInstance()->getTextureCache()->addImage(texFile);
|
2014-06-23 19:08:26 +08:00
|
|
|
setTexture(tex);
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3D::setTexture(Texture2D* texture)
|
|
|
|
{
|
2014-08-22 13:25:26 +08:00
|
|
|
for (auto& state : _meshes) {
|
2014-08-20 19:01:04 +08:00
|
|
|
state->setTexture(texture);
|
|
|
|
}
|
2014-07-29 10:49:06 +08:00
|
|
|
}
|
|
|
|
AttachNode* Sprite3D::getAttachNode(const std::string& boneName)
|
|
|
|
{
|
|
|
|
auto it = _attachments.find(boneName);
|
|
|
|
if (it != _attachments.end())
|
|
|
|
return it->second;
|
|
|
|
|
2014-08-18 11:13:08 +08:00
|
|
|
if (_skeleton)
|
2014-07-29 10:49:06 +08:00
|
|
|
{
|
2014-08-18 11:13:08 +08:00
|
|
|
auto bone = _skeleton->getBoneByName(boneName);
|
2014-07-29 10:49:06 +08:00
|
|
|
auto attachNode = AttachNode::create(bone);
|
|
|
|
addChild(attachNode);
|
|
|
|
_attachments[boneName] = attachNode;
|
|
|
|
return attachNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3D::removeAttachNode(const std::string& boneName)
|
|
|
|
{
|
|
|
|
auto it = _attachments.find(boneName);
|
|
|
|
if (it != _attachments.end())
|
|
|
|
{
|
|
|
|
removeChild(it->second);
|
|
|
|
_attachments.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3D::removeAllAttachNode()
|
|
|
|
{
|
|
|
|
for (auto& it : _attachments) {
|
|
|
|
removeChild(it.second);
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
2014-07-29 10:49:06 +08:00
|
|
|
_attachments.clear();
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
void Sprite3D::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
2014-08-16 14:16:17 +08:00
|
|
|
if (_skeleton)
|
|
|
|
_skeleton->updateBoneMatrix();
|
2014-07-30 18:32:34 +08:00
|
|
|
|
2014-05-19 05:49:16 +08:00
|
|
|
Color4F color(getDisplayedColor());
|
|
|
|
color.a = getDisplayedOpacity() / 255.0f;
|
|
|
|
|
2014-08-16 14:16:17 +08:00
|
|
|
int i = 0;
|
2014-08-22 13:25:26 +08:00
|
|
|
for (auto& mesh : _meshes) {
|
|
|
|
if (!mesh->isVisible())
|
2014-08-19 15:25:00 +08:00
|
|
|
{
|
|
|
|
i++;
|
2014-07-29 10:49:06 +08:00
|
|
|
continue;
|
2014-08-19 15:25:00 +08:00
|
|
|
}
|
2014-08-22 13:25:26 +08:00
|
|
|
auto programstate = mesh->getGLProgramState();
|
|
|
|
auto& meshCommand = mesh->getMeshCommand();
|
2014-07-29 10:49:06 +08:00
|
|
|
|
2014-08-22 13:25:26 +08:00
|
|
|
GLuint textureID = mesh->getTexture() ? mesh->getTexture()->getName() : 0;
|
2014-07-29 10:49:06 +08:00
|
|
|
|
2014-08-22 13:25:26 +08:00
|
|
|
meshCommand.init(_globalZOrder, textureID, programstate, _blend, mesh->getVertexBuffer(), mesh->getIndexBuffer(), mesh->getPrimitiveType(), mesh->getIndexFormat(), mesh->getIndexCount(), transform);
|
|
|
|
|
|
|
|
auto skin = mesh->getSkin();
|
2014-08-16 14:16:17 +08:00
|
|
|
if (skin)
|
2014-07-29 10:49:06 +08:00
|
|
|
{
|
2014-08-16 14:16:17 +08:00
|
|
|
meshCommand.setMatrixPaletteSize((int)skin->getMatrixPaletteSize());
|
|
|
|
meshCommand.setMatrixPalette(skin->getMatrixPalette());
|
2014-07-29 10:49:06 +08:00
|
|
|
}
|
|
|
|
//support tint and fade
|
|
|
|
meshCommand.setDisplayColor(Vec4(color.r, color.g, color.b, color.a));
|
2014-09-18 12:00:44 +08:00
|
|
|
meshCommand.setTransparent(mesh->_isTransparent);
|
|
|
|
renderer->addCommand(&meshCommand);
|
2014-06-05 16:36:01 +08:00
|
|
|
}
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
|
|
|
|
2014-08-20 19:01:04 +08:00
|
|
|
void Sprite3D::setGLProgramState(GLProgramState *glProgramState)
|
|
|
|
{
|
|
|
|
Node::setGLProgramState(glProgramState);
|
2014-08-22 13:25:26 +08:00
|
|
|
for (auto& state : _meshes) {
|
2014-08-20 19:01:04 +08:00
|
|
|
state->setGLProgramState(glProgramState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void Sprite3D::setGLProgram(GLProgram *glprogram)
|
|
|
|
{
|
|
|
|
Node::setGLProgram(glprogram);
|
|
|
|
setGLProgramState(getGLProgramState());
|
|
|
|
}
|
|
|
|
|
2014-05-19 05:49:16 +08:00
|
|
|
void Sprite3D::setBlendFunc(const BlendFunc &blendFunc)
|
|
|
|
{
|
|
|
|
if(_blend.src != blendFunc.src || _blend.dst != blendFunc.dst)
|
|
|
|
{
|
|
|
|
_blend = blendFunc;
|
2014-08-22 13:25:26 +08:00
|
|
|
for(auto& state : _meshes)
|
2014-08-20 19:01:04 +08:00
|
|
|
{
|
|
|
|
state->setBlendFunc(blendFunc);
|
|
|
|
}
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const BlendFunc& Sprite3D::getBlendFunc() const
|
|
|
|
{
|
|
|
|
return _blend;
|
|
|
|
}
|
|
|
|
|
2014-08-18 17:02:13 +08:00
|
|
|
const AABB& Sprite3D::getAABB() const
|
2014-08-08 17:54:39 +08:00
|
|
|
{
|
2014-08-12 13:45:06 +08:00
|
|
|
Mat4 nodeToWorldTransform(getNodeToWorldTransform());
|
2014-08-08 17:54:39 +08:00
|
|
|
|
2014-08-12 13:45:06 +08:00
|
|
|
// If nodeToWorldTransform matrix isn't changed, we don't need to transform aabb.
|
2014-08-18 17:02:13 +08:00
|
|
|
if (memcmp(_nodeToWorldTransform.m, nodeToWorldTransform.m, sizeof(Mat4)) == 0 && !_aabbDirty)
|
2014-08-08 17:54:39 +08:00
|
|
|
{
|
2014-08-12 13:45:06 +08:00
|
|
|
return _aabb;
|
2014-08-08 17:54:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-18 17:02:13 +08:00
|
|
|
_aabb.reset();
|
2014-08-12 13:45:06 +08:00
|
|
|
Mat4 transform(nodeToWorldTransform);
|
2014-08-22 13:25:26 +08:00
|
|
|
for (const auto& it : _meshes) {
|
2014-08-18 17:02:13 +08:00
|
|
|
if (it->isVisible())
|
|
|
|
_aabb.merge(it->getAABB());
|
|
|
|
}
|
2014-08-12 13:45:06 +08:00
|
|
|
|
|
|
|
_aabb.transform(transform);
|
|
|
|
_nodeToWorldTransform = nodeToWorldTransform;
|
2014-08-08 17:54:39 +08:00
|
|
|
}
|
|
|
|
|
2014-08-12 13:45:06 +08:00
|
|
|
return _aabb;
|
2014-08-08 17:54:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Rect Sprite3D::getBoundingBox() const
|
|
|
|
{
|
|
|
|
AABB aabb = getAABB();
|
|
|
|
Rect ret(aabb._min.x, aabb._min.y, (aabb._max.x - aabb._min.x), (aabb._max.y - aabb._min.y));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-08-13 17:38:16 +08:00
|
|
|
void Sprite3D::setCullFace(GLenum cullFace)
|
|
|
|
{
|
2014-08-22 13:25:26 +08:00
|
|
|
for (auto& it : _meshes) {
|
2014-08-20 18:20:19 +08:00
|
|
|
it->getMeshCommand().setCullFace(cullFace);
|
2014-08-13 17:38:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3D::setCullFaceEnabled(bool enable)
|
|
|
|
{
|
2014-08-22 13:25:26 +08:00
|
|
|
for (auto& it : _meshes) {
|
2014-08-20 18:20:19 +08:00
|
|
|
it->getMeshCommand().setCullFaceEnabled(enable);
|
2014-08-13 17:38:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-22 13:25:26 +08:00
|
|
|
Mesh* Sprite3D::getMeshByIndex(int index) const
|
2014-08-18 11:13:08 +08:00
|
|
|
{
|
2014-08-22 13:25:26 +08:00
|
|
|
CCASSERT(index < _meshes.size(), "invald index");
|
|
|
|
return _meshes.at(index);
|
2014-08-18 11:13:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**get SubMeshState by Name */
|
2014-08-22 13:25:26 +08:00
|
|
|
Mesh* Sprite3D::getMeshByName(const std::string& name) const
|
2014-08-18 11:13:08 +08:00
|
|
|
{
|
2014-08-22 13:25:26 +08:00
|
|
|
for (const auto& it : _meshes) {
|
2014-08-18 11:13:08 +08:00
|
|
|
if (it->getName() == name)
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
MeshSkin* Sprite3D::getSkin() const
|
|
|
|
{
|
2014-08-22 13:25:26 +08:00
|
|
|
for (const auto& it : _meshes) {
|
2014-08-18 11:13:08 +08:00
|
|
|
if (it->getSkin())
|
|
|
|
return it->getSkin();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-08-19 11:56:09 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
Sprite3DCache* Sprite3DCache::_cacheInstance = nullptr;
|
|
|
|
Sprite3DCache* Sprite3DCache::getInstance()
|
|
|
|
{
|
|
|
|
if (_cacheInstance == nullptr)
|
2014-08-28 07:31:57 +08:00
|
|
|
_cacheInstance = new (std::nothrow) Sprite3DCache();
|
2014-08-19 11:56:09 +08:00
|
|
|
return _cacheInstance;
|
|
|
|
}
|
|
|
|
void Sprite3DCache::destroyInstance()
|
|
|
|
{
|
|
|
|
if (_cacheInstance)
|
|
|
|
{
|
|
|
|
delete _cacheInstance;
|
|
|
|
_cacheInstance = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Sprite3DCache::Sprite3DData* Sprite3DCache::getSpriteData(const std::string& key) const
|
|
|
|
{
|
|
|
|
auto it = _spriteDatas.find(key);
|
|
|
|
if (it != _spriteDatas.end())
|
|
|
|
return it->second;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Sprite3DCache::addSprite3DData(const std::string& key, Sprite3DCache::Sprite3DData* spritedata)
|
|
|
|
{
|
|
|
|
auto it = _spriteDatas.find(key);
|
|
|
|
if (it == _spriteDatas.end())
|
|
|
|
{
|
|
|
|
_spriteDatas[key] = spritedata;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3DCache::removeSprite3DData(const std::string& key)
|
|
|
|
{
|
|
|
|
auto it = _spriteDatas.find(key);
|
|
|
|
if (it != _spriteDatas.end())
|
|
|
|
{
|
|
|
|
delete it->second;
|
|
|
|
}
|
|
|
|
_spriteDatas.erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3DCache::removeAllSprite3DData()
|
|
|
|
{
|
|
|
|
for (auto& it : _spriteDatas) {
|
|
|
|
delete it.second;
|
|
|
|
}
|
|
|
|
_spriteDatas.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
Sprite3DCache::Sprite3DCache()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
Sprite3DCache::~Sprite3DCache()
|
|
|
|
{
|
|
|
|
removeAllSprite3DData();
|
|
|
|
}
|
|
|
|
|
2014-05-19 05:49:16 +08:00
|
|
|
NS_CC_END
|