sprite3d with skin render&bundle

This commit is contained in:
yangxiao 2014-06-05 16:36:01 +08:00
parent 54caf26a57
commit ed8f8a9cad
13 changed files with 312 additions and 18 deletions

View File

@ -1775,6 +1775,10 @@
B3AF01A31842FBA400A98B85 /* b2MotorJoint.h in Headers */ = {isa = PBXBuildFile; fileRef = B3AF019F1842FBA400A98B85 /* b2MotorJoint.h */; };
B3B12A5A17E7F44000026B4A /* libchipmunk Mac.a in Frameworks */ = {isa = PBXBuildFile; fileRef = A03F2CB81780BD04006731B9 /* libchipmunk Mac.a */; };
B3B12A5B17E7F45C00026B4A /* libchipmunk iOS.a in Frameworks */ = {isa = PBXBuildFile; fileRef = A07A4F3B178387670073F6A7 /* libchipmunk iOS.a */; };
B6AAF84119404E0D0069DE01 /* CCBundle3D.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B6AAF83F19404E0D0069DE01 /* CCBundle3D.cpp */; };
B6AAF84219404E0D0069DE01 /* CCBundle3D.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B6AAF83F19404E0D0069DE01 /* CCBundle3D.cpp */; };
B6AAF84319404E0D0069DE01 /* CCBundle3D.h in Headers */ = {isa = PBXBuildFile; fileRef = B6AAF84019404E0D0069DE01 /* CCBundle3D.h */; };
B6AAF84419404E0D0069DE01 /* CCBundle3D.h in Headers */ = {isa = PBXBuildFile; fileRef = B6AAF84019404E0D0069DE01 /* CCBundle3D.h */; };
B6ACD897193D6693005E0B8A /* CCMeshSkin.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B6ACD895193D6693005E0B8A /* CCMeshSkin.cpp */; };
B6ACD898193D6693005E0B8A /* CCMeshSkin.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B6ACD895193D6693005E0B8A /* CCMeshSkin.cpp */; };
B6ACD899193D6693005E0B8A /* CCMeshSkin.h in Headers */ = {isa = PBXBuildFile; fileRef = B6ACD896193D6693005E0B8A /* CCMeshSkin.h */; };
@ -2824,6 +2828,8 @@
B375104F1823AC7B00B3BA6A /* CCPhysicsWorldInfo_chipmunk.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCPhysicsWorldInfo_chipmunk.h; sourceTree = "<group>"; };
B3AF019E1842FBA400A98B85 /* b2MotorJoint.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = b2MotorJoint.cpp; sourceTree = "<group>"; };
B3AF019F1842FBA400A98B85 /* b2MotorJoint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = b2MotorJoint.h; sourceTree = "<group>"; };
B6AAF83F19404E0D0069DE01 /* CCBundle3D.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCBundle3D.cpp; sourceTree = "<group>"; };
B6AAF84019404E0D0069DE01 /* CCBundle3D.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCBundle3D.h; sourceTree = "<group>"; };
B6ACD895193D6693005E0B8A /* CCMeshSkin.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCMeshSkin.cpp; sourceTree = "<group>"; };
B6ACD896193D6693005E0B8A /* CCMeshSkin.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCMeshSkin.h; sourceTree = "<group>"; };
B6ACD89C193DC0CC005E0B8A /* CCAnimate3D.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCAnimate3D.cpp; sourceTree = "<group>"; };
@ -4843,6 +4849,8 @@
B29594B81926D61F003EEF37 /* 3d */ = {
isa = PBXGroup;
children = (
B6AAF83F19404E0D0069DE01 /* CCBundle3D.cpp */,
B6AAF84019404E0D0069DE01 /* CCBundle3D.h */,
B6ACD89C193DC0CC005E0B8A /* CCAnimate3D.cpp */,
B6ACD89D193DC0CC005E0B8A /* CCAnimate3D.h */,
B6ACD895193D6693005E0B8A /* CCMeshSkin.cpp */,
@ -5222,6 +5230,7 @@
B6B26345193884D60088FE25 /* CCAnimation3D.h in Headers */,
B6ACD899193D6693005E0B8A /* CCMeshSkin.h in Headers */,
B6ACD8A0193DC0CC005E0B8A /* CCAnimate3D.h in Headers */,
B6AAF84319404E0D0069DE01 /* CCBundle3D.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -5722,6 +5731,7 @@
B6B26346193884D60088FE25 /* CCAnimation3D.h in Headers */,
B6ACD89A193D6693005E0B8A /* CCMeshSkin.h in Headers */,
B6ACD8A1193DC0CC005E0B8A /* CCAnimate3D.h in Headers */,
B6AAF84419404E0D0069DE01 /* CCBundle3D.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -6376,6 +6386,7 @@
B6B26343193884D60088FE25 /* CCAnimation3D.cpp in Sources */,
B6ACD897193D6693005E0B8A /* CCMeshSkin.cpp in Sources */,
B6ACD89E193DC0CC005E0B8A /* CCAnimate3D.cpp in Sources */,
B6AAF84119404E0D0069DE01 /* CCBundle3D.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -6820,6 +6831,7 @@
B6B26344193884D60088FE25 /* CCAnimation3D.cpp in Sources */,
B6ACD898193D6693005E0B8A /* CCMeshSkin.cpp in Sources */,
B6ACD89F193DC0CC005E0B8A /* CCAnimate3D.cpp in Sources */,
B6AAF84219404E0D0069DE01 /* CCBundle3D.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@ -73,7 +73,7 @@ protected:
int determineIndex(float time) const;
float* _value; //
float* _keytime; //key time, start time _keytime[0], end time _keytime[_count - 1]
float* _keytime; //key time(0 - 1), start time _keytime[0], end time _keytime[_count - 1]
int _count;
int _componentSizeByte; //component size in byte, position and scale 3 * sizeof(float), rotation 4 * sizeof(float)

65
cocos/3d/CCBundle3D.cpp Normal file
View File

@ -0,0 +1,65 @@
/****************************************************************************
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 "CCBundle3D.h"
#include "CCAnimation3D.h"
#include "CCSprite3D.h"
#include "CCMeshSkin.h"
#include "base/ccMacros.h"
#include "platform/CCFileUtils.h"
NS_CC_BEGIN
Bundle3D* Bundle3D::_instance = nullptr;
Bundle3D* Bundle3D::getInstance()
{
if (_instance == nullptr)
_instance = new Bundle3D();
return _instance;
}
void Bundle3D::purgeBundle3D()
{
CC_SAFE_DELETE(_instance);
}
bool Bundle3D::load(const std::string& path)
{
return true;
}
Bundle3D::Bundle3D()
{
}
Bundle3D::~Bundle3D()
{
}
NS_CC_END

114
cocos/3d/CCBundle3D.h Normal file
View File

@ -0,0 +1,114 @@
/****************************************************************************
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 __CCBUNDLE3D_H__
#define __CCBUNDLE3D_H__
#include <map>
#include "base/ccMacros.h"
#include "base/CCRef.h"
#include "base/ccTypes.h"
#include "3d/CCMesh.h"
NS_CC_BEGIN
class Animation3D;
class Bundle3D
{
public:
struct MeshData
{
float* vertex;
int vertexSizeInFloat;
unsigned short* indices;
int numIndex;
MeshVertexAttrib* attribs;
int attribCount;
};
struct SkinData
{
Mat4 bindShape;
std::vector<std::string> boneNames;
std::vector<Mat4> inverseBindPoseMatrices; //bind pose of bone
std::map<int, std::vector<int> > boneChild;//key parent, value child
int rootBoneIndex;
};
struct MaterialData
{
std::string texturePath;
};
struct Animation3DData
{
Animation3D* animation;
};
static Bundle3D* getInstance();
static void purgeBundle3D();
bool load(const std::string& path);
//
/**
* load mesh data from bundle
* @param id The ID of the mesh, load the first Mesh in the bundle
*/
bool loadMeshData(const std::string& id, MeshData* meshdata);
//
/**
* load skin data from bundle
* @param id The ID of the skin, load the first Skin in the bundle
*/
bool loadSkinData(const std::string& id, SkinData* skindata);
//
/**
* load material data from bundle
* @param id The ID of the material, load the first Material in the bundle
*/
bool loadMaterialData(const std::string& id, MaterialData* materialdata);
/**
* load material data from bundle
* @param id The ID of the animation, load the first animation in the bundle
*/
bool loadAnimationData(const std::string& id, Animation3DData* animationdata);
protected:
Bundle3D();
~Bundle3D();
static Bundle3D* _instance;
bool _isBinary;
};
NS_CC_END
#endif // __CCANIMATE3D_H__

View File

@ -27,6 +27,7 @@
#include "3d/CCMesh.h"
#include "3d/CCObjLoader.h"
#include "3d/CCMeshSkin.h"
#include "3d/CCBundle3D.h"
#include "base/CCDirector.h"
#include "base/CCPlatformMacros.h"
@ -136,15 +137,15 @@ bool Sprite3D::loadFromObj(const std::string& path)
return true;
}
bool Sprite3D::loadFromC3b(const std::string& path)
bool Sprite3D::loadFromC3x(const std::string& path)
{
//load from .c3b
return true;
}
bool Sprite3D::loadFromC3t(const std::string& path)
{
//load from .c3t
//load from .c3b or .c3t
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(path);
if (!Bundle3D::getInstance()->load(fullPath))
return false;
//Bundle3D::getInstance()->loadMeshData(<#const std::string &id#>, <#cocos2d::Bundle3D::MeshData *meshdata#>)
return true;
}
@ -193,13 +194,9 @@ bool Sprite3D::initWithFile(const std::string &path)
{
return loadFromObj(path);
}
else if (ext == ".c3b")
else if (ext == ".c3b" || ext == ".c3t")
{
return loadFromC3b(path);
}
else if (ext == ".c3t")
{
return loadFromC3t(path);
return loadFromC3x(path);
}
return false;
@ -227,8 +224,14 @@ void Sprite3D::genGLProgramState()
GLProgram* Sprite3D::getDefaultGLProgram(bool textured)
{
bool hasSkin = _skin && _mesh->hasVertexAttrib(GLProgram::VERTEX_ATTRIB_BLEND_INDEX)
&& _mesh->hasVertexAttrib(GLProgram::VERTEX_ATTRIB_BLEND_WEIGHT);
if(textured)
{
if (hasSkin)
return GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_3D_SKINPOSITION_TEXTURE);
return GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_3D_POSITION_TEXTURE);
}
else
@ -276,6 +279,11 @@ void Sprite3D::draw(Renderer *renderer, const Mat4 &transform, bool transformUpd
_meshCommand.setCullFaceEnabled(true);
_meshCommand.setDepthTestEnabled(true);
if (_skin)
{
_meshCommand.setMatrixPaletteSize(_skin->getMatrixPaletteSize());
_meshCommand.setMatrixPalette(_skin->getMatrixPalette());
}
//support tint and fade
_meshCommand.setDisplayColor(Vec4(color.r, color.g, color.b, color.a));
Director::getInstance()->getRenderer()->addCommand(&_meshCommand);

View File

@ -70,9 +70,8 @@ protected:
//.mtl file should at the same directory with the same name if exist
bool loadFromObj(const std::string& path);
bool loadFromC3b(const std::string& path);
bool loadFromC3t(const std::string& path);
//load from .c3b or .c3t
bool loadFromC3x(const std::string& path);
virtual void draw(Renderer *renderer, const Mat4 &transform, bool transformUpdated) override;

View File

@ -73,6 +73,7 @@ const char* GLProgram::SHADER_NAME_LABEL_OUTLINE = "ShaderLabelOutline";
const char* GLProgram::SHADER_3D_POSITION = "Shader3DPosition";
const char* GLProgram::SHADER_3D_POSITION_TEXTURE = "Shader3DPositionTexture";
const char* GLProgram::SHADER_3D_SKINPOSITION_TEXTURE = "Shader3DSkinPositionTexture";
// uniform names

View File

@ -132,6 +132,7 @@ public:
//3D
static const char* SHADER_3D_POSITION;
static const char* SHADER_3D_POSITION_TEXTURE;
static const char* SHADER_3D_SKINPOSITION_TEXTURE;
// uniform names
static const char* UNIFORM_NAME_P_MATRIX;

View File

@ -51,6 +51,7 @@ enum {
kShaderType_LabelOutline,
kShaderType_3DPosition,
kShaderType_3DPositionTex,
kShaderType_3DSkinPositionTex,
kShaderType_MAX,
};
@ -200,6 +201,9 @@ void GLProgramCache::loadDefaultGLPrograms()
loadDefaultGLProgram(p, kShaderType_3DPositionTex);
_programs.insert( std::make_pair(GLProgram::SHADER_3D_POSITION_TEXTURE, p) );
p = new GLProgram();
loadDefaultGLProgram(p, kShaderType_3DSkinPositionTex);
_programs.insert(std::make_pair(GLProgram::SHADER_3D_SKINPOSITION_TEXTURE, p));
}
void GLProgramCache::reloadDefaultGLPrograms()
@ -297,6 +301,9 @@ void GLProgramCache::reloadDefaultGLPrograms()
p->reset();
loadDefaultGLProgram(p, kShaderType_3DPositionTex);
p = getGLProgram(GLProgram::SHADER_3D_SKINPOSITION_TEXTURE);
p->reset();
loadDefaultGLProgram(p, kShaderType_3DSkinPositionTex);
}
void GLProgramCache::loadDefaultGLProgram(GLProgram *p, int type)
@ -356,6 +363,9 @@ void GLProgramCache::loadDefaultGLProgram(GLProgram *p, int type)
case kShaderType_3DPositionTex:
p->initWithByteArrays(cc3D_PositionTex_vert, cc3D_ColorTex_frag);
break;
case kShaderType_3DSkinPositionTex:
p->initWithByteArrays(cc3D_SkinPositionTex_vert, cc3D_ColorTex_frag);
break;
default:
CCLOG("cocos2d: %s:%d, error shader type", __FUNCTION__, __LINE__);
return;

View File

@ -26,6 +26,7 @@
#include "base/CCDirector.h"
#include "renderer/CCMeshCommand.h"
#include "renderer/ccGLStateCache.h"
#include "renderer/CCGLProgram.h"
#include "renderer/CCGLProgramState.h"
#include "renderer/CCRenderer.h"
#include "renderer/CCTextureAtlas.h"
@ -42,6 +43,8 @@ MeshCommand::MeshCommand()
, _depthTestEnabled(false)
, _depthWriteEnabled(false)
, _displayColor(1.0f, 1.0f, 1.0f, 1.0f)
, _matrixPalette(nullptr)
, _matrixPaletteSize(0)
{
_type = RenderCommand::Type::MESH_COMMAND;
}
@ -145,6 +148,17 @@ void MeshCommand::execute()
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
_glProgramState->setUniformVec4("u_color", _displayColor);
if (_matrixPaletteSize && _matrixPalette)
{
auto glProgram = _glProgramState->getGLProgram();
auto uniform = glProgram->getUniform("u_matrixPalette");
if (uniform)
{
glProgram->setUniformLocationWith4fv(uniform->location, (const float*)_matrixPalette, _matrixPaletteSize);
}
}
_glProgramState->apply(_mv);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);

View File

@ -53,6 +53,10 @@ public:
void setDepthWriteEnabled(bool enable);
void setDisplayColor(const Vec4& color);
void setMatrixPalette(const Vec4* matrixPalette) { _matrixPalette = matrixPalette; }
void setMatrixPaletteSize(int size) { _matrixPaletteSize = size; }
void execute();
@ -71,6 +75,10 @@ protected:
Vec4 _displayColor; // in order to support tint and fade in fade out
// used for skin
const Vec4* _matrixPalette;
int _matrixPaletteSize;
GLuint _vertexBuffer;
GLuint _indexBuffer;
GLenum _primitive;

View File

@ -13,3 +13,64 @@ void main(void)
TextureCoordOut.y = 1.0 - TextureCoordOut.y;
}
);
const char* cc3D_SkinPositionTex_vert = STRINGIFY(
attribute vec4 a_position;
attribute vec4 a_blendWeights;
attribute vec4 a_blendIndices;
attribute vec2 a_texCoord;
const int SKINNING_JOINT_COUNT = 30;
// Uniforms
uniform vec4 u_matrixPalette[SKINNING_JOINT_COUNT * 3];
// Varyings
varying vec2 TextureCoordOut;
vec4 _skinnedPosition;
vec4 tmp;
void skinPosition(float blendWeight, int matrixIndex)
{
tmp.x = dot(a_position, u_matrixPalette[matrixIndex]);
tmp.y = dot(a_position, u_matrixPalette[matrixIndex + 1]);
tmp.z = dot(a_position, u_matrixPalette[matrixIndex + 2]);
tmp.w = a_position.w;
_skinnedPosition += blendWeight * tmp;
}
vec4 getPosition()
{
_skinnedPosition = vec4(0.0);
float blendWeight = a_blendWeights[0];
int matrixIndex = int (a_blendIndices[0]) * 3;
skinPosition(blendWeight, matrixIndex);
blendWeight = a_blendWeights[1];
matrixIndex = int(a_blendIndices[1]) * 3;
skinPosition(blendWeight, matrixIndex);
blendWeight = a_blendWeights[2];
matrixIndex = int(a_blendIndices[2]) * 3;
skinPosition(blendWeight, matrixIndex);
blendWeight = a_blendWeights[3];
matrixIndex = int(a_blendIndices[3]) * 3;
skinPosition(blendWeight, matrixIndex);
return _skinnedPosition;
}
void main()
{
vec4 position = getPosition();
gl_Position = CC_MVPMatrix * position;
TextureCoordOut = a_texCoord;
TextureCoordOut.y = 1.0 - TextureCoordOut.y;
}
);

View File

@ -70,6 +70,7 @@ extern CC_DLL const GLchar * ccLabelOutline_frag;
extern CC_DLL const GLchar * ccLabel_vert;
extern CC_DLL const GLchar * cc3D_PositionTex_vert;
extern CC_DLL const GLchar * cc3D_SkinPositionTex_vert;
extern CC_DLL const GLchar * cc3D_ColorTex_frag;
extern CC_DLL const GLchar * cc3D_Color_frag;
// end of shaders group