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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __CCSPRITE3D_H__
|
|
|
|
#define __CCSPRITE3D_H__
|
|
|
|
|
2014-07-29 10:49:06 +08:00
|
|
|
#include <unordered_map>
|
2014-05-19 05:49:16 +08:00
|
|
|
|
|
|
|
#include "base/CCVector.h"
|
|
|
|
#include "base/ccTypes.h"
|
|
|
|
#include "base/CCProtocols.h"
|
|
|
|
#include "2d/CCNode.h"
|
|
|
|
#include "renderer/CCMeshCommand.h"
|
2014-09-17 13:42:08 +08:00
|
|
|
#include "renderer/CCGLProgramState.h"
|
2014-08-29 15:39:52 +08:00
|
|
|
#include "3d/CCSkeleton3D.h" // need to include for lua-binding
|
2014-08-15 20:52:45 +08:00
|
|
|
#include "3d/CCAABB.h"
|
|
|
|
#include "3d/CCBundle3DData.h"
|
2014-08-22 13:52:03 +08:00
|
|
|
#include "3d/CCMeshVertexIndexData.h"
|
2014-05-19 05:49:16 +08:00
|
|
|
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2015-03-27 14:10:41 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup _3d
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2014-05-19 05:49:16 +08:00
|
|
|
class Mesh;
|
|
|
|
class Texture2D;
|
2014-06-04 18:17:09 +08:00
|
|
|
class MeshSkin;
|
2014-07-29 10:49:06 +08:00
|
|
|
class AttachNode;
|
2014-08-15 17:45:37 +08:00
|
|
|
struct NodeData;
|
2015-09-09 11:37:41 +08:00
|
|
|
/** @brief Sprite3D: A sprite can be loaded from 3D model files, .obj, .c3t, .c3b, then can be drawn as sprite */
|
2014-10-09 09:30:23 +08:00
|
|
|
class CC_DLL Sprite3D : public Node, public BlendProtocol
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
|
|
|
public:
|
2015-01-14 08:41:24 +08:00
|
|
|
/**
|
|
|
|
* Creates an empty sprite3D without 3D model and texture.
|
|
|
|
*
|
|
|
|
* @return An autoreleased sprite3D object.
|
|
|
|
*/
|
|
|
|
static Sprite3D* create();
|
|
|
|
|
2014-06-19 14:11:14 +08:00
|
|
|
/** creates a Sprite3D*/
|
2014-05-19 05:49:16 +08:00
|
|
|
static Sprite3D* create(const std::string &modelPath);
|
2014-08-16 09:59:08 +08:00
|
|
|
|
2014-05-19 05:49:16 +08:00
|
|
|
// creates a Sprite3D. It only supports one texture, and overrides the internal texture with 'texturePath'
|
|
|
|
static Sprite3D* create(const std::string &modelPath, const std::string &texturePath);
|
|
|
|
|
2014-12-05 16:44:49 +08:00
|
|
|
/** create 3d sprite asynchronously
|
2014-12-05 16:11:14 +08:00
|
|
|
* If the 3d model was previously loaded, it will create a new 3d sprite and the callback will be called at once.
|
2015-09-09 11:37:41 +08:00
|
|
|
* Otherwise it will load the model file in a new thread, and when the 3d sprite is loaded, the callback will be called with the created Sprite3D and a user-defined parameter.
|
2014-12-03 17:40:27 +08:00
|
|
|
* The callback will be called from the main thread, so it is safe to create any cocos2d object from the callback.
|
2014-12-05 16:11:14 +08:00
|
|
|
* @param modelPath model to be loaded
|
|
|
|
* @param callback callback after loading
|
|
|
|
* @param callbackparam user defined parameter for the callback
|
2014-12-03 17:40:27 +08:00
|
|
|
*/
|
2014-12-05 15:01:30 +08:00
|
|
|
static void createAsync(const std::string &modelPath, const std::function<void(Sprite3D*, void*)>& callback, void* callbackparam);
|
2014-12-03 17:40:27 +08:00
|
|
|
|
2014-12-05 15:01:30 +08:00
|
|
|
static void createAsync(const std::string &modelPath, const std::string &texturePath, const std::function<void(Sprite3D*, void*)>& callback, void* callbackparam);
|
2014-12-03 17:40:27 +08:00
|
|
|
|
2014-07-29 10:49:06 +08:00
|
|
|
/**set texture, set the first if multiple textures exist*/
|
2014-05-19 05:49:16 +08:00
|
|
|
void setTexture(const std::string& texFile);
|
|
|
|
void setTexture(Texture2D* texture);
|
2014-07-29 10:49:06 +08:00
|
|
|
|
2014-10-25 12:27:03 +08:00
|
|
|
/**get Mesh by index*/
|
2014-08-22 13:25:26 +08:00
|
|
|
Mesh* getMeshByIndex(int index) const;
|
2014-08-18 11:13:08 +08:00
|
|
|
|
2014-10-25 12:27:03 +08:00
|
|
|
/**get Mesh by Name, it returns the first one if there are more than one mesh with the same name */
|
2014-08-22 13:25:26 +08:00
|
|
|
Mesh* getMeshByName(const std::string& name) const;
|
2014-10-25 12:27:03 +08:00
|
|
|
|
2015-03-30 15:47:47 +08:00
|
|
|
/**
|
|
|
|
* get mesh array by name, returns all meshes with the given name
|
|
|
|
*
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2014-10-25 12:27:03 +08:00
|
|
|
std::vector<Mesh*> getMeshArrayByName(const std::string& name) const;
|
2014-05-19 05:49:16 +08:00
|
|
|
|
2014-06-19 14:11:14 +08:00
|
|
|
/**get mesh*/
|
2014-08-18 11:13:08 +08:00
|
|
|
Mesh* getMesh() const { return _meshes.at(0); }
|
2014-06-04 18:17:09 +08:00
|
|
|
|
2014-10-24 15:57:47 +08:00
|
|
|
/** get mesh count */
|
|
|
|
ssize_t getMeshCount() const { return _meshes.size(); }
|
|
|
|
|
2014-06-19 14:11:14 +08:00
|
|
|
/**get skin*/
|
2014-08-18 11:13:08 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE MeshSkin* getSkin() const;
|
2014-07-29 10:49:06 +08:00
|
|
|
|
2014-08-16 16:56:04 +08:00
|
|
|
Skeleton3D* getSkeleton() const { return _skeleton; }
|
|
|
|
|
2014-07-29 10:49:06 +08:00
|
|
|
/**get AttachNode by bone name, return nullptr if not exist*/
|
|
|
|
AttachNode* getAttachNode(const std::string& boneName);
|
|
|
|
|
|
|
|
/**remove attach node*/
|
|
|
|
void removeAttachNode(const std::string& boneName);
|
|
|
|
|
|
|
|
/**remove all attach nodes*/
|
|
|
|
void removeAllAttachNode();
|
2014-05-19 05:49:16 +08:00
|
|
|
|
|
|
|
// overrides
|
|
|
|
virtual void setBlendFunc(const BlendFunc &blendFunc) override;
|
|
|
|
virtual const BlendFunc &getBlendFunc() const override;
|
2014-08-13 17:38:16 +08:00
|
|
|
|
2014-08-20 19:01:04 +08:00
|
|
|
// overrides
|
|
|
|
/** set GLProgramState, you should bind attributes by yourself */
|
|
|
|
virtual void setGLProgramState(GLProgramState *glProgramState) override;
|
2015-09-09 11:37:41 +08:00
|
|
|
/** just remember bind attributes */
|
2014-08-20 19:01:04 +08:00
|
|
|
virtual void setGLProgram(GLProgram *glprogram) override;
|
|
|
|
|
2014-08-08 17:54:39 +08:00
|
|
|
/*
|
|
|
|
* Get AABB
|
2015-09-09 11:37:41 +08:00
|
|
|
* If the sprite has animation, it can't be calculated accurately,
|
2014-08-08 17:54:39 +08:00
|
|
|
* because bone can drive the vertices, we just use the origin vertices
|
|
|
|
* to calculate the AABB.
|
|
|
|
*/
|
2014-08-18 17:02:13 +08:00
|
|
|
const AABB& getAABB() const;
|
2014-08-08 17:54:39 +08:00
|
|
|
|
2015-04-03 18:04:33 +08:00
|
|
|
/*
|
|
|
|
* Get AABB Recursively
|
|
|
|
* Because some times we may have an empty Sprite3D Node as parent, but
|
|
|
|
* the Sprite3D don't contain any meshes, so getAABB()
|
|
|
|
* will return a wrong value at that time.
|
|
|
|
*/
|
2015-04-08 16:40:04 +08:00
|
|
|
AABB getAABBRecursively();
|
2015-04-03 18:04:33 +08:00
|
|
|
|
2015-01-28 09:05:51 +08:00
|
|
|
/**
|
|
|
|
* Executes an action, and returns the action that is executed. For Sprite3D special logic are needed to take care of Fading.
|
|
|
|
*
|
|
|
|
* This node becomes the action's target. Refer to Action::getTarget()
|
|
|
|
* @warning Actions don't retain their target.
|
|
|
|
*
|
|
|
|
* @return An Action pointer
|
|
|
|
*/
|
|
|
|
virtual Action* runAction(Action* action) override;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Force to write to depth buffer, this is useful if you want to achieve effects like fading.
|
|
|
|
*/
|
|
|
|
void setForceDepthWrite(bool value) { _forceDepthWrite = value; }
|
|
|
|
bool isForceDepthWrite() const { return _forceDepthWrite;};
|
|
|
|
|
2014-08-08 17:54:39 +08:00
|
|
|
/**
|
|
|
|
* Returns 2d bounding-box
|
2015-09-09 11:37:41 +08:00
|
|
|
* Note: the bounding-box is just get from the AABB which as Z=0, so that is not very accurate.
|
2014-08-08 17:54:39 +08:00
|
|
|
*/
|
|
|
|
virtual Rect getBoundingBox() const override;
|
2014-05-19 05:49:16 +08:00
|
|
|
|
2014-08-13 17:38:16 +08:00
|
|
|
// set which face is going to cull, GL_BACK, GL_FRONT, GL_FRONT_AND_BACK, default GL_BACK
|
|
|
|
void setCullFace(GLenum cullFace);
|
|
|
|
// set cull face enable or not
|
|
|
|
void setCullFaceEnabled(bool enable);
|
2014-09-19 14:50:20 +08:00
|
|
|
|
2014-09-22 11:14:30 +08:00
|
|
|
/** light mask getter & setter, light works only when _lightmask & light's flag is true, default value of _lightmask is 0xffff */
|
2014-09-26 14:13:26 +08:00
|
|
|
void setLightMask(unsigned int mask) { _lightMask = mask; }
|
|
|
|
unsigned int getLightMask() const { return _lightMask; }
|
2015-02-11 18:14:22 +08:00
|
|
|
|
|
|
|
/**draw*/
|
|
|
|
virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override;
|
2014-05-19 05:49:16 +08:00
|
|
|
|
2015-05-06 04:07:32 +08:00
|
|
|
/** Adds a new material to the sprite.
|
|
|
|
The Material will be applied to all the meshes that belong to the sprite.
|
|
|
|
Internally it will call `setMaterial(material,-1)`
|
|
|
|
*/
|
|
|
|
void setMaterial(Material* material);
|
|
|
|
|
|
|
|
/** Adds a new material to a particular mesh of the sprite.
|
|
|
|
meshIndex is the mesh that will be applied to.
|
|
|
|
if meshIndex == -1, then it will be applied to all the meshes that belong to the sprite.
|
|
|
|
*/
|
|
|
|
void setMaterial(Material* material, int meshIndex);
|
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
/** Adds a new material to a particular mesh of the sprite.
|
|
|
|
meshIndex is the mesh that will be applied to.
|
|
|
|
if meshIndex == -1, then it will be applied to all the meshes that belong to the sprite.
|
|
|
|
*/
|
|
|
|
Material* getMaterial(int meshIndex) const;
|
2015-07-01 14:24:22 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* force set this Sprite3D to 2D render queue
|
|
|
|
*/
|
|
|
|
void setForce2DQueue(bool force2D);
|
2015-05-12 12:31:33 +08:00
|
|
|
|
2015-11-13 17:44:23 +08:00
|
|
|
Vector<Mesh*>& getMeshes() { return _meshes; }
|
|
|
|
const AABB& getAABB(bool world) const;
|
|
|
|
|
2014-06-17 19:18:56 +08:00
|
|
|
CC_CONSTRUCTOR_ACCESS:
|
|
|
|
|
2014-05-19 05:49:16 +08:00
|
|
|
Sprite3D();
|
|
|
|
virtual ~Sprite3D();
|
2015-01-14 08:41:24 +08:00
|
|
|
|
2015-03-16 23:39:43 +08:00
|
|
|
virtual bool init() override;
|
2015-01-14 08:41:24 +08:00
|
|
|
|
2014-05-19 05:49:16 +08:00
|
|
|
bool initWithFile(const std::string &path);
|
|
|
|
|
2014-08-18 14:16:34 +08:00
|
|
|
bool initFrom(const NodeDatas& nodedatas, const MeshDatas& meshdatas, const MaterialDatas& materialdatas);
|
|
|
|
|
2014-07-29 10:49:06 +08:00
|
|
|
/**load sprite3d from cache, return true if succeed, false otherwise*/
|
|
|
|
bool loadFromCache(const std::string& path);
|
|
|
|
|
2014-12-03 17:40:27 +08:00
|
|
|
/** load file and set it to meshedatas, nodedatas and materialdatas, obj file .mtl file should be at the same directory if exist */
|
|
|
|
bool loadFromFile(const std::string& path, NodeDatas* nodedatas, MeshDatas* meshdatas, MaterialDatas* materialdatas);
|
2014-05-19 05:49:16 +08:00
|
|
|
|
2015-01-13 12:06:50 +08:00
|
|
|
/**
|
|
|
|
* Visits this Sprite3D's children and draw them recursively.
|
|
|
|
* Note: all its children will rendered as 3D objects
|
|
|
|
*/
|
2015-01-13 14:52:20 +08:00
|
|
|
virtual void visit(Renderer *renderer, const Mat4& parentTransform, uint32_t parentFlags) override;
|
2015-01-13 12:06:50 +08:00
|
|
|
|
2015-09-17 14:45:16 +08:00
|
|
|
/**generate default material*/
|
|
|
|
void genMaterial(bool useLight = false);
|
2014-08-18 11:13:08 +08:00
|
|
|
|
2014-08-18 14:16:34 +08:00
|
|
|
void createNode(NodeData* nodedata, Node* root, const MaterialDatas& matrialdatas, bool singleSprite);
|
2014-08-21 11:23:31 +08:00
|
|
|
void createAttachSprite3DNode(NodeData* nodedata,const MaterialDatas& matrialdatas);
|
|
|
|
Sprite3D* createSprite3DNode(NodeData* nodedata,ModelData* modeldata,const MaterialDatas& matrialdatas);
|
2014-08-22 13:25:26 +08:00
|
|
|
|
|
|
|
/**get MeshIndexData by Id*/
|
|
|
|
MeshIndexData* getMeshIndexData(const std::string& indexId) const;
|
|
|
|
|
2015-05-06 04:07:32 +08:00
|
|
|
void addMesh(Mesh* mesh);
|
2014-08-18 14:16:34 +08:00
|
|
|
|
2014-08-18 17:02:13 +08:00
|
|
|
void onAABBDirty() { _aabbDirty = true; }
|
|
|
|
|
2014-12-04 18:31:10 +08:00
|
|
|
void afterAsyncLoad(void* param);
|
2015-07-09 18:05:47 +08:00
|
|
|
|
|
|
|
static AABB getAABBRecursivelyImp(Node *node);
|
2014-12-03 17:40:27 +08:00
|
|
|
|
2014-06-17 19:18:56 +08:00
|
|
|
protected:
|
2014-08-18 11:13:08 +08:00
|
|
|
|
2014-07-30 18:32:34 +08:00
|
|
|
Skeleton3D* _skeleton; //skeleton
|
2014-07-29 10:49:06 +08:00
|
|
|
|
2014-08-22 13:25:26 +08:00
|
|
|
Vector<MeshVertexData*> _meshVertexDatas;
|
2014-07-29 10:49:06 +08:00
|
|
|
|
|
|
|
std::unordered_map<std::string, AttachNode*> _attachments;
|
|
|
|
|
|
|
|
BlendFunc _blend;
|
2014-08-15 14:58:30 +08:00
|
|
|
|
2014-08-20 18:20:19 +08:00
|
|
|
Vector<Mesh*> _meshes;
|
2014-08-15 16:36:17 +08:00
|
|
|
|
2014-08-12 13:45:06 +08:00
|
|
|
mutable AABB _aabb; // cache current aabb
|
|
|
|
mutable Mat4 _nodeToWorldTransform; // cache the matrix
|
2015-04-01 10:57:19 +08:00
|
|
|
mutable bool _aabbDirty;
|
2014-09-26 14:13:26 +08:00
|
|
|
unsigned int _lightMask;
|
2014-09-25 17:13:46 +08:00
|
|
|
bool _shaderUsingLight; // is current shader using light ?
|
2015-01-28 09:05:51 +08:00
|
|
|
bool _forceDepthWrite; // Always write to depth buffer
|
2015-05-12 12:31:33 +08:00
|
|
|
bool _usingAutogeneratedGLProgram;
|
2014-12-03 17:40:27 +08:00
|
|
|
|
|
|
|
struct AsyncLoadParam
|
|
|
|
{
|
2014-12-05 15:01:30 +08:00
|
|
|
std::function<void(Sprite3D*, void*)> afterLoadCallback; // callback after load
|
|
|
|
void* callbackParam;
|
2014-12-03 17:40:27 +08:00
|
|
|
bool result; // sprite load result
|
2014-12-22 17:40:02 +08:00
|
|
|
std::string modlePath;
|
2014-12-04 18:31:10 +08:00
|
|
|
std::string texPath; //
|
2014-12-03 17:40:27 +08:00
|
|
|
MeshDatas* meshdatas;
|
|
|
|
MaterialDatas* materialdatas;
|
|
|
|
NodeDatas* nodeDatas;
|
|
|
|
};
|
|
|
|
AsyncLoadParam _asyncLoadParam;
|
2014-05-19 05:49:16 +08:00
|
|
|
};
|
|
|
|
|
2014-08-19 11:56:09 +08:00
|
|
|
///////////////////////////////////////////////////////
|
2015-03-20 17:20:22 +08:00
|
|
|
/**
|
|
|
|
* Sprite3DCache
|
|
|
|
* @brief the cache data of Sprite3D, use to speed up Sprite3D::create
|
|
|
|
*/
|
2014-12-19 11:30:51 +08:00
|
|
|
class CC_DLL Sprite3DCache
|
2014-08-19 11:56:09 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
struct Sprite3DData
|
|
|
|
{
|
2014-08-22 13:25:26 +08:00
|
|
|
Vector<MeshVertexData*> meshVertexDatas;
|
2014-09-17 13:42:08 +08:00
|
|
|
Vector<GLProgramState*> glProgramStates;
|
2014-08-19 11:56:09 +08:00
|
|
|
NodeDatas* nodedatas;
|
|
|
|
MaterialDatas* materialdatas;
|
|
|
|
~Sprite3DData()
|
|
|
|
{
|
|
|
|
if (nodedatas)
|
|
|
|
delete nodedatas;
|
|
|
|
if (materialdatas)
|
|
|
|
delete materialdatas;
|
2014-08-22 13:25:26 +08:00
|
|
|
meshVertexDatas.clear();
|
2014-09-17 13:42:08 +08:00
|
|
|
glProgramStates.clear();
|
2014-08-19 11:56:09 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**get & destroy*/
|
|
|
|
static Sprite3DCache* getInstance();
|
|
|
|
static void destroyInstance();
|
2015-03-20 17:20:22 +08:00
|
|
|
|
2015-03-30 15:47:47 +08:00
|
|
|
/**
|
|
|
|
* get the SpriteData struct
|
|
|
|
*
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2014-08-19 11:56:09 +08:00
|
|
|
Sprite3DData* getSpriteData(const std::string& key) const;
|
|
|
|
|
2015-03-30 15:47:47 +08:00
|
|
|
/**
|
|
|
|
* add the SpriteData into Sprite3D by given the specified key
|
|
|
|
*
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2014-08-19 11:56:09 +08:00
|
|
|
bool addSprite3DData(const std::string& key, Sprite3DData* spritedata);
|
|
|
|
|
2015-03-20 17:20:22 +08:00
|
|
|
/**remove the SpriteData from Sprite3D by given the specified key*/
|
2014-08-19 11:56:09 +08:00
|
|
|
void removeSprite3DData(const std::string& key);
|
|
|
|
|
2015-03-20 17:20:22 +08:00
|
|
|
/**remove all the SpriteData from Sprite3D*/
|
2014-08-19 11:56:09 +08:00
|
|
|
void removeAllSprite3DData();
|
|
|
|
|
|
|
|
CC_CONSTRUCTOR_ACCESS:
|
|
|
|
Sprite3DCache();
|
|
|
|
~Sprite3DCache();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
|
|
|
|
static Sprite3DCache* _cacheInstance;
|
2015-09-09 11:37:41 +08:00
|
|
|
std::unordered_map<std::string, Sprite3DData*> _spriteDatas; //cached sprite data
|
2014-08-19 11:56:09 +08:00
|
|
|
};
|
|
|
|
|
2015-04-01 10:23:53 +08:00
|
|
|
// end of 3d group
|
2015-03-27 14:10:41 +08:00
|
|
|
/// @}
|
2014-05-19 05:49:16 +08:00
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
#endif // __SPRITE3D_H_
|