From 0587122ccc82dd1bb4d0c2fb8f9769b753c8fada Mon Sep 17 00:00:00 2001 From: zhukaixy Date: Thu, 19 Mar 2015 15:10:33 +0800 Subject: [PATCH 01/10] Move Skybox.shader from Sprite3DTest to Shader3D folder --- cocos/3d/CCSkybox.cpp | 2 +- .../{Sprite3DTest/skybox.frag => Shaders3D/Skybox.frag} | 0 .../{Sprite3DTest/skybox.vert => Shaders3D/Skybox.vert} | 0 3 files changed, 1 insertion(+), 1 deletion(-) rename tests/cpp-tests/Resources/{Sprite3DTest/skybox.frag => Shaders3D/Skybox.frag} (100%) rename tests/cpp-tests/Resources/{Sprite3DTest/skybox.vert => Shaders3D/Skybox.vert} (100%) diff --git a/cocos/3d/CCSkybox.cpp b/cocos/3d/CCSkybox.cpp index caf7f95a48..e32fb0e287 100644 --- a/cocos/3d/CCSkybox.cpp +++ b/cocos/3d/CCSkybox.cpp @@ -56,7 +56,7 @@ Skybox::~Skybox() bool Skybox::init() { // create and set our custom shader - auto shader = GLProgram::createWithFilenames("Sprite3DTest/skybox.vert", "Sprite3DTest/skybox.frag"); + auto shader = GLProgram::createWithFilenames("Shaders3D/Skybox.vert", "Shaders3D/Skybox.frag"); auto state = GLProgramState::create(shader); state->setVertexAttribPointer(GLProgram::ATTRIBUTE_NAME_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), nullptr); setGLProgramState(state); diff --git a/tests/cpp-tests/Resources/Sprite3DTest/skybox.frag b/tests/cpp-tests/Resources/Shaders3D/Skybox.frag similarity index 100% rename from tests/cpp-tests/Resources/Sprite3DTest/skybox.frag rename to tests/cpp-tests/Resources/Shaders3D/Skybox.frag diff --git a/tests/cpp-tests/Resources/Sprite3DTest/skybox.vert b/tests/cpp-tests/Resources/Shaders3D/Skybox.vert similarity index 100% rename from tests/cpp-tests/Resources/Sprite3DTest/skybox.vert rename to tests/cpp-tests/Resources/Shaders3D/Skybox.vert From 52280e637684627215eb7dc7031299a9bd4809c7 Mon Sep 17 00:00:00 2001 From: zhukaixy Date: Fri, 20 Mar 2015 17:53:05 +0800 Subject: [PATCH 02/10] add 3d test case(skybox and texture cube) for lua project --- cocos/cocos2d.h | 2 + .../auto/lua_cocos2dx_3d_auto.cpp | 218 ++++++++++++++++-- .../src/Sprite3DTest/Sprite3DTest.lua | 68 +++++- 3 files changed, 263 insertions(+), 25 deletions(-) diff --git a/cocos/cocos2d.h b/cocos/cocos2d.h index 4ce3defc65..48d53380df 100644 --- a/cocos/cocos2d.h +++ b/cocos/cocos2d.h @@ -285,6 +285,8 @@ THE SOFTWARE. #include "3d/CCBillBoard.h" #include "3d/CCFrustum.h" #include "3d/CCPlane.h" +#include "3d/CCTextureCube.h" +#include "3d/CCSkybox.h" // Deprecated include #include "deprecated/CCDictionary.h" diff --git a/cocos/scripting/lua-bindings/auto/lua_cocos2dx_3d_auto.cpp b/cocos/scripting/lua-bindings/auto/lua_cocos2dx_3d_auto.cpp index 9968e1800a..e500da44bb 100644 --- a/cocos/scripting/lua-bindings/auto/lua_cocos2dx_3d_auto.cpp +++ b/cocos/scripting/lua-bindings/auto/lua_cocos2dx_3d_auto.cpp @@ -1293,14 +1293,14 @@ int lua_cocos2dx_3d_Sprite3D_createAsync(lua_State* tolua_S) if (!ok) { break; } std::function arg2; do { - // Lambda binding for lua is not supported. - assert(false); - } while(0) - ; + // Lambda binding for lua is not supported. + assert(false); + } while(0) + ; if (!ok) { break; } void* arg3; #pragma warning NO CONVERSION TO NATIVE FOR void* - ok = false; + ok = false; if (!ok) { break; } cocos2d::Sprite3D::createAsync(arg0, arg1, arg2, arg3); lua_settop(tolua_S, 1); @@ -1317,14 +1317,14 @@ int lua_cocos2dx_3d_Sprite3D_createAsync(lua_State* tolua_S) if (!ok) { break; } std::function arg1; do { - // Lambda binding for lua is not supported. - assert(false); - } while(0) - ; + // Lambda binding for lua is not supported. + assert(false); + } while(0) + ; if (!ok) { break; } void* arg2; #pragma warning NO CONVERSION TO NATIVE FOR void* - ok = false; + ok = false; if (!ok) { break; } cocos2d::Sprite3D::createAsync(arg0, arg1, arg2); lua_settop(tolua_S, 1); @@ -2871,22 +2871,192 @@ int lua_register_cocos2dx_3d_BillBoard(lua_State* tolua_S) g_typeCast["BillBoard"] = "cc.BillBoard"; return 1; } -TOLUA_API int register_all_cocos2dx_3d(lua_State* tolua_S) + +int lua_cocos2dx_3d_TextureCube_create(lua_State* tolua_S) { - tolua_open(tolua_S); - - tolua_module(tolua_S,"cc",0); - tolua_beginmodule(tolua_S,"cc"); + int argc = 0; + bool ok = true; +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif - lua_register_cocos2dx_3d_Animate3D(tolua_S); - lua_register_cocos2dx_3d_Sprite3D(tolua_S); - lua_register_cocos2dx_3d_AttachNode(tolua_S); - lua_register_cocos2dx_3d_BillBoard(tolua_S); - lua_register_cocos2dx_3d_Animation3D(tolua_S); - lua_register_cocos2dx_3d_Skeleton3D(tolua_S); - lua_register_cocos2dx_3d_Mesh(tolua_S); +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertable(tolua_S, 1, "cc.TextureCube", 0, &tolua_err)) goto tolua_lerror; +#endif - tolua_endmodule(tolua_S); - return 1; + argc = lua_gettop(tolua_S) - 1; + + do + { + if (argc == 6) + { + std::string positive_x; + ok &= luaval_to_std_string(tolua_S, 2, &positive_x, "cc.TextureCube:create"); + if (!ok) { break; } + + std::string negative_x; + ok &= luaval_to_std_string(tolua_S, 3, &negative_x, "cc.TextureCube:create"); + if (!ok){ break; } + + std::string positive_y; + ok &= luaval_to_std_string(tolua_S, 4, &positive_y, "cc.TextureCube:create"); + if (!ok) { break; } + + std::string negative_y; + ok &= luaval_to_std_string(tolua_S, 5, &negative_y, "cc.TextureCube:create"); + if (!ok){ break; } + + std::string positive_z; + ok &= luaval_to_std_string(tolua_S, 6, &positive_z, "cc.TextureCube:create"); + if (!ok) { break; } + + std::string negative_z; + ok &= luaval_to_std_string(tolua_S, 7, &negative_z, "cc.TextureCube:create"); + if (!ok){ break; } + + cocos2d::TextureCube* ret = cocos2d::TextureCube::create(positive_x, negative_x, + positive_y, negative_y, + positive_z, negative_z); + object_to_luaval(tolua_S, "cc.TextureCube", (cocos2d::TextureCube*)ret); + return 1; + } + } while (0); + + luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d", "cc.TextureCube:create", argc, 6); + return 0; +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S, "#ferror in function 'lua_cocos2dx_3d_TextureCube_create'.", &tolua_err); +#endif + return 0; +} + +int lua_register_cocos2dx_3d_TextureCube(lua_State* tolua_S) +{ + tolua_usertype(tolua_S, "cc.TextureCube"); + tolua_cclass(tolua_S, "TextureCube", "cc.TextureCube", "cc.Texture2D", nullptr); + + tolua_beginmodule(tolua_S, "TextureCube"); + tolua_function(tolua_S, "create", lua_cocos2dx_3d_TextureCube_create); + tolua_endmodule(tolua_S); + std::string typeName = typeid(cocos2d::TextureCube).name(); + g_luaType[typeName] = "cc.TextureCube"; + g_typeCast["TextureCube"] = "cc.TextureCube"; + return 1; +} + +int lua_cocos2dx_3d_Skybox_create(lua_State* tolua_S) +{ + int argc = 0; + bool ok = true; +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertable(tolua_S, 1, "cc.Skybox", 0, &tolua_err)) goto tolua_lerror; +#endif + + argc = lua_gettop(tolua_S) - 1; + + do + { + if (argc == 0) + { + cocos2d::Skybox* ret = cocos2d::Skybox::create(); + object_to_luaval(tolua_S, "cc.Skybox", (cocos2d::Skybox*)ret); + return 1; + } + } while (0); + + luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d", "cc.Skybox:create", argc, 0); + return 0; +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S, "#ferror in function 'lua_cocos2dx_3d_Skybox_create'.", &tolua_err); +#endif + return 0; +} + +int lua_cocos2dx_3d_Skybox_setTexture(lua_State* tolua_S) +{ + int argc = 0; + cocos2d::Skybox* cobj = nullptr; + bool ok = true; +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S, 1, "cc.Skybox", 0, &tolua_err)) goto tolua_lerror; +#endif + cobj = (cocos2d::Skybox*)tolua_tousertype(tolua_S, 1, 0); +#if COCOS2D_DEBUG >= 1 + if (!cobj) + { + tolua_error(tolua_S, "invalid 'cobj' in function 'lua_cocos2dx_3d_Skybox_setTexture'", nullptr); + return 0; + } +#endif + argc = lua_gettop(tolua_S) - 1; + do{ + if (argc == 1) + { + cocos2d::TextureCube* arg0; + ok &= luaval_to_object(tolua_S, 2, "cc.TextureCube", &arg0); + + if (!ok) { break; } + cobj->setTexture(arg0); + lua_settop(tolua_S, 1); + return 1; + } + } while (0); + + luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d \n", "cc.Skybox:setTexture", argc, 1); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S, "#ferror in function 'lua_cocos2dx_3d_Skybox_setTexture'.", &tolua_err); +#endif + + return 0; +} + +int lua_register_cocos2dx_3d_Skybox(lua_State* tolua_S) +{ + tolua_usertype(tolua_S, "cc.Skybox"); + tolua_cclass(tolua_S, "Skybox", "cc.Skybox", "cc.Node", nullptr); + + tolua_beginmodule(tolua_S, "Skybox"); + tolua_function(tolua_S, "create", lua_cocos2dx_3d_Skybox_create); + tolua_function(tolua_S, "setTexture", lua_cocos2dx_3d_Skybox_setTexture); + tolua_endmodule(tolua_S); + + std::string typeName = typeid(cocos2d::Skybox).name(); + g_luaType[typeName] = "cc.Skybox"; + g_typeCast["Skybox"] = "cc.Skybox"; + return 1; +} + +TOLUA_API int register_all_cocos2dx_3d(lua_State* tolua_S) +{ + tolua_open(tolua_S); + + tolua_module(tolua_S,"cc",0); + tolua_beginmodule(tolua_S,"cc"); + + lua_register_cocos2dx_3d_Animate3D(tolua_S); + lua_register_cocos2dx_3d_Sprite3D(tolua_S); + lua_register_cocos2dx_3d_AttachNode(tolua_S); + lua_register_cocos2dx_3d_BillBoard(tolua_S); + lua_register_cocos2dx_3d_Animation3D(tolua_S); + lua_register_cocos2dx_3d_Skeleton3D(tolua_S); + lua_register_cocos2dx_3d_Mesh(tolua_S); + lua_register_cocos2dx_3d_TextureCube(tolua_S); + lua_register_cocos2dx_3d_Skybox(tolua_S); + + tolua_endmodule(tolua_S); + return 1; } diff --git a/tests/lua-tests/src/Sprite3DTest/Sprite3DTest.lua b/tests/lua-tests/src/Sprite3DTest/Sprite3DTest.lua index bb95144f61..efdf34dbb4 100644 --- a/tests/lua-tests/src/Sprite3DTest/Sprite3DTest.lua +++ b/tests/lua-tests/src/Sprite3DTest/Sprite3DTest.lua @@ -919,10 +919,75 @@ function Sprite3DMirrorTest.create() return layer end + +---------------------------------------- +----Sprite3DCubeTexture +---------------------------------------- +local Sprite3DCubeMap = {} +Sprite3DCubeMap.__index = Sprite3DCubeMap + +function Sprite3DCubeMap.create() +local layer = cc.Layer:create() +Helper.initWithLayer(layer) +Helper.titleLabel:setString("Sprite3D CubeMap/Skybox Test") + +local visSize = cc.Director:getInstance():getVisibleSize() +local camera = cc.Camera:createPerspective(68, visSize.width / visSize.height, 0.1, 200) +camera:setCameraFlag(2) +layer:addChild(camera) + +local fileName = "Sprite3DTest/teapot.c3b" +local teapot = cc.Sprite3D:create(fileName) +teapot:setPosition3D({x = 0, y = -5, z = -20}) +teapot:setRotation3D({x = -90, y = 180, z = 0}) + +local texCube = cc.TextureCube:create("Sprite3DTest/skybox/left.jpg", "Sprite3DTest/skybox/right.jpg", + "Sprite3DTest/skybox/top.jpg", "Sprite3DTest/skybox/bottom.jpg", + "Sprite3DTest/skybox/front.jpg", "Sprite3DTest/skybox/back.jpg"); + +local program = cc.GLProgram:createWithFilenames("Sprite3DTest/cube_map.vert", "Sprite3DTest/cube_map.frag") +local state = cc.GLProgramState:create(program) + +attriNames = { + "a_position", "a_color", + "a_texCoord", "a_texCoord1", "a_texCoord2", "a_texCoord3", + "a_normal", "a_blendWeight", "a_blendIndex" + } + +--pass mesh's attribute to shader +local offset = 0 +local attributeCount = teapot:getMesh():getMeshVertexAttribCount() +for i = 0, attributeCount-1 do + local meshattribute = teapot:getMesh():getMeshVertexAttribute(i) + state:setVertexAttribPointer(attriNames[meshattribute.vertexAttrib+1], + meshattribute.size, + meshattribute.type, + false, + teapot:getMesh():getVertexSizeInBytes(), + offset); + offset = offset + meshattribute.attribSizeBytes +end + +state:setUniformTexture("u_cubeTex", texCube:getName()) + +teapot:setGLProgramState(state) +teapot:setCameraMask(2) + +local rotate_action = cc.RotateBy:create(1.5, { x = 0.0, y = 30.0, z = 0.0}) +teapot:runAction(cc.RepeatForever:create(rotate_action)); +layer:addChild(teapot) + +local skybox = cc.Skybox:create() +skybox:setTexture(texCube) +layer:addChild(skybox) + +return layer +end + function Sprite3DTest() local scene = cc.Scene:create() - Helper.createFunctionTable = + Helper.createFunctionTable = { Sprite3DBasicTest.create, Sprite3DHitTest.create, @@ -932,6 +997,7 @@ function Sprite3DTest() Sprite3DReskinTest.create, Sprite3DWithOBBPerfromanceTest.create, Sprite3DMirrorTest.create, + Sprite3DCubeMap.create, } scene:addChild(Sprite3DBasicTest.create()) From 6eaa78d1cbc8742654301f9d971e306833fa139b Mon Sep 17 00:00:00 2001 From: zhukaixy Date: Tue, 24 Mar 2015 15:43:59 +0800 Subject: [PATCH 03/10] add reload mechanism for TextureCube and Skybox --- cocos/3d/CCSkybox.cpp | 64 +- cocos/3d/CCSkybox.h | 2 + cocos/3d/CCTextureCube.cpp | 13 + cocos/3d/CCTextureCube.h | 4 + .../Classes/Sprite3DTest/Sprite3DTest.cpp | 699 +++++++++--------- .../Classes/Sprite3DTest/Sprite3DTest.h | 204 ++--- 6 files changed, 529 insertions(+), 457 deletions(-) diff --git a/cocos/3d/CCSkybox.cpp b/cocos/3d/CCSkybox.cpp index e32fb0e287..d871a971e5 100644 --- a/cocos/3d/CCSkybox.cpp +++ b/cocos/3d/CCSkybox.cpp @@ -61,22 +61,8 @@ bool Skybox::init() state->setVertexAttribPointer(GLProgram::ATTRIBUTE_NAME_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), nullptr); setGLProgramState(state); - if (Configuration::getInstance()->supportsShareableVAO()) - { - glGenVertexArrays(1, &_vao); - GL::bindVAO(_vao); - } - initBuffers(); - if (Configuration::getInstance()->supportsShareableVAO()) - { - glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION); - state->applyAttributes(false); - - GL::bindVAO(0); - } - CHECK_GL_ERROR_DEBUG(); return true; @@ -84,6 +70,12 @@ bool Skybox::init() void Skybox::initBuffers() { + if (Configuration::getInstance()->supportsShareableVAO()) + { + glGenVertexArrays(1, &_vao); + GL::bindVAO(_vao); + } + // init vertex buffer object Vec3 vexBuf[] = { @@ -97,16 +89,24 @@ void Skybox::initBuffers() // init index buffer object unsigned char idxBuf[] = { 2, 1, 0, 3, 2, 0, // font - 1, 5, 4, 1, 4, 0, // right - 4, 5, 6, 4, 6, 7, // back - 7, 6, 2, 7, 2, 3, // left - 2, 6, 5, 2, 5, 1, // up - 3, 0, 4, 3, 4, 7 // down - }; + 1, 5, 4, 1, 4, 0, // right + 4, 5, 6, 4, 6, 7, // back + 7, 6, 2, 7, 2, 3, // left + 2, 6, 5, 2, 5, 1, // up + 3, 0, 4, 3, 4, 7 // down + }; glGenBuffers(1, &_indexBuffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(idxBuf), idxBuf, GL_STATIC_DRAW); + + if (Configuration::getInstance()->supportsShareableVAO()) + { + glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION); + getGLProgramState()->applyAttributes(false); + + GL::bindVAO(0); + } } void Skybox::draw(Renderer* renderer, const Mat4& transform, uint32_t flags) @@ -122,7 +122,7 @@ void Skybox::onDraw(const Mat4& transform, uint32_t flags) state->apply(transform); GLboolean depthFlag = glIsEnabled(GL_DEPTH_TEST); - GLint depthFunc; + GLint depthFunc; glGetIntegerv(GL_DEPTH_FUNC, &depthFunc); glEnable(GL_DEPTH_TEST); @@ -167,11 +167,25 @@ void Skybox::setTexture(TextureCube* texture) { CCASSERT(texture != nullptr, __FUNCTION__); - _texture = texture; - _texture->retain(); + texture->retain(); - auto state = getGLProgramState(); - state->setUniformTexture("u_Env", _texture); + if (_texture) + _texture->release(); + + _texture = texture; + + getGLProgramState()->setUniformTexture("u_Env", _texture); +} + +void Skybox::reload() +{ + auto glProgram = getGLProgramState()->getGLProgram(); + glProgram->reset(); + glProgram->initWithFilenames("Shaders3D/Skybox.vert", "Shaders3D/Skybox.frag"); + glProgram->link(); + glProgram->updateUniforms(); + + initBuffers(); } NS_CC_END diff --git a/cocos/3d/CCSkybox.h b/cocos/3d/CCSkybox.h index 5fd019ac73..55954e42c4 100644 --- a/cocos/3d/CCSkybox.h +++ b/cocos/3d/CCSkybox.h @@ -43,6 +43,8 @@ public: // Overrides virtual void draw(Renderer* renderer, const Mat4& transform, uint32_t flags) override; + void reload(); + CC_CONSTRUCTOR_ACCESS: Skybox(); virtual ~Skybox(); diff --git a/cocos/3d/CCTextureCube.cpp b/cocos/3d/CCTextureCube.cpp index 6c91d63cf9..5cd5e80322 100644 --- a/cocos/3d/CCTextureCube.cpp +++ b/cocos/3d/CCTextureCube.cpp @@ -146,6 +146,7 @@ Image* createImage(const std::string& path) TextureCube::TextureCube() { + _imgPath.resize(6); } TextureCube::~TextureCube() @@ -170,6 +171,13 @@ bool TextureCube::init(const std::string& positive_x, const std::string& negativ const std::string& positive_y, const std::string& negative_y, const std::string& positive_z, const std::string& negative_z) { + _imgPath[0] = positive_x; + _imgPath[1] = negative_x; + _imgPath[2] = positive_y; + _imgPath[3] = negative_y; + _imgPath[4] = positive_z; + _imgPath[5] = negative_z; + std::vector images(6); images[0] = createImage(positive_x); @@ -247,4 +255,9 @@ void TextureCube::setTexParameters(const TexParams& texParams) glBindTexture(GL_TEXTURE_CUBE_MAP, 0); } +bool TextureCube::reloadTexture() +{ + return init(_imgPath[0], _imgPath[1], _imgPath[2], _imgPath[3], _imgPath[4], _imgPath[5]); +} + NS_CC_END diff --git a/cocos/3d/CCTextureCube.h b/cocos/3d/CCTextureCube.h index 6e4272a536..460b4c6f0a 100644 --- a/cocos/3d/CCTextureCube.h +++ b/cocos/3d/CCTextureCube.h @@ -51,6 +51,8 @@ public: const std::string& positive_z, const std::string& negative_z); void setTexParameters(const TexParams&); + + bool reloadTexture(); CC_CONSTRUCTOR_ACCESS: TextureCube(); TextureCube(const TextureCube&); @@ -60,6 +62,8 @@ protected: bool init(const std::string& positive_x, const std::string& negative_x, const std::string& positive_y, const std::string& negative_y, const std::string& positive_z, const std::string& negative_z); +private: + std::vector _imgPath; }; NS_CC_END diff --git a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp index edbb889330..059d9b055e 100644 --- a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp +++ b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp @@ -1,26 +1,26 @@ /**************************************************************************** - Copyright (c) 2012 cocos2d-x.org - Copyright (c) 2013-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. + Copyright (c) 2012 cocos2d-x.org + Copyright (c) 2013-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 "Sprite3DTest.h" @@ -54,7 +54,7 @@ static std::function createFunctions[] = CL(Sprite3DHitTest), CL(AsyncLoadSprite3DTest), #if (CC_TARGET_PLATFORM != CC_PLATFORM_WP8) - // 3DEffect use custom shader which is not supported on WP8/WinRT yet. + // 3DEffect use custom shader which is not supported on WP8/WinRT yet. CL(Sprite3DEffectTest), CL(Sprite3DUVAnimationTest), CL(Sprite3DFakeShadowTest), @@ -82,7 +82,7 @@ static Layer* nextSpriteTestAction() { sceneIdx++; sceneIdx = sceneIdx % MAX_LAYER; - + auto layer = (createFunctions[sceneIdx])(); return layer; } @@ -93,7 +93,7 @@ static Layer* backSpriteTestAction() int total = MAX_LAYER; if( sceneIdx < 0 ) sceneIdx += total; - + auto layer = (createFunctions[sceneIdx])(); return layer; } @@ -111,7 +111,7 @@ static Layer* restartSpriteTestAction() //------------------------------------------------------------------ Sprite3DTestDemo::Sprite3DTestDemo(void) -: BaseTest() + : BaseTest() { } @@ -138,7 +138,7 @@ void Sprite3DTestDemo::restartCallback(Ref* sender) { auto s = new (std::nothrow) Sprite3DTestScene(); s->addChild(restartSpriteTestAction()); - + Director::getInstance()->replaceScene(s); s->release(); } @@ -195,7 +195,7 @@ Sprite3DBasicTest::Sprite3DBasicTest() auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DBasicTest::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); - + auto s = Director::getInstance()->getWinSize(); addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) ); } @@ -205,26 +205,26 @@ void Sprite3DBasicTest::addNewSpriteWithCoords(Vec2 p) //int idx = (int)(CCRANDOM_0_1() * 1400.0f / 100.0f); //int x = (idx%5) * 85; //int y = (idx/5) * 121; - -// //option 1: load a obj that contain the texture in it -// auto sprite = Sprite3D::create("sprite3dTest/scene01.obj"); - + + // //option 1: load a obj that contain the texture in it + // auto sprite = Sprite3D::create("sprite3dTest/scene01.obj"); + //option 2: load obj and assign the texture auto sprite = Sprite3D::create("Sprite3DTest/boss1.obj"); sprite->setScale(3.f); sprite->setTexture("Sprite3DTest/boss.png"); - + // //sprite->setEffect(cocos2d::EFFECT_OUTLINE); - + //add to scene addChild( sprite ); - + sprite->setPosition( Vec2( p.x, p.y) ); - + ActionInterval* action; float random = CCRANDOM_0_1(); - + if( random < 0.20 ) action = ScaleBy::create(3, 2); else if(random < 0.40) @@ -237,7 +237,7 @@ void Sprite3DBasicTest::addNewSpriteWithCoords(Vec2 p) action = FadeOut::create(2); auto action_back = action->reverse(); auto seq = Sequence::create( action, action_back, nullptr ); - + sprite->runAction( RepeatForever::create(seq) ); } @@ -246,7 +246,7 @@ void Sprite3DBasicTest::onTouchesEnded(const std::vector& touches, Event for (auto touch: touches) { auto location = touch->getLocation(); - + addNewSpriteWithCoords( location ); } } @@ -294,11 +294,11 @@ Sprite3DUVAnimationTest::Sprite3DUVAnimationTest() for (auto i = 0; i < attributeCount; i++) { auto meshattribute = cylinder->getMesh()->getMeshVertexAttribute(i); _state->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib], - meshattribute.size, - meshattribute.type, - GL_FALSE, - cylinder->getMesh()->getVertexSizeInBytes(), - (GLvoid*)offset); + meshattribute.size, + meshattribute.type, + GL_FALSE, + cylinder->getMesh()->getVertexSizeInBytes(), + (GLvoid*)offset); offset += meshattribute.attribSizeBytes; } @@ -309,13 +309,13 @@ Sprite3DUVAnimationTest::Sprite3DUVAnimationTest() tRepeatParams.minFilter = GL_NEAREST; tRepeatParams.wrapS = GL_REPEAT; tRepeatParams.wrapT = GL_REPEAT; - shining_texture->setTexParameters(tRepeatParams); + shining_texture->setTexParameters(tRepeatParams); //pass the texture sampler to our custom shader _state->setUniformTexture("caustics",shining_texture); this->addChild(cylinder); - this->setCameraMask(2); + this->setCameraMask(2); this->addChild(camera); //adjust cylinder's position & rotation @@ -324,18 +324,18 @@ Sprite3DUVAnimationTest::Sprite3DUVAnimationTest() //the callback function update cylinder's texcoord schedule(schedule_selector(Sprite3DUVAnimationTest::cylinderUpdate)); - + #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) _backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, - [this](EventCustom*) - { - auto glProgram = _state->getGLProgram(); - glProgram->reset(); - glProgram->initWithFilenames("Sprite3DTest/cylinder.vert", "Sprite3DTest/cylinder.frag"); - glProgram->link(); - glProgram->updateUniforms(); - } - ); + [this](EventCustom*) + { + auto glProgram = _state->getGLProgram(); + glProgram->reset(); + glProgram->initWithFilenames("Sprite3DTest/cylinder.vert", "Sprite3DTest/cylinder.frag"); + glProgram->link(); + glProgram->updateUniforms(); + } + ); Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1); #endif } @@ -347,12 +347,12 @@ Sprite3DUVAnimationTest::~Sprite3DUVAnimationTest() #endif } -std::string Sprite3DUVAnimationTest::title() const +std::string Sprite3DUVAnimationTest::title() const { return "Testing UV Animation"; } -std::string Sprite3DUVAnimationTest::subtitle() const +std::string Sprite3DUVAnimationTest::subtitle() const { return ""; } @@ -387,13 +387,13 @@ void Sprite3DUVAnimationTest::cylinderUpdate(float dt) Sprite3DFakeShadowTest::Sprite3DFakeShadowTest() { Size visibleSize = Director::getInstance()->getVisibleSize(); - + auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesBegan = CC_CALLBACK_2(Sprite3DFakeShadowTest::onTouchesBegan, this); listener->onTouchesMoved = CC_CALLBACK_2(Sprite3DFakeShadowTest::onTouchesMoved, this); listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DFakeShadowTest::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); - + auto layer = Layer::create(); addChild(layer,0); //create Camera @@ -412,18 +412,18 @@ Sprite3DFakeShadowTest::Sprite3DFakeShadowTest() _plane->setGLProgramState(_state); //pass mesh's attribute to shader - long offset = 0; + long offset = 0; auto attributeCount = _plane->getMesh()->getMeshVertexAttribCount(); for (auto i = 0; i < attributeCount; i++) { auto meshattribute = _plane->getMesh()->getMeshVertexAttribute(i); _state->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib], - meshattribute.size, - meshattribute.type, - GL_FALSE, - _plane->getMesh()->getVertexSizeInBytes(), - (GLvoid*)offset); + meshattribute.size, + meshattribute.type, + GL_FALSE, + _plane->getMesh()->getVertexSizeInBytes(), + (GLvoid*)offset); offset += meshattribute.attribSizeBytes; - } + } _state->setUniformMat4("u_model_matrix",_plane->getNodeToWorldTransform()); //create shadow texture @@ -433,9 +433,9 @@ Sprite3DFakeShadowTest::Sprite3DFakeShadowTest() tRepeatParams.minFilter = GL_LINEAR; tRepeatParams.wrapS = GL_CLAMP_TO_EDGE; tRepeatParams.wrapT = GL_CLAMP_TO_EDGE; - shadowTexture->setTexParameters(tRepeatParams); + shadowTexture->setTexParameters(tRepeatParams); _state->setUniformTexture("u_shadowTexture",shadowTexture); - layer->addChild(_plane); + layer->addChild(_plane); //create the orc _orc = Sprite3D::create("Sprite3DTest/orc.c3b"); @@ -449,30 +449,30 @@ Sprite3DFakeShadowTest::Sprite3DFakeShadowTest() layer->setCameraMask(2); schedule(CC_SCHEDULE_SELECTOR(Sprite3DFakeShadowTest::updateCamera), 0.0f); - + #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) _backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, - [this](EventCustom*) - { - auto glProgram = _state->getGLProgram(); - glProgram->reset(); - glProgram->initWithFilenames("Sprite3DTest/simple_shadow.vert","Sprite3DTest/simple_shadow.frag"); - glProgram->link(); - glProgram->updateUniforms(); - - _state->setUniformMat4("u_model_matrix",_plane->getNodeToWorldTransform()); - - //create shadow texture - auto shadowTexture = Director::getInstance()->getTextureCache()->addImage("Sprite3DTest/shadowCircle.png"); - Texture2D::TexParams tRepeatParams;//set texture parameters - tRepeatParams.magFilter = GL_LINEAR; - tRepeatParams.minFilter = GL_LINEAR; - tRepeatParams.wrapS = GL_CLAMP_TO_EDGE; - tRepeatParams.wrapT = GL_CLAMP_TO_EDGE; - shadowTexture->setTexParameters(tRepeatParams); - _state->setUniformTexture("u_shadowTexture",shadowTexture); - } - ); + [this](EventCustom*) + { + auto glProgram = _state->getGLProgram(); + glProgram->reset(); + glProgram->initWithFilenames("Sprite3DTest/simple_shadow.vert","Sprite3DTest/simple_shadow.frag"); + glProgram->link(); + glProgram->updateUniforms(); + + _state->setUniformMat4("u_model_matrix",_plane->getNodeToWorldTransform()); + + //create shadow texture + auto shadowTexture = Director::getInstance()->getTextureCache()->addImage("Sprite3DTest/shadowCircle.png"); + Texture2D::TexParams tRepeatParams;//set texture parameters + tRepeatParams.magFilter = GL_LINEAR; + tRepeatParams.minFilter = GL_LINEAR; + tRepeatParams.wrapS = GL_CLAMP_TO_EDGE; + tRepeatParams.wrapT = GL_CLAMP_TO_EDGE; + shadowTexture->setTexParameters(tRepeatParams); + _state->setUniformTexture("u_shadowTexture",shadowTexture); + } + ); Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1); #endif } @@ -484,12 +484,12 @@ Sprite3DFakeShadowTest::~Sprite3DFakeShadowTest() #endif } -std::string Sprite3DFakeShadowTest::title() const +std::string Sprite3DFakeShadowTest::title() const { return "fake shadow effect"; } -std::string Sprite3DFakeShadowTest::subtitle() const +std::string Sprite3DFakeShadowTest::subtitle() const { return "touch the screen to move around"; } @@ -594,18 +594,18 @@ bool Sprite3DFakeShadowTest::isState(unsigned int state,unsigned int bit) const return (state & bit) == bit; } -void Sprite3DFakeShadowTest::onTouchesBegan(const std::vector& touches, cocos2d::Event *event) +void Sprite3DFakeShadowTest::onTouchesBegan(const std::vector& touches, cocos2d::Event* event) { } -void Sprite3DFakeShadowTest::onTouchesMoved(const std::vector& touches, cocos2d::Event *event) +void Sprite3DFakeShadowTest::onTouchesMoved(const std::vector& touches, cocos2d::Event* event) { } -void Sprite3DFakeShadowTest::onTouchesEnded(const std::vector& touches, cocos2d::Event *event) +void Sprite3DFakeShadowTest::onTouchesEnded(const std::vector& touches, cocos2d::Event* event) { - for ( auto &item: touches ) + for ( auto& item: touches ) { auto touch = item; auto location = touch->getLocationInView(); @@ -653,43 +653,43 @@ Sprite3DBasicToonShaderTest::Sprite3DBasicToonShaderTest() auto _camera = Camera::createPerspective(60, visibleSize.width/visibleSize.height, 0.1f, 200); _camera->setCameraFlag(CameraFlag::USER1); // create a teapot - auto teapot = Sprite3D::create("Sprite3DTest/teapot.c3b"); - //create and set our custom shader + auto teapot = Sprite3D::create("Sprite3DTest/teapot.c3b"); + //create and set our custom shader auto shader =GLProgram::createWithFilenames("Sprite3DTest/toon.vert","Sprite3DTest/toon.frag"); _state = GLProgramState::create(shader); teapot->setGLProgramState(_state); teapot->setPosition3D(Vec3(0,-5,-20)); - teapot->setRotation3D(Vec3(-90,180,0)); + teapot->setRotation3D(Vec3(-90,180,0)); auto rotate_action = RotateBy::create(1.5,Vec3(0,30,0)); - teapot->runAction(RepeatForever::create(rotate_action)); + teapot->runAction(RepeatForever::create(rotate_action)); //pass mesh's attribute to shader long offset = 0; auto attributeCount = teapot->getMesh()->getMeshVertexAttribCount(); for (auto i = 0; i < attributeCount; i++) { auto meshattribute = teapot->getMesh()->getMeshVertexAttribute(i); _state->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib], - meshattribute.size, - meshattribute.type, - GL_FALSE, - teapot->getMesh()->getVertexSizeInBytes(), - (GLvoid*)offset); + meshattribute.size, + meshattribute.type, + GL_FALSE, + teapot->getMesh()->getVertexSizeInBytes(), + (GLvoid*)offset); offset += meshattribute.attribSizeBytes; } addChild(teapot); addChild(_camera); setCameraMask(2); - + #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) _backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, - [this](EventCustom*) - { - auto glProgram = _state->getGLProgram(); - glProgram->reset(); - glProgram->initWithFilenames("Sprite3DTest/toon.vert","Sprite3DTest/toon.frag"); - glProgram->link(); - glProgram->updateUniforms(); - } - ); + [this](EventCustom*) + { + auto glProgram = _state->getGLProgram(); + glProgram->reset(); + glProgram->initWithFilenames("Sprite3DTest/toon.vert","Sprite3DTest/toon.frag"); + glProgram->link(); + glProgram->updateUniforms(); + } + ); Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1); #endif } @@ -701,19 +701,19 @@ Sprite3DBasicToonShaderTest::~Sprite3DBasicToonShaderTest() #endif } -std::string Sprite3DBasicToonShaderTest::title() const +std::string Sprite3DBasicToonShaderTest::title() const { return "basic toon shader test"; } -std::string Sprite3DBasicToonShaderTest::subtitle() const +std::string Sprite3DBasicToonShaderTest::subtitle() const { return " "; } //------------------------------------------------------------------ // -// Sprite3DLightMapTest +// Sprite3DLightMapTest // //------------------------------------------------------------------ Sprite3DLightMapTest::Sprite3DLightMapTest() @@ -725,16 +725,16 @@ Sprite3DLightMapTest::Sprite3DLightMapTest() _camera->setCameraFlag(CameraFlag::USER1); _camera->setPosition3D(Vec3(0,25,15)); _camera->setRotation3D(Vec3(-35,0,0)); - auto LightMapScene = Sprite3D::create("Sprite3DTest/LightMapScene.c3b"); - LightMapScene->setScale(0.1f); + auto LightMapScene = Sprite3D::create("Sprite3DTest/LightMapScene.c3b"); + LightMapScene->setScale(0.1f); addChild(LightMapScene); - addChild(_camera); - setCameraMask(2); + addChild(_camera); + setCameraMask(2); //add a point light auto light = PointLight::create(Vec3(35,75,-20.5),Color3B(255,255,255),150); addChild(light); - //set the ambient light + //set the ambient light auto ambient = AmbientLight::create(Color3B(55,55,55)); addChild(ambient); @@ -745,14 +745,14 @@ Sprite3DLightMapTest::Sprite3DLightMapTest() } Sprite3DLightMapTest::~Sprite3DLightMapTest() { - + } std::string Sprite3DLightMapTest::title() const { return "light map test"; } -std::string Sprite3DLightMapTest::subtitle() const +std::string Sprite3DLightMapTest::subtitle() const { return "drag the screen to move around"; } @@ -776,9 +776,9 @@ void Sprite3DLightMapTest::onTouchesMoved(const std::vector& to cameraRightDir.normalize(); cameraRightDir.y=0; Vec3 cameraPos= _camera->getPosition3D(); - cameraPos+=cameraDir*newPos.y*delta; + cameraPos+=cameraDir*newPos.y*delta; cameraPos+=cameraRightDir*newPos.x*delta; - _camera->setPosition3D(cameraPos); + _camera->setPosition3D(cameraPos); } } @@ -790,33 +790,33 @@ void Sprite3DLightMapTest::onTouchesMoved(const std::vector& to Sprite3DHitTest::Sprite3DHitTest() { auto s = Director::getInstance()->getWinSize(); - + auto sprite1 = Sprite3D::create("Sprite3DTest/boss1.obj"); - + sprite1->setScale(4.f); sprite1->setTexture("Sprite3DTest/boss.png"); sprite1->setPosition( Vec2(s.width/2, s.height/2) ); - + //add to scene addChild( sprite1 ); sprite1->runAction(RepeatForever::create(RotateBy::create(3, 360))); - + auto sprite2 = Sprite3D::create("Sprite3DTest/boss1.obj"); - + sprite2->setScale(4.f); sprite2->setTexture("Sprite3DTest/boss.png"); sprite2->setPosition( Vec2(s.width/2, s.height/2) ); sprite2->setAnchorPoint(Vec2(0.5, 0.5)); - + //add to scene addChild( sprite2 ); sprite2->runAction(RepeatForever::create(RotateBy::create(3, -360))); - - + + // Make sprite1 touchable auto listener1 = EventListenerTouchOneByOne::create(); listener1->setSwallowTouches(true); - + listener1->onTouchBegan = [](Touch* touch, Event* event){ auto target = static_cast(event->getCurrentTarget()); @@ -840,10 +840,10 @@ Sprite3DHitTest::Sprite3DHitTest() log("sprite3d onTouchesEnded.. "); target->setOpacity(255); }; - + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, sprite1); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), sprite2); - + } std::string Sprite3DHitTest::title() const @@ -860,21 +860,21 @@ void Sprite3DTestScene::runThisTest() { auto layer = nextSpriteTestAction(); addChild(layer); - + Director::getInstance()->replaceScene(this); } Sprite3DTestScene::Sprite3DTestScene() { - + } -static int tuple_sort( const std::tuple &tuple1, const std::tuple &tuple2 ) +static int tuple_sort( const std::tuple& tuple1, const std::tuple& tuple2 ) { return std::get<0>(tuple1) < std::get<0>(tuple2); } -EffectSprite3D* EffectSprite3D::createFromObjFileAndTexture(const std::string &objFilePath, const std::string &textureFilePath) +EffectSprite3D* EffectSprite3D::createFromObjFileAndTexture(const std::string& objFilePath, const std::string& textureFilePath) { auto sprite = new (std::nothrow) EffectSprite3D(); if (sprite && sprite->initWithFile(objFilePath)) @@ -888,11 +888,11 @@ EffectSprite3D* EffectSprite3D::createFromObjFileAndTexture(const std::string &o return nullptr; } -EffectSprite3D* EffectSprite3D::create(const std::string &path) +EffectSprite3D* EffectSprite3D::create(const std::string& path) { if (path.length() < 4) CCASSERT(false, "improper name specified when creating Sprite3D"); - + auto sprite = new (std::nothrow) EffectSprite3D(); if (sprite && sprite->initWithFile(path)) { @@ -904,9 +904,9 @@ EffectSprite3D* EffectSprite3D::create(const std::string &path) } EffectSprite3D::EffectSprite3D() -: _defaultEffect(nullptr) + : _defaultEffect(nullptr) { - + } EffectSprite3D::~EffectSprite3D() @@ -918,7 +918,7 @@ EffectSprite3D::~EffectSprite3D() CC_SAFE_RELEASE(_defaultEffect); } -void EffectSprite3D::setEffect3D(Effect3D *effect) +void EffectSprite3D::setEffect3D(Effect3D* effect) { if(_defaultEffect == effect) return; CC_SAFE_RETAIN(effect); @@ -931,7 +931,7 @@ void EffectSprite3D::addEffect(Effect3DOutline* effect, ssize_t order) if(nullptr == effect) return; effect->retain(); effect->setTarget(this); - + _effects.push_back(std::make_tuple(order,effect,CustomCommand())); std::sort(std::begin(_effects), std::end(_effects), tuple_sort); @@ -986,26 +986,26 @@ Effect3DOutline* Effect3DOutline::create() bool Effect3DOutline::init() { - + return true; } Effect3DOutline::Effect3DOutline() -: _outlineWidth(1.0f) -, _outlineColor(1, 1, 1) -, _sprite(nullptr) + : _outlineWidth(1.0f) + , _outlineColor(1, 1, 1) + , _sprite(nullptr) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) _backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, - [this](EventCustom*) - { - auto glProgram = _glProgramState->getGLProgram(); - glProgram->reset(); - glProgram->initWithFilenames(_vertShaderFile, _fragShaderFile); - glProgram->link(); - glProgram->updateUniforms(); - } - ); + [this](EventCustom*) + { + auto glProgram = _glProgramState->getGLProgram(); + glProgram->reset(); + glProgram->initWithFilenames(_vertShaderFile, _fragShaderFile); + glProgram->link(); + glProgram->updateUniforms(); + } + ); Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1); #endif } @@ -1037,10 +1037,10 @@ void Effect3DOutline::setOutlineWidth(float width) } } -void Effect3DOutline::setTarget(EffectSprite3D *sprite) +void Effect3DOutline::setTarget(EffectSprite3D* sprite) { CCASSERT(nullptr != sprite && nullptr != sprite->getMesh(),"Error: Setting a null pointer or a null mesh EffectSprite3D to Effect3D"); - + if(sprite != _sprite) { GLProgram* glprogram; @@ -1054,16 +1054,16 @@ void Effect3DOutline::setTarget(EffectSprite3D *sprite) _glProgramState->retain(); _glProgramState->setUniformVec3("OutLineColor", _outlineColor); _glProgramState->setUniformFloat("OutlineWidth", _outlineWidth); - - + + _sprite = sprite; - + auto mesh = sprite->getMesh(); long offset = 0; for (auto i = 0; i < mesh->getMeshVertexAttribCount(); i++) { auto meshvertexattrib = mesh->getMeshVertexAttribute(i); - + _glProgramState->setVertexAttribPointer(s_attributeNames[meshvertexattrib.vertexAttrib], meshvertexattrib.size, meshvertexattrib.type, @@ -1072,12 +1072,12 @@ void Effect3DOutline::setTarget(EffectSprite3D *sprite) (void*)offset); offset += meshvertexattrib.attribSizeBytes; } - + Color4F color(_sprite->getDisplayedColor()); color.a = _sprite->getDisplayedOpacity() / 255.0f; _glProgramState->setUniformVec4("u_color", Vec4(color.r, color.g, color.b, color.a)); } - + } static void MatrixPalleteCallBack( GLProgram* glProgram, Uniform* uniform, int paletteSize, const float* palette) @@ -1085,7 +1085,7 @@ static void MatrixPalleteCallBack( GLProgram* glProgram, Uniform* uniform, int p glUniform4fv( uniform->location, (GLsizei)paletteSize, (const float*)palette ); } -void Effect3DOutline::draw(const Mat4 &transform) +void Effect3DOutline::draw(const Mat4& transform) { //draw Color4F color(_sprite->getDisplayedColor()); @@ -1096,10 +1096,10 @@ void Effect3DOutline::draw(const Mat4 &transform) glEnable(GL_CULL_FACE); glCullFace(GL_FRONT); glEnable(GL_DEPTH_TEST); - + auto mesh = _sprite->getMesh(); glBindBuffer(GL_ARRAY_BUFFER, mesh->getVertexBuffer()); - + auto skin = _sprite->getMesh()->getSkin(); if(_sprite && skin) { @@ -1107,14 +1107,14 @@ void Effect3DOutline::draw(const Mat4 &transform) skin->getMatrixPaletteSize(), (float*)skin->getMatrixPalette()); _glProgramState->setUniformCallback("u_matrixPalette", function); } - + if(_sprite) _glProgramState->apply(transform); - + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->getIndexBuffer()); glDrawElements(mesh->getPrimitiveType(), (GLsizei)mesh->getIndexCount(), mesh->getIndexFormat(), 0); CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, mesh->getIndexCount()); - + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); glDisable(GL_DEPTH_TEST); @@ -1123,18 +1123,18 @@ void Effect3DOutline::draw(const Mat4 &transform) } } -void EffectSprite3D::draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, uint32_t flags) +void EffectSprite3D::draw(cocos2d::Renderer* renderer, const cocos2d::Mat4& transform, uint32_t flags) { - for(auto &effect : _effects) + for(auto& effect : _effects) { if(std::get<0>(effect) >=0) break; - CustomCommand &cc = std::get<2>(effect); + CustomCommand& cc = std::get<2>(effect); cc.func = CC_CALLBACK_0(Effect3D::draw,std::get<1>(effect),transform); renderer->addCommand(&cc); - + } - + if(!_defaultEffect) { Sprite3D::draw(renderer, transform, flags); @@ -1145,15 +1145,15 @@ void EffectSprite3D::draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &tran _command.func = CC_CALLBACK_0(Effect3D::draw, _defaultEffect, transform); renderer->addCommand(&_command); } - - for(auto &effect : _effects) + + for(auto& effect : _effects) { if(std::get<0>(effect) <=0) continue; - CustomCommand &cc = std::get<2>(effect); + CustomCommand& cc = std::get<2>(effect); cc.func = CC_CALLBACK_0(Effect3D::draw,std::get<1>(effect),transform); renderer->addCommand(&cc); - + } } @@ -1161,7 +1161,7 @@ Sprite3DEffectTest::Sprite3DEffectTest() { auto s = Director::getInstance()->getWinSize(); addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) ); - + auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DEffectTest::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); @@ -1184,22 +1184,22 @@ void Sprite3DEffectTest::addNewSpriteWithCoords(Vec2 p) sprite->addEffect(effect, -1); effect->setOutlineColor(Vec3(1,0,0)); effect->setOutlineWidth(0.01f); - + Effect3DOutline* effect2 = Effect3DOutline::create(); sprite->addEffect(effect2, -2); effect2->setOutlineWidth(0.02f); effect2->setOutlineColor(Vec3(1,1,0)); //sprite->setEffect3D(effect); sprite->setScale(6.f); - + //add to scene addChild( sprite ); - + sprite->setPosition( Vec2( p.x, p.y) ); - + ActionInterval* action; float random = CCRANDOM_0_1(); - + if( random < 0.20 ) action = ScaleBy::create(3, 2); else if(random < 0.40) @@ -1212,7 +1212,7 @@ void Sprite3DEffectTest::addNewSpriteWithCoords(Vec2 p) action = FadeOut::create(2); auto action_back = action->reverse(); auto seq = Sequence::create( action, action_back, nullptr ); - + sprite->runAction( RepeatForever::create(seq) ); } @@ -1221,7 +1221,7 @@ void Sprite3DEffectTest::onTouchesEnded(const std::vector& touches, Even for (auto touch: touches) { auto location = touch->getLocation(); - + addNewSpriteWithCoords( location ); } } @@ -1233,22 +1233,22 @@ AsyncLoadSprite3DTest::AsyncLoadSprite3DTest() _paths.push_back("Sprite3DTest/orc.c3b"); _paths.push_back("Sprite3DTest/ReskinGirl.c3b"); _paths.push_back("Sprite3DTest/axe.c3b"); - + TTFConfig ttfConfig("fonts/arial.ttf", 15); auto label1 = Label::createWithTTF(ttfConfig,"AsyncLoad Sprite3D"); auto item1 = MenuItemLabel::create(label1,CC_CALLBACK_1(AsyncLoadSprite3DTest::menuCallback_asyncLoadSprite,this) ); - + auto s = Director::getInstance()->getWinSize(); item1->setPosition( s.width * .5f, s.height * .8f); - + auto pMenu1 = CCMenu::create(item1, nullptr); pMenu1->setPosition(Vec2(0,0)); this->addChild(pMenu1, 10); - + auto node = Node::create(); node->setTag(101); this->addChild(node); - + menuCallback_asyncLoadSprite(nullptr); } @@ -1269,10 +1269,10 @@ void AsyncLoadSprite3DTest::menuCallback_asyncLoadSprite(Ref* sender) { //Note that you must stop the tasks before leaving the scene. AsyncTaskPool::getInstance()->stopTasks(AsyncTaskPool::TaskType::TASK_IO); - + auto node = getChildByTag(101); node->removeAllChildren(); //remove all loaded sprite - + //remove cache data Sprite3DCache::getInstance()->removeAllSprite3DData(); long index = 0; @@ -1298,7 +1298,7 @@ Sprite3DWithSkinTest::Sprite3DWithSkinTest() auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DWithSkinTest::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); - + auto s = Director::getInstance()->getWinSize(); addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) ); } @@ -1347,7 +1347,7 @@ void Sprite3DWithSkinTest::onTouchesEnded(const std::vector& touches, Ev for (auto touch: touches) { auto location = touch->getLocation(); - + addNewSpriteWithCoords( location ); } } @@ -1357,7 +1357,7 @@ Sprite3DWithSkinOutlineTest::Sprite3DWithSkinOutlineTest() auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DWithSkinOutlineTest::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); - + auto s = Director::getInstance()->getWinSize(); addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) ); } @@ -1372,33 +1372,33 @@ std::string Sprite3DWithSkinOutlineTest::subtitle() const void Sprite3DWithSkinOutlineTest::addNewSpriteWithCoords(Vec2 p) { - + std::string fileName = "Sprite3DTest/orc.c3b"; auto sprite = EffectSprite3D::create(fileName); - + Effect3DOutline* effect = Effect3DOutline::create(); effect->setOutlineColor(Vec3(1,0,0)); effect->setOutlineWidth(0.01f); sprite->addEffect(effect, -1); - + Effect3DOutline* effect2 = Effect3DOutline::create(); effect2->setOutlineWidth(0.02f); effect2->setOutlineColor(Vec3(1,1,0)); sprite->addEffect(effect2, -2); - + sprite->setScale(3); sprite->setRotation3D(Vec3(0,180,0)); addChild(sprite); sprite->setPosition( Vec2( p.x, p.y) ); - + auto animation = Animation3D::create(fileName); if (animation) { auto animate = Animate3D::create(animation); bool inverse = (std::rand() % 3 == 0); - + int rand2 = std::rand(); float speed = 1.0f; if(rand2 % 3 == 1) @@ -1410,7 +1410,7 @@ void Sprite3DWithSkinOutlineTest::addNewSpriteWithCoords(Vec2 p) speed = animate->getSpeed() - 0.5 * CCRANDOM_0_1(); } animate->setSpeed(inverse ? -speed : speed); - + sprite->runAction(RepeatForever::create(animate)); } } @@ -1420,24 +1420,24 @@ void Sprite3DWithSkinOutlineTest::onTouchesEnded(const std::vector& touc for (auto touch: touches) { auto location = touch->getLocation(); - + addNewSpriteWithCoords( location ); } } Animate3DTest::Animate3DTest() -: _hurt(nullptr) -, _swim(nullptr) -, _sprite(nullptr) -, _moveAction(nullptr) -, _elapseTransTime(0.f) + : _hurt(nullptr) + , _swim(nullptr) + , _sprite(nullptr) + , _moveAction(nullptr) + , _elapseTransTime(0.f) { addSprite3D(); - + auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(Animate3DTest::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); - + scheduleUpdate(); } @@ -1463,7 +1463,7 @@ void Animate3DTest::update(float dt) if (_state == State::HURT_TO_SWIMMING) { _elapseTransTime += dt; - + if (_elapseTransTime >= Animate3D::getTransitionTime()) { _sprite->stopAction(_hurt); @@ -1496,13 +1496,13 @@ void Animate3DTest::addSprite3D() auto animate = Animate3D::create(animation, 0.f, 1.933f); _swim = RepeatForever::create(animate); sprite->runAction(_swim); - + _swim->retain(); _hurt = Animate3D::create(animation, 1.933f, 2.8f); _hurt->retain(); _state = State::SWIMMING; } - + _moveAction = MoveTo::create(4.f, Vec2(s.width / 5.f, s.height / 2.f)); _moveAction->retain(); auto seq = Sequence::create(_moveAction, CallFunc::create(CC_CALLBACK_0(Animate3DTest::reachEndCallBack, this)), nullptr); @@ -1536,7 +1536,7 @@ void Animate3DTest::onTouchesEnded(const std::vector& touches, Event* ev for (auto touch: touches) { auto location = touch->getLocation(); - + if (_sprite) { float len = (_sprite->getPosition() - location).length(); @@ -1561,12 +1561,12 @@ void Animate3DTest::onTouchesEnded(const std::vector& touches, Event* ev } AttachmentTest::AttachmentTest() -: _hasWeapon(false) -, _sprite(nullptr) + : _hasWeapon(false) + , _sprite(nullptr) { auto s = Director::getInstance()->getWinSize(); addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) ); - + auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(AttachmentTest::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); @@ -1588,16 +1588,16 @@ void AttachmentTest::addNewSpriteWithCoords(Vec2 p) sprite->setRotation3D(Vec3(0,180,0)); addChild(sprite); sprite->setPosition( Vec2( p.x, p.y) ); - + //test attach auto sp = Sprite3D::create("Sprite3DTest/axe.c3b"); sprite->getAttachNode("Bip001 R Hand")->addChild(sp); - + auto animation = Animation3D::create(fileName); if (animation) { auto animate = Animate3D::create(animation); - + sprite->runAction(RepeatForever::create(animate)); } _sprite = sprite; @@ -1618,11 +1618,11 @@ void AttachmentTest::onTouchesEnded(const std::vector& touches, Event* e _hasWeapon = !_hasWeapon; } Sprite3DReskinTest::Sprite3DReskinTest() -: _sprite(nullptr) + : _sprite(nullptr) { auto s = Director::getInstance()->getWinSize(); addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) ); - + auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DReskinTest::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); @@ -1630,11 +1630,11 @@ Sprite3DReskinTest::Sprite3DReskinTest() auto label1 = Label::createWithTTF(ttfConfig,"Hair"); auto item1 = MenuItemLabel::create(label1,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) ); auto label2 = Label::createWithTTF(ttfConfig,"Glasses"); - auto item2 = MenuItemLabel::create(label2, CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) ); + auto item2 = MenuItemLabel::create(label2, CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) ); auto label3 = Label::createWithTTF(ttfConfig,"Coat"); auto item3 = MenuItemLabel::create(label3,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) ); auto label4 = Label::createWithTTF(ttfConfig,"Pants"); - auto item4 = MenuItemLabel::create(label4, CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) ); + auto item4 = MenuItemLabel::create(label4, CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) ); auto label5 = Label::createWithTTF(ttfConfig,"Shoes"); auto item5 = MenuItemLabel::create(label5,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) ); item1->setPosition( Vec2(VisibleRect::left().x+50, VisibleRect::bottom().y+item1->getContentSize().height*4 ) ); @@ -1650,7 +1650,7 @@ Sprite3DReskinTest::Sprite3DReskinTest() auto pMenu1 = CCMenu::create(item1, item2, item3, item4, item5, nullptr); pMenu1->setPosition(Vec2(0,0)); this->addChild(pMenu1, 10); - + } void Sprite3DReskinTest::menuCallback_reSkin(Ref* sender) { @@ -1683,41 +1683,41 @@ void Sprite3DReskinTest::addNewSpriteWithCoords(Vec2 p) if (animation) { auto animate = Animate3D::create(animation); - + sprite->runAction(RepeatForever::create(animate)); } _sprite = sprite; - + auto& body = _skins[(int)SkinType::UPPER_BODY]; body.push_back("Girl_UpperBody01"); body.push_back("Girl_UpperBody02"); - + auto& pants = _skins[(int)SkinType::PANTS]; pants.push_back("Girl_LowerBody01"); pants.push_back("Girl_LowerBody02"); - + auto& shoes = _skins[(int)SkinType::SHOES]; shoes.push_back("Girl_Shoes01"); shoes.push_back("Girl_Shoes02"); - + auto& hair = _skins[(int)SkinType::HAIR]; hair.push_back("Girl_Hair01"); hair.push_back("Girl_Hair02"); - + auto& face = _skins[(int)SkinType::FACE]; face.push_back("Girl_Face01"); face.push_back("Girl_Face02"); - + auto& hand = _skins[(int)SkinType::HAND]; hand.push_back("Girl_Hand01"); hand.push_back("Girl_Hand02"); - + auto& glasses = _skins[(int)SkinType::GLASSES]; glasses.push_back(""); glasses.push_back("Girl_Glasses01"); - + memset(_curSkin, 0, (int)SkinType::MAX_TYPE * sizeof(int)); - + applyCurSkin(); } @@ -1750,7 +1750,7 @@ Sprite3DWithOBBPerformanceTest::Sprite3DWithOBBPerformanceTest() _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); auto s = Director::getInstance()->getWinSize(); initDrawBox(); - + addNewSpriteWithCoords(Vec2(s.width/2, s.height/2)); MenuItemFont::setFontName("fonts/arial.ttf"); MenuItemFont::setFontSize(65); @@ -1758,12 +1758,12 @@ Sprite3DWithOBBPerformanceTest::Sprite3DWithOBBPerformanceTest() decrease->setColor(Color3B(0,200,20)); auto increase = MenuItemFont::create(" + ", CC_CALLBACK_1(Sprite3DWithOBBPerformanceTest::addOBBCallback, this)); increase->setColor(Color3B(0,200,20)); - + auto menu = Menu::create(decrease, increase, nullptr); menu->alignItemsHorizontally(); menu->setPosition(Vec2(s.width/2, s.height-65)); addChild(menu, 1); - + TTFConfig ttfCount("fonts/Marker Felt.ttf", 30); _labelCubeCount = Label::createWithTTF(ttfCount,"0 cubes"); _labelCubeCount->setColor(Color3B(0,200,20)); @@ -1815,14 +1815,14 @@ void Sprite3DWithOBBPerformanceTest::onTouchesBegan(const std::vector& t void Sprite3DWithOBBPerformanceTest::onTouchesEnded(const std::vector& touches, Event* event) { - + } void Sprite3DWithOBBPerformanceTest::onTouchesMoved(const std::vector& touches, Event* event) { for (const auto& touch: touches) { - auto location = touch->getLocation(); + auto location = touch->getLocation(); auto obbSize = _obb.size(); for(decltype(obbSize) i = 0; i < obbSize; i++) @@ -1838,23 +1838,23 @@ void Sprite3DWithOBBPerformanceTest::update(float dt) char szText[16]; sprintf(szText,"%lu cubes",_obb.size()); _labelCubeCount->setString(szText); - + if (_drawDebug) { _drawDebug->clear(); - + Mat4 mat = _sprite->getNodeToWorldTransform(); mat.getRightVector(&_obbt._xAxis); _obbt._xAxis.normalize(); - + mat.getUpVector(&_obbt._yAxis); _obbt._yAxis.normalize(); - + mat.getForwardVector(&_obbt._zAxis); _obbt._zAxis.normalize(); - + _obbt._center = _sprite->getPosition3D(); - + Vec3 corners[8] = {}; _obbt.getCorners(corners); _drawDebug->drawCube(corners, Color4F(0,0,1,1)); @@ -1893,16 +1893,16 @@ void Sprite3DWithOBBPerformanceTest::addNewSpriteWithCoords(Vec2 p) auto animate = Animate3D::create(animation, 0.f, 1.933f); sprite->runAction(RepeatForever::create(animate)); } - + _moveAction = MoveTo::create(4.f, Vec2(s.width / 5.f, s.height / 2.f)); _moveAction->retain(); auto seq = Sequence::create(_moveAction, CallFunc::create(CC_CALLBACK_0(Sprite3DWithOBBPerformanceTest::reachEndCallBack, this)), nullptr); seq->setTag(100); sprite->runAction(seq); - + AABB aabb = _sprite->getAABB(); _obbt = OBB(aabb); - + _drawDebug = DrawNode3D::create(); addChild(_drawDebug); } @@ -1956,23 +1956,23 @@ void Sprite3DWithOBBPerformanceTest::delOBBWithCount(float value) void Sprite3DWithOBBPerformanceTest::unproject(const Mat4& viewProjection, const Size* viewport, Vec3* src, Vec3* dst) { assert(dst); - + assert(viewport->width != 0.0f && viewport->height != 0.0f); Vec4 screen(src->x / viewport->width, ((viewport->height - src->y)) / viewport->height, src->z, 1.0f); - + screen.x = screen.x * 2.0f - 1.0f; screen.y = screen.y * 2.0f - 1.0f; screen.z = screen.z * 2.0f - 1.0f; - + viewProjection.getInversed().transformVector(screen, &screen); - + if (screen.w != 0.0f) { screen.x /= screen.w; screen.y /= screen.w; screen.z /= screen.w; } - + dst->set(screen.x, screen.y, screen.z); } @@ -1981,26 +1981,26 @@ void Sprite3DWithOBBPerformanceTest::calculateRayByLocationInView(Ray* ray, cons auto dir = Director::getInstance(); auto view = dir->getWinSize(); auto mat = dir->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION); - + Vec3 src = Vec3(location.x, location.y, -1); Vec3 nearPoint; unproject(mat, &view, &src, &nearPoint); - + src = Vec3(location.x, location.y, 1); Vec3 farPoint; unproject(mat, &view, &src, &farPoint); - + Vec3 direction; Vec3::subtract(farPoint, nearPoint, &direction); direction.normalize(); - + ray->_origin = nearPoint; ray->_direction = direction; } Sprite3DMirrorTest::Sprite3DMirrorTest() -: _sprite(nullptr) -, _mirrorSprite(nullptr) + : _sprite(nullptr) + , _mirrorSprite(nullptr) { auto s = Director::getInstance()->getWinSize(); addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) ); @@ -2022,21 +2022,21 @@ void Sprite3DMirrorTest::addNewSpriteWithCoords(Vec2 p) sprite->setRotation3D(Vec3(0,180,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); - + auto animation = Animation3D::create(fileName); if (animation) { auto animate = Animate3D::create(animation); - + sprite->runAction(RepeatForever::create(animate)); } _sprite = sprite; _hasWeapon = true; - + //create mirror Sprite3D sprite = Sprite3D::create(fileName); sprite->setScale(5); @@ -2045,25 +2045,25 @@ void Sprite3DMirrorTest::addNewSpriteWithCoords(Vec2 p) 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; } QuaternionTest::QuaternionTest() -: _arcSpeed(CC_DEGREES_TO_RADIANS(90)) -, _radius(100.f) -, _accAngle(0.f) + : _arcSpeed(CC_DEGREES_TO_RADIANS(90)) + , _radius(100.f) + , _accAngle(0.f) { auto s = Director::getInstance()->getWinSize(); addNewSpriteWithCoords(Vec2(s.width / 2.f, s.height / 2.f)); @@ -2101,20 +2101,20 @@ void QuaternionTest::update(float delta) const float pi = M_PI; if (_accAngle >= 2 * pi) _accAngle -= 2 * pi; - + auto s = Director::getInstance()->getWinSize(); _sprite->setPosition(Vec2(s.width / 2.f + _radius * cosf(_accAngle), s.height / 2.f + _radius * sinf(_accAngle))); - + Quaternion quat; Quaternion::createFromAxisAngle(Vec3(0.f, 0.f, 1.f), _accAngle - pi * 0.5f, &quat); _sprite->setRotationQuat(quat); } UseCaseSprite3D::UseCaseSprite3D() -: _caseIdx(0) + : _caseIdx(0) { auto s = Director::getInstance()->getWinSize(); - + _useCaseTitles[0] = "transparent 3d sprite and 2d sprite"; _useCaseTitles[1] = "ui - 3d - ui"; @@ -2131,28 +2131,28 @@ UseCaseSprite3D::UseCaseSprite3D() _caseIdx++; if (_caseIdx >= (int)USECASE::MAX_CASE_NUM) _caseIdx = 0; - this->switchCase(); - }); - + this->switchCase(); + }); + auto menu = Menu::create(itemPrev, itemNext, nullptr); menu->alignItemsHorizontally(); menu->setScale(0.5); menu->setAnchorPoint(Vec2(0,0)); menu->setPosition(Vec2(s.width/2,70)); - + _label = Label::create(); _label->setPosition(s.width * 0.5f, s.height * 0.8f); addChild(_label); - + addChild(menu); - + //setup camera auto camera = Camera::createPerspective(40, s.width / s.height, 0.01f, 1000.f); camera->setCameraFlag(CameraFlag::USER1); camera->setPosition3D(Vec3(0.f, 30.f, 100.f)); camera->lookAt(Vec3(0.f, 0.f, 0.f)); addChild(camera); - + switchCase(); } @@ -2169,7 +2169,7 @@ std::string UseCaseSprite3D::subtitle() const void UseCaseSprite3D::switchCase() { removeChildByTag(101); - + auto s = Director::getInstance()->getWinSize(); _label->setString(_useCaseTitles[_caseIdx]); if (_caseIdx == 0) // use case 1, 3d transparent sprite + 2d sprite @@ -2181,33 +2181,33 @@ void UseCaseSprite3D::switchCase() if (animation) { auto animate = Animate3D::create(animation); - + sprite->runAction(RepeatForever::create(animate)); } - + auto circleBack = Sprite3D::create(); auto circle = Sprite::create("Sprite3DTest/circle.png"); circleBack->setScale(0.5f); circleBack->addChild(circle); circle->runAction(RepeatForever::create(RotateBy::create(3, Vec3(0.f, 0.f, 360.f)))); - + circleBack->setRotation3D(Vec3(90, 0, 0)); - + auto pos = sprite->getPosition3D(); circleBack->setPosition3D(Vec3(pos.x, pos.y, pos.z - 1)); - + sprite->setOpacity(250); sprite->setCameraMask(2); circleBack->setCameraMask(2); sprite->setTag(3); circleBack->setTag(2); - + auto node = Node::create(); node->addChild(sprite); node->addChild(circleBack); node->setTag(101); addChild(node); - + scheduleUpdate(); update(0.f); } @@ -2217,7 +2217,7 @@ void UseCaseSprite3D::switchCase() layer->setPosition(s.width * 0.25f, s.height * 0.25f); layer->setGlobalZOrder(-1); addChild(layer); - + std::string filename = "Sprite3DTest/girl.c3b"; auto sprite = Sprite3D::create(filename); sprite->setScale(0.5f); @@ -2229,20 +2229,20 @@ void UseCaseSprite3D::switchCase() } sprite->setPosition(s.width * 0.25f, s.height * 0.125f); layer->addChild(sprite); - + TTFConfig ttfConfig("fonts/arial.ttf", 15); auto label1 = Label::createWithTTF(ttfConfig,"Message"); auto item1 = MenuItemLabel::create(label1,CC_CALLBACK_1(UseCaseSprite3D::menuCallback_Message,this) ); auto label2 = Label::createWithTTF(ttfConfig,"Message"); - auto item2 = MenuItemLabel::create(label2, CC_CALLBACK_1(UseCaseSprite3D::menuCallback_Message,this) ); - + auto item2 = MenuItemLabel::create(label2, CC_CALLBACK_1(UseCaseSprite3D::menuCallback_Message,this) ); + item1->setPosition( Vec2(s.width * 0.5f - item1->getContentSize().width * 0.5f, s.height * 0.5f - item1->getContentSize().height ) ); item2->setPosition( Vec2(s.width * 0.5f - item1->getContentSize().width * 0.5f, s.height * 0.5f - item1->getContentSize().height * 2.f ) ); - + auto pMenu1 = CCMenu::create(item1, item2, nullptr); pMenu1->setPosition(Vec2(0,0)); layer->addChild(pMenu1); - + layer->setTag(101); } } @@ -2275,14 +2275,14 @@ void UseCaseSprite3D::update(float delta) { static float accAngle = 0.f; accAngle += delta * CC_DEGREES_TO_RADIANS(60); - + float radius = 30.f; float x = cosf(accAngle) * radius, z = sinf(accAngle) * radius; - + auto node = getChildByTag(101); auto sprite3d = node->getChildByTag(3); auto circle = node->getChildByTag(2); - + sprite3d->setPositionX(x); sprite3d->setPositionZ(z); circle->setPositionX(x); @@ -2291,8 +2291,9 @@ void UseCaseSprite3D::update(float delta) } Sprite3DCubeMapTest::Sprite3DCubeMapTest() : -_sprite(nullptr), -_textureCube(nullptr) + _textureCube(nullptr), + _skyBox(nullptr), + _teapot(nullptr) { auto s = Director::getInstance()->getWinSize(); addNewSpriteWithCoords(Vec2(s.width / 2, s.height / 2)); @@ -2300,6 +2301,12 @@ _textureCube(nullptr) Sprite3DCubeMapTest::~Sprite3DCubeMapTest() { +#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) + Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener); +#endif + + _teapot->release(); + _skyBox->release(); _textureCube->release(); } @@ -2320,18 +2327,18 @@ void Sprite3DCubeMapTest::addNewSpriteWithCoords(Vec2 p) _camera->setCameraFlag(CameraFlag::USER1); // create a teapot - auto teapot = Sprite3D::create("Sprite3DTest/teapot.c3b"); + _teapot = Sprite3D::create("Sprite3DTest/teapot.c3b"); + _teapot->retain(); //create and set our custom shader auto shader = GLProgram::createWithFilenames("Sprite3DTest/cube_map.vert", "Sprite3DTest/cube_map.frag"); - auto _state = GLProgramState::create(shader); + auto state = GLProgramState::create(shader); // create the second texture for cylinder _textureCube = TextureCube::create("Sprite3DTest/skybox/left.jpg", "Sprite3DTest/skybox/right.jpg", - "Sprite3DTest/skybox/top.jpg", "Sprite3DTest/skybox/bottom.jpg", - "Sprite3DTest/skybox/front.jpg", "Sprite3DTest/skybox/back.jpg"); + "Sprite3DTest/skybox/top.jpg", "Sprite3DTest/skybox/bottom.jpg", + "Sprite3DTest/skybox/front.jpg", "Sprite3DTest/skybox/back.jpg"); - // temporary solution _textureCube->retain(); //set texture parameters @@ -2343,40 +2350,66 @@ void Sprite3DCubeMapTest::addNewSpriteWithCoords(Vec2 p) _textureCube->setTexParameters(tRepeatParams); // pass the texture sampler to our custom shader - _state->setUniformTexture("u_cubeTex", _textureCube); + state->setUniformTexture("u_cubeTex", _textureCube); - teapot->setGLProgramState(_state); - teapot->setPosition3D(Vec3(0, -5, -20)); - teapot->setRotation3D(Vec3(-90, 180, 0)); + _teapot->setGLProgramState(state); + _teapot->setPosition3D(Vec3(0, -5, -20)); + _teapot->setRotation3D(Vec3(-90, 180, 0)); auto rotate_action = RotateBy::create(1.5, Vec3(0, 30, 0)); - teapot->runAction(RepeatForever::create(rotate_action)); + _teapot->runAction(RepeatForever::create(rotate_action)); //pass mesh's attribute to shader long offset = 0; - auto attributeCount = teapot->getMesh()->getMeshVertexAttribCount(); + auto attributeCount = _teapot->getMesh()->getMeshVertexAttribCount(); for (auto i = 0; i < attributeCount; i++) { - auto meshattribute = teapot->getMesh()->getMeshVertexAttribute(i); - _state->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib], - meshattribute.size, - meshattribute.type, - GL_FALSE, - teapot->getMesh()->getVertexSizeInBytes(), - (GLvoid*)offset); + auto meshattribute = _teapot->getMesh()->getMeshVertexAttribute(i); + state->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib], + meshattribute.size, + meshattribute.type, + GL_FALSE, + _teapot->getMesh()->getVertexSizeInBytes(), + (GLvoid*)offset); offset += meshattribute.attribSizeBytes; } - addChild(teapot); + addChild(_teapot); addChild(_camera); setCameraMask(2); { - // config skybox - Skybox* box = Skybox::create(); - box->setTexture(_textureCube); - addChild(box); + // config skybox + _skyBox = Skybox::create(); + _skyBox->retain(); - //auto rotate_action = RotateBy::create(1.5, Vec3(0, -30, 0)); - //box->runAction(RepeatForever::create(rotate_action)); + _skyBox->setTexture(_textureCube); + addChild(_skyBox); } + +#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) + _backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, + [this](EventCustom*) + { + auto state = _teapot->getGLProgramState(); + auto glProgram = state->getGLProgram(); + glProgram->reset(); + glProgram->initWithFilenames("Sprite3DTest/cube_map.vert", "Sprite3DTest/cube_map.frag"); + glProgram->link(); + glProgram->updateUniforms(); + + _textureCube->reloadTexture(); + + Texture2D::TexParams tRepeatParams; + tRepeatParams.magFilter = GL_NEAREST; + tRepeatParams.minFilter = GL_NEAREST; + tRepeatParams.wrapS = GL_MIRRORED_REPEAT; + tRepeatParams.wrapT = GL_MIRRORED_REPEAT; + _textureCube->setTexParameters(tRepeatParams); + state->setUniformTexture("u_cubeTex", _textureCube); + + _skyBox->reload(); + _skyBox->setTexture(_textureCube); + }); + Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1); +#endif } diff --git a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h index 7a7ff1a74d..8e1cbf20f3 100644 --- a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h +++ b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h @@ -1,25 +1,25 @@ /**************************************************************************** - Copyright (c) 2013 cocos2d-x.org - - 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. + Copyright (c) 2013 cocos2d-x.org + + 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 _SPRITE3D_TEST_H_ @@ -30,12 +30,12 @@ #include namespace cocos2d { - class Animate3D; - class Sprite3D; - class Delay; - class Ray; - class DrawNode3D; - class GLProgramState; +class Animate3D; +class Sprite3D; +class Delay; +class Ray; +class DrawNode3D; +class GLProgramState; } class Sprite3DTestDemo : public BaseTest @@ -43,11 +43,11 @@ class Sprite3DTestDemo : public BaseTest public: Sprite3DTestDemo(void); virtual ~Sprite3DTestDemo(void); - + void restartCallback(Ref* sender); void nextCallback(Ref* sender); void backCallback(Ref* sender); - + // overrides virtual std::string title() const override; virtual std::string subtitle() const override; @@ -70,7 +70,7 @@ public: Sprite3DBasicTest(); virtual std::string title() const override; virtual std::string subtitle() const override; - + void addNewSpriteWithCoords(Vec2 p); void onTouchesEnded(const std::vector& touches, Event* event); }; @@ -83,14 +83,14 @@ public: virtual ~Sprite3DUVAnimationTest(); virtual std::string title() const override; virtual std::string subtitle() const override; - + protected: void cylinderUpdate(float dt); - + float _cylinder_texture_offset; float _shining_duraion; - GLProgramState * _state; - + GLProgramState* _state; + #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) EventListenerCustom* _backToForegroundListener; #endif @@ -116,17 +116,17 @@ public: void move3D(float elapsedTime); void updateState(float elapsedTime); bool isState(unsigned int state,unsigned int bit) const; - void onTouchesBegan(const std::vector& touches, cocos2d::Event *event); - void onTouchesMoved(const std::vector& touches, cocos2d::Event *event); - void onTouchesEnded(const std::vector& touches, cocos2d::Event *event); + void onTouchesBegan(const std::vector& touches, cocos2d::Event* event); + void onTouchesMoved(const std::vector& touches, cocos2d::Event* event); + void onTouchesEnded(const std::vector& touches, cocos2d::Event* event); private: - cocos2d::Camera * _camera; + cocos2d::Camera* _camera; Vec3 _targetPos; unsigned int _curState; - cocos2d::Sprite3D * _plane; - cocos2d::Sprite3D * _orc; - GLProgramState * _state; - + cocos2d::Sprite3D* _plane; + cocos2d::Sprite3D* _orc; + GLProgramState* _state; + #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) EventListenerCustom* _backToForegroundListener; #endif @@ -142,7 +142,7 @@ public: virtual std::string subtitle() const override; void onTouchesMoved(const std::vector& touches, cocos2d::Event* event); private: - Camera * _camera; + Camera* _camera; }; class Sprite3DBasicToonShaderTest : public Sprite3DTestDemo @@ -153,10 +153,10 @@ public: virtual ~Sprite3DBasicToonShaderTest(); virtual std::string title() const override; virtual std::string subtitle() const override; - + protected: - GLProgramState * _state; - + GLProgramState* _state; + #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) EventListenerCustom* _backToForegroundListener; #endif @@ -167,8 +167,8 @@ class EffectSprite3D; class Effect3D : public Ref { public: - virtual void draw(const Mat4 &transform) = 0; - virtual void setTarget(EffectSprite3D *sprite) = 0; + virtual void draw(const Mat4& transform) = 0; + virtual void setTarget(EffectSprite3D* sprite) = 0; protected: Effect3D() : _glProgramState(nullptr) {} virtual ~Effect3D() @@ -183,20 +183,20 @@ class Effect3DOutline: public Effect3D { public: static Effect3DOutline* create(); - + void setOutlineColor(const Vec3& color); - + void setOutlineWidth(float width); - - virtual void draw(const Mat4 &transform) override; - virtual void setTarget(EffectSprite3D *sprite) override; + + virtual void draw(const Mat4& transform) override; + virtual void setTarget(EffectSprite3D* sprite) override; protected: - + Effect3DOutline(); virtual ~Effect3DOutline(); - + bool init(); - + Vec3 _outlineColor; float _outlineWidth; //weak reference @@ -204,16 +204,16 @@ protected: #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) EventListenerCustom* _backToForegroundListener; #endif - + protected: static const std::string _vertShaderFile; static const std::string _fragShaderFile; static const std::string _keyInGLProgramCache; - + static const std::string _vertSkinnedShaderFile; static const std::string _fragSkinnedShaderFile; static const std::string _keySkinnedInGLProgramCache; - + static GLProgram* getOrCreateProgram(bool isSkinned = false); }; @@ -231,14 +231,14 @@ class EffectSprite3D : public Sprite3D public: static EffectSprite3D* createFromObjFileAndTexture(const std::string& objFilePath, const std::string& textureFilePath); static EffectSprite3D* create(const std::string& path); - + void setEffect3D(Effect3D* effect); void addEffect(Effect3DOutline* effect, ssize_t order); - virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override; + virtual void draw(Renderer* renderer, const Mat4& transform, uint32_t flags) override; protected: EffectSprite3D(); virtual ~EffectSprite3D(); - + std::vector> _effects; Effect3D* _defaultEffect; CustomCommand _command; @@ -251,9 +251,9 @@ public: Sprite3DEffectTest(); virtual std::string title() const override; virtual std::string subtitle() const override; - + void addNewSpriteWithCoords(Vec2 p); - + void onTouchesEnded(const std::vector& touches, Event* event); }; @@ -265,11 +265,11 @@ public: virtual ~AsyncLoadSprite3DTest(); virtual std::string title() const override; virtual std::string subtitle() const override; - + void menuCallback_asyncLoadSprite(Ref* sender); - + void asyncLoad_Callback(Sprite3D* sprite, void* param); - + protected: std::vector _paths; //model paths to be loaded }; @@ -281,9 +281,9 @@ public: Sprite3DWithSkinTest(); virtual std::string title() const override; virtual std::string subtitle() const override; - + void addNewSpriteWithCoords(Vec2 p); - + void onTouchesEnded(const std::vector& touches, Event* event); }; @@ -294,9 +294,9 @@ public: Sprite3DWithSkinOutlineTest(); virtual std::string title() const override; virtual std::string subtitle() const override; - + void addNewSpriteWithCoords(Vec2 p); - + void onTouchesEnded(const std::vector& touches, Event* event); }; @@ -308,14 +308,14 @@ public: ~Animate3DTest(); virtual std::string title() const override; virtual std::string subtitle() const override; - + void onTouchesEnded(const std::vector& touches, Event* event); - + virtual void update(float dt) override; - + protected: void addSprite3D(); - + enum class State { SWIMMING, @@ -323,20 +323,20 @@ protected: HURT, HURT_TO_SWIMMING, }; - + void reachEndCallBack(); - + void renewCallBack(); - + cocos2d::Sprite3D* _sprite; - + cocos2d::Action* _swim; cocos2d::Animate3D* _hurt; float _elapseTransTime; - + State _state; - + MoveTo* _moveAction; }; @@ -347,11 +347,11 @@ public: AttachmentTest(); virtual std::string title() const override; virtual std::string subtitle() const override; - + void onTouchesEnded(const std::vector& touches, Event* event); - + void addNewSpriteWithCoords(Vec2 p); - + protected: bool _hasWeapon; cocos2d::Sprite3D* _sprite; @@ -366,11 +366,11 @@ public: virtual std::string subtitle() const override; void onTouchesEnded(const std::vector& touches, Event* event); void addNewSpriteWithCoords(Vec2 p); - + void menuCallback_reSkin(Ref* sender); protected: void applyCurSkin(); - + enum class SkinType { UPPER_BODY = 0, @@ -382,7 +382,7 @@ protected: GLASSES, MAX_TYPE, }; - + std::vector _skins[(int)SkinType::MAX_TYPE]; //all skins int _curSkin[(int)SkinType::MAX_TYPE]; //current skin index cocos2d::Sprite3D* _sprite; @@ -416,7 +416,7 @@ protected: std::set _intersetList; void initDrawBox(); void reachEndCallBack(); - + void unproject(const Mat4& viewProjection, const Size* viewport, Vec3* src, Vec3* dst); void calculateRayByLocationInView(Ray* ray, const Vec2& location); }; @@ -428,9 +428,9 @@ public: Sprite3DMirrorTest(); virtual std::string title() const override; virtual std::string subtitle() const override; - + void addNewSpriteWithCoords(Vec2 p); - + protected: bool _hasWeapon; cocos2d::Sprite3D* _sprite; @@ -444,10 +444,10 @@ public: QuaternionTest(); virtual std::string title() const override; virtual std::string subtitle() const override; - + void addNewSpriteWithCoords(Vec2 p); virtual void update(float delta) override; - + protected: cocos2d::Sprite3D* _sprite; float _arcSpeed; @@ -463,15 +463,15 @@ public: UseCaseSprite3D(); virtual std::string title() const override; virtual std::string subtitle() const override; - + virtual void update(float delta) override; - + void menuCallback_Message(Ref* sender); - + protected: - + void switchCase(); - + enum class USECASE{ _3D_WITH_2D, _UI_3D_UI, @@ -491,7 +491,8 @@ public: namespace cocos2d { - class TextureCube; +class TextureCube; +class Skybox; } class Sprite3DCubeMapTest : public Sprite3DTestDemo { @@ -505,8 +506,13 @@ public: void addNewSpriteWithCoords(Vec2); protected: - cocos2d::Sprite3D* _sprite; cocos2d::TextureCube* _textureCube; + Skybox* _skyBox; + Sprite3D* _teapot; + +#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) + EventListenerCustom* _backToForegroundListener; +#endif }; #endif From aa0193044b36d00f2392a300ce7d232a86ad99b0 Mon Sep 17 00:00:00 2001 From: zhukaixy Date: Tue, 24 Mar 2015 15:49:24 +0800 Subject: [PATCH 04/10] discard modify of lua_cocos2dx_3d_auto.cpp --- .../auto/lua_cocos2dx_3d_auto.cpp | 216 ++---------------- 1 file changed, 23 insertions(+), 193 deletions(-) diff --git a/cocos/scripting/lua-bindings/auto/lua_cocos2dx_3d_auto.cpp b/cocos/scripting/lua-bindings/auto/lua_cocos2dx_3d_auto.cpp index e500da44bb..9968e1800a 100644 --- a/cocos/scripting/lua-bindings/auto/lua_cocos2dx_3d_auto.cpp +++ b/cocos/scripting/lua-bindings/auto/lua_cocos2dx_3d_auto.cpp @@ -1293,14 +1293,14 @@ int lua_cocos2dx_3d_Sprite3D_createAsync(lua_State* tolua_S) if (!ok) { break; } std::function arg2; do { - // Lambda binding for lua is not supported. - assert(false); - } while(0) - ; + // Lambda binding for lua is not supported. + assert(false); + } while(0) + ; if (!ok) { break; } void* arg3; #pragma warning NO CONVERSION TO NATIVE FOR void* - ok = false; + ok = false; if (!ok) { break; } cocos2d::Sprite3D::createAsync(arg0, arg1, arg2, arg3); lua_settop(tolua_S, 1); @@ -1317,14 +1317,14 @@ int lua_cocos2dx_3d_Sprite3D_createAsync(lua_State* tolua_S) if (!ok) { break; } std::function arg1; do { - // Lambda binding for lua is not supported. - assert(false); - } while(0) - ; + // Lambda binding for lua is not supported. + assert(false); + } while(0) + ; if (!ok) { break; } void* arg2; #pragma warning NO CONVERSION TO NATIVE FOR void* - ok = false; + ok = false; if (!ok) { break; } cocos2d::Sprite3D::createAsync(arg0, arg1, arg2); lua_settop(tolua_S, 1); @@ -2871,192 +2871,22 @@ int lua_register_cocos2dx_3d_BillBoard(lua_State* tolua_S) g_typeCast["BillBoard"] = "cc.BillBoard"; return 1; } - -int lua_cocos2dx_3d_TextureCube_create(lua_State* tolua_S) -{ - int argc = 0; - bool ok = true; -#if COCOS2D_DEBUG >= 1 - tolua_Error tolua_err; -#endif - -#if COCOS2D_DEBUG >= 1 - if (!tolua_isusertable(tolua_S, 1, "cc.TextureCube", 0, &tolua_err)) goto tolua_lerror; -#endif - - argc = lua_gettop(tolua_S) - 1; - - do - { - if (argc == 6) - { - std::string positive_x; - ok &= luaval_to_std_string(tolua_S, 2, &positive_x, "cc.TextureCube:create"); - if (!ok) { break; } - - std::string negative_x; - ok &= luaval_to_std_string(tolua_S, 3, &negative_x, "cc.TextureCube:create"); - if (!ok){ break; } - - std::string positive_y; - ok &= luaval_to_std_string(tolua_S, 4, &positive_y, "cc.TextureCube:create"); - if (!ok) { break; } - - std::string negative_y; - ok &= luaval_to_std_string(tolua_S, 5, &negative_y, "cc.TextureCube:create"); - if (!ok){ break; } - - std::string positive_z; - ok &= luaval_to_std_string(tolua_S, 6, &positive_z, "cc.TextureCube:create"); - if (!ok) { break; } - - std::string negative_z; - ok &= luaval_to_std_string(tolua_S, 7, &negative_z, "cc.TextureCube:create"); - if (!ok){ break; } - - cocos2d::TextureCube* ret = cocos2d::TextureCube::create(positive_x, negative_x, - positive_y, negative_y, - positive_z, negative_z); - object_to_luaval(tolua_S, "cc.TextureCube", (cocos2d::TextureCube*)ret); - return 1; - } - } while (0); - - luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d", "cc.TextureCube:create", argc, 6); - return 0; -#if COCOS2D_DEBUG >= 1 -tolua_lerror: - tolua_error(tolua_S, "#ferror in function 'lua_cocos2dx_3d_TextureCube_create'.", &tolua_err); -#endif - return 0; -} - -int lua_register_cocos2dx_3d_TextureCube(lua_State* tolua_S) -{ - tolua_usertype(tolua_S, "cc.TextureCube"); - tolua_cclass(tolua_S, "TextureCube", "cc.TextureCube", "cc.Texture2D", nullptr); - - tolua_beginmodule(tolua_S, "TextureCube"); - tolua_function(tolua_S, "create", lua_cocos2dx_3d_TextureCube_create); - tolua_endmodule(tolua_S); - std::string typeName = typeid(cocos2d::TextureCube).name(); - g_luaType[typeName] = "cc.TextureCube"; - g_typeCast["TextureCube"] = "cc.TextureCube"; - return 1; -} - -int lua_cocos2dx_3d_Skybox_create(lua_State* tolua_S) -{ - int argc = 0; - bool ok = true; -#if COCOS2D_DEBUG >= 1 - tolua_Error tolua_err; -#endif - -#if COCOS2D_DEBUG >= 1 - if (!tolua_isusertable(tolua_S, 1, "cc.Skybox", 0, &tolua_err)) goto tolua_lerror; -#endif - - argc = lua_gettop(tolua_S) - 1; - - do - { - if (argc == 0) - { - cocos2d::Skybox* ret = cocos2d::Skybox::create(); - object_to_luaval(tolua_S, "cc.Skybox", (cocos2d::Skybox*)ret); - return 1; - } - } while (0); - - luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d", "cc.Skybox:create", argc, 0); - return 0; -#if COCOS2D_DEBUG >= 1 -tolua_lerror: - tolua_error(tolua_S, "#ferror in function 'lua_cocos2dx_3d_Skybox_create'.", &tolua_err); -#endif - return 0; -} - -int lua_cocos2dx_3d_Skybox_setTexture(lua_State* tolua_S) -{ - int argc = 0; - cocos2d::Skybox* cobj = nullptr; - bool ok = true; -#if COCOS2D_DEBUG >= 1 - tolua_Error tolua_err; -#endif - -#if COCOS2D_DEBUG >= 1 - if (!tolua_isusertype(tolua_S, 1, "cc.Skybox", 0, &tolua_err)) goto tolua_lerror; -#endif - cobj = (cocos2d::Skybox*)tolua_tousertype(tolua_S, 1, 0); -#if COCOS2D_DEBUG >= 1 - if (!cobj) - { - tolua_error(tolua_S, "invalid 'cobj' in function 'lua_cocos2dx_3d_Skybox_setTexture'", nullptr); - return 0; - } -#endif - argc = lua_gettop(tolua_S) - 1; - do{ - if (argc == 1) - { - cocos2d::TextureCube* arg0; - ok &= luaval_to_object(tolua_S, 2, "cc.TextureCube", &arg0); - - if (!ok) { break; } - cobj->setTexture(arg0); - lua_settop(tolua_S, 1); - return 1; - } - } while (0); - - luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d \n", "cc.Skybox:setTexture", argc, 1); - return 0; - -#if COCOS2D_DEBUG >= 1 -tolua_lerror: - tolua_error(tolua_S, "#ferror in function 'lua_cocos2dx_3d_Skybox_setTexture'.", &tolua_err); -#endif - - return 0; -} - -int lua_register_cocos2dx_3d_Skybox(lua_State* tolua_S) -{ - tolua_usertype(tolua_S, "cc.Skybox"); - tolua_cclass(tolua_S, "Skybox", "cc.Skybox", "cc.Node", nullptr); - - tolua_beginmodule(tolua_S, "Skybox"); - tolua_function(tolua_S, "create", lua_cocos2dx_3d_Skybox_create); - tolua_function(tolua_S, "setTexture", lua_cocos2dx_3d_Skybox_setTexture); - tolua_endmodule(tolua_S); - - std::string typeName = typeid(cocos2d::Skybox).name(); - g_luaType[typeName] = "cc.Skybox"; - g_typeCast["Skybox"] = "cc.Skybox"; - return 1; -} - TOLUA_API int register_all_cocos2dx_3d(lua_State* tolua_S) { - tolua_open(tolua_S); - - tolua_module(tolua_S,"cc",0); - tolua_beginmodule(tolua_S,"cc"); + tolua_open(tolua_S); + + tolua_module(tolua_S,"cc",0); + tolua_beginmodule(tolua_S,"cc"); - lua_register_cocos2dx_3d_Animate3D(tolua_S); - lua_register_cocos2dx_3d_Sprite3D(tolua_S); - lua_register_cocos2dx_3d_AttachNode(tolua_S); - lua_register_cocos2dx_3d_BillBoard(tolua_S); - lua_register_cocos2dx_3d_Animation3D(tolua_S); - lua_register_cocos2dx_3d_Skeleton3D(tolua_S); - lua_register_cocos2dx_3d_Mesh(tolua_S); - lua_register_cocos2dx_3d_TextureCube(tolua_S); - lua_register_cocos2dx_3d_Skybox(tolua_S); + lua_register_cocos2dx_3d_Animate3D(tolua_S); + lua_register_cocos2dx_3d_Sprite3D(tolua_S); + lua_register_cocos2dx_3d_AttachNode(tolua_S); + lua_register_cocos2dx_3d_BillBoard(tolua_S); + lua_register_cocos2dx_3d_Animation3D(tolua_S); + lua_register_cocos2dx_3d_Skeleton3D(tolua_S); + lua_register_cocos2dx_3d_Mesh(tolua_S); - tolua_endmodule(tolua_S); - return 1; + tolua_endmodule(tolua_S); + return 1; } From 3b8a796404f06e4be1819f1e52c34098ef8cf2a6 Mon Sep 17 00:00:00 2001 From: zhukaixy Date: Tue, 24 Mar 2015 16:01:06 +0800 Subject: [PATCH 05/10] config project config file for cocos2d-win8.1-universal --- .../libcocos2d_8_1.Shared.vcxitems | 4 ++++ .../libcocos2d_8_1.Shared.vcxitems.filters | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.Shared/libcocos2d_8_1.Shared.vcxitems b/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.Shared/libcocos2d_8_1.Shared.vcxitems index 37ebf55506..ca8763fcb5 100644 --- a/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.Shared/libcocos2d_8_1.Shared.vcxitems +++ b/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.Shared/libcocos2d_8_1.Shared.vcxitems @@ -78,8 +78,10 @@ + + @@ -504,8 +506,10 @@ + + NotUsing diff --git a/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.Shared/libcocos2d_8_1.Shared.vcxitems.filters b/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.Shared/libcocos2d_8_1.Shared.vcxitems.filters index 41cc21558d..2df2089162 100644 --- a/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.Shared/libcocos2d_8_1.Shared.vcxitems.filters +++ b/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.Shared/libcocos2d_8_1.Shared.vcxitems.filters @@ -1291,6 +1291,12 @@ physics + + 3d + + + 3d + @@ -2378,6 +2384,12 @@ base\allocator + + 3d + + + 3d + From 2ebc854335b62b11d0f8eadd4a5177cb1f2eee44 Mon Sep 17 00:00:00 2001 From: zhukaixy Date: Wed, 25 Mar 2015 11:36:11 +0800 Subject: [PATCH 06/10] Modify code style --- .../Classes/Sprite3DTest/Sprite3DTest.cpp | 612 +++++++++--------- 1 file changed, 306 insertions(+), 306 deletions(-) diff --git a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp index 059d9b055e..2aa3c7a972 100644 --- a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp +++ b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp @@ -1,26 +1,26 @@ /**************************************************************************** - Copyright (c) 2012 cocos2d-x.org - Copyright (c) 2013-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. + Copyright (c) 2012 cocos2d-x.org + Copyright (c) 2013-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 "Sprite3DTest.h" @@ -54,7 +54,7 @@ static std::function createFunctions[] = CL(Sprite3DHitTest), CL(AsyncLoadSprite3DTest), #if (CC_TARGET_PLATFORM != CC_PLATFORM_WP8) - // 3DEffect use custom shader which is not supported on WP8/WinRT yet. + // 3DEffect use custom shader which is not supported on WP8/WinRT yet. CL(Sprite3DEffectTest), CL(Sprite3DUVAnimationTest), CL(Sprite3DFakeShadowTest), @@ -82,7 +82,7 @@ static Layer* nextSpriteTestAction() { sceneIdx++; sceneIdx = sceneIdx % MAX_LAYER; - + auto layer = (createFunctions[sceneIdx])(); return layer; } @@ -93,7 +93,7 @@ static Layer* backSpriteTestAction() int total = MAX_LAYER; if( sceneIdx < 0 ) sceneIdx += total; - + auto layer = (createFunctions[sceneIdx])(); return layer; } @@ -111,7 +111,7 @@ static Layer* restartSpriteTestAction() //------------------------------------------------------------------ Sprite3DTestDemo::Sprite3DTestDemo(void) - : BaseTest() +: BaseTest() { } @@ -138,7 +138,7 @@ void Sprite3DTestDemo::restartCallback(Ref* sender) { auto s = new (std::nothrow) Sprite3DTestScene(); s->addChild(restartSpriteTestAction()); - + Director::getInstance()->replaceScene(s); s->release(); } @@ -195,7 +195,7 @@ Sprite3DBasicTest::Sprite3DBasicTest() auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DBasicTest::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); - + auto s = Director::getInstance()->getWinSize(); addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) ); } @@ -205,26 +205,26 @@ void Sprite3DBasicTest::addNewSpriteWithCoords(Vec2 p) //int idx = (int)(CCRANDOM_0_1() * 1400.0f / 100.0f); //int x = (idx%5) * 85; //int y = (idx/5) * 121; - - // //option 1: load a obj that contain the texture in it - // auto sprite = Sprite3D::create("sprite3dTest/scene01.obj"); - + +// //option 1: load a obj that contain the texture in it +// auto sprite = Sprite3D::create("sprite3dTest/scene01.obj"); + //option 2: load obj and assign the texture auto sprite = Sprite3D::create("Sprite3DTest/boss1.obj"); sprite->setScale(3.f); sprite->setTexture("Sprite3DTest/boss.png"); - + // //sprite->setEffect(cocos2d::EFFECT_OUTLINE); - + //add to scene addChild( sprite ); - + sprite->setPosition( Vec2( p.x, p.y) ); - + ActionInterval* action; float random = CCRANDOM_0_1(); - + if( random < 0.20 ) action = ScaleBy::create(3, 2); else if(random < 0.40) @@ -237,7 +237,7 @@ void Sprite3DBasicTest::addNewSpriteWithCoords(Vec2 p) action = FadeOut::create(2); auto action_back = action->reverse(); auto seq = Sequence::create( action, action_back, nullptr ); - + sprite->runAction( RepeatForever::create(seq) ); } @@ -246,7 +246,7 @@ void Sprite3DBasicTest::onTouchesEnded(const std::vector& touches, Event for (auto touch: touches) { auto location = touch->getLocation(); - + addNewSpriteWithCoords( location ); } } @@ -294,11 +294,11 @@ Sprite3DUVAnimationTest::Sprite3DUVAnimationTest() for (auto i = 0; i < attributeCount; i++) { auto meshattribute = cylinder->getMesh()->getMeshVertexAttribute(i); _state->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib], - meshattribute.size, - meshattribute.type, - GL_FALSE, - cylinder->getMesh()->getVertexSizeInBytes(), - (GLvoid*)offset); + meshattribute.size, + meshattribute.type, + GL_FALSE, + cylinder->getMesh()->getVertexSizeInBytes(), + (GLvoid*)offset); offset += meshattribute.attribSizeBytes; } @@ -309,13 +309,13 @@ Sprite3DUVAnimationTest::Sprite3DUVAnimationTest() tRepeatParams.minFilter = GL_NEAREST; tRepeatParams.wrapS = GL_REPEAT; tRepeatParams.wrapT = GL_REPEAT; - shining_texture->setTexParameters(tRepeatParams); + shining_texture->setTexParameters(tRepeatParams); //pass the texture sampler to our custom shader _state->setUniformTexture("caustics",shining_texture); this->addChild(cylinder); - this->setCameraMask(2); + this->setCameraMask(2); this->addChild(camera); //adjust cylinder's position & rotation @@ -324,18 +324,18 @@ Sprite3DUVAnimationTest::Sprite3DUVAnimationTest() //the callback function update cylinder's texcoord schedule(schedule_selector(Sprite3DUVAnimationTest::cylinderUpdate)); - + #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) _backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, - [this](EventCustom*) - { - auto glProgram = _state->getGLProgram(); - glProgram->reset(); - glProgram->initWithFilenames("Sprite3DTest/cylinder.vert", "Sprite3DTest/cylinder.frag"); - glProgram->link(); - glProgram->updateUniforms(); - } - ); + [this](EventCustom*) + { + auto glProgram = _state->getGLProgram(); + glProgram->reset(); + glProgram->initWithFilenames("Sprite3DTest/cylinder.vert", "Sprite3DTest/cylinder.frag"); + glProgram->link(); + glProgram->updateUniforms(); + } + ); Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1); #endif } @@ -347,12 +347,12 @@ Sprite3DUVAnimationTest::~Sprite3DUVAnimationTest() #endif } -std::string Sprite3DUVAnimationTest::title() const +std::string Sprite3DUVAnimationTest::title() const { return "Testing UV Animation"; } -std::string Sprite3DUVAnimationTest::subtitle() const +std::string Sprite3DUVAnimationTest::subtitle() const { return ""; } @@ -387,13 +387,13 @@ void Sprite3DUVAnimationTest::cylinderUpdate(float dt) Sprite3DFakeShadowTest::Sprite3DFakeShadowTest() { Size visibleSize = Director::getInstance()->getVisibleSize(); - + auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesBegan = CC_CALLBACK_2(Sprite3DFakeShadowTest::onTouchesBegan, this); listener->onTouchesMoved = CC_CALLBACK_2(Sprite3DFakeShadowTest::onTouchesMoved, this); listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DFakeShadowTest::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); - + auto layer = Layer::create(); addChild(layer,0); //create Camera @@ -412,18 +412,18 @@ Sprite3DFakeShadowTest::Sprite3DFakeShadowTest() _plane->setGLProgramState(_state); //pass mesh's attribute to shader - long offset = 0; + long offset = 0; auto attributeCount = _plane->getMesh()->getMeshVertexAttribCount(); for (auto i = 0; i < attributeCount; i++) { auto meshattribute = _plane->getMesh()->getMeshVertexAttribute(i); _state->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib], - meshattribute.size, - meshattribute.type, - GL_FALSE, - _plane->getMesh()->getVertexSizeInBytes(), - (GLvoid*)offset); + meshattribute.size, + meshattribute.type, + GL_FALSE, + _plane->getMesh()->getVertexSizeInBytes(), + (GLvoid*)offset); offset += meshattribute.attribSizeBytes; - } + } _state->setUniformMat4("u_model_matrix",_plane->getNodeToWorldTransform()); //create shadow texture @@ -433,9 +433,9 @@ Sprite3DFakeShadowTest::Sprite3DFakeShadowTest() tRepeatParams.minFilter = GL_LINEAR; tRepeatParams.wrapS = GL_CLAMP_TO_EDGE; tRepeatParams.wrapT = GL_CLAMP_TO_EDGE; - shadowTexture->setTexParameters(tRepeatParams); + shadowTexture->setTexParameters(tRepeatParams); _state->setUniformTexture("u_shadowTexture",shadowTexture); - layer->addChild(_plane); + layer->addChild(_plane); //create the orc _orc = Sprite3D::create("Sprite3DTest/orc.c3b"); @@ -449,30 +449,30 @@ Sprite3DFakeShadowTest::Sprite3DFakeShadowTest() layer->setCameraMask(2); schedule(CC_SCHEDULE_SELECTOR(Sprite3DFakeShadowTest::updateCamera), 0.0f); - + #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) _backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, - [this](EventCustom*) - { - auto glProgram = _state->getGLProgram(); - glProgram->reset(); - glProgram->initWithFilenames("Sprite3DTest/simple_shadow.vert","Sprite3DTest/simple_shadow.frag"); - glProgram->link(); - glProgram->updateUniforms(); - - _state->setUniformMat4("u_model_matrix",_plane->getNodeToWorldTransform()); - - //create shadow texture - auto shadowTexture = Director::getInstance()->getTextureCache()->addImage("Sprite3DTest/shadowCircle.png"); - Texture2D::TexParams tRepeatParams;//set texture parameters - tRepeatParams.magFilter = GL_LINEAR; - tRepeatParams.minFilter = GL_LINEAR; - tRepeatParams.wrapS = GL_CLAMP_TO_EDGE; - tRepeatParams.wrapT = GL_CLAMP_TO_EDGE; - shadowTexture->setTexParameters(tRepeatParams); - _state->setUniformTexture("u_shadowTexture",shadowTexture); - } - ); + [this](EventCustom*) + { + auto glProgram = _state->getGLProgram(); + glProgram->reset(); + glProgram->initWithFilenames("Sprite3DTest/simple_shadow.vert","Sprite3DTest/simple_shadow.frag"); + glProgram->link(); + glProgram->updateUniforms(); + + _state->setUniformMat4("u_model_matrix",_plane->getNodeToWorldTransform()); + + //create shadow texture + auto shadowTexture = Director::getInstance()->getTextureCache()->addImage("Sprite3DTest/shadowCircle.png"); + Texture2D::TexParams tRepeatParams;//set texture parameters + tRepeatParams.magFilter = GL_LINEAR; + tRepeatParams.minFilter = GL_LINEAR; + tRepeatParams.wrapS = GL_CLAMP_TO_EDGE; + tRepeatParams.wrapT = GL_CLAMP_TO_EDGE; + shadowTexture->setTexParameters(tRepeatParams); + _state->setUniformTexture("u_shadowTexture",shadowTexture); + } + ); Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1); #endif } @@ -484,12 +484,12 @@ Sprite3DFakeShadowTest::~Sprite3DFakeShadowTest() #endif } -std::string Sprite3DFakeShadowTest::title() const +std::string Sprite3DFakeShadowTest::title() const { return "fake shadow effect"; } -std::string Sprite3DFakeShadowTest::subtitle() const +std::string Sprite3DFakeShadowTest::subtitle() const { return "touch the screen to move around"; } @@ -594,18 +594,18 @@ bool Sprite3DFakeShadowTest::isState(unsigned int state,unsigned int bit) const return (state & bit) == bit; } -void Sprite3DFakeShadowTest::onTouchesBegan(const std::vector& touches, cocos2d::Event* event) +void Sprite3DFakeShadowTest::onTouchesBegan(const std::vector& touches, cocos2d::Event *event) { } -void Sprite3DFakeShadowTest::onTouchesMoved(const std::vector& touches, cocos2d::Event* event) +void Sprite3DFakeShadowTest::onTouchesMoved(const std::vector& touches, cocos2d::Event *event) { } -void Sprite3DFakeShadowTest::onTouchesEnded(const std::vector& touches, cocos2d::Event* event) +void Sprite3DFakeShadowTest::onTouchesEnded(const std::vector& touches, cocos2d::Event *event) { - for ( auto& item: touches ) + for ( auto &item: touches ) { auto touch = item; auto location = touch->getLocationInView(); @@ -653,43 +653,43 @@ Sprite3DBasicToonShaderTest::Sprite3DBasicToonShaderTest() auto _camera = Camera::createPerspective(60, visibleSize.width/visibleSize.height, 0.1f, 200); _camera->setCameraFlag(CameraFlag::USER1); // create a teapot - auto teapot = Sprite3D::create("Sprite3DTest/teapot.c3b"); - //create and set our custom shader + auto teapot = Sprite3D::create("Sprite3DTest/teapot.c3b"); + //create and set our custom shader auto shader =GLProgram::createWithFilenames("Sprite3DTest/toon.vert","Sprite3DTest/toon.frag"); _state = GLProgramState::create(shader); teapot->setGLProgramState(_state); teapot->setPosition3D(Vec3(0,-5,-20)); - teapot->setRotation3D(Vec3(-90,180,0)); + teapot->setRotation3D(Vec3(-90,180,0)); auto rotate_action = RotateBy::create(1.5,Vec3(0,30,0)); - teapot->runAction(RepeatForever::create(rotate_action)); + teapot->runAction(RepeatForever::create(rotate_action)); //pass mesh's attribute to shader long offset = 0; auto attributeCount = teapot->getMesh()->getMeshVertexAttribCount(); for (auto i = 0; i < attributeCount; i++) { auto meshattribute = teapot->getMesh()->getMeshVertexAttribute(i); _state->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib], - meshattribute.size, - meshattribute.type, - GL_FALSE, - teapot->getMesh()->getVertexSizeInBytes(), - (GLvoid*)offset); + meshattribute.size, + meshattribute.type, + GL_FALSE, + teapot->getMesh()->getVertexSizeInBytes(), + (GLvoid*)offset); offset += meshattribute.attribSizeBytes; } addChild(teapot); addChild(_camera); setCameraMask(2); - + #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) _backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, - [this](EventCustom*) - { - auto glProgram = _state->getGLProgram(); - glProgram->reset(); - glProgram->initWithFilenames("Sprite3DTest/toon.vert","Sprite3DTest/toon.frag"); - glProgram->link(); - glProgram->updateUniforms(); - } - ); + [this](EventCustom*) + { + auto glProgram = _state->getGLProgram(); + glProgram->reset(); + glProgram->initWithFilenames("Sprite3DTest/toon.vert","Sprite3DTest/toon.frag"); + glProgram->link(); + glProgram->updateUniforms(); + } + ); Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1); #endif } @@ -701,19 +701,19 @@ Sprite3DBasicToonShaderTest::~Sprite3DBasicToonShaderTest() #endif } -std::string Sprite3DBasicToonShaderTest::title() const +std::string Sprite3DBasicToonShaderTest::title() const { return "basic toon shader test"; } -std::string Sprite3DBasicToonShaderTest::subtitle() const +std::string Sprite3DBasicToonShaderTest::subtitle() const { return " "; } //------------------------------------------------------------------ // -// Sprite3DLightMapTest +// Sprite3DLightMapTest // //------------------------------------------------------------------ Sprite3DLightMapTest::Sprite3DLightMapTest() @@ -725,16 +725,16 @@ Sprite3DLightMapTest::Sprite3DLightMapTest() _camera->setCameraFlag(CameraFlag::USER1); _camera->setPosition3D(Vec3(0,25,15)); _camera->setRotation3D(Vec3(-35,0,0)); - auto LightMapScene = Sprite3D::create("Sprite3DTest/LightMapScene.c3b"); - LightMapScene->setScale(0.1f); + auto LightMapScene = Sprite3D::create("Sprite3DTest/LightMapScene.c3b"); + LightMapScene->setScale(0.1f); addChild(LightMapScene); - addChild(_camera); - setCameraMask(2); + addChild(_camera); + setCameraMask(2); //add a point light auto light = PointLight::create(Vec3(35,75,-20.5),Color3B(255,255,255),150); addChild(light); - //set the ambient light + //set the ambient light auto ambient = AmbientLight::create(Color3B(55,55,55)); addChild(ambient); @@ -745,14 +745,14 @@ Sprite3DLightMapTest::Sprite3DLightMapTest() } Sprite3DLightMapTest::~Sprite3DLightMapTest() { - + } std::string Sprite3DLightMapTest::title() const { return "light map test"; } -std::string Sprite3DLightMapTest::subtitle() const +std::string Sprite3DLightMapTest::subtitle() const { return "drag the screen to move around"; } @@ -776,9 +776,9 @@ void Sprite3DLightMapTest::onTouchesMoved(const std::vector& to cameraRightDir.normalize(); cameraRightDir.y=0; Vec3 cameraPos= _camera->getPosition3D(); - cameraPos+=cameraDir*newPos.y*delta; + cameraPos+=cameraDir*newPos.y*delta; cameraPos+=cameraRightDir*newPos.x*delta; - _camera->setPosition3D(cameraPos); + _camera->setPosition3D(cameraPos); } } @@ -790,33 +790,33 @@ void Sprite3DLightMapTest::onTouchesMoved(const std::vector& to Sprite3DHitTest::Sprite3DHitTest() { auto s = Director::getInstance()->getWinSize(); - + auto sprite1 = Sprite3D::create("Sprite3DTest/boss1.obj"); - + sprite1->setScale(4.f); sprite1->setTexture("Sprite3DTest/boss.png"); sprite1->setPosition( Vec2(s.width/2, s.height/2) ); - + //add to scene addChild( sprite1 ); sprite1->runAction(RepeatForever::create(RotateBy::create(3, 360))); - + auto sprite2 = Sprite3D::create("Sprite3DTest/boss1.obj"); - + sprite2->setScale(4.f); sprite2->setTexture("Sprite3DTest/boss.png"); sprite2->setPosition( Vec2(s.width/2, s.height/2) ); sprite2->setAnchorPoint(Vec2(0.5, 0.5)); - + //add to scene addChild( sprite2 ); sprite2->runAction(RepeatForever::create(RotateBy::create(3, -360))); - - + + // Make sprite1 touchable auto listener1 = EventListenerTouchOneByOne::create(); listener1->setSwallowTouches(true); - + listener1->onTouchBegan = [](Touch* touch, Event* event){ auto target = static_cast(event->getCurrentTarget()); @@ -840,10 +840,10 @@ Sprite3DHitTest::Sprite3DHitTest() log("sprite3d onTouchesEnded.. "); target->setOpacity(255); }; - + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, sprite1); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), sprite2); - + } std::string Sprite3DHitTest::title() const @@ -860,21 +860,21 @@ void Sprite3DTestScene::runThisTest() { auto layer = nextSpriteTestAction(); addChild(layer); - + Director::getInstance()->replaceScene(this); } Sprite3DTestScene::Sprite3DTestScene() { - + } -static int tuple_sort( const std::tuple& tuple1, const std::tuple& tuple2 ) +static int tuple_sort( const std::tuple &tuple1, const std::tuple &tuple2 ) { return std::get<0>(tuple1) < std::get<0>(tuple2); } -EffectSprite3D* EffectSprite3D::createFromObjFileAndTexture(const std::string& objFilePath, const std::string& textureFilePath) +EffectSprite3D* EffectSprite3D::createFromObjFileAndTexture(const std::string &objFilePath, const std::string &textureFilePath) { auto sprite = new (std::nothrow) EffectSprite3D(); if (sprite && sprite->initWithFile(objFilePath)) @@ -888,11 +888,11 @@ EffectSprite3D* EffectSprite3D::createFromObjFileAndTexture(const std::string& o return nullptr; } -EffectSprite3D* EffectSprite3D::create(const std::string& path) +EffectSprite3D* EffectSprite3D::create(const std::string &path) { if (path.length() < 4) CCASSERT(false, "improper name specified when creating Sprite3D"); - + auto sprite = new (std::nothrow) EffectSprite3D(); if (sprite && sprite->initWithFile(path)) { @@ -904,9 +904,9 @@ EffectSprite3D* EffectSprite3D::create(const std::string& path) } EffectSprite3D::EffectSprite3D() - : _defaultEffect(nullptr) +: _defaultEffect(nullptr) { - + } EffectSprite3D::~EffectSprite3D() @@ -918,7 +918,7 @@ EffectSprite3D::~EffectSprite3D() CC_SAFE_RELEASE(_defaultEffect); } -void EffectSprite3D::setEffect3D(Effect3D* effect) +void EffectSprite3D::setEffect3D(Effect3D *effect) { if(_defaultEffect == effect) return; CC_SAFE_RETAIN(effect); @@ -931,7 +931,7 @@ void EffectSprite3D::addEffect(Effect3DOutline* effect, ssize_t order) if(nullptr == effect) return; effect->retain(); effect->setTarget(this); - + _effects.push_back(std::make_tuple(order,effect,CustomCommand())); std::sort(std::begin(_effects), std::end(_effects), tuple_sort); @@ -986,26 +986,26 @@ Effect3DOutline* Effect3DOutline::create() bool Effect3DOutline::init() { - + return true; } Effect3DOutline::Effect3DOutline() - : _outlineWidth(1.0f) - , _outlineColor(1, 1, 1) - , _sprite(nullptr) +: _outlineWidth(1.0f) +, _outlineColor(1, 1, 1) +, _sprite(nullptr) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) _backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, - [this](EventCustom*) - { - auto glProgram = _glProgramState->getGLProgram(); - glProgram->reset(); - glProgram->initWithFilenames(_vertShaderFile, _fragShaderFile); - glProgram->link(); - glProgram->updateUniforms(); - } - ); + [this](EventCustom*) + { + auto glProgram = _glProgramState->getGLProgram(); + glProgram->reset(); + glProgram->initWithFilenames(_vertShaderFile, _fragShaderFile); + glProgram->link(); + glProgram->updateUniforms(); + } + ); Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1); #endif } @@ -1037,10 +1037,10 @@ void Effect3DOutline::setOutlineWidth(float width) } } -void Effect3DOutline::setTarget(EffectSprite3D* sprite) +void Effect3DOutline::setTarget(EffectSprite3D *sprite) { CCASSERT(nullptr != sprite && nullptr != sprite->getMesh(),"Error: Setting a null pointer or a null mesh EffectSprite3D to Effect3D"); - + if(sprite != _sprite) { GLProgram* glprogram; @@ -1054,16 +1054,16 @@ void Effect3DOutline::setTarget(EffectSprite3D* sprite) _glProgramState->retain(); _glProgramState->setUniformVec3("OutLineColor", _outlineColor); _glProgramState->setUniformFloat("OutlineWidth", _outlineWidth); - - + + _sprite = sprite; - + auto mesh = sprite->getMesh(); long offset = 0; for (auto i = 0; i < mesh->getMeshVertexAttribCount(); i++) { auto meshvertexattrib = mesh->getMeshVertexAttribute(i); - + _glProgramState->setVertexAttribPointer(s_attributeNames[meshvertexattrib.vertexAttrib], meshvertexattrib.size, meshvertexattrib.type, @@ -1072,12 +1072,12 @@ void Effect3DOutline::setTarget(EffectSprite3D* sprite) (void*)offset); offset += meshvertexattrib.attribSizeBytes; } - + Color4F color(_sprite->getDisplayedColor()); color.a = _sprite->getDisplayedOpacity() / 255.0f; _glProgramState->setUniformVec4("u_color", Vec4(color.r, color.g, color.b, color.a)); } - + } static void MatrixPalleteCallBack( GLProgram* glProgram, Uniform* uniform, int paletteSize, const float* palette) @@ -1085,7 +1085,7 @@ static void MatrixPalleteCallBack( GLProgram* glProgram, Uniform* uniform, int p glUniform4fv( uniform->location, (GLsizei)paletteSize, (const float*)palette ); } -void Effect3DOutline::draw(const Mat4& transform) +void Effect3DOutline::draw(const Mat4 &transform) { //draw Color4F color(_sprite->getDisplayedColor()); @@ -1096,10 +1096,10 @@ void Effect3DOutline::draw(const Mat4& transform) glEnable(GL_CULL_FACE); glCullFace(GL_FRONT); glEnable(GL_DEPTH_TEST); - + auto mesh = _sprite->getMesh(); glBindBuffer(GL_ARRAY_BUFFER, mesh->getVertexBuffer()); - + auto skin = _sprite->getMesh()->getSkin(); if(_sprite && skin) { @@ -1107,14 +1107,14 @@ void Effect3DOutline::draw(const Mat4& transform) skin->getMatrixPaletteSize(), (float*)skin->getMatrixPalette()); _glProgramState->setUniformCallback("u_matrixPalette", function); } - + if(_sprite) _glProgramState->apply(transform); - + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->getIndexBuffer()); glDrawElements(mesh->getPrimitiveType(), (GLsizei)mesh->getIndexCount(), mesh->getIndexFormat(), 0); CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, mesh->getIndexCount()); - + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); glDisable(GL_DEPTH_TEST); @@ -1123,18 +1123,18 @@ void Effect3DOutline::draw(const Mat4& transform) } } -void EffectSprite3D::draw(cocos2d::Renderer* renderer, const cocos2d::Mat4& transform, uint32_t flags) +void EffectSprite3D::draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, uint32_t flags) { - for(auto& effect : _effects) + for(auto &effect : _effects) { if(std::get<0>(effect) >=0) break; - CustomCommand& cc = std::get<2>(effect); + CustomCommand &cc = std::get<2>(effect); cc.func = CC_CALLBACK_0(Effect3D::draw,std::get<1>(effect),transform); renderer->addCommand(&cc); - + } - + if(!_defaultEffect) { Sprite3D::draw(renderer, transform, flags); @@ -1145,15 +1145,15 @@ void EffectSprite3D::draw(cocos2d::Renderer* renderer, const cocos2d::Mat4& tran _command.func = CC_CALLBACK_0(Effect3D::draw, _defaultEffect, transform); renderer->addCommand(&_command); } - - for(auto& effect : _effects) + + for(auto &effect : _effects) { if(std::get<0>(effect) <=0) continue; - CustomCommand& cc = std::get<2>(effect); + CustomCommand &cc = std::get<2>(effect); cc.func = CC_CALLBACK_0(Effect3D::draw,std::get<1>(effect),transform); renderer->addCommand(&cc); - + } } @@ -1161,7 +1161,7 @@ Sprite3DEffectTest::Sprite3DEffectTest() { auto s = Director::getInstance()->getWinSize(); addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) ); - + auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DEffectTest::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); @@ -1184,22 +1184,22 @@ void Sprite3DEffectTest::addNewSpriteWithCoords(Vec2 p) sprite->addEffect(effect, -1); effect->setOutlineColor(Vec3(1,0,0)); effect->setOutlineWidth(0.01f); - + Effect3DOutline* effect2 = Effect3DOutline::create(); sprite->addEffect(effect2, -2); effect2->setOutlineWidth(0.02f); effect2->setOutlineColor(Vec3(1,1,0)); //sprite->setEffect3D(effect); sprite->setScale(6.f); - + //add to scene addChild( sprite ); - + sprite->setPosition( Vec2( p.x, p.y) ); - + ActionInterval* action; float random = CCRANDOM_0_1(); - + if( random < 0.20 ) action = ScaleBy::create(3, 2); else if(random < 0.40) @@ -1212,7 +1212,7 @@ void Sprite3DEffectTest::addNewSpriteWithCoords(Vec2 p) action = FadeOut::create(2); auto action_back = action->reverse(); auto seq = Sequence::create( action, action_back, nullptr ); - + sprite->runAction( RepeatForever::create(seq) ); } @@ -1221,7 +1221,7 @@ void Sprite3DEffectTest::onTouchesEnded(const std::vector& touches, Even for (auto touch: touches) { auto location = touch->getLocation(); - + addNewSpriteWithCoords( location ); } } @@ -1233,22 +1233,22 @@ AsyncLoadSprite3DTest::AsyncLoadSprite3DTest() _paths.push_back("Sprite3DTest/orc.c3b"); _paths.push_back("Sprite3DTest/ReskinGirl.c3b"); _paths.push_back("Sprite3DTest/axe.c3b"); - + TTFConfig ttfConfig("fonts/arial.ttf", 15); auto label1 = Label::createWithTTF(ttfConfig,"AsyncLoad Sprite3D"); auto item1 = MenuItemLabel::create(label1,CC_CALLBACK_1(AsyncLoadSprite3DTest::menuCallback_asyncLoadSprite,this) ); - + auto s = Director::getInstance()->getWinSize(); item1->setPosition( s.width * .5f, s.height * .8f); - + auto pMenu1 = CCMenu::create(item1, nullptr); pMenu1->setPosition(Vec2(0,0)); this->addChild(pMenu1, 10); - + auto node = Node::create(); node->setTag(101); this->addChild(node); - + menuCallback_asyncLoadSprite(nullptr); } @@ -1269,10 +1269,10 @@ void AsyncLoadSprite3DTest::menuCallback_asyncLoadSprite(Ref* sender) { //Note that you must stop the tasks before leaving the scene. AsyncTaskPool::getInstance()->stopTasks(AsyncTaskPool::TaskType::TASK_IO); - + auto node = getChildByTag(101); node->removeAllChildren(); //remove all loaded sprite - + //remove cache data Sprite3DCache::getInstance()->removeAllSprite3DData(); long index = 0; @@ -1298,7 +1298,7 @@ Sprite3DWithSkinTest::Sprite3DWithSkinTest() auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DWithSkinTest::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); - + auto s = Director::getInstance()->getWinSize(); addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) ); } @@ -1347,7 +1347,7 @@ void Sprite3DWithSkinTest::onTouchesEnded(const std::vector& touches, Ev for (auto touch: touches) { auto location = touch->getLocation(); - + addNewSpriteWithCoords( location ); } } @@ -1357,7 +1357,7 @@ Sprite3DWithSkinOutlineTest::Sprite3DWithSkinOutlineTest() auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DWithSkinOutlineTest::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); - + auto s = Director::getInstance()->getWinSize(); addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) ); } @@ -1372,33 +1372,33 @@ std::string Sprite3DWithSkinOutlineTest::subtitle() const void Sprite3DWithSkinOutlineTest::addNewSpriteWithCoords(Vec2 p) { - + std::string fileName = "Sprite3DTest/orc.c3b"; auto sprite = EffectSprite3D::create(fileName); - + Effect3DOutline* effect = Effect3DOutline::create(); effect->setOutlineColor(Vec3(1,0,0)); effect->setOutlineWidth(0.01f); sprite->addEffect(effect, -1); - + Effect3DOutline* effect2 = Effect3DOutline::create(); effect2->setOutlineWidth(0.02f); effect2->setOutlineColor(Vec3(1,1,0)); sprite->addEffect(effect2, -2); - + sprite->setScale(3); sprite->setRotation3D(Vec3(0,180,0)); addChild(sprite); sprite->setPosition( Vec2( p.x, p.y) ); - + auto animation = Animation3D::create(fileName); if (animation) { auto animate = Animate3D::create(animation); bool inverse = (std::rand() % 3 == 0); - + int rand2 = std::rand(); float speed = 1.0f; if(rand2 % 3 == 1) @@ -1410,7 +1410,7 @@ void Sprite3DWithSkinOutlineTest::addNewSpriteWithCoords(Vec2 p) speed = animate->getSpeed() - 0.5 * CCRANDOM_0_1(); } animate->setSpeed(inverse ? -speed : speed); - + sprite->runAction(RepeatForever::create(animate)); } } @@ -1420,24 +1420,24 @@ void Sprite3DWithSkinOutlineTest::onTouchesEnded(const std::vector& touc for (auto touch: touches) { auto location = touch->getLocation(); - + addNewSpriteWithCoords( location ); } } Animate3DTest::Animate3DTest() - : _hurt(nullptr) - , _swim(nullptr) - , _sprite(nullptr) - , _moveAction(nullptr) - , _elapseTransTime(0.f) +: _hurt(nullptr) +, _swim(nullptr) +, _sprite(nullptr) +, _moveAction(nullptr) +, _elapseTransTime(0.f) { addSprite3D(); - + auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(Animate3DTest::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); - + scheduleUpdate(); } @@ -1463,7 +1463,7 @@ void Animate3DTest::update(float dt) if (_state == State::HURT_TO_SWIMMING) { _elapseTransTime += dt; - + if (_elapseTransTime >= Animate3D::getTransitionTime()) { _sprite->stopAction(_hurt); @@ -1496,13 +1496,13 @@ void Animate3DTest::addSprite3D() auto animate = Animate3D::create(animation, 0.f, 1.933f); _swim = RepeatForever::create(animate); sprite->runAction(_swim); - + _swim->retain(); _hurt = Animate3D::create(animation, 1.933f, 2.8f); _hurt->retain(); _state = State::SWIMMING; } - + _moveAction = MoveTo::create(4.f, Vec2(s.width / 5.f, s.height / 2.f)); _moveAction->retain(); auto seq = Sequence::create(_moveAction, CallFunc::create(CC_CALLBACK_0(Animate3DTest::reachEndCallBack, this)), nullptr); @@ -1536,7 +1536,7 @@ void Animate3DTest::onTouchesEnded(const std::vector& touches, Event* ev for (auto touch: touches) { auto location = touch->getLocation(); - + if (_sprite) { float len = (_sprite->getPosition() - location).length(); @@ -1561,12 +1561,12 @@ void Animate3DTest::onTouchesEnded(const std::vector& touches, Event* ev } AttachmentTest::AttachmentTest() - : _hasWeapon(false) - , _sprite(nullptr) +: _hasWeapon(false) +, _sprite(nullptr) { auto s = Director::getInstance()->getWinSize(); addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) ); - + auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(AttachmentTest::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); @@ -1588,16 +1588,16 @@ void AttachmentTest::addNewSpriteWithCoords(Vec2 p) sprite->setRotation3D(Vec3(0,180,0)); addChild(sprite); sprite->setPosition( Vec2( p.x, p.y) ); - + //test attach auto sp = Sprite3D::create("Sprite3DTest/axe.c3b"); sprite->getAttachNode("Bip001 R Hand")->addChild(sp); - + auto animation = Animation3D::create(fileName); if (animation) { auto animate = Animate3D::create(animation); - + sprite->runAction(RepeatForever::create(animate)); } _sprite = sprite; @@ -1618,11 +1618,11 @@ void AttachmentTest::onTouchesEnded(const std::vector& touches, Event* e _hasWeapon = !_hasWeapon; } Sprite3DReskinTest::Sprite3DReskinTest() - : _sprite(nullptr) +: _sprite(nullptr) { auto s = Director::getInstance()->getWinSize(); addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) ); - + auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DReskinTest::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); @@ -1630,11 +1630,11 @@ Sprite3DReskinTest::Sprite3DReskinTest() auto label1 = Label::createWithTTF(ttfConfig,"Hair"); auto item1 = MenuItemLabel::create(label1,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) ); auto label2 = Label::createWithTTF(ttfConfig,"Glasses"); - auto item2 = MenuItemLabel::create(label2, CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) ); + auto item2 = MenuItemLabel::create(label2, CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) ); auto label3 = Label::createWithTTF(ttfConfig,"Coat"); auto item3 = MenuItemLabel::create(label3,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) ); auto label4 = Label::createWithTTF(ttfConfig,"Pants"); - auto item4 = MenuItemLabel::create(label4, CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) ); + auto item4 = MenuItemLabel::create(label4, CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) ); auto label5 = Label::createWithTTF(ttfConfig,"Shoes"); auto item5 = MenuItemLabel::create(label5,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) ); item1->setPosition( Vec2(VisibleRect::left().x+50, VisibleRect::bottom().y+item1->getContentSize().height*4 ) ); @@ -1650,7 +1650,7 @@ Sprite3DReskinTest::Sprite3DReskinTest() auto pMenu1 = CCMenu::create(item1, item2, item3, item4, item5, nullptr); pMenu1->setPosition(Vec2(0,0)); this->addChild(pMenu1, 10); - + } void Sprite3DReskinTest::menuCallback_reSkin(Ref* sender) { @@ -1683,41 +1683,41 @@ void Sprite3DReskinTest::addNewSpriteWithCoords(Vec2 p) if (animation) { auto animate = Animate3D::create(animation); - + sprite->runAction(RepeatForever::create(animate)); } _sprite = sprite; - + auto& body = _skins[(int)SkinType::UPPER_BODY]; body.push_back("Girl_UpperBody01"); body.push_back("Girl_UpperBody02"); - + auto& pants = _skins[(int)SkinType::PANTS]; pants.push_back("Girl_LowerBody01"); pants.push_back("Girl_LowerBody02"); - + auto& shoes = _skins[(int)SkinType::SHOES]; shoes.push_back("Girl_Shoes01"); shoes.push_back("Girl_Shoes02"); - + auto& hair = _skins[(int)SkinType::HAIR]; hair.push_back("Girl_Hair01"); hair.push_back("Girl_Hair02"); - + auto& face = _skins[(int)SkinType::FACE]; face.push_back("Girl_Face01"); face.push_back("Girl_Face02"); - + auto& hand = _skins[(int)SkinType::HAND]; hand.push_back("Girl_Hand01"); hand.push_back("Girl_Hand02"); - + auto& glasses = _skins[(int)SkinType::GLASSES]; glasses.push_back(""); glasses.push_back("Girl_Glasses01"); - + memset(_curSkin, 0, (int)SkinType::MAX_TYPE * sizeof(int)); - + applyCurSkin(); } @@ -1750,7 +1750,7 @@ Sprite3DWithOBBPerformanceTest::Sprite3DWithOBBPerformanceTest() _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); auto s = Director::getInstance()->getWinSize(); initDrawBox(); - + addNewSpriteWithCoords(Vec2(s.width/2, s.height/2)); MenuItemFont::setFontName("fonts/arial.ttf"); MenuItemFont::setFontSize(65); @@ -1758,12 +1758,12 @@ Sprite3DWithOBBPerformanceTest::Sprite3DWithOBBPerformanceTest() decrease->setColor(Color3B(0,200,20)); auto increase = MenuItemFont::create(" + ", CC_CALLBACK_1(Sprite3DWithOBBPerformanceTest::addOBBCallback, this)); increase->setColor(Color3B(0,200,20)); - + auto menu = Menu::create(decrease, increase, nullptr); menu->alignItemsHorizontally(); menu->setPosition(Vec2(s.width/2, s.height-65)); addChild(menu, 1); - + TTFConfig ttfCount("fonts/Marker Felt.ttf", 30); _labelCubeCount = Label::createWithTTF(ttfCount,"0 cubes"); _labelCubeCount->setColor(Color3B(0,200,20)); @@ -1815,14 +1815,14 @@ void Sprite3DWithOBBPerformanceTest::onTouchesBegan(const std::vector& t void Sprite3DWithOBBPerformanceTest::onTouchesEnded(const std::vector& touches, Event* event) { - + } void Sprite3DWithOBBPerformanceTest::onTouchesMoved(const std::vector& touches, Event* event) { for (const auto& touch: touches) { - auto location = touch->getLocation(); + auto location = touch->getLocation(); auto obbSize = _obb.size(); for(decltype(obbSize) i = 0; i < obbSize; i++) @@ -1838,23 +1838,23 @@ void Sprite3DWithOBBPerformanceTest::update(float dt) char szText[16]; sprintf(szText,"%lu cubes",_obb.size()); _labelCubeCount->setString(szText); - + if (_drawDebug) { _drawDebug->clear(); - + Mat4 mat = _sprite->getNodeToWorldTransform(); mat.getRightVector(&_obbt._xAxis); _obbt._xAxis.normalize(); - + mat.getUpVector(&_obbt._yAxis); _obbt._yAxis.normalize(); - + mat.getForwardVector(&_obbt._zAxis); _obbt._zAxis.normalize(); - + _obbt._center = _sprite->getPosition3D(); - + Vec3 corners[8] = {}; _obbt.getCorners(corners); _drawDebug->drawCube(corners, Color4F(0,0,1,1)); @@ -1893,16 +1893,16 @@ void Sprite3DWithOBBPerformanceTest::addNewSpriteWithCoords(Vec2 p) auto animate = Animate3D::create(animation, 0.f, 1.933f); sprite->runAction(RepeatForever::create(animate)); } - + _moveAction = MoveTo::create(4.f, Vec2(s.width / 5.f, s.height / 2.f)); _moveAction->retain(); auto seq = Sequence::create(_moveAction, CallFunc::create(CC_CALLBACK_0(Sprite3DWithOBBPerformanceTest::reachEndCallBack, this)), nullptr); seq->setTag(100); sprite->runAction(seq); - + AABB aabb = _sprite->getAABB(); _obbt = OBB(aabb); - + _drawDebug = DrawNode3D::create(); addChild(_drawDebug); } @@ -1956,23 +1956,23 @@ void Sprite3DWithOBBPerformanceTest::delOBBWithCount(float value) void Sprite3DWithOBBPerformanceTest::unproject(const Mat4& viewProjection, const Size* viewport, Vec3* src, Vec3* dst) { assert(dst); - + assert(viewport->width != 0.0f && viewport->height != 0.0f); Vec4 screen(src->x / viewport->width, ((viewport->height - src->y)) / viewport->height, src->z, 1.0f); - + screen.x = screen.x * 2.0f - 1.0f; screen.y = screen.y * 2.0f - 1.0f; screen.z = screen.z * 2.0f - 1.0f; - + viewProjection.getInversed().transformVector(screen, &screen); - + if (screen.w != 0.0f) { screen.x /= screen.w; screen.y /= screen.w; screen.z /= screen.w; } - + dst->set(screen.x, screen.y, screen.z); } @@ -1981,26 +1981,26 @@ void Sprite3DWithOBBPerformanceTest::calculateRayByLocationInView(Ray* ray, cons auto dir = Director::getInstance(); auto view = dir->getWinSize(); auto mat = dir->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION); - + Vec3 src = Vec3(location.x, location.y, -1); Vec3 nearPoint; unproject(mat, &view, &src, &nearPoint); - + src = Vec3(location.x, location.y, 1); Vec3 farPoint; unproject(mat, &view, &src, &farPoint); - + Vec3 direction; Vec3::subtract(farPoint, nearPoint, &direction); direction.normalize(); - + ray->_origin = nearPoint; ray->_direction = direction; } Sprite3DMirrorTest::Sprite3DMirrorTest() - : _sprite(nullptr) - , _mirrorSprite(nullptr) +: _sprite(nullptr) +, _mirrorSprite(nullptr) { auto s = Director::getInstance()->getWinSize(); addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) ); @@ -2022,21 +2022,21 @@ void Sprite3DMirrorTest::addNewSpriteWithCoords(Vec2 p) sprite->setRotation3D(Vec3(0,180,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); - + auto animation = Animation3D::create(fileName); if (animation) { auto animate = Animate3D::create(animation); - + sprite->runAction(RepeatForever::create(animate)); } _sprite = sprite; _hasWeapon = true; - + //create mirror Sprite3D sprite = Sprite3D::create(fileName); sprite->setScale(5); @@ -2045,25 +2045,25 @@ void Sprite3DMirrorTest::addNewSpriteWithCoords(Vec2 p) 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; } QuaternionTest::QuaternionTest() - : _arcSpeed(CC_DEGREES_TO_RADIANS(90)) - , _radius(100.f) - , _accAngle(0.f) +: _arcSpeed(CC_DEGREES_TO_RADIANS(90)) +, _radius(100.f) +, _accAngle(0.f) { auto s = Director::getInstance()->getWinSize(); addNewSpriteWithCoords(Vec2(s.width / 2.f, s.height / 2.f)); @@ -2101,20 +2101,20 @@ void QuaternionTest::update(float delta) const float pi = M_PI; if (_accAngle >= 2 * pi) _accAngle -= 2 * pi; - + auto s = Director::getInstance()->getWinSize(); _sprite->setPosition(Vec2(s.width / 2.f + _radius * cosf(_accAngle), s.height / 2.f + _radius * sinf(_accAngle))); - + Quaternion quat; Quaternion::createFromAxisAngle(Vec3(0.f, 0.f, 1.f), _accAngle - pi * 0.5f, &quat); _sprite->setRotationQuat(quat); } UseCaseSprite3D::UseCaseSprite3D() - : _caseIdx(0) +: _caseIdx(0) { auto s = Director::getInstance()->getWinSize(); - + _useCaseTitles[0] = "transparent 3d sprite and 2d sprite"; _useCaseTitles[1] = "ui - 3d - ui"; @@ -2131,28 +2131,28 @@ UseCaseSprite3D::UseCaseSprite3D() _caseIdx++; if (_caseIdx >= (int)USECASE::MAX_CASE_NUM) _caseIdx = 0; - this->switchCase(); - }); - + this->switchCase(); + }); + auto menu = Menu::create(itemPrev, itemNext, nullptr); menu->alignItemsHorizontally(); menu->setScale(0.5); menu->setAnchorPoint(Vec2(0,0)); menu->setPosition(Vec2(s.width/2,70)); - + _label = Label::create(); _label->setPosition(s.width * 0.5f, s.height * 0.8f); addChild(_label); - + addChild(menu); - + //setup camera auto camera = Camera::createPerspective(40, s.width / s.height, 0.01f, 1000.f); camera->setCameraFlag(CameraFlag::USER1); camera->setPosition3D(Vec3(0.f, 30.f, 100.f)); camera->lookAt(Vec3(0.f, 0.f, 0.f)); addChild(camera); - + switchCase(); } @@ -2169,7 +2169,7 @@ std::string UseCaseSprite3D::subtitle() const void UseCaseSprite3D::switchCase() { removeChildByTag(101); - + auto s = Director::getInstance()->getWinSize(); _label->setString(_useCaseTitles[_caseIdx]); if (_caseIdx == 0) // use case 1, 3d transparent sprite + 2d sprite @@ -2181,33 +2181,33 @@ void UseCaseSprite3D::switchCase() if (animation) { auto animate = Animate3D::create(animation); - + sprite->runAction(RepeatForever::create(animate)); } - + auto circleBack = Sprite3D::create(); auto circle = Sprite::create("Sprite3DTest/circle.png"); circleBack->setScale(0.5f); circleBack->addChild(circle); circle->runAction(RepeatForever::create(RotateBy::create(3, Vec3(0.f, 0.f, 360.f)))); - + circleBack->setRotation3D(Vec3(90, 0, 0)); - + auto pos = sprite->getPosition3D(); circleBack->setPosition3D(Vec3(pos.x, pos.y, pos.z - 1)); - + sprite->setOpacity(250); sprite->setCameraMask(2); circleBack->setCameraMask(2); sprite->setTag(3); circleBack->setTag(2); - + auto node = Node::create(); node->addChild(sprite); node->addChild(circleBack); node->setTag(101); addChild(node); - + scheduleUpdate(); update(0.f); } @@ -2217,7 +2217,7 @@ void UseCaseSprite3D::switchCase() layer->setPosition(s.width * 0.25f, s.height * 0.25f); layer->setGlobalZOrder(-1); addChild(layer); - + std::string filename = "Sprite3DTest/girl.c3b"; auto sprite = Sprite3D::create(filename); sprite->setScale(0.5f); @@ -2229,20 +2229,20 @@ void UseCaseSprite3D::switchCase() } sprite->setPosition(s.width * 0.25f, s.height * 0.125f); layer->addChild(sprite); - + TTFConfig ttfConfig("fonts/arial.ttf", 15); auto label1 = Label::createWithTTF(ttfConfig,"Message"); auto item1 = MenuItemLabel::create(label1,CC_CALLBACK_1(UseCaseSprite3D::menuCallback_Message,this) ); auto label2 = Label::createWithTTF(ttfConfig,"Message"); - auto item2 = MenuItemLabel::create(label2, CC_CALLBACK_1(UseCaseSprite3D::menuCallback_Message,this) ); - + auto item2 = MenuItemLabel::create(label2, CC_CALLBACK_1(UseCaseSprite3D::menuCallback_Message,this) ); + item1->setPosition( Vec2(s.width * 0.5f - item1->getContentSize().width * 0.5f, s.height * 0.5f - item1->getContentSize().height ) ); item2->setPosition( Vec2(s.width * 0.5f - item1->getContentSize().width * 0.5f, s.height * 0.5f - item1->getContentSize().height * 2.f ) ); - + auto pMenu1 = CCMenu::create(item1, item2, nullptr); pMenu1->setPosition(Vec2(0,0)); layer->addChild(pMenu1); - + layer->setTag(101); } } @@ -2275,14 +2275,14 @@ void UseCaseSprite3D::update(float delta) { static float accAngle = 0.f; accAngle += delta * CC_DEGREES_TO_RADIANS(60); - + float radius = 30.f; float x = cosf(accAngle) * radius, z = sinf(accAngle) * radius; - + auto node = getChildByTag(101); auto sprite3d = node->getChildByTag(3); auto circle = node->getChildByTag(2); - + sprite3d->setPositionX(x); sprite3d->setPositionZ(z); circle->setPositionX(x); From 54472497a4690bbf97b0ed07f646a65d526c5c68 Mon Sep 17 00:00:00 2001 From: zhukaixy Date: Wed, 25 Mar 2015 13:43:55 +0800 Subject: [PATCH 07/10] fix comments --- cocos/3d/CCSkybox.cpp | 56 ++++++++-------- .../Classes/Sprite3DTest/Sprite3DTest.cpp | 66 +++++++++---------- 2 files changed, 61 insertions(+), 61 deletions(-) diff --git a/cocos/3d/CCSkybox.cpp b/cocos/3d/CCSkybox.cpp index d871a971e5..11f1b85ba5 100644 --- a/cocos/3d/CCSkybox.cpp +++ b/cocos/3d/CCSkybox.cpp @@ -1,25 +1,25 @@ /**************************************************************************** - 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. + 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 "CCSkybox.h" @@ -88,13 +88,13 @@ void Skybox::initBuffers() glBufferData(GL_ARRAY_BUFFER, sizeof(vexBuf), vexBuf, GL_STATIC_DRAW); // init index buffer object - unsigned char idxBuf[] = { 2, 1, 0, 3, 2, 0, // font - 1, 5, 4, 1, 4, 0, // right - 4, 5, 6, 4, 6, 7, // back - 7, 6, 2, 7, 2, 3, // left - 2, 6, 5, 2, 5, 1, // up - 3, 0, 4, 3, 4, 7 // down - }; + const unsigned char idxBuf[] = { 2, 1, 0, 3, 2, 0, // font + 1, 5, 4, 1, 4, 0, // right + 4, 5, 6, 4, 6, 7, // back + 7, 6, 2, 7, 2, 3, // left + 2, 6, 5, 2, 5, 1, // up + 3, 0, 4, 3, 4, 7 // down + }; glGenBuffers(1, &_indexBuffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer); diff --git a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp index 2aa3c7a972..3ac91f2251 100644 --- a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp +++ b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp @@ -1,27 +1,26 @@ /**************************************************************************** - Copyright (c) 2012 cocos2d-x.org - Copyright (c) 2013-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. - ****************************************************************************/ +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 "Sprite3DTest.h" #include "base/CCAsyncTaskPool.h" @@ -1630,11 +1629,11 @@ Sprite3DReskinTest::Sprite3DReskinTest() auto label1 = Label::createWithTTF(ttfConfig,"Hair"); auto item1 = MenuItemLabel::create(label1,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) ); auto label2 = Label::createWithTTF(ttfConfig,"Glasses"); - auto item2 = MenuItemLabel::create(label2, CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) ); + auto item2 = MenuItemLabel::create(label2, CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) ); auto label3 = Label::createWithTTF(ttfConfig,"Coat"); auto item3 = MenuItemLabel::create(label3,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) ); auto label4 = Label::createWithTTF(ttfConfig,"Pants"); - auto item4 = MenuItemLabel::create(label4, CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) ); + auto item4 = MenuItemLabel::create(label4, CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) ); auto label5 = Label::createWithTTF(ttfConfig,"Shoes"); auto item5 = MenuItemLabel::create(label5,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) ); item1->setPosition( Vec2(VisibleRect::left().x+50, VisibleRect::bottom().y+item1->getContentSize().height*4 ) ); @@ -2234,7 +2233,7 @@ void UseCaseSprite3D::switchCase() auto label1 = Label::createWithTTF(ttfConfig,"Message"); auto item1 = MenuItemLabel::create(label1,CC_CALLBACK_1(UseCaseSprite3D::menuCallback_Message,this) ); auto label2 = Label::createWithTTF(ttfConfig,"Message"); - auto item2 = MenuItemLabel::create(label2, CC_CALLBACK_1(UseCaseSprite3D::menuCallback_Message,this) ); + auto item2 = MenuItemLabel::create(label2, CC_CALLBACK_1(UseCaseSprite3D::menuCallback_Message,this) ); item1->setPosition( Vec2(s.width * 0.5f - item1->getContentSize().width * 0.5f, s.height * 0.5f - item1->getContentSize().height ) ); item2->setPosition( Vec2(s.width * 0.5f - item1->getContentSize().width * 0.5f, s.height * 0.5f - item1->getContentSize().height * 2.f ) ); @@ -2336,8 +2335,8 @@ void Sprite3DCubeMapTest::addNewSpriteWithCoords(Vec2 p) // create the second texture for cylinder _textureCube = TextureCube::create("Sprite3DTest/skybox/left.jpg", "Sprite3DTest/skybox/right.jpg", - "Sprite3DTest/skybox/top.jpg", "Sprite3DTest/skybox/bottom.jpg", - "Sprite3DTest/skybox/front.jpg", "Sprite3DTest/skybox/back.jpg"); + "Sprite3DTest/skybox/top.jpg", "Sprite3DTest/skybox/bottom.jpg", + "Sprite3DTest/skybox/front.jpg", "Sprite3DTest/skybox/back.jpg"); _textureCube->retain(); @@ -2366,11 +2365,12 @@ void Sprite3DCubeMapTest::addNewSpriteWithCoords(Vec2 p) { auto meshattribute = _teapot->getMesh()->getMeshVertexAttribute(i); state->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib], - meshattribute.size, - meshattribute.type, - GL_FALSE, - _teapot->getMesh()->getVertexSizeInBytes(), - (GLvoid*)offset); + meshattribute.size, + meshattribute.type, + GL_FALSE, + _teapot->getMesh()->getVertexSizeInBytes(), + (GLvoid*)offset); + offset += meshattribute.attribSizeBytes; } addChild(_teapot); From c1e5e47bdb2a296a19ac19c02c5aa634bd03baab Mon Sep 17 00:00:00 2001 From: zhukaixy Date: Wed, 25 Mar 2015 13:46:30 +0800 Subject: [PATCH 08/10] fix code style issue --- .../Classes/Sprite3DTest/Sprite3DTest.h | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h index 8e1cbf20f3..5b852bc4e1 100644 --- a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h +++ b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h @@ -1,26 +1,26 @@ /**************************************************************************** - Copyright (c) 2013 cocos2d-x.org +Copyright (c) 2014 Chukong Technologies Inc. - http://www.cocos2d-x.org +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: +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 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. - ****************************************************************************/ +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 _SPRITE3D_TEST_H_ #define _SPRITE3D_TEST_H_ From aead9e5855bd2f4ea0aeb36577b326245d8942fc Mon Sep 17 00:00:00 2001 From: zhukaixy Date: Thu, 26 Mar 2015 09:53:41 +0800 Subject: [PATCH 09/10] correct for code indent --- cocos/3d/CCSkybox.cpp | 42 +- .../Classes/Sprite3DTest/Sprite3DTest.cpp | 660 +++++++++--------- .../Classes/Sprite3DTest/Sprite3DTest.h | 184 ++--- 3 files changed, 449 insertions(+), 437 deletions(-) diff --git a/cocos/3d/CCSkybox.cpp b/cocos/3d/CCSkybox.cpp index 11f1b85ba5..baab531046 100644 --- a/cocos/3d/CCSkybox.cpp +++ b/cocos/3d/CCSkybox.cpp @@ -1,25 +1,25 @@ /**************************************************************************** - 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. + 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 "CCSkybox.h" diff --git a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp index 3ac91f2251..97037e3dd1 100644 --- a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp +++ b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp @@ -1,26 +1,27 @@ /**************************************************************************** -Copyright (c) 2014 Chukong Technologies Inc. + Copyright (c) 2012 cocos2d-x.org + Copyright (c) 2013-2014 Chukong Technologies Inc. -http://www.cocos2d-x.org + 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: + 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 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. -****************************************************************************/ + 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 "Sprite3DTest.h" #include "base/CCAsyncTaskPool.h" @@ -53,7 +54,7 @@ static std::function createFunctions[] = CL(Sprite3DHitTest), CL(AsyncLoadSprite3DTest), #if (CC_TARGET_PLATFORM != CC_PLATFORM_WP8) - // 3DEffect use custom shader which is not supported on WP8/WinRT yet. + // 3DEffect use custom shader which is not supported on WP8/WinRT yet. CL(Sprite3DEffectTest), CL(Sprite3DUVAnimationTest), CL(Sprite3DFakeShadowTest), @@ -81,7 +82,7 @@ static Layer* nextSpriteTestAction() { sceneIdx++; sceneIdx = sceneIdx % MAX_LAYER; - + auto layer = (createFunctions[sceneIdx])(); return layer; } @@ -92,7 +93,7 @@ static Layer* backSpriteTestAction() int total = MAX_LAYER; if( sceneIdx < 0 ) sceneIdx += total; - + auto layer = (createFunctions[sceneIdx])(); return layer; } @@ -110,7 +111,7 @@ static Layer* restartSpriteTestAction() //------------------------------------------------------------------ Sprite3DTestDemo::Sprite3DTestDemo(void) -: BaseTest() + : BaseTest() { } @@ -137,7 +138,7 @@ void Sprite3DTestDemo::restartCallback(Ref* sender) { auto s = new (std::nothrow) Sprite3DTestScene(); s->addChild(restartSpriteTestAction()); - + Director::getInstance()->replaceScene(s); s->release(); } @@ -194,7 +195,7 @@ Sprite3DBasicTest::Sprite3DBasicTest() auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DBasicTest::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); - + auto s = Director::getInstance()->getWinSize(); addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) ); } @@ -204,26 +205,26 @@ void Sprite3DBasicTest::addNewSpriteWithCoords(Vec2 p) //int idx = (int)(CCRANDOM_0_1() * 1400.0f / 100.0f); //int x = (idx%5) * 85; //int y = (idx/5) * 121; - -// //option 1: load a obj that contain the texture in it -// auto sprite = Sprite3D::create("sprite3dTest/scene01.obj"); - + + // //option 1: load a obj that contain the texture in it + // auto sprite = Sprite3D::create("sprite3dTest/scene01.obj"); + //option 2: load obj and assign the texture auto sprite = Sprite3D::create("Sprite3DTest/boss1.obj"); sprite->setScale(3.f); sprite->setTexture("Sprite3DTest/boss.png"); - + // //sprite->setEffect(cocos2d::EFFECT_OUTLINE); - + //add to scene addChild( sprite ); - + sprite->setPosition( Vec2( p.x, p.y) ); - + ActionInterval* action; float random = CCRANDOM_0_1(); - + if( random < 0.20 ) action = ScaleBy::create(3, 2); else if(random < 0.40) @@ -236,7 +237,7 @@ void Sprite3DBasicTest::addNewSpriteWithCoords(Vec2 p) action = FadeOut::create(2); auto action_back = action->reverse(); auto seq = Sequence::create( action, action_back, nullptr ); - + sprite->runAction( RepeatForever::create(seq) ); } @@ -245,7 +246,7 @@ void Sprite3DBasicTest::onTouchesEnded(const std::vector& touches, Event for (auto touch: touches) { auto location = touch->getLocation(); - + addNewSpriteWithCoords( location ); } } @@ -290,14 +291,15 @@ Sprite3DUVAnimationTest::Sprite3DUVAnimationTest() //pass mesh's attribute to shader long offset = 0; auto attributeCount = cylinder->getMesh()->getMeshVertexAttribCount(); - for (auto i = 0; i < attributeCount; i++) { + for (auto i = 0; i < attributeCount; i++) + { auto meshattribute = cylinder->getMesh()->getMeshVertexAttribute(i); _state->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib], - meshattribute.size, - meshattribute.type, - GL_FALSE, - cylinder->getMesh()->getVertexSizeInBytes(), - (GLvoid*)offset); + meshattribute.size, + meshattribute.type, + GL_FALSE, + cylinder->getMesh()->getVertexSizeInBytes(), + (GLvoid*)offset); offset += meshattribute.attribSizeBytes; } @@ -308,13 +310,13 @@ Sprite3DUVAnimationTest::Sprite3DUVAnimationTest() tRepeatParams.minFilter = GL_NEAREST; tRepeatParams.wrapS = GL_REPEAT; tRepeatParams.wrapT = GL_REPEAT; - shining_texture->setTexParameters(tRepeatParams); + shining_texture->setTexParameters(tRepeatParams); //pass the texture sampler to our custom shader _state->setUniformTexture("caustics",shining_texture); this->addChild(cylinder); - this->setCameraMask(2); + this->setCameraMask(2); this->addChild(camera); //adjust cylinder's position & rotation @@ -323,18 +325,18 @@ Sprite3DUVAnimationTest::Sprite3DUVAnimationTest() //the callback function update cylinder's texcoord schedule(schedule_selector(Sprite3DUVAnimationTest::cylinderUpdate)); - + #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) _backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, - [this](EventCustom*) - { - auto glProgram = _state->getGLProgram(); - glProgram->reset(); - glProgram->initWithFilenames("Sprite3DTest/cylinder.vert", "Sprite3DTest/cylinder.frag"); - glProgram->link(); - glProgram->updateUniforms(); - } - ); + [this](EventCustom*) + { + auto glProgram = _state->getGLProgram(); + glProgram->reset(); + glProgram->initWithFilenames("Sprite3DTest/cylinder.vert", "Sprite3DTest/cylinder.frag"); + glProgram->link(); + glProgram->updateUniforms(); + } + ); Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1); #endif } @@ -346,12 +348,12 @@ Sprite3DUVAnimationTest::~Sprite3DUVAnimationTest() #endif } -std::string Sprite3DUVAnimationTest::title() const +std::string Sprite3DUVAnimationTest::title() const { return "Testing UV Animation"; } -std::string Sprite3DUVAnimationTest::subtitle() const +std::string Sprite3DUVAnimationTest::subtitle() const { return ""; } @@ -386,13 +388,13 @@ void Sprite3DUVAnimationTest::cylinderUpdate(float dt) Sprite3DFakeShadowTest::Sprite3DFakeShadowTest() { Size visibleSize = Director::getInstance()->getVisibleSize(); - + auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesBegan = CC_CALLBACK_2(Sprite3DFakeShadowTest::onTouchesBegan, this); listener->onTouchesMoved = CC_CALLBACK_2(Sprite3DFakeShadowTest::onTouchesMoved, this); listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DFakeShadowTest::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); - + auto layer = Layer::create(); addChild(layer,0); //create Camera @@ -411,18 +413,19 @@ Sprite3DFakeShadowTest::Sprite3DFakeShadowTest() _plane->setGLProgramState(_state); //pass mesh's attribute to shader - long offset = 0; + long offset = 0; auto attributeCount = _plane->getMesh()->getMeshVertexAttribCount(); - for (auto i = 0; i < attributeCount; i++) { + for (auto i = 0; i < attributeCount; i++) + { auto meshattribute = _plane->getMesh()->getMeshVertexAttribute(i); _state->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib], - meshattribute.size, - meshattribute.type, - GL_FALSE, - _plane->getMesh()->getVertexSizeInBytes(), - (GLvoid*)offset); + meshattribute.size, + meshattribute.type, + GL_FALSE, + _plane->getMesh()->getVertexSizeInBytes(), + (GLvoid*)offset); offset += meshattribute.attribSizeBytes; - } + } _state->setUniformMat4("u_model_matrix",_plane->getNodeToWorldTransform()); //create shadow texture @@ -432,9 +435,9 @@ Sprite3DFakeShadowTest::Sprite3DFakeShadowTest() tRepeatParams.minFilter = GL_LINEAR; tRepeatParams.wrapS = GL_CLAMP_TO_EDGE; tRepeatParams.wrapT = GL_CLAMP_TO_EDGE; - shadowTexture->setTexParameters(tRepeatParams); + shadowTexture->setTexParameters(tRepeatParams); _state->setUniformTexture("u_shadowTexture",shadowTexture); - layer->addChild(_plane); + layer->addChild(_plane); //create the orc _orc = Sprite3D::create("Sprite3DTest/orc.c3b"); @@ -448,30 +451,30 @@ Sprite3DFakeShadowTest::Sprite3DFakeShadowTest() layer->setCameraMask(2); schedule(CC_SCHEDULE_SELECTOR(Sprite3DFakeShadowTest::updateCamera), 0.0f); - + #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) _backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, - [this](EventCustom*) - { - auto glProgram = _state->getGLProgram(); - glProgram->reset(); - glProgram->initWithFilenames("Sprite3DTest/simple_shadow.vert","Sprite3DTest/simple_shadow.frag"); - glProgram->link(); - glProgram->updateUniforms(); - - _state->setUniformMat4("u_model_matrix",_plane->getNodeToWorldTransform()); - - //create shadow texture - auto shadowTexture = Director::getInstance()->getTextureCache()->addImage("Sprite3DTest/shadowCircle.png"); - Texture2D::TexParams tRepeatParams;//set texture parameters - tRepeatParams.magFilter = GL_LINEAR; - tRepeatParams.minFilter = GL_LINEAR; - tRepeatParams.wrapS = GL_CLAMP_TO_EDGE; - tRepeatParams.wrapT = GL_CLAMP_TO_EDGE; - shadowTexture->setTexParameters(tRepeatParams); - _state->setUniformTexture("u_shadowTexture",shadowTexture); - } - ); + [this](EventCustom*) + { + auto glProgram = _state->getGLProgram(); + glProgram->reset(); + glProgram->initWithFilenames("Sprite3DTest/simple_shadow.vert","Sprite3DTest/simple_shadow.frag"); + glProgram->link(); + glProgram->updateUniforms(); + + _state->setUniformMat4("u_model_matrix",_plane->getNodeToWorldTransform()); + + //create shadow texture + auto shadowTexture = Director::getInstance()->getTextureCache()->addImage("Sprite3DTest/shadowCircle.png"); + Texture2D::TexParams tRepeatParams;//set texture parameters + tRepeatParams.magFilter = GL_LINEAR; + tRepeatParams.minFilter = GL_LINEAR; + tRepeatParams.wrapS = GL_CLAMP_TO_EDGE; + tRepeatParams.wrapT = GL_CLAMP_TO_EDGE; + shadowTexture->setTexParameters(tRepeatParams); + _state->setUniformTexture("u_shadowTexture",shadowTexture); + } + ); Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1); #endif } @@ -483,12 +486,12 @@ Sprite3DFakeShadowTest::~Sprite3DFakeShadowTest() #endif } -std::string Sprite3DFakeShadowTest::title() const +std::string Sprite3DFakeShadowTest::title() const { return "fake shadow effect"; } -std::string Sprite3DFakeShadowTest::subtitle() const +std::string Sprite3DFakeShadowTest::subtitle() const { return "touch the screen to move around"; } @@ -593,18 +596,18 @@ bool Sprite3DFakeShadowTest::isState(unsigned int state,unsigned int bit) const return (state & bit) == bit; } -void Sprite3DFakeShadowTest::onTouchesBegan(const std::vector& touches, cocos2d::Event *event) +void Sprite3DFakeShadowTest::onTouchesBegan(const std::vector& touches, cocos2d::Event* event) { } -void Sprite3DFakeShadowTest::onTouchesMoved(const std::vector& touches, cocos2d::Event *event) +void Sprite3DFakeShadowTest::onTouchesMoved(const std::vector& touches, cocos2d::Event* event) { } -void Sprite3DFakeShadowTest::onTouchesEnded(const std::vector& touches, cocos2d::Event *event) +void Sprite3DFakeShadowTest::onTouchesEnded(const std::vector& touches, cocos2d::Event* event) { - for ( auto &item: touches ) + for ( auto& item: touches ) { auto touch = item; auto location = touch->getLocationInView(); @@ -652,43 +655,44 @@ Sprite3DBasicToonShaderTest::Sprite3DBasicToonShaderTest() auto _camera = Camera::createPerspective(60, visibleSize.width/visibleSize.height, 0.1f, 200); _camera->setCameraFlag(CameraFlag::USER1); // create a teapot - auto teapot = Sprite3D::create("Sprite3DTest/teapot.c3b"); - //create and set our custom shader + auto teapot = Sprite3D::create("Sprite3DTest/teapot.c3b"); + //create and set our custom shader auto shader =GLProgram::createWithFilenames("Sprite3DTest/toon.vert","Sprite3DTest/toon.frag"); _state = GLProgramState::create(shader); teapot->setGLProgramState(_state); teapot->setPosition3D(Vec3(0,-5,-20)); - teapot->setRotation3D(Vec3(-90,180,0)); + teapot->setRotation3D(Vec3(-90,180,0)); auto rotate_action = RotateBy::create(1.5,Vec3(0,30,0)); - teapot->runAction(RepeatForever::create(rotate_action)); + teapot->runAction(RepeatForever::create(rotate_action)); //pass mesh's attribute to shader long offset = 0; auto attributeCount = teapot->getMesh()->getMeshVertexAttribCount(); - for (auto i = 0; i < attributeCount; i++) { + for (auto i = 0; i < attributeCount; i++) + { auto meshattribute = teapot->getMesh()->getMeshVertexAttribute(i); _state->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib], - meshattribute.size, - meshattribute.type, - GL_FALSE, - teapot->getMesh()->getVertexSizeInBytes(), - (GLvoid*)offset); + meshattribute.size, + meshattribute.type, + GL_FALSE, + teapot->getMesh()->getVertexSizeInBytes(), + (GLvoid*)offset); offset += meshattribute.attribSizeBytes; } addChild(teapot); addChild(_camera); setCameraMask(2); - + #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) _backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, - [this](EventCustom*) - { - auto glProgram = _state->getGLProgram(); - glProgram->reset(); - glProgram->initWithFilenames("Sprite3DTest/toon.vert","Sprite3DTest/toon.frag"); - glProgram->link(); - glProgram->updateUniforms(); - } - ); + [this](EventCustom*) + { + auto glProgram = _state->getGLProgram(); + glProgram->reset(); + glProgram->initWithFilenames("Sprite3DTest/toon.vert","Sprite3DTest/toon.frag"); + glProgram->link(); + glProgram->updateUniforms(); + } + ); Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1); #endif } @@ -700,19 +704,19 @@ Sprite3DBasicToonShaderTest::~Sprite3DBasicToonShaderTest() #endif } -std::string Sprite3DBasicToonShaderTest::title() const +std::string Sprite3DBasicToonShaderTest::title() const { return "basic toon shader test"; } -std::string Sprite3DBasicToonShaderTest::subtitle() const +std::string Sprite3DBasicToonShaderTest::subtitle() const { return " "; } //------------------------------------------------------------------ // -// Sprite3DLightMapTest +// Sprite3DLightMapTest // //------------------------------------------------------------------ Sprite3DLightMapTest::Sprite3DLightMapTest() @@ -724,16 +728,16 @@ Sprite3DLightMapTest::Sprite3DLightMapTest() _camera->setCameraFlag(CameraFlag::USER1); _camera->setPosition3D(Vec3(0,25,15)); _camera->setRotation3D(Vec3(-35,0,0)); - auto LightMapScene = Sprite3D::create("Sprite3DTest/LightMapScene.c3b"); - LightMapScene->setScale(0.1f); + auto LightMapScene = Sprite3D::create("Sprite3DTest/LightMapScene.c3b"); + LightMapScene->setScale(0.1f); addChild(LightMapScene); - addChild(_camera); - setCameraMask(2); + addChild(_camera); + setCameraMask(2); //add a point light auto light = PointLight::create(Vec3(35,75,-20.5),Color3B(255,255,255),150); addChild(light); - //set the ambient light + //set the ambient light auto ambient = AmbientLight::create(Color3B(55,55,55)); addChild(ambient); @@ -744,14 +748,14 @@ Sprite3DLightMapTest::Sprite3DLightMapTest() } Sprite3DLightMapTest::~Sprite3DLightMapTest() { - + } std::string Sprite3DLightMapTest::title() const { return "light map test"; } -std::string Sprite3DLightMapTest::subtitle() const +std::string Sprite3DLightMapTest::subtitle() const { return "drag the screen to move around"; } @@ -775,9 +779,9 @@ void Sprite3DLightMapTest::onTouchesMoved(const std::vector& to cameraRightDir.normalize(); cameraRightDir.y=0; Vec3 cameraPos= _camera->getPosition3D(); - cameraPos+=cameraDir*newPos.y*delta; + cameraPos+=cameraDir*newPos.y*delta; cameraPos+=cameraRightDir*newPos.x*delta; - _camera->setPosition3D(cameraPos); + _camera->setPosition3D(cameraPos); } } @@ -789,37 +793,38 @@ void Sprite3DLightMapTest::onTouchesMoved(const std::vector& to Sprite3DHitTest::Sprite3DHitTest() { auto s = Director::getInstance()->getWinSize(); - + auto sprite1 = Sprite3D::create("Sprite3DTest/boss1.obj"); - + sprite1->setScale(4.f); sprite1->setTexture("Sprite3DTest/boss.png"); sprite1->setPosition( Vec2(s.width/2, s.height/2) ); - + //add to scene addChild( sprite1 ); sprite1->runAction(RepeatForever::create(RotateBy::create(3, 360))); - + auto sprite2 = Sprite3D::create("Sprite3DTest/boss1.obj"); - + sprite2->setScale(4.f); sprite2->setTexture("Sprite3DTest/boss.png"); sprite2->setPosition( Vec2(s.width/2, s.height/2) ); sprite2->setAnchorPoint(Vec2(0.5, 0.5)); - + //add to scene addChild( sprite2 ); sprite2->runAction(RepeatForever::create(RotateBy::create(3, -360))); - - + + // Make sprite1 touchable auto listener1 = EventListenerTouchOneByOne::create(); listener1->setSwallowTouches(true); - - listener1->onTouchBegan = [](Touch* touch, Event* event){ + + listener1->onTouchBegan = [](Touch* touch, Event* event) + { auto target = static_cast(event->getCurrentTarget()); - - Rect rect = target->getBoundingBox(); + + Rect rect = target->getBoundingBox(); if (rect.containsPoint(touch->getLocation())) { log("sprite3d began... x = %f, y = %f", touch->getLocation().x, touch->getLocation().y); @@ -828,21 +833,23 @@ Sprite3DHitTest::Sprite3DHitTest() } return false; }; - - listener1->onTouchMoved = [](Touch* touch, Event* event){ + + listener1->onTouchMoved = [](Touch* touch, Event* event) + { auto target = static_cast(event->getCurrentTarget()); target->setPosition(target->getPosition() + touch->getDelta()); }; - - listener1->onTouchEnded = [=](Touch* touch, Event* event){ + + listener1->onTouchEnded = [=](Touch* touch, Event* event) + { auto target = static_cast(event->getCurrentTarget()); log("sprite3d onTouchesEnded.. "); target->setOpacity(255); }; - + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, sprite1); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), sprite2); - + } std::string Sprite3DHitTest::title() const @@ -859,21 +866,21 @@ void Sprite3DTestScene::runThisTest() { auto layer = nextSpriteTestAction(); addChild(layer); - + Director::getInstance()->replaceScene(this); } Sprite3DTestScene::Sprite3DTestScene() { - + } -static int tuple_sort( const std::tuple &tuple1, const std::tuple &tuple2 ) +static int tuple_sort( const std::tuple& tuple1, const std::tuple& tuple2 ) { return std::get<0>(tuple1) < std::get<0>(tuple2); } -EffectSprite3D* EffectSprite3D::createFromObjFileAndTexture(const std::string &objFilePath, const std::string &textureFilePath) +EffectSprite3D* EffectSprite3D::createFromObjFileAndTexture(const std::string& objFilePath, const std::string& textureFilePath) { auto sprite = new (std::nothrow) EffectSprite3D(); if (sprite && sprite->initWithFile(objFilePath)) @@ -887,11 +894,11 @@ EffectSprite3D* EffectSprite3D::createFromObjFileAndTexture(const std::string &o return nullptr; } -EffectSprite3D* EffectSprite3D::create(const std::string &path) +EffectSprite3D* EffectSprite3D::create(const std::string& path) { if (path.length() < 4) CCASSERT(false, "improper name specified when creating Sprite3D"); - + auto sprite = new (std::nothrow) EffectSprite3D(); if (sprite && sprite->initWithFile(path)) { @@ -903,9 +910,9 @@ EffectSprite3D* EffectSprite3D::create(const std::string &path) } EffectSprite3D::EffectSprite3D() -: _defaultEffect(nullptr) + : _defaultEffect(nullptr) { - + } EffectSprite3D::~EffectSprite3D() @@ -917,7 +924,7 @@ EffectSprite3D::~EffectSprite3D() CC_SAFE_RELEASE(_defaultEffect); } -void EffectSprite3D::setEffect3D(Effect3D *effect) +void EffectSprite3D::setEffect3D(Effect3D* effect) { if(_defaultEffect == effect) return; CC_SAFE_RETAIN(effect); @@ -930,7 +937,7 @@ void EffectSprite3D::addEffect(Effect3DOutline* effect, ssize_t order) if(nullptr == effect) return; effect->retain(); effect->setTarget(this); - + _effects.push_back(std::make_tuple(order,effect,CustomCommand())); std::sort(std::begin(_effects), std::end(_effects), tuple_sort); @@ -985,26 +992,26 @@ Effect3DOutline* Effect3DOutline::create() bool Effect3DOutline::init() { - + return true; } Effect3DOutline::Effect3DOutline() -: _outlineWidth(1.0f) -, _outlineColor(1, 1, 1) -, _sprite(nullptr) + : _outlineWidth(1.0f) + , _outlineColor(1, 1, 1) + , _sprite(nullptr) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) _backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, - [this](EventCustom*) - { - auto glProgram = _glProgramState->getGLProgram(); - glProgram->reset(); - glProgram->initWithFilenames(_vertShaderFile, _fragShaderFile); - glProgram->link(); - glProgram->updateUniforms(); - } - ); + [this](EventCustom*) + { + auto glProgram = _glProgramState->getGLProgram(); + glProgram->reset(); + glProgram->initWithFilenames(_vertShaderFile, _fragShaderFile); + glProgram->link(); + glProgram->updateUniforms(); + } + ); Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1); #endif } @@ -1036,10 +1043,10 @@ void Effect3DOutline::setOutlineWidth(float width) } } -void Effect3DOutline::setTarget(EffectSprite3D *sprite) +void Effect3DOutline::setTarget(EffectSprite3D* sprite) { CCASSERT(nullptr != sprite && nullptr != sprite->getMesh(),"Error: Setting a null pointer or a null mesh EffectSprite3D to Effect3D"); - + if(sprite != _sprite) { GLProgram* glprogram; @@ -1053,16 +1060,16 @@ void Effect3DOutline::setTarget(EffectSprite3D *sprite) _glProgramState->retain(); _glProgramState->setUniformVec3("OutLineColor", _outlineColor); _glProgramState->setUniformFloat("OutlineWidth", _outlineWidth); - - + + _sprite = sprite; - + auto mesh = sprite->getMesh(); long offset = 0; for (auto i = 0; i < mesh->getMeshVertexAttribCount(); i++) { auto meshvertexattrib = mesh->getMeshVertexAttribute(i); - + _glProgramState->setVertexAttribPointer(s_attributeNames[meshvertexattrib.vertexAttrib], meshvertexattrib.size, meshvertexattrib.type, @@ -1071,12 +1078,12 @@ void Effect3DOutline::setTarget(EffectSprite3D *sprite) (void*)offset); offset += meshvertexattrib.attribSizeBytes; } - + Color4F color(_sprite->getDisplayedColor()); color.a = _sprite->getDisplayedOpacity() / 255.0f; _glProgramState->setUniformVec4("u_color", Vec4(color.r, color.g, color.b, color.a)); } - + } static void MatrixPalleteCallBack( GLProgram* glProgram, Uniform* uniform, int paletteSize, const float* palette) @@ -1084,7 +1091,7 @@ static void MatrixPalleteCallBack( GLProgram* glProgram, Uniform* uniform, int p glUniform4fv( uniform->location, (GLsizei)paletteSize, (const float*)palette ); } -void Effect3DOutline::draw(const Mat4 &transform) +void Effect3DOutline::draw(const Mat4& transform) { //draw Color4F color(_sprite->getDisplayedColor()); @@ -1095,10 +1102,10 @@ void Effect3DOutline::draw(const Mat4 &transform) glEnable(GL_CULL_FACE); glCullFace(GL_FRONT); glEnable(GL_DEPTH_TEST); - + auto mesh = _sprite->getMesh(); glBindBuffer(GL_ARRAY_BUFFER, mesh->getVertexBuffer()); - + auto skin = _sprite->getMesh()->getSkin(); if(_sprite && skin) { @@ -1106,14 +1113,14 @@ void Effect3DOutline::draw(const Mat4 &transform) skin->getMatrixPaletteSize(), (float*)skin->getMatrixPalette()); _glProgramState->setUniformCallback("u_matrixPalette", function); } - + if(_sprite) _glProgramState->apply(transform); - + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->getIndexBuffer()); glDrawElements(mesh->getPrimitiveType(), (GLsizei)mesh->getIndexCount(), mesh->getIndexFormat(), 0); CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, mesh->getIndexCount()); - + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); glDisable(GL_DEPTH_TEST); @@ -1122,18 +1129,18 @@ void Effect3DOutline::draw(const Mat4 &transform) } } -void EffectSprite3D::draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, uint32_t flags) +void EffectSprite3D::draw(cocos2d::Renderer* renderer, const cocos2d::Mat4& transform, uint32_t flags) { - for(auto &effect : _effects) + for(auto& effect : _effects) { if(std::get<0>(effect) >=0) break; - CustomCommand &cc = std::get<2>(effect); + CustomCommand& cc = std::get<2>(effect); cc.func = CC_CALLBACK_0(Effect3D::draw,std::get<1>(effect),transform); renderer->addCommand(&cc); - + } - + if(!_defaultEffect) { Sprite3D::draw(renderer, transform, flags); @@ -1144,15 +1151,15 @@ void EffectSprite3D::draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &tran _command.func = CC_CALLBACK_0(Effect3D::draw, _defaultEffect, transform); renderer->addCommand(&_command); } - - for(auto &effect : _effects) + + for(auto& effect : _effects) { if(std::get<0>(effect) <=0) continue; - CustomCommand &cc = std::get<2>(effect); + CustomCommand& cc = std::get<2>(effect); cc.func = CC_CALLBACK_0(Effect3D::draw,std::get<1>(effect),transform); renderer->addCommand(&cc); - + } } @@ -1160,7 +1167,7 @@ Sprite3DEffectTest::Sprite3DEffectTest() { auto s = Director::getInstance()->getWinSize(); addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) ); - + auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DEffectTest::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); @@ -1183,22 +1190,22 @@ void Sprite3DEffectTest::addNewSpriteWithCoords(Vec2 p) sprite->addEffect(effect, -1); effect->setOutlineColor(Vec3(1,0,0)); effect->setOutlineWidth(0.01f); - + Effect3DOutline* effect2 = Effect3DOutline::create(); sprite->addEffect(effect2, -2); effect2->setOutlineWidth(0.02f); effect2->setOutlineColor(Vec3(1,1,0)); //sprite->setEffect3D(effect); sprite->setScale(6.f); - + //add to scene addChild( sprite ); - + sprite->setPosition( Vec2( p.x, p.y) ); - + ActionInterval* action; float random = CCRANDOM_0_1(); - + if( random < 0.20 ) action = ScaleBy::create(3, 2); else if(random < 0.40) @@ -1211,7 +1218,7 @@ void Sprite3DEffectTest::addNewSpriteWithCoords(Vec2 p) action = FadeOut::create(2); auto action_back = action->reverse(); auto seq = Sequence::create( action, action_back, nullptr ); - + sprite->runAction( RepeatForever::create(seq) ); } @@ -1220,7 +1227,7 @@ void Sprite3DEffectTest::onTouchesEnded(const std::vector& touches, Even for (auto touch: touches) { auto location = touch->getLocation(); - + addNewSpriteWithCoords( location ); } } @@ -1232,22 +1239,22 @@ AsyncLoadSprite3DTest::AsyncLoadSprite3DTest() _paths.push_back("Sprite3DTest/orc.c3b"); _paths.push_back("Sprite3DTest/ReskinGirl.c3b"); _paths.push_back("Sprite3DTest/axe.c3b"); - + TTFConfig ttfConfig("fonts/arial.ttf", 15); auto label1 = Label::createWithTTF(ttfConfig,"AsyncLoad Sprite3D"); auto item1 = MenuItemLabel::create(label1,CC_CALLBACK_1(AsyncLoadSprite3DTest::menuCallback_asyncLoadSprite,this) ); - + auto s = Director::getInstance()->getWinSize(); item1->setPosition( s.width * .5f, s.height * .8f); - + auto pMenu1 = CCMenu::create(item1, nullptr); pMenu1->setPosition(Vec2(0,0)); this->addChild(pMenu1, 10); - + auto node = Node::create(); node->setTag(101); this->addChild(node); - + menuCallback_asyncLoadSprite(nullptr); } @@ -1268,14 +1275,15 @@ void AsyncLoadSprite3DTest::menuCallback_asyncLoadSprite(Ref* sender) { //Note that you must stop the tasks before leaving the scene. AsyncTaskPool::getInstance()->stopTasks(AsyncTaskPool::TaskType::TASK_IO); - + auto node = getChildByTag(101); node->removeAllChildren(); //remove all loaded sprite - + //remove cache data Sprite3DCache::getInstance()->removeAllSprite3DData(); long index = 0; - for (const auto& path : _paths) { + for (const auto& path : _paths) + { Sprite3D::createAsync(path, CC_CALLBACK_2(AsyncLoadSprite3DTest::asyncLoad_Callback, this), (void*)index++); } } @@ -1297,7 +1305,7 @@ Sprite3DWithSkinTest::Sprite3DWithSkinTest() auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DWithSkinTest::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); - + auto s = Director::getInstance()->getWinSize(); addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) ); } @@ -1346,7 +1354,7 @@ void Sprite3DWithSkinTest::onTouchesEnded(const std::vector& touches, Ev for (auto touch: touches) { auto location = touch->getLocation(); - + addNewSpriteWithCoords( location ); } } @@ -1356,7 +1364,7 @@ Sprite3DWithSkinOutlineTest::Sprite3DWithSkinOutlineTest() auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DWithSkinOutlineTest::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); - + auto s = Director::getInstance()->getWinSize(); addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) ); } @@ -1371,33 +1379,33 @@ std::string Sprite3DWithSkinOutlineTest::subtitle() const void Sprite3DWithSkinOutlineTest::addNewSpriteWithCoords(Vec2 p) { - + std::string fileName = "Sprite3DTest/orc.c3b"; auto sprite = EffectSprite3D::create(fileName); - + Effect3DOutline* effect = Effect3DOutline::create(); effect->setOutlineColor(Vec3(1,0,0)); effect->setOutlineWidth(0.01f); sprite->addEffect(effect, -1); - + Effect3DOutline* effect2 = Effect3DOutline::create(); effect2->setOutlineWidth(0.02f); effect2->setOutlineColor(Vec3(1,1,0)); sprite->addEffect(effect2, -2); - + sprite->setScale(3); sprite->setRotation3D(Vec3(0,180,0)); addChild(sprite); sprite->setPosition( Vec2( p.x, p.y) ); - + auto animation = Animation3D::create(fileName); if (animation) { auto animate = Animate3D::create(animation); bool inverse = (std::rand() % 3 == 0); - + int rand2 = std::rand(); float speed = 1.0f; if(rand2 % 3 == 1) @@ -1409,7 +1417,7 @@ void Sprite3DWithSkinOutlineTest::addNewSpriteWithCoords(Vec2 p) speed = animate->getSpeed() - 0.5 * CCRANDOM_0_1(); } animate->setSpeed(inverse ? -speed : speed); - + sprite->runAction(RepeatForever::create(animate)); } } @@ -1419,24 +1427,24 @@ void Sprite3DWithSkinOutlineTest::onTouchesEnded(const std::vector& touc for (auto touch: touches) { auto location = touch->getLocation(); - + addNewSpriteWithCoords( location ); } } Animate3DTest::Animate3DTest() -: _hurt(nullptr) -, _swim(nullptr) -, _sprite(nullptr) -, _moveAction(nullptr) -, _elapseTransTime(0.f) + : _hurt(nullptr) + , _swim(nullptr) + , _sprite(nullptr) + , _moveAction(nullptr) + , _elapseTransTime(0.f) { addSprite3D(); - + auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(Animate3DTest::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); - + scheduleUpdate(); } @@ -1462,7 +1470,7 @@ void Animate3DTest::update(float dt) if (_state == State::HURT_TO_SWIMMING) { _elapseTransTime += dt; - + if (_elapseTransTime >= Animate3D::getTransitionTime()) { _sprite->stopAction(_hurt); @@ -1495,13 +1503,13 @@ void Animate3DTest::addSprite3D() auto animate = Animate3D::create(animation, 0.f, 1.933f); _swim = RepeatForever::create(animate); sprite->runAction(_swim); - + _swim->retain(); _hurt = Animate3D::create(animation, 1.933f, 2.8f); _hurt->retain(); _state = State::SWIMMING; } - + _moveAction = MoveTo::create(4.f, Vec2(s.width / 5.f, s.height / 2.f)); _moveAction->retain(); auto seq = Sequence::create(_moveAction, CallFunc::create(CC_CALLBACK_0(Animate3DTest::reachEndCallBack, this)), nullptr); @@ -1535,7 +1543,7 @@ void Animate3DTest::onTouchesEnded(const std::vector& touches, Event* ev for (auto touch: touches) { auto location = touch->getLocation(); - + if (_sprite) { float len = (_sprite->getPosition() - location).length(); @@ -1560,12 +1568,12 @@ void Animate3DTest::onTouchesEnded(const std::vector& touches, Event* ev } AttachmentTest::AttachmentTest() -: _hasWeapon(false) -, _sprite(nullptr) + : _hasWeapon(false) + , _sprite(nullptr) { auto s = Director::getInstance()->getWinSize(); addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) ); - + auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(AttachmentTest::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); @@ -1587,16 +1595,16 @@ void AttachmentTest::addNewSpriteWithCoords(Vec2 p) sprite->setRotation3D(Vec3(0,180,0)); addChild(sprite); sprite->setPosition( Vec2( p.x, p.y) ); - + //test attach auto sp = Sprite3D::create("Sprite3DTest/axe.c3b"); sprite->getAttachNode("Bip001 R Hand")->addChild(sp); - + auto animation = Animation3D::create(fileName); if (animation) { auto animate = Animate3D::create(animation); - + sprite->runAction(RepeatForever::create(animate)); } _sprite = sprite; @@ -1617,11 +1625,11 @@ void AttachmentTest::onTouchesEnded(const std::vector& touches, Event* e _hasWeapon = !_hasWeapon; } Sprite3DReskinTest::Sprite3DReskinTest() -: _sprite(nullptr) + : _sprite(nullptr) { auto s = Director::getInstance()->getWinSize(); addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) ); - + auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DReskinTest::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); @@ -1649,7 +1657,7 @@ Sprite3DReskinTest::Sprite3DReskinTest() auto pMenu1 = CCMenu::create(item1, item2, item3, item4, item5, nullptr); pMenu1->setPosition(Vec2(0,0)); this->addChild(pMenu1, 10); - + } void Sprite3DReskinTest::menuCallback_reSkin(Ref* sender) { @@ -1682,41 +1690,41 @@ void Sprite3DReskinTest::addNewSpriteWithCoords(Vec2 p) if (animation) { auto animate = Animate3D::create(animation); - + sprite->runAction(RepeatForever::create(animate)); } _sprite = sprite; - + auto& body = _skins[(int)SkinType::UPPER_BODY]; body.push_back("Girl_UpperBody01"); body.push_back("Girl_UpperBody02"); - + auto& pants = _skins[(int)SkinType::PANTS]; pants.push_back("Girl_LowerBody01"); pants.push_back("Girl_LowerBody02"); - + auto& shoes = _skins[(int)SkinType::SHOES]; shoes.push_back("Girl_Shoes01"); shoes.push_back("Girl_Shoes02"); - + auto& hair = _skins[(int)SkinType::HAIR]; hair.push_back("Girl_Hair01"); hair.push_back("Girl_Hair02"); - + auto& face = _skins[(int)SkinType::FACE]; face.push_back("Girl_Face01"); face.push_back("Girl_Face02"); - + auto& hand = _skins[(int)SkinType::HAND]; hand.push_back("Girl_Hand01"); hand.push_back("Girl_Hand02"); - + auto& glasses = _skins[(int)SkinType::GLASSES]; glasses.push_back(""); glasses.push_back("Girl_Glasses01"); - + memset(_curSkin, 0, (int)SkinType::MAX_TYPE * sizeof(int)); - + applyCurSkin(); } @@ -1726,10 +1734,12 @@ void Sprite3DReskinTest::onTouchesEnded(const std::vector& touches, Even void Sprite3DReskinTest::applyCurSkin() { - for (ssize_t i = 0; i < _sprite->getMeshCount(); i++) { + for (ssize_t i = 0; i < _sprite->getMeshCount(); i++) + { auto mesh = _sprite->getMeshByIndex(static_cast(i)); bool isVisible = false; - for (int j = 0; j < (int)SkinType::MAX_TYPE; j++) { + for (int j = 0; j < (int)SkinType::MAX_TYPE; j++) + { if (mesh->getName() == _skins[j].at(_curSkin[j])) { isVisible = true; @@ -1749,7 +1759,7 @@ Sprite3DWithOBBPerformanceTest::Sprite3DWithOBBPerformanceTest() _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); auto s = Director::getInstance()->getWinSize(); initDrawBox(); - + addNewSpriteWithCoords(Vec2(s.width/2, s.height/2)); MenuItemFont::setFontName("fonts/arial.ttf"); MenuItemFont::setFontSize(65); @@ -1757,12 +1767,12 @@ Sprite3DWithOBBPerformanceTest::Sprite3DWithOBBPerformanceTest() decrease->setColor(Color3B(0,200,20)); auto increase = MenuItemFont::create(" + ", CC_CALLBACK_1(Sprite3DWithOBBPerformanceTest::addOBBCallback, this)); increase->setColor(Color3B(0,200,20)); - + auto menu = Menu::create(decrease, increase, nullptr); menu->alignItemsHorizontally(); menu->setPosition(Vec2(s.width/2, s.height-65)); addChild(menu, 1); - + TTFConfig ttfCount("fonts/Marker Felt.ttf", 30); _labelCubeCount = Label::createWithTTF(ttfCount,"0 cubes"); _labelCubeCount->setColor(Color3B(0,200,20)); @@ -1814,14 +1824,14 @@ void Sprite3DWithOBBPerformanceTest::onTouchesBegan(const std::vector& t void Sprite3DWithOBBPerformanceTest::onTouchesEnded(const std::vector& touches, Event* event) { - + } void Sprite3DWithOBBPerformanceTest::onTouchesMoved(const std::vector& touches, Event* event) { for (const auto& touch: touches) { - auto location = touch->getLocation(); + auto location = touch->getLocation(); auto obbSize = _obb.size(); for(decltype(obbSize) i = 0; i < obbSize; i++) @@ -1837,23 +1847,23 @@ void Sprite3DWithOBBPerformanceTest::update(float dt) char szText[16]; sprintf(szText,"%lu cubes",_obb.size()); _labelCubeCount->setString(szText); - + if (_drawDebug) { _drawDebug->clear(); - + Mat4 mat = _sprite->getNodeToWorldTransform(); mat.getRightVector(&_obbt._xAxis); _obbt._xAxis.normalize(); - + mat.getUpVector(&_obbt._yAxis); _obbt._yAxis.normalize(); - + mat.getForwardVector(&_obbt._zAxis); _obbt._zAxis.normalize(); - + _obbt._center = _sprite->getPosition3D(); - + Vec3 corners[8] = {}; _obbt.getCorners(corners); _drawDebug->drawCube(corners, Color4F(0,0,1,1)); @@ -1892,16 +1902,16 @@ void Sprite3DWithOBBPerformanceTest::addNewSpriteWithCoords(Vec2 p) auto animate = Animate3D::create(animation, 0.f, 1.933f); sprite->runAction(RepeatForever::create(animate)); } - + _moveAction = MoveTo::create(4.f, Vec2(s.width / 5.f, s.height / 2.f)); _moveAction->retain(); auto seq = Sequence::create(_moveAction, CallFunc::create(CC_CALLBACK_0(Sprite3DWithOBBPerformanceTest::reachEndCallBack, this)), nullptr); seq->setTag(100); sprite->runAction(seq); - + AABB aabb = _sprite->getAABB(); _obbt = OBB(aabb); - + _drawDebug = DrawNode3D::create(); addChild(_drawDebug); } @@ -1955,23 +1965,23 @@ void Sprite3DWithOBBPerformanceTest::delOBBWithCount(float value) void Sprite3DWithOBBPerformanceTest::unproject(const Mat4& viewProjection, const Size* viewport, Vec3* src, Vec3* dst) { assert(dst); - + assert(viewport->width != 0.0f && viewport->height != 0.0f); Vec4 screen(src->x / viewport->width, ((viewport->height - src->y)) / viewport->height, src->z, 1.0f); - + screen.x = screen.x * 2.0f - 1.0f; screen.y = screen.y * 2.0f - 1.0f; screen.z = screen.z * 2.0f - 1.0f; - + viewProjection.getInversed().transformVector(screen, &screen); - + if (screen.w != 0.0f) { screen.x /= screen.w; screen.y /= screen.w; screen.z /= screen.w; } - + dst->set(screen.x, screen.y, screen.z); } @@ -1980,26 +1990,26 @@ void Sprite3DWithOBBPerformanceTest::calculateRayByLocationInView(Ray* ray, cons auto dir = Director::getInstance(); auto view = dir->getWinSize(); auto mat = dir->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION); - + Vec3 src = Vec3(location.x, location.y, -1); Vec3 nearPoint; unproject(mat, &view, &src, &nearPoint); - + src = Vec3(location.x, location.y, 1); Vec3 farPoint; unproject(mat, &view, &src, &farPoint); - + Vec3 direction; Vec3::subtract(farPoint, nearPoint, &direction); direction.normalize(); - + ray->_origin = nearPoint; ray->_direction = direction; } Sprite3DMirrorTest::Sprite3DMirrorTest() -: _sprite(nullptr) -, _mirrorSprite(nullptr) + : _sprite(nullptr) + , _mirrorSprite(nullptr) { auto s = Director::getInstance()->getWinSize(); addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) ); @@ -2021,21 +2031,21 @@ void Sprite3DMirrorTest::addNewSpriteWithCoords(Vec2 p) sprite->setRotation3D(Vec3(0,180,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); - + auto animation = Animation3D::create(fileName); if (animation) { auto animate = Animate3D::create(animation); - + sprite->runAction(RepeatForever::create(animate)); } _sprite = sprite; _hasWeapon = true; - + //create mirror Sprite3D sprite = Sprite3D::create(fileName); sprite->setScale(5); @@ -2044,25 +2054,25 @@ void Sprite3DMirrorTest::addNewSpriteWithCoords(Vec2 p) 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; } QuaternionTest::QuaternionTest() -: _arcSpeed(CC_DEGREES_TO_RADIANS(90)) -, _radius(100.f) -, _accAngle(0.f) + : _arcSpeed(CC_DEGREES_TO_RADIANS(90)) + , _radius(100.f) + , _accAngle(0.f) { auto s = Director::getInstance()->getWinSize(); addNewSpriteWithCoords(Vec2(s.width / 2.f, s.height / 2.f)); @@ -2100,58 +2110,60 @@ void QuaternionTest::update(float delta) const float pi = M_PI; if (_accAngle >= 2 * pi) _accAngle -= 2 * pi; - + auto s = Director::getInstance()->getWinSize(); _sprite->setPosition(Vec2(s.width / 2.f + _radius * cosf(_accAngle), s.height / 2.f + _radius * sinf(_accAngle))); - + Quaternion quat; Quaternion::createFromAxisAngle(Vec3(0.f, 0.f, 1.f), _accAngle - pi * 0.5f, &quat); _sprite->setRotationQuat(quat); } UseCaseSprite3D::UseCaseSprite3D() -: _caseIdx(0) + : _caseIdx(0) { auto s = Director::getInstance()->getWinSize(); - + _useCaseTitles[0] = "transparent 3d sprite and 2d sprite"; _useCaseTitles[1] = "ui - 3d - ui"; - + auto itemPrev = MenuItemImage::create("Images/b1.png", "Images/b2.png", - [&](Ref *sender) { - _caseIdx--; - if (_caseIdx < 0) - _caseIdx = (int)USECASE::MAX_CASE_NUM - 1; - this->switchCase(); - }); - + [&](Ref *sender) + { + _caseIdx--; + if (_caseIdx < 0) + _caseIdx = (int)USECASE::MAX_CASE_NUM - 1; + this->switchCase(); + }); + auto itemNext = MenuItemImage::create("Images/f1.png", "Images/f2.png", - [&](Ref *sender) { - _caseIdx++; - if (_caseIdx >= (int)USECASE::MAX_CASE_NUM) - _caseIdx = 0; - this->switchCase(); - }); - + [&](Ref *sender) + { + _caseIdx++; + if (_caseIdx >= (int)USECASE::MAX_CASE_NUM) + _caseIdx = 0; + this->switchCase(); + }); + auto menu = Menu::create(itemPrev, itemNext, nullptr); menu->alignItemsHorizontally(); menu->setScale(0.5); menu->setAnchorPoint(Vec2(0,0)); menu->setPosition(Vec2(s.width/2,70)); - + _label = Label::create(); _label->setPosition(s.width * 0.5f, s.height * 0.8f); addChild(_label); - + addChild(menu); - + //setup camera auto camera = Camera::createPerspective(40, s.width / s.height, 0.01f, 1000.f); camera->setCameraFlag(CameraFlag::USER1); camera->setPosition3D(Vec3(0.f, 30.f, 100.f)); camera->lookAt(Vec3(0.f, 0.f, 0.f)); addChild(camera); - + switchCase(); } @@ -2168,7 +2180,7 @@ std::string UseCaseSprite3D::subtitle() const void UseCaseSprite3D::switchCase() { removeChildByTag(101); - + auto s = Director::getInstance()->getWinSize(); _label->setString(_useCaseTitles[_caseIdx]); if (_caseIdx == 0) // use case 1, 3d transparent sprite + 2d sprite @@ -2180,33 +2192,33 @@ void UseCaseSprite3D::switchCase() if (animation) { auto animate = Animate3D::create(animation); - + sprite->runAction(RepeatForever::create(animate)); } - + auto circleBack = Sprite3D::create(); auto circle = Sprite::create("Sprite3DTest/circle.png"); circleBack->setScale(0.5f); circleBack->addChild(circle); circle->runAction(RepeatForever::create(RotateBy::create(3, Vec3(0.f, 0.f, 360.f)))); - + circleBack->setRotation3D(Vec3(90, 0, 0)); - + auto pos = sprite->getPosition3D(); circleBack->setPosition3D(Vec3(pos.x, pos.y, pos.z - 1)); - + sprite->setOpacity(250); sprite->setCameraMask(2); circleBack->setCameraMask(2); sprite->setTag(3); circleBack->setTag(2); - + auto node = Node::create(); node->addChild(sprite); node->addChild(circleBack); node->setTag(101); addChild(node); - + scheduleUpdate(); update(0.f); } @@ -2216,7 +2228,7 @@ void UseCaseSprite3D::switchCase() layer->setPosition(s.width * 0.25f, s.height * 0.25f); layer->setGlobalZOrder(-1); addChild(layer); - + std::string filename = "Sprite3DTest/girl.c3b"; auto sprite = Sprite3D::create(filename); sprite->setScale(0.5f); @@ -2228,20 +2240,20 @@ void UseCaseSprite3D::switchCase() } sprite->setPosition(s.width * 0.25f, s.height * 0.125f); layer->addChild(sprite); - + TTFConfig ttfConfig("fonts/arial.ttf", 15); auto label1 = Label::createWithTTF(ttfConfig,"Message"); auto item1 = MenuItemLabel::create(label1,CC_CALLBACK_1(UseCaseSprite3D::menuCallback_Message,this) ); auto label2 = Label::createWithTTF(ttfConfig,"Message"); auto item2 = MenuItemLabel::create(label2, CC_CALLBACK_1(UseCaseSprite3D::menuCallback_Message,this) ); - + item1->setPosition( Vec2(s.width * 0.5f - item1->getContentSize().width * 0.5f, s.height * 0.5f - item1->getContentSize().height ) ); item2->setPosition( Vec2(s.width * 0.5f - item1->getContentSize().width * 0.5f, s.height * 0.5f - item1->getContentSize().height * 2.f ) ); - + auto pMenu1 = CCMenu::create(item1, item2, nullptr); pMenu1->setPosition(Vec2(0,0)); layer->addChild(pMenu1); - + layer->setTag(101); } } @@ -2274,14 +2286,14 @@ void UseCaseSprite3D::update(float delta) { static float accAngle = 0.f; accAngle += delta * CC_DEGREES_TO_RADIANS(60); - + float radius = 30.f; float x = cosf(accAngle) * radius, z = sinf(accAngle) * radius; - + auto node = getChildByTag(101); auto sprite3d = node->getChildByTag(3); auto circle = node->getChildByTag(2); - + sprite3d->setPositionX(x); sprite3d->setPositionZ(z); circle->setPositionX(x); diff --git a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h index 5b852bc4e1..5c49119afc 100644 --- a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h +++ b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h @@ -1,26 +1,26 @@ /**************************************************************************** -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. -****************************************************************************/ + Copyright (c) 2013 cocos2d-x.org + + 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 _SPRITE3D_TEST_H_ #define _SPRITE3D_TEST_H_ @@ -43,11 +43,11 @@ class Sprite3DTestDemo : public BaseTest public: Sprite3DTestDemo(void); virtual ~Sprite3DTestDemo(void); - + void restartCallback(Ref* sender); void nextCallback(Ref* sender); void backCallback(Ref* sender); - + // overrides virtual std::string title() const override; virtual std::string subtitle() const override; @@ -70,7 +70,7 @@ public: Sprite3DBasicTest(); virtual std::string title() const override; virtual std::string subtitle() const override; - + void addNewSpriteWithCoords(Vec2 p); void onTouchesEnded(const std::vector& touches, Event* event); }; @@ -83,14 +83,14 @@ public: virtual ~Sprite3DUVAnimationTest(); virtual std::string title() const override; virtual std::string subtitle() const override; - + protected: void cylinderUpdate(float dt); - + float _cylinder_texture_offset; float _shining_duraion; - GLProgramState* _state; - + GLProgramState * _state; + #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) EventListenerCustom* _backToForegroundListener; #endif @@ -116,17 +116,17 @@ public: void move3D(float elapsedTime); void updateState(float elapsedTime); bool isState(unsigned int state,unsigned int bit) const; - void onTouchesBegan(const std::vector& touches, cocos2d::Event* event); - void onTouchesMoved(const std::vector& touches, cocos2d::Event* event); - void onTouchesEnded(const std::vector& touches, cocos2d::Event* event); + void onTouchesBegan(const std::vector& touches, cocos2d::Event *event); + void onTouchesMoved(const std::vector& touches, cocos2d::Event *event); + void onTouchesEnded(const std::vector& touches, cocos2d::Event *event); private: - cocos2d::Camera* _camera; + cocos2d::Camera * _camera; Vec3 _targetPos; unsigned int _curState; - cocos2d::Sprite3D* _plane; - cocos2d::Sprite3D* _orc; - GLProgramState* _state; - + cocos2d::Sprite3D * _plane; + cocos2d::Sprite3D * _orc; + GLProgramState * _state; + #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) EventListenerCustom* _backToForegroundListener; #endif @@ -142,7 +142,7 @@ public: virtual std::string subtitle() const override; void onTouchesMoved(const std::vector& touches, cocos2d::Event* event); private: - Camera* _camera; + Camera * _camera; }; class Sprite3DBasicToonShaderTest : public Sprite3DTestDemo @@ -153,10 +153,10 @@ public: virtual ~Sprite3DBasicToonShaderTest(); virtual std::string title() const override; virtual std::string subtitle() const override; - + protected: - GLProgramState* _state; - + GLProgramState * _state; + #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) EventListenerCustom* _backToForegroundListener; #endif @@ -167,8 +167,8 @@ class EffectSprite3D; class Effect3D : public Ref { public: - virtual void draw(const Mat4& transform) = 0; - virtual void setTarget(EffectSprite3D* sprite) = 0; + virtual void draw(const Mat4 &transform) = 0; + virtual void setTarget(EffectSprite3D *sprite) = 0; protected: Effect3D() : _glProgramState(nullptr) {} virtual ~Effect3D() @@ -183,20 +183,20 @@ class Effect3DOutline: public Effect3D { public: static Effect3DOutline* create(); - + void setOutlineColor(const Vec3& color); - + void setOutlineWidth(float width); - - virtual void draw(const Mat4& transform) override; - virtual void setTarget(EffectSprite3D* sprite) override; + + virtual void draw(const Mat4 &transform) override; + virtual void setTarget(EffectSprite3D *sprite) override; protected: - + Effect3DOutline(); virtual ~Effect3DOutline(); - + bool init(); - + Vec3 _outlineColor; float _outlineWidth; //weak reference @@ -204,16 +204,16 @@ protected: #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) EventListenerCustom* _backToForegroundListener; #endif - + protected: static const std::string _vertShaderFile; static const std::string _fragShaderFile; static const std::string _keyInGLProgramCache; - + static const std::string _vertSkinnedShaderFile; static const std::string _fragSkinnedShaderFile; static const std::string _keySkinnedInGLProgramCache; - + static GLProgram* getOrCreateProgram(bool isSkinned = false); }; @@ -231,14 +231,14 @@ class EffectSprite3D : public Sprite3D public: static EffectSprite3D* createFromObjFileAndTexture(const std::string& objFilePath, const std::string& textureFilePath); static EffectSprite3D* create(const std::string& path); - + void setEffect3D(Effect3D* effect); void addEffect(Effect3DOutline* effect, ssize_t order); - virtual void draw(Renderer* renderer, const Mat4& transform, uint32_t flags) override; + virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override; protected: EffectSprite3D(); virtual ~EffectSprite3D(); - + std::vector> _effects; Effect3D* _defaultEffect; CustomCommand _command; @@ -251,9 +251,9 @@ public: Sprite3DEffectTest(); virtual std::string title() const override; virtual std::string subtitle() const override; - + void addNewSpriteWithCoords(Vec2 p); - + void onTouchesEnded(const std::vector& touches, Event* event); }; @@ -265,11 +265,11 @@ public: virtual ~AsyncLoadSprite3DTest(); virtual std::string title() const override; virtual std::string subtitle() const override; - + void menuCallback_asyncLoadSprite(Ref* sender); - + void asyncLoad_Callback(Sprite3D* sprite, void* param); - + protected: std::vector _paths; //model paths to be loaded }; @@ -281,9 +281,9 @@ public: Sprite3DWithSkinTest(); virtual std::string title() const override; virtual std::string subtitle() const override; - + void addNewSpriteWithCoords(Vec2 p); - + void onTouchesEnded(const std::vector& touches, Event* event); }; @@ -294,9 +294,9 @@ public: Sprite3DWithSkinOutlineTest(); virtual std::string title() const override; virtual std::string subtitle() const override; - + void addNewSpriteWithCoords(Vec2 p); - + void onTouchesEnded(const std::vector& touches, Event* event); }; @@ -308,14 +308,14 @@ public: ~Animate3DTest(); virtual std::string title() const override; virtual std::string subtitle() const override; - + void onTouchesEnded(const std::vector& touches, Event* event); - + virtual void update(float dt) override; - + protected: void addSprite3D(); - + enum class State { SWIMMING, @@ -323,20 +323,20 @@ protected: HURT, HURT_TO_SWIMMING, }; - + void reachEndCallBack(); - + void renewCallBack(); - + cocos2d::Sprite3D* _sprite; - + cocos2d::Action* _swim; cocos2d::Animate3D* _hurt; float _elapseTransTime; - + State _state; - + MoveTo* _moveAction; }; @@ -347,11 +347,11 @@ public: AttachmentTest(); virtual std::string title() const override; virtual std::string subtitle() const override; - + void onTouchesEnded(const std::vector& touches, Event* event); - + void addNewSpriteWithCoords(Vec2 p); - + protected: bool _hasWeapon; cocos2d::Sprite3D* _sprite; @@ -366,11 +366,11 @@ public: virtual std::string subtitle() const override; void onTouchesEnded(const std::vector& touches, Event* event); void addNewSpriteWithCoords(Vec2 p); - + void menuCallback_reSkin(Ref* sender); protected: void applyCurSkin(); - + enum class SkinType { UPPER_BODY = 0, @@ -382,7 +382,7 @@ protected: GLASSES, MAX_TYPE, }; - + std::vector _skins[(int)SkinType::MAX_TYPE]; //all skins int _curSkin[(int)SkinType::MAX_TYPE]; //current skin index cocos2d::Sprite3D* _sprite; @@ -416,7 +416,7 @@ protected: std::set _intersetList; void initDrawBox(); void reachEndCallBack(); - + void unproject(const Mat4& viewProjection, const Size* viewport, Vec3* src, Vec3* dst); void calculateRayByLocationInView(Ray* ray, const Vec2& location); }; @@ -428,9 +428,9 @@ public: Sprite3DMirrorTest(); virtual std::string title() const override; virtual std::string subtitle() const override; - + void addNewSpriteWithCoords(Vec2 p); - + protected: bool _hasWeapon; cocos2d::Sprite3D* _sprite; @@ -444,10 +444,10 @@ public: QuaternionTest(); virtual std::string title() const override; virtual std::string subtitle() const override; - + void addNewSpriteWithCoords(Vec2 p); virtual void update(float delta) override; - + protected: cocos2d::Sprite3D* _sprite; float _arcSpeed; @@ -463,15 +463,15 @@ public: UseCaseSprite3D(); virtual std::string title() const override; virtual std::string subtitle() const override; - + virtual void update(float delta) override; - + void menuCallback_Message(Ref* sender); - + protected: - + void switchCase(); - + enum class USECASE{ _3D_WITH_2D, _UI_3D_UI, From 572ede87f2f21d573ea7088b775b796c208aabfa Mon Sep 17 00:00:00 2001 From: zhukaixy Date: Thu, 26 Mar 2015 10:05:43 +0800 Subject: [PATCH 10/10] correct for code indent --- .../Classes/Sprite3DTest/Sprite3DTest.cpp | 673 +++++++++--------- 1 file changed, 331 insertions(+), 342 deletions(-) diff --git a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp index 97037e3dd1..05e47b9ec8 100644 --- a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp +++ b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp @@ -1,26 +1,26 @@ /**************************************************************************** - Copyright (c) 2012 cocos2d-x.org - Copyright (c) 2013-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. + Copyright (c) 2012 cocos2d-x.org + Copyright (c) 2013-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 "Sprite3DTest.h" @@ -54,7 +54,7 @@ static std::function createFunctions[] = CL(Sprite3DHitTest), CL(AsyncLoadSprite3DTest), #if (CC_TARGET_PLATFORM != CC_PLATFORM_WP8) - // 3DEffect use custom shader which is not supported on WP8/WinRT yet. + // 3DEffect use custom shader which is not supported on WP8/WinRT yet. CL(Sprite3DEffectTest), CL(Sprite3DUVAnimationTest), CL(Sprite3DFakeShadowTest), @@ -82,7 +82,7 @@ static Layer* nextSpriteTestAction() { sceneIdx++; sceneIdx = sceneIdx % MAX_LAYER; - + auto layer = (createFunctions[sceneIdx])(); return layer; } @@ -93,7 +93,7 @@ static Layer* backSpriteTestAction() int total = MAX_LAYER; if( sceneIdx < 0 ) sceneIdx += total; - + auto layer = (createFunctions[sceneIdx])(); return layer; } @@ -111,7 +111,7 @@ static Layer* restartSpriteTestAction() //------------------------------------------------------------------ Sprite3DTestDemo::Sprite3DTestDemo(void) - : BaseTest() +: BaseTest() { } @@ -138,7 +138,7 @@ void Sprite3DTestDemo::restartCallback(Ref* sender) { auto s = new (std::nothrow) Sprite3DTestScene(); s->addChild(restartSpriteTestAction()); - + Director::getInstance()->replaceScene(s); s->release(); } @@ -195,7 +195,7 @@ Sprite3DBasicTest::Sprite3DBasicTest() auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DBasicTest::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); - + auto s = Director::getInstance()->getWinSize(); addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) ); } @@ -205,26 +205,26 @@ void Sprite3DBasicTest::addNewSpriteWithCoords(Vec2 p) //int idx = (int)(CCRANDOM_0_1() * 1400.0f / 100.0f); //int x = (idx%5) * 85; //int y = (idx/5) * 121; - - // //option 1: load a obj that contain the texture in it - // auto sprite = Sprite3D::create("sprite3dTest/scene01.obj"); - + +// //option 1: load a obj that contain the texture in it +// auto sprite = Sprite3D::create("sprite3dTest/scene01.obj"); + //option 2: load obj and assign the texture auto sprite = Sprite3D::create("Sprite3DTest/boss1.obj"); sprite->setScale(3.f); sprite->setTexture("Sprite3DTest/boss.png"); - + // //sprite->setEffect(cocos2d::EFFECT_OUTLINE); - + //add to scene addChild( sprite ); - + sprite->setPosition( Vec2( p.x, p.y) ); - + ActionInterval* action; float random = CCRANDOM_0_1(); - + if( random < 0.20 ) action = ScaleBy::create(3, 2); else if(random < 0.40) @@ -237,7 +237,7 @@ void Sprite3DBasicTest::addNewSpriteWithCoords(Vec2 p) action = FadeOut::create(2); auto action_back = action->reverse(); auto seq = Sequence::create( action, action_back, nullptr ); - + sprite->runAction( RepeatForever::create(seq) ); } @@ -246,7 +246,7 @@ void Sprite3DBasicTest::onTouchesEnded(const std::vector& touches, Event for (auto touch: touches) { auto location = touch->getLocation(); - + addNewSpriteWithCoords( location ); } } @@ -291,15 +291,14 @@ Sprite3DUVAnimationTest::Sprite3DUVAnimationTest() //pass mesh's attribute to shader long offset = 0; auto attributeCount = cylinder->getMesh()->getMeshVertexAttribCount(); - for (auto i = 0; i < attributeCount; i++) - { + for (auto i = 0; i < attributeCount; i++) { auto meshattribute = cylinder->getMesh()->getMeshVertexAttribute(i); _state->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib], - meshattribute.size, - meshattribute.type, - GL_FALSE, - cylinder->getMesh()->getVertexSizeInBytes(), - (GLvoid*)offset); + meshattribute.size, + meshattribute.type, + GL_FALSE, + cylinder->getMesh()->getVertexSizeInBytes(), + (GLvoid*)offset); offset += meshattribute.attribSizeBytes; } @@ -310,13 +309,13 @@ Sprite3DUVAnimationTest::Sprite3DUVAnimationTest() tRepeatParams.minFilter = GL_NEAREST; tRepeatParams.wrapS = GL_REPEAT; tRepeatParams.wrapT = GL_REPEAT; - shining_texture->setTexParameters(tRepeatParams); + shining_texture->setTexParameters(tRepeatParams); //pass the texture sampler to our custom shader _state->setUniformTexture("caustics",shining_texture); this->addChild(cylinder); - this->setCameraMask(2); + this->setCameraMask(2); this->addChild(camera); //adjust cylinder's position & rotation @@ -325,18 +324,18 @@ Sprite3DUVAnimationTest::Sprite3DUVAnimationTest() //the callback function update cylinder's texcoord schedule(schedule_selector(Sprite3DUVAnimationTest::cylinderUpdate)); - + #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) _backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, - [this](EventCustom*) - { - auto glProgram = _state->getGLProgram(); - glProgram->reset(); - glProgram->initWithFilenames("Sprite3DTest/cylinder.vert", "Sprite3DTest/cylinder.frag"); - glProgram->link(); - glProgram->updateUniforms(); - } - ); + [this](EventCustom*) + { + auto glProgram = _state->getGLProgram(); + glProgram->reset(); + glProgram->initWithFilenames("Sprite3DTest/cylinder.vert", "Sprite3DTest/cylinder.frag"); + glProgram->link(); + glProgram->updateUniforms(); + } + ); Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1); #endif } @@ -348,12 +347,12 @@ Sprite3DUVAnimationTest::~Sprite3DUVAnimationTest() #endif } -std::string Sprite3DUVAnimationTest::title() const +std::string Sprite3DUVAnimationTest::title() const { return "Testing UV Animation"; } -std::string Sprite3DUVAnimationTest::subtitle() const +std::string Sprite3DUVAnimationTest::subtitle() const { return ""; } @@ -388,13 +387,13 @@ void Sprite3DUVAnimationTest::cylinderUpdate(float dt) Sprite3DFakeShadowTest::Sprite3DFakeShadowTest() { Size visibleSize = Director::getInstance()->getVisibleSize(); - + auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesBegan = CC_CALLBACK_2(Sprite3DFakeShadowTest::onTouchesBegan, this); listener->onTouchesMoved = CC_CALLBACK_2(Sprite3DFakeShadowTest::onTouchesMoved, this); listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DFakeShadowTest::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); - + auto layer = Layer::create(); addChild(layer,0); //create Camera @@ -413,19 +412,18 @@ Sprite3DFakeShadowTest::Sprite3DFakeShadowTest() _plane->setGLProgramState(_state); //pass mesh's attribute to shader - long offset = 0; + long offset = 0; auto attributeCount = _plane->getMesh()->getMeshVertexAttribCount(); - for (auto i = 0; i < attributeCount; i++) - { + for (auto i = 0; i < attributeCount; i++) { auto meshattribute = _plane->getMesh()->getMeshVertexAttribute(i); _state->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib], - meshattribute.size, - meshattribute.type, - GL_FALSE, - _plane->getMesh()->getVertexSizeInBytes(), - (GLvoid*)offset); + meshattribute.size, + meshattribute.type, + GL_FALSE, + _plane->getMesh()->getVertexSizeInBytes(), + (GLvoid*)offset); offset += meshattribute.attribSizeBytes; - } + } _state->setUniformMat4("u_model_matrix",_plane->getNodeToWorldTransform()); //create shadow texture @@ -435,9 +433,9 @@ Sprite3DFakeShadowTest::Sprite3DFakeShadowTest() tRepeatParams.minFilter = GL_LINEAR; tRepeatParams.wrapS = GL_CLAMP_TO_EDGE; tRepeatParams.wrapT = GL_CLAMP_TO_EDGE; - shadowTexture->setTexParameters(tRepeatParams); + shadowTexture->setTexParameters(tRepeatParams); _state->setUniformTexture("u_shadowTexture",shadowTexture); - layer->addChild(_plane); + layer->addChild(_plane); //create the orc _orc = Sprite3D::create("Sprite3DTest/orc.c3b"); @@ -451,30 +449,30 @@ Sprite3DFakeShadowTest::Sprite3DFakeShadowTest() layer->setCameraMask(2); schedule(CC_SCHEDULE_SELECTOR(Sprite3DFakeShadowTest::updateCamera), 0.0f); - + #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) _backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, - [this](EventCustom*) - { - auto glProgram = _state->getGLProgram(); - glProgram->reset(); - glProgram->initWithFilenames("Sprite3DTest/simple_shadow.vert","Sprite3DTest/simple_shadow.frag"); - glProgram->link(); - glProgram->updateUniforms(); - - _state->setUniformMat4("u_model_matrix",_plane->getNodeToWorldTransform()); - - //create shadow texture - auto shadowTexture = Director::getInstance()->getTextureCache()->addImage("Sprite3DTest/shadowCircle.png"); - Texture2D::TexParams tRepeatParams;//set texture parameters - tRepeatParams.magFilter = GL_LINEAR; - tRepeatParams.minFilter = GL_LINEAR; - tRepeatParams.wrapS = GL_CLAMP_TO_EDGE; - tRepeatParams.wrapT = GL_CLAMP_TO_EDGE; - shadowTexture->setTexParameters(tRepeatParams); - _state->setUniformTexture("u_shadowTexture",shadowTexture); - } - ); + [this](EventCustom*) + { + auto glProgram = _state->getGLProgram(); + glProgram->reset(); + glProgram->initWithFilenames("Sprite3DTest/simple_shadow.vert","Sprite3DTest/simple_shadow.frag"); + glProgram->link(); + glProgram->updateUniforms(); + + _state->setUniformMat4("u_model_matrix",_plane->getNodeToWorldTransform()); + + //create shadow texture + auto shadowTexture = Director::getInstance()->getTextureCache()->addImage("Sprite3DTest/shadowCircle.png"); + Texture2D::TexParams tRepeatParams;//set texture parameters + tRepeatParams.magFilter = GL_LINEAR; + tRepeatParams.minFilter = GL_LINEAR; + tRepeatParams.wrapS = GL_CLAMP_TO_EDGE; + tRepeatParams.wrapT = GL_CLAMP_TO_EDGE; + shadowTexture->setTexParameters(tRepeatParams); + _state->setUniformTexture("u_shadowTexture",shadowTexture); + } + ); Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1); #endif } @@ -486,12 +484,12 @@ Sprite3DFakeShadowTest::~Sprite3DFakeShadowTest() #endif } -std::string Sprite3DFakeShadowTest::title() const +std::string Sprite3DFakeShadowTest::title() const { return "fake shadow effect"; } -std::string Sprite3DFakeShadowTest::subtitle() const +std::string Sprite3DFakeShadowTest::subtitle() const { return "touch the screen to move around"; } @@ -596,18 +594,18 @@ bool Sprite3DFakeShadowTest::isState(unsigned int state,unsigned int bit) const return (state & bit) == bit; } -void Sprite3DFakeShadowTest::onTouchesBegan(const std::vector& touches, cocos2d::Event* event) +void Sprite3DFakeShadowTest::onTouchesBegan(const std::vector& touches, cocos2d::Event *event) { } -void Sprite3DFakeShadowTest::onTouchesMoved(const std::vector& touches, cocos2d::Event* event) +void Sprite3DFakeShadowTest::onTouchesMoved(const std::vector& touches, cocos2d::Event *event) { } -void Sprite3DFakeShadowTest::onTouchesEnded(const std::vector& touches, cocos2d::Event* event) +void Sprite3DFakeShadowTest::onTouchesEnded(const std::vector& touches, cocos2d::Event *event) { - for ( auto& item: touches ) + for ( auto &item: touches ) { auto touch = item; auto location = touch->getLocationInView(); @@ -655,44 +653,43 @@ Sprite3DBasicToonShaderTest::Sprite3DBasicToonShaderTest() auto _camera = Camera::createPerspective(60, visibleSize.width/visibleSize.height, 0.1f, 200); _camera->setCameraFlag(CameraFlag::USER1); // create a teapot - auto teapot = Sprite3D::create("Sprite3DTest/teapot.c3b"); - //create and set our custom shader + auto teapot = Sprite3D::create("Sprite3DTest/teapot.c3b"); + //create and set our custom shader auto shader =GLProgram::createWithFilenames("Sprite3DTest/toon.vert","Sprite3DTest/toon.frag"); _state = GLProgramState::create(shader); teapot->setGLProgramState(_state); teapot->setPosition3D(Vec3(0,-5,-20)); - teapot->setRotation3D(Vec3(-90,180,0)); + teapot->setRotation3D(Vec3(-90,180,0)); auto rotate_action = RotateBy::create(1.5,Vec3(0,30,0)); - teapot->runAction(RepeatForever::create(rotate_action)); + teapot->runAction(RepeatForever::create(rotate_action)); //pass mesh's attribute to shader long offset = 0; auto attributeCount = teapot->getMesh()->getMeshVertexAttribCount(); - for (auto i = 0; i < attributeCount; i++) - { + for (auto i = 0; i < attributeCount; i++) { auto meshattribute = teapot->getMesh()->getMeshVertexAttribute(i); _state->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib], - meshattribute.size, - meshattribute.type, - GL_FALSE, - teapot->getMesh()->getVertexSizeInBytes(), - (GLvoid*)offset); + meshattribute.size, + meshattribute.type, + GL_FALSE, + teapot->getMesh()->getVertexSizeInBytes(), + (GLvoid*)offset); offset += meshattribute.attribSizeBytes; } addChild(teapot); addChild(_camera); setCameraMask(2); - + #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) _backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, - [this](EventCustom*) - { - auto glProgram = _state->getGLProgram(); - glProgram->reset(); - glProgram->initWithFilenames("Sprite3DTest/toon.vert","Sprite3DTest/toon.frag"); - glProgram->link(); - glProgram->updateUniforms(); - } - ); + [this](EventCustom*) + { + auto glProgram = _state->getGLProgram(); + glProgram->reset(); + glProgram->initWithFilenames("Sprite3DTest/toon.vert","Sprite3DTest/toon.frag"); + glProgram->link(); + glProgram->updateUniforms(); + } + ); Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1); #endif } @@ -704,19 +701,19 @@ Sprite3DBasicToonShaderTest::~Sprite3DBasicToonShaderTest() #endif } -std::string Sprite3DBasicToonShaderTest::title() const +std::string Sprite3DBasicToonShaderTest::title() const { return "basic toon shader test"; } -std::string Sprite3DBasicToonShaderTest::subtitle() const +std::string Sprite3DBasicToonShaderTest::subtitle() const { return " "; } //------------------------------------------------------------------ // -// Sprite3DLightMapTest +// Sprite3DLightMapTest // //------------------------------------------------------------------ Sprite3DLightMapTest::Sprite3DLightMapTest() @@ -728,16 +725,16 @@ Sprite3DLightMapTest::Sprite3DLightMapTest() _camera->setCameraFlag(CameraFlag::USER1); _camera->setPosition3D(Vec3(0,25,15)); _camera->setRotation3D(Vec3(-35,0,0)); - auto LightMapScene = Sprite3D::create("Sprite3DTest/LightMapScene.c3b"); - LightMapScene->setScale(0.1f); + auto LightMapScene = Sprite3D::create("Sprite3DTest/LightMapScene.c3b"); + LightMapScene->setScale(0.1f); addChild(LightMapScene); - addChild(_camera); - setCameraMask(2); + addChild(_camera); + setCameraMask(2); //add a point light auto light = PointLight::create(Vec3(35,75,-20.5),Color3B(255,255,255),150); addChild(light); - //set the ambient light + //set the ambient light auto ambient = AmbientLight::create(Color3B(55,55,55)); addChild(ambient); @@ -748,14 +745,14 @@ Sprite3DLightMapTest::Sprite3DLightMapTest() } Sprite3DLightMapTest::~Sprite3DLightMapTest() { - + } std::string Sprite3DLightMapTest::title() const { return "light map test"; } -std::string Sprite3DLightMapTest::subtitle() const +std::string Sprite3DLightMapTest::subtitle() const { return "drag the screen to move around"; } @@ -779,9 +776,9 @@ void Sprite3DLightMapTest::onTouchesMoved(const std::vector& to cameraRightDir.normalize(); cameraRightDir.y=0; Vec3 cameraPos= _camera->getPosition3D(); - cameraPos+=cameraDir*newPos.y*delta; + cameraPos+=cameraDir*newPos.y*delta; cameraPos+=cameraRightDir*newPos.x*delta; - _camera->setPosition3D(cameraPos); + _camera->setPosition3D(cameraPos); } } @@ -793,38 +790,37 @@ void Sprite3DLightMapTest::onTouchesMoved(const std::vector& to Sprite3DHitTest::Sprite3DHitTest() { auto s = Director::getInstance()->getWinSize(); - + auto sprite1 = Sprite3D::create("Sprite3DTest/boss1.obj"); - + sprite1->setScale(4.f); sprite1->setTexture("Sprite3DTest/boss.png"); sprite1->setPosition( Vec2(s.width/2, s.height/2) ); - + //add to scene addChild( sprite1 ); sprite1->runAction(RepeatForever::create(RotateBy::create(3, 360))); - + auto sprite2 = Sprite3D::create("Sprite3DTest/boss1.obj"); - + sprite2->setScale(4.f); sprite2->setTexture("Sprite3DTest/boss.png"); sprite2->setPosition( Vec2(s.width/2, s.height/2) ); sprite2->setAnchorPoint(Vec2(0.5, 0.5)); - + //add to scene addChild( sprite2 ); sprite2->runAction(RepeatForever::create(RotateBy::create(3, -360))); - - + + // Make sprite1 touchable auto listener1 = EventListenerTouchOneByOne::create(); listener1->setSwallowTouches(true); - - listener1->onTouchBegan = [](Touch* touch, Event* event) - { + + listener1->onTouchBegan = [](Touch* touch, Event* event){ auto target = static_cast(event->getCurrentTarget()); - - Rect rect = target->getBoundingBox(); + + Rect rect = target->getBoundingBox(); if (rect.containsPoint(touch->getLocation())) { log("sprite3d began... x = %f, y = %f", touch->getLocation().x, touch->getLocation().y); @@ -833,23 +829,21 @@ Sprite3DHitTest::Sprite3DHitTest() } return false; }; - - listener1->onTouchMoved = [](Touch* touch, Event* event) - { + + listener1->onTouchMoved = [](Touch* touch, Event* event){ auto target = static_cast(event->getCurrentTarget()); target->setPosition(target->getPosition() + touch->getDelta()); }; - - listener1->onTouchEnded = [=](Touch* touch, Event* event) - { + + listener1->onTouchEnded = [=](Touch* touch, Event* event){ auto target = static_cast(event->getCurrentTarget()); log("sprite3d onTouchesEnded.. "); target->setOpacity(255); }; - + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, sprite1); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), sprite2); - + } std::string Sprite3DHitTest::title() const @@ -866,21 +860,21 @@ void Sprite3DTestScene::runThisTest() { auto layer = nextSpriteTestAction(); addChild(layer); - + Director::getInstance()->replaceScene(this); } Sprite3DTestScene::Sprite3DTestScene() { - + } -static int tuple_sort( const std::tuple& tuple1, const std::tuple& tuple2 ) +static int tuple_sort( const std::tuple &tuple1, const std::tuple &tuple2 ) { return std::get<0>(tuple1) < std::get<0>(tuple2); } -EffectSprite3D* EffectSprite3D::createFromObjFileAndTexture(const std::string& objFilePath, const std::string& textureFilePath) +EffectSprite3D* EffectSprite3D::createFromObjFileAndTexture(const std::string &objFilePath, const std::string &textureFilePath) { auto sprite = new (std::nothrow) EffectSprite3D(); if (sprite && sprite->initWithFile(objFilePath)) @@ -894,11 +888,11 @@ EffectSprite3D* EffectSprite3D::createFromObjFileAndTexture(const std::string& o return nullptr; } -EffectSprite3D* EffectSprite3D::create(const std::string& path) +EffectSprite3D* EffectSprite3D::create(const std::string &path) { if (path.length() < 4) CCASSERT(false, "improper name specified when creating Sprite3D"); - + auto sprite = new (std::nothrow) EffectSprite3D(); if (sprite && sprite->initWithFile(path)) { @@ -910,9 +904,9 @@ EffectSprite3D* EffectSprite3D::create(const std::string& path) } EffectSprite3D::EffectSprite3D() - : _defaultEffect(nullptr) +: _defaultEffect(nullptr) { - + } EffectSprite3D::~EffectSprite3D() @@ -924,7 +918,7 @@ EffectSprite3D::~EffectSprite3D() CC_SAFE_RELEASE(_defaultEffect); } -void EffectSprite3D::setEffect3D(Effect3D* effect) +void EffectSprite3D::setEffect3D(Effect3D *effect) { if(_defaultEffect == effect) return; CC_SAFE_RETAIN(effect); @@ -937,7 +931,7 @@ void EffectSprite3D::addEffect(Effect3DOutline* effect, ssize_t order) if(nullptr == effect) return; effect->retain(); effect->setTarget(this); - + _effects.push_back(std::make_tuple(order,effect,CustomCommand())); std::sort(std::begin(_effects), std::end(_effects), tuple_sort); @@ -992,26 +986,26 @@ Effect3DOutline* Effect3DOutline::create() bool Effect3DOutline::init() { - + return true; } Effect3DOutline::Effect3DOutline() - : _outlineWidth(1.0f) - , _outlineColor(1, 1, 1) - , _sprite(nullptr) +: _outlineWidth(1.0f) +, _outlineColor(1, 1, 1) +, _sprite(nullptr) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) _backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, - [this](EventCustom*) - { - auto glProgram = _glProgramState->getGLProgram(); - glProgram->reset(); - glProgram->initWithFilenames(_vertShaderFile, _fragShaderFile); - glProgram->link(); - glProgram->updateUniforms(); - } - ); + [this](EventCustom*) + { + auto glProgram = _glProgramState->getGLProgram(); + glProgram->reset(); + glProgram->initWithFilenames(_vertShaderFile, _fragShaderFile); + glProgram->link(); + glProgram->updateUniforms(); + } + ); Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1); #endif } @@ -1043,10 +1037,10 @@ void Effect3DOutline::setOutlineWidth(float width) } } -void Effect3DOutline::setTarget(EffectSprite3D* sprite) +void Effect3DOutline::setTarget(EffectSprite3D *sprite) { CCASSERT(nullptr != sprite && nullptr != sprite->getMesh(),"Error: Setting a null pointer or a null mesh EffectSprite3D to Effect3D"); - + if(sprite != _sprite) { GLProgram* glprogram; @@ -1060,16 +1054,16 @@ void Effect3DOutline::setTarget(EffectSprite3D* sprite) _glProgramState->retain(); _glProgramState->setUniformVec3("OutLineColor", _outlineColor); _glProgramState->setUniformFloat("OutlineWidth", _outlineWidth); - - + + _sprite = sprite; - + auto mesh = sprite->getMesh(); long offset = 0; for (auto i = 0; i < mesh->getMeshVertexAttribCount(); i++) { auto meshvertexattrib = mesh->getMeshVertexAttribute(i); - + _glProgramState->setVertexAttribPointer(s_attributeNames[meshvertexattrib.vertexAttrib], meshvertexattrib.size, meshvertexattrib.type, @@ -1078,12 +1072,12 @@ void Effect3DOutline::setTarget(EffectSprite3D* sprite) (void*)offset); offset += meshvertexattrib.attribSizeBytes; } - + Color4F color(_sprite->getDisplayedColor()); color.a = _sprite->getDisplayedOpacity() / 255.0f; _glProgramState->setUniformVec4("u_color", Vec4(color.r, color.g, color.b, color.a)); } - + } static void MatrixPalleteCallBack( GLProgram* glProgram, Uniform* uniform, int paletteSize, const float* palette) @@ -1091,7 +1085,7 @@ static void MatrixPalleteCallBack( GLProgram* glProgram, Uniform* uniform, int p glUniform4fv( uniform->location, (GLsizei)paletteSize, (const float*)palette ); } -void Effect3DOutline::draw(const Mat4& transform) +void Effect3DOutline::draw(const Mat4 &transform) { //draw Color4F color(_sprite->getDisplayedColor()); @@ -1102,10 +1096,10 @@ void Effect3DOutline::draw(const Mat4& transform) glEnable(GL_CULL_FACE); glCullFace(GL_FRONT); glEnable(GL_DEPTH_TEST); - + auto mesh = _sprite->getMesh(); glBindBuffer(GL_ARRAY_BUFFER, mesh->getVertexBuffer()); - + auto skin = _sprite->getMesh()->getSkin(); if(_sprite && skin) { @@ -1113,14 +1107,14 @@ void Effect3DOutline::draw(const Mat4& transform) skin->getMatrixPaletteSize(), (float*)skin->getMatrixPalette()); _glProgramState->setUniformCallback("u_matrixPalette", function); } - + if(_sprite) _glProgramState->apply(transform); - + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->getIndexBuffer()); glDrawElements(mesh->getPrimitiveType(), (GLsizei)mesh->getIndexCount(), mesh->getIndexFormat(), 0); CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, mesh->getIndexCount()); - + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); glDisable(GL_DEPTH_TEST); @@ -1129,18 +1123,18 @@ void Effect3DOutline::draw(const Mat4& transform) } } -void EffectSprite3D::draw(cocos2d::Renderer* renderer, const cocos2d::Mat4& transform, uint32_t flags) +void EffectSprite3D::draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, uint32_t flags) { - for(auto& effect : _effects) + for(auto &effect : _effects) { if(std::get<0>(effect) >=0) break; - CustomCommand& cc = std::get<2>(effect); + CustomCommand &cc = std::get<2>(effect); cc.func = CC_CALLBACK_0(Effect3D::draw,std::get<1>(effect),transform); renderer->addCommand(&cc); - + } - + if(!_defaultEffect) { Sprite3D::draw(renderer, transform, flags); @@ -1151,15 +1145,15 @@ void EffectSprite3D::draw(cocos2d::Renderer* renderer, const cocos2d::Mat4& tran _command.func = CC_CALLBACK_0(Effect3D::draw, _defaultEffect, transform); renderer->addCommand(&_command); } - - for(auto& effect : _effects) + + for(auto &effect : _effects) { if(std::get<0>(effect) <=0) continue; - CustomCommand& cc = std::get<2>(effect); + CustomCommand &cc = std::get<2>(effect); cc.func = CC_CALLBACK_0(Effect3D::draw,std::get<1>(effect),transform); renderer->addCommand(&cc); - + } } @@ -1167,7 +1161,7 @@ Sprite3DEffectTest::Sprite3DEffectTest() { auto s = Director::getInstance()->getWinSize(); addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) ); - + auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DEffectTest::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); @@ -1190,22 +1184,22 @@ void Sprite3DEffectTest::addNewSpriteWithCoords(Vec2 p) sprite->addEffect(effect, -1); effect->setOutlineColor(Vec3(1,0,0)); effect->setOutlineWidth(0.01f); - + Effect3DOutline* effect2 = Effect3DOutline::create(); sprite->addEffect(effect2, -2); effect2->setOutlineWidth(0.02f); effect2->setOutlineColor(Vec3(1,1,0)); //sprite->setEffect3D(effect); sprite->setScale(6.f); - + //add to scene addChild( sprite ); - + sprite->setPosition( Vec2( p.x, p.y) ); - + ActionInterval* action; float random = CCRANDOM_0_1(); - + if( random < 0.20 ) action = ScaleBy::create(3, 2); else if(random < 0.40) @@ -1218,7 +1212,7 @@ void Sprite3DEffectTest::addNewSpriteWithCoords(Vec2 p) action = FadeOut::create(2); auto action_back = action->reverse(); auto seq = Sequence::create( action, action_back, nullptr ); - + sprite->runAction( RepeatForever::create(seq) ); } @@ -1227,7 +1221,7 @@ void Sprite3DEffectTest::onTouchesEnded(const std::vector& touches, Even for (auto touch: touches) { auto location = touch->getLocation(); - + addNewSpriteWithCoords( location ); } } @@ -1239,22 +1233,22 @@ AsyncLoadSprite3DTest::AsyncLoadSprite3DTest() _paths.push_back("Sprite3DTest/orc.c3b"); _paths.push_back("Sprite3DTest/ReskinGirl.c3b"); _paths.push_back("Sprite3DTest/axe.c3b"); - + TTFConfig ttfConfig("fonts/arial.ttf", 15); auto label1 = Label::createWithTTF(ttfConfig,"AsyncLoad Sprite3D"); auto item1 = MenuItemLabel::create(label1,CC_CALLBACK_1(AsyncLoadSprite3DTest::menuCallback_asyncLoadSprite,this) ); - + auto s = Director::getInstance()->getWinSize(); item1->setPosition( s.width * .5f, s.height * .8f); - + auto pMenu1 = CCMenu::create(item1, nullptr); pMenu1->setPosition(Vec2(0,0)); this->addChild(pMenu1, 10); - + auto node = Node::create(); node->setTag(101); this->addChild(node); - + menuCallback_asyncLoadSprite(nullptr); } @@ -1275,15 +1269,14 @@ void AsyncLoadSprite3DTest::menuCallback_asyncLoadSprite(Ref* sender) { //Note that you must stop the tasks before leaving the scene. AsyncTaskPool::getInstance()->stopTasks(AsyncTaskPool::TaskType::TASK_IO); - + auto node = getChildByTag(101); node->removeAllChildren(); //remove all loaded sprite - + //remove cache data Sprite3DCache::getInstance()->removeAllSprite3DData(); long index = 0; - for (const auto& path : _paths) - { + for (const auto& path : _paths) { Sprite3D::createAsync(path, CC_CALLBACK_2(AsyncLoadSprite3DTest::asyncLoad_Callback, this), (void*)index++); } } @@ -1305,7 +1298,7 @@ Sprite3DWithSkinTest::Sprite3DWithSkinTest() auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DWithSkinTest::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); - + auto s = Director::getInstance()->getWinSize(); addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) ); } @@ -1354,7 +1347,7 @@ void Sprite3DWithSkinTest::onTouchesEnded(const std::vector& touches, Ev for (auto touch: touches) { auto location = touch->getLocation(); - + addNewSpriteWithCoords( location ); } } @@ -1364,7 +1357,7 @@ Sprite3DWithSkinOutlineTest::Sprite3DWithSkinOutlineTest() auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DWithSkinOutlineTest::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); - + auto s = Director::getInstance()->getWinSize(); addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) ); } @@ -1379,33 +1372,33 @@ std::string Sprite3DWithSkinOutlineTest::subtitle() const void Sprite3DWithSkinOutlineTest::addNewSpriteWithCoords(Vec2 p) { - + std::string fileName = "Sprite3DTest/orc.c3b"; auto sprite = EffectSprite3D::create(fileName); - + Effect3DOutline* effect = Effect3DOutline::create(); effect->setOutlineColor(Vec3(1,0,0)); effect->setOutlineWidth(0.01f); sprite->addEffect(effect, -1); - + Effect3DOutline* effect2 = Effect3DOutline::create(); effect2->setOutlineWidth(0.02f); effect2->setOutlineColor(Vec3(1,1,0)); sprite->addEffect(effect2, -2); - + sprite->setScale(3); sprite->setRotation3D(Vec3(0,180,0)); addChild(sprite); sprite->setPosition( Vec2( p.x, p.y) ); - + auto animation = Animation3D::create(fileName); if (animation) { auto animate = Animate3D::create(animation); bool inverse = (std::rand() % 3 == 0); - + int rand2 = std::rand(); float speed = 1.0f; if(rand2 % 3 == 1) @@ -1417,7 +1410,7 @@ void Sprite3DWithSkinOutlineTest::addNewSpriteWithCoords(Vec2 p) speed = animate->getSpeed() - 0.5 * CCRANDOM_0_1(); } animate->setSpeed(inverse ? -speed : speed); - + sprite->runAction(RepeatForever::create(animate)); } } @@ -1427,24 +1420,24 @@ void Sprite3DWithSkinOutlineTest::onTouchesEnded(const std::vector& touc for (auto touch: touches) { auto location = touch->getLocation(); - + addNewSpriteWithCoords( location ); } } Animate3DTest::Animate3DTest() - : _hurt(nullptr) - , _swim(nullptr) - , _sprite(nullptr) - , _moveAction(nullptr) - , _elapseTransTime(0.f) +: _hurt(nullptr) +, _swim(nullptr) +, _sprite(nullptr) +, _moveAction(nullptr) +, _elapseTransTime(0.f) { addSprite3D(); - + auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(Animate3DTest::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); - + scheduleUpdate(); } @@ -1470,7 +1463,7 @@ void Animate3DTest::update(float dt) if (_state == State::HURT_TO_SWIMMING) { _elapseTransTime += dt; - + if (_elapseTransTime >= Animate3D::getTransitionTime()) { _sprite->stopAction(_hurt); @@ -1503,13 +1496,13 @@ void Animate3DTest::addSprite3D() auto animate = Animate3D::create(animation, 0.f, 1.933f); _swim = RepeatForever::create(animate); sprite->runAction(_swim); - + _swim->retain(); _hurt = Animate3D::create(animation, 1.933f, 2.8f); _hurt->retain(); _state = State::SWIMMING; } - + _moveAction = MoveTo::create(4.f, Vec2(s.width / 5.f, s.height / 2.f)); _moveAction->retain(); auto seq = Sequence::create(_moveAction, CallFunc::create(CC_CALLBACK_0(Animate3DTest::reachEndCallBack, this)), nullptr); @@ -1543,7 +1536,7 @@ void Animate3DTest::onTouchesEnded(const std::vector& touches, Event* ev for (auto touch: touches) { auto location = touch->getLocation(); - + if (_sprite) { float len = (_sprite->getPosition() - location).length(); @@ -1568,12 +1561,12 @@ void Animate3DTest::onTouchesEnded(const std::vector& touches, Event* ev } AttachmentTest::AttachmentTest() - : _hasWeapon(false) - , _sprite(nullptr) +: _hasWeapon(false) +, _sprite(nullptr) { auto s = Director::getInstance()->getWinSize(); addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) ); - + auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(AttachmentTest::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); @@ -1595,16 +1588,16 @@ void AttachmentTest::addNewSpriteWithCoords(Vec2 p) sprite->setRotation3D(Vec3(0,180,0)); addChild(sprite); sprite->setPosition( Vec2( p.x, p.y) ); - + //test attach auto sp = Sprite3D::create("Sprite3DTest/axe.c3b"); sprite->getAttachNode("Bip001 R Hand")->addChild(sp); - + auto animation = Animation3D::create(fileName); if (animation) { auto animate = Animate3D::create(animation); - + sprite->runAction(RepeatForever::create(animate)); } _sprite = sprite; @@ -1625,11 +1618,11 @@ void AttachmentTest::onTouchesEnded(const std::vector& touches, Event* e _hasWeapon = !_hasWeapon; } Sprite3DReskinTest::Sprite3DReskinTest() - : _sprite(nullptr) +: _sprite(nullptr) { auto s = Director::getInstance()->getWinSize(); addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) ); - + auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DReskinTest::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); @@ -1637,11 +1630,11 @@ Sprite3DReskinTest::Sprite3DReskinTest() auto label1 = Label::createWithTTF(ttfConfig,"Hair"); auto item1 = MenuItemLabel::create(label1,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) ); auto label2 = Label::createWithTTF(ttfConfig,"Glasses"); - auto item2 = MenuItemLabel::create(label2, CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) ); + auto item2 = MenuItemLabel::create(label2, CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) ); auto label3 = Label::createWithTTF(ttfConfig,"Coat"); auto item3 = MenuItemLabel::create(label3,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) ); auto label4 = Label::createWithTTF(ttfConfig,"Pants"); - auto item4 = MenuItemLabel::create(label4, CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) ); + auto item4 = MenuItemLabel::create(label4, CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) ); auto label5 = Label::createWithTTF(ttfConfig,"Shoes"); auto item5 = MenuItemLabel::create(label5,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) ); item1->setPosition( Vec2(VisibleRect::left().x+50, VisibleRect::bottom().y+item1->getContentSize().height*4 ) ); @@ -1657,7 +1650,7 @@ Sprite3DReskinTest::Sprite3DReskinTest() auto pMenu1 = CCMenu::create(item1, item2, item3, item4, item5, nullptr); pMenu1->setPosition(Vec2(0,0)); this->addChild(pMenu1, 10); - + } void Sprite3DReskinTest::menuCallback_reSkin(Ref* sender) { @@ -1690,41 +1683,41 @@ void Sprite3DReskinTest::addNewSpriteWithCoords(Vec2 p) if (animation) { auto animate = Animate3D::create(animation); - + sprite->runAction(RepeatForever::create(animate)); } _sprite = sprite; - + auto& body = _skins[(int)SkinType::UPPER_BODY]; body.push_back("Girl_UpperBody01"); body.push_back("Girl_UpperBody02"); - + auto& pants = _skins[(int)SkinType::PANTS]; pants.push_back("Girl_LowerBody01"); pants.push_back("Girl_LowerBody02"); - + auto& shoes = _skins[(int)SkinType::SHOES]; shoes.push_back("Girl_Shoes01"); shoes.push_back("Girl_Shoes02"); - + auto& hair = _skins[(int)SkinType::HAIR]; hair.push_back("Girl_Hair01"); hair.push_back("Girl_Hair02"); - + auto& face = _skins[(int)SkinType::FACE]; face.push_back("Girl_Face01"); face.push_back("Girl_Face02"); - + auto& hand = _skins[(int)SkinType::HAND]; hand.push_back("Girl_Hand01"); hand.push_back("Girl_Hand02"); - + auto& glasses = _skins[(int)SkinType::GLASSES]; glasses.push_back(""); glasses.push_back("Girl_Glasses01"); - + memset(_curSkin, 0, (int)SkinType::MAX_TYPE * sizeof(int)); - + applyCurSkin(); } @@ -1734,12 +1727,10 @@ void Sprite3DReskinTest::onTouchesEnded(const std::vector& touches, Even void Sprite3DReskinTest::applyCurSkin() { - for (ssize_t i = 0; i < _sprite->getMeshCount(); i++) - { + for (ssize_t i = 0; i < _sprite->getMeshCount(); i++) { auto mesh = _sprite->getMeshByIndex(static_cast(i)); bool isVisible = false; - for (int j = 0; j < (int)SkinType::MAX_TYPE; j++) - { + for (int j = 0; j < (int)SkinType::MAX_TYPE; j++) { if (mesh->getName() == _skins[j].at(_curSkin[j])) { isVisible = true; @@ -1759,7 +1750,7 @@ Sprite3DWithOBBPerformanceTest::Sprite3DWithOBBPerformanceTest() _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); auto s = Director::getInstance()->getWinSize(); initDrawBox(); - + addNewSpriteWithCoords(Vec2(s.width/2, s.height/2)); MenuItemFont::setFontName("fonts/arial.ttf"); MenuItemFont::setFontSize(65); @@ -1767,12 +1758,12 @@ Sprite3DWithOBBPerformanceTest::Sprite3DWithOBBPerformanceTest() decrease->setColor(Color3B(0,200,20)); auto increase = MenuItemFont::create(" + ", CC_CALLBACK_1(Sprite3DWithOBBPerformanceTest::addOBBCallback, this)); increase->setColor(Color3B(0,200,20)); - + auto menu = Menu::create(decrease, increase, nullptr); menu->alignItemsHorizontally(); menu->setPosition(Vec2(s.width/2, s.height-65)); addChild(menu, 1); - + TTFConfig ttfCount("fonts/Marker Felt.ttf", 30); _labelCubeCount = Label::createWithTTF(ttfCount,"0 cubes"); _labelCubeCount->setColor(Color3B(0,200,20)); @@ -1824,14 +1815,14 @@ void Sprite3DWithOBBPerformanceTest::onTouchesBegan(const std::vector& t void Sprite3DWithOBBPerformanceTest::onTouchesEnded(const std::vector& touches, Event* event) { - + } void Sprite3DWithOBBPerformanceTest::onTouchesMoved(const std::vector& touches, Event* event) { for (const auto& touch: touches) { - auto location = touch->getLocation(); + auto location = touch->getLocation(); auto obbSize = _obb.size(); for(decltype(obbSize) i = 0; i < obbSize; i++) @@ -1847,23 +1838,23 @@ void Sprite3DWithOBBPerformanceTest::update(float dt) char szText[16]; sprintf(szText,"%lu cubes",_obb.size()); _labelCubeCount->setString(szText); - + if (_drawDebug) { _drawDebug->clear(); - + Mat4 mat = _sprite->getNodeToWorldTransform(); mat.getRightVector(&_obbt._xAxis); _obbt._xAxis.normalize(); - + mat.getUpVector(&_obbt._yAxis); _obbt._yAxis.normalize(); - + mat.getForwardVector(&_obbt._zAxis); _obbt._zAxis.normalize(); - + _obbt._center = _sprite->getPosition3D(); - + Vec3 corners[8] = {}; _obbt.getCorners(corners); _drawDebug->drawCube(corners, Color4F(0,0,1,1)); @@ -1902,16 +1893,16 @@ void Sprite3DWithOBBPerformanceTest::addNewSpriteWithCoords(Vec2 p) auto animate = Animate3D::create(animation, 0.f, 1.933f); sprite->runAction(RepeatForever::create(animate)); } - + _moveAction = MoveTo::create(4.f, Vec2(s.width / 5.f, s.height / 2.f)); _moveAction->retain(); auto seq = Sequence::create(_moveAction, CallFunc::create(CC_CALLBACK_0(Sprite3DWithOBBPerformanceTest::reachEndCallBack, this)), nullptr); seq->setTag(100); sprite->runAction(seq); - + AABB aabb = _sprite->getAABB(); _obbt = OBB(aabb); - + _drawDebug = DrawNode3D::create(); addChild(_drawDebug); } @@ -1965,23 +1956,23 @@ void Sprite3DWithOBBPerformanceTest::delOBBWithCount(float value) void Sprite3DWithOBBPerformanceTest::unproject(const Mat4& viewProjection, const Size* viewport, Vec3* src, Vec3* dst) { assert(dst); - + assert(viewport->width != 0.0f && viewport->height != 0.0f); Vec4 screen(src->x / viewport->width, ((viewport->height - src->y)) / viewport->height, src->z, 1.0f); - + screen.x = screen.x * 2.0f - 1.0f; screen.y = screen.y * 2.0f - 1.0f; screen.z = screen.z * 2.0f - 1.0f; - + viewProjection.getInversed().transformVector(screen, &screen); - + if (screen.w != 0.0f) { screen.x /= screen.w; screen.y /= screen.w; screen.z /= screen.w; } - + dst->set(screen.x, screen.y, screen.z); } @@ -1990,26 +1981,26 @@ void Sprite3DWithOBBPerformanceTest::calculateRayByLocationInView(Ray* ray, cons auto dir = Director::getInstance(); auto view = dir->getWinSize(); auto mat = dir->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION); - + Vec3 src = Vec3(location.x, location.y, -1); Vec3 nearPoint; unproject(mat, &view, &src, &nearPoint); - + src = Vec3(location.x, location.y, 1); Vec3 farPoint; unproject(mat, &view, &src, &farPoint); - + Vec3 direction; Vec3::subtract(farPoint, nearPoint, &direction); direction.normalize(); - + ray->_origin = nearPoint; ray->_direction = direction; } Sprite3DMirrorTest::Sprite3DMirrorTest() - : _sprite(nullptr) - , _mirrorSprite(nullptr) +: _sprite(nullptr) +, _mirrorSprite(nullptr) { auto s = Director::getInstance()->getWinSize(); addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) ); @@ -2031,21 +2022,21 @@ void Sprite3DMirrorTest::addNewSpriteWithCoords(Vec2 p) sprite->setRotation3D(Vec3(0,180,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); - + auto animation = Animation3D::create(fileName); if (animation) { auto animate = Animate3D::create(animation); - + sprite->runAction(RepeatForever::create(animate)); } _sprite = sprite; _hasWeapon = true; - + //create mirror Sprite3D sprite = Sprite3D::create(fileName); sprite->setScale(5); @@ -2054,25 +2045,25 @@ void Sprite3DMirrorTest::addNewSpriteWithCoords(Vec2 p) 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; } QuaternionTest::QuaternionTest() - : _arcSpeed(CC_DEGREES_TO_RADIANS(90)) - , _radius(100.f) - , _accAngle(0.f) +: _arcSpeed(CC_DEGREES_TO_RADIANS(90)) +, _radius(100.f) +, _accAngle(0.f) { auto s = Director::getInstance()->getWinSize(); addNewSpriteWithCoords(Vec2(s.width / 2.f, s.height / 2.f)); @@ -2110,60 +2101,58 @@ void QuaternionTest::update(float delta) const float pi = M_PI; if (_accAngle >= 2 * pi) _accAngle -= 2 * pi; - + auto s = Director::getInstance()->getWinSize(); _sprite->setPosition(Vec2(s.width / 2.f + _radius * cosf(_accAngle), s.height / 2.f + _radius * sinf(_accAngle))); - + Quaternion quat; Quaternion::createFromAxisAngle(Vec3(0.f, 0.f, 1.f), _accAngle - pi * 0.5f, &quat); _sprite->setRotationQuat(quat); } UseCaseSprite3D::UseCaseSprite3D() - : _caseIdx(0) +: _caseIdx(0) { auto s = Director::getInstance()->getWinSize(); - + _useCaseTitles[0] = "transparent 3d sprite and 2d sprite"; _useCaseTitles[1] = "ui - 3d - ui"; - + auto itemPrev = MenuItemImage::create("Images/b1.png", "Images/b2.png", - [&](Ref *sender) - { - _caseIdx--; - if (_caseIdx < 0) - _caseIdx = (int)USECASE::MAX_CASE_NUM - 1; - this->switchCase(); - }); - + [&](Ref *sender) { + _caseIdx--; + if (_caseIdx < 0) + _caseIdx = (int)USECASE::MAX_CASE_NUM - 1; + this->switchCase(); + }); + auto itemNext = MenuItemImage::create("Images/f1.png", "Images/f2.png", - [&](Ref *sender) - { - _caseIdx++; - if (_caseIdx >= (int)USECASE::MAX_CASE_NUM) - _caseIdx = 0; - this->switchCase(); - }); - + [&](Ref *sender) { + _caseIdx++; + if (_caseIdx >= (int)USECASE::MAX_CASE_NUM) + _caseIdx = 0; + this->switchCase(); + }); + auto menu = Menu::create(itemPrev, itemNext, nullptr); menu->alignItemsHorizontally(); menu->setScale(0.5); menu->setAnchorPoint(Vec2(0,0)); menu->setPosition(Vec2(s.width/2,70)); - + _label = Label::create(); _label->setPosition(s.width * 0.5f, s.height * 0.8f); addChild(_label); - + addChild(menu); - + //setup camera auto camera = Camera::createPerspective(40, s.width / s.height, 0.01f, 1000.f); camera->setCameraFlag(CameraFlag::USER1); camera->setPosition3D(Vec3(0.f, 30.f, 100.f)); camera->lookAt(Vec3(0.f, 0.f, 0.f)); addChild(camera); - + switchCase(); } @@ -2180,7 +2169,7 @@ std::string UseCaseSprite3D::subtitle() const void UseCaseSprite3D::switchCase() { removeChildByTag(101); - + auto s = Director::getInstance()->getWinSize(); _label->setString(_useCaseTitles[_caseIdx]); if (_caseIdx == 0) // use case 1, 3d transparent sprite + 2d sprite @@ -2192,33 +2181,33 @@ void UseCaseSprite3D::switchCase() if (animation) { auto animate = Animate3D::create(animation); - + sprite->runAction(RepeatForever::create(animate)); } - + auto circleBack = Sprite3D::create(); auto circle = Sprite::create("Sprite3DTest/circle.png"); circleBack->setScale(0.5f); circleBack->addChild(circle); circle->runAction(RepeatForever::create(RotateBy::create(3, Vec3(0.f, 0.f, 360.f)))); - + circleBack->setRotation3D(Vec3(90, 0, 0)); - + auto pos = sprite->getPosition3D(); circleBack->setPosition3D(Vec3(pos.x, pos.y, pos.z - 1)); - + sprite->setOpacity(250); sprite->setCameraMask(2); circleBack->setCameraMask(2); sprite->setTag(3); circleBack->setTag(2); - + auto node = Node::create(); node->addChild(sprite); node->addChild(circleBack); node->setTag(101); addChild(node); - + scheduleUpdate(); update(0.f); } @@ -2228,7 +2217,7 @@ void UseCaseSprite3D::switchCase() layer->setPosition(s.width * 0.25f, s.height * 0.25f); layer->setGlobalZOrder(-1); addChild(layer); - + std::string filename = "Sprite3DTest/girl.c3b"; auto sprite = Sprite3D::create(filename); sprite->setScale(0.5f); @@ -2240,20 +2229,20 @@ void UseCaseSprite3D::switchCase() } sprite->setPosition(s.width * 0.25f, s.height * 0.125f); layer->addChild(sprite); - + TTFConfig ttfConfig("fonts/arial.ttf", 15); auto label1 = Label::createWithTTF(ttfConfig,"Message"); auto item1 = MenuItemLabel::create(label1,CC_CALLBACK_1(UseCaseSprite3D::menuCallback_Message,this) ); auto label2 = Label::createWithTTF(ttfConfig,"Message"); - auto item2 = MenuItemLabel::create(label2, CC_CALLBACK_1(UseCaseSprite3D::menuCallback_Message,this) ); - + auto item2 = MenuItemLabel::create(label2, CC_CALLBACK_1(UseCaseSprite3D::menuCallback_Message,this) ); + item1->setPosition( Vec2(s.width * 0.5f - item1->getContentSize().width * 0.5f, s.height * 0.5f - item1->getContentSize().height ) ); item2->setPosition( Vec2(s.width * 0.5f - item1->getContentSize().width * 0.5f, s.height * 0.5f - item1->getContentSize().height * 2.f ) ); - + auto pMenu1 = CCMenu::create(item1, item2, nullptr); pMenu1->setPosition(Vec2(0,0)); layer->addChild(pMenu1); - + layer->setTag(101); } } @@ -2286,14 +2275,14 @@ void UseCaseSprite3D::update(float delta) { static float accAngle = 0.f; accAngle += delta * CC_DEGREES_TO_RADIANS(60); - + float radius = 30.f; float x = cosf(accAngle) * radius, z = sinf(accAngle) * radius; - + auto node = getChildByTag(101); auto sprite3d = node->getChildByTag(3); auto circle = node->getChildByTag(2); - + sprite3d->setPositionX(x); sprite3d->setPositionZ(z); circle->setPositionX(x);