diff --git a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp index 900a140b0a..f82e00adeb 100644 --- a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp +++ b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp @@ -1,27 +1,27 @@ /**************************************************************************** - 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" #include "3d/CCAnimation3D.h" @@ -53,6 +53,8 @@ static std::function createFunctions[] = // 3DEffect use custom shader which is not supported on WP8/WinRT yet. CL(Sprite3DEffectTest), CL(Sprite3DUVAnimationTest), + CL(Sprite3DFakeShadowTest), + CL(Sprite3DBasicToonShaderTest), #endif CL(Sprite3DWithSkinTest), #if (CC_TARGET_PLATFORM != CC_PLATFORM_WP8) && (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT) @@ -71,7 +73,7 @@ static Layer* nextSpriteTestAction() { sceneIdx++; sceneIdx = sceneIdx % MAX_LAYER; - + auto layer = (createFunctions[sceneIdx])(); return layer; } @@ -82,7 +84,7 @@ static Layer* backSpriteTestAction() int total = MAX_LAYER; if( sceneIdx < 0 ) sceneIdx += total; - + auto layer = (createFunctions[sceneIdx])(); return layer; } @@ -100,7 +102,7 @@ static Layer* restartSpriteTestAction() //------------------------------------------------------------------ Sprite3DTestDemo::Sprite3DTestDemo(void) -: BaseTest() + : BaseTest() { } @@ -127,7 +129,7 @@ void Sprite3DTestDemo::restartCallback(Ref* sender) { auto s = new (std::nothrow) Sprite3DTestScene(); s->addChild(restartSpriteTestAction()); - + Director::getInstance()->replaceScene(s); s->release(); } @@ -160,7 +162,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) ); } @@ -170,26 +172,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) @@ -202,7 +204,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) ); } @@ -211,7 +213,7 @@ void Sprite3DBasicTest::onTouchesEnded(const std::vector& touches, Event for (auto touch: touches) { auto location = touch->getLocation(); - + addNewSpriteWithCoords( location ); } } @@ -270,8 +272,8 @@ Sprite3DUVAnimationTest::Sprite3DUVAnimationTest() //create the second texture for cylinder auto shining_texture = Director::getInstance()->getTextureCache()->addImage("Sprite3DTest/caustics.png"); Texture2D::TexParams tRepeatParams;//set texture parameters - tRepeatParams.magFilter = GL_NEAREST; - tRepeatParams.minFilter = GL_NEAREST; + tRepeatParams.magFilter = GL_LINEAR; + tRepeatParams.minFilter = GL_LINEAR; tRepeatParams.wrapS = GL_REPEAT; tRepeatParams.wrapT = GL_REPEAT; shining_texture->setTexParameters(tRepeatParams); @@ -331,36 +333,36 @@ void Sprite3DUVAnimationTest::cylinderUpdate(float dt) 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()); - + Rect rect = target->getBoundingBox(); if (rect.containsPoint(touch->getLocation())) { @@ -370,21 +372,21 @@ Sprite3DHitTest::Sprite3DHitTest() } return false; }; - + listener1->onTouchMoved = [](Touch* touch, Event* event){ auto target = static_cast(event->getCurrentTarget()); target->setPosition(target->getPosition() + touch->getDelta()); }; - + 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 @@ -401,13 +403,13 @@ 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 ) @@ -433,7 +435,7 @@ 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)) { @@ -445,9 +447,9 @@ EffectSprite3D* EffectSprite3D::create(const std::string &path) } EffectSprite3D::EffectSprite3D() -: _defaultEffect(nullptr) + : _defaultEffect(nullptr) { - + } EffectSprite3D::~EffectSprite3D() @@ -472,7 +474,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); @@ -527,33 +529,33 @@ 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) +#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) _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 } Effect3DOutline::~Effect3DOutline() { -#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) +#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener); #endif } @@ -581,7 +583,7 @@ void Effect3DOutline::setOutlineWidth(float width) 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; @@ -595,30 +597,29 @@ 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, - GL_FALSE, - mesh->getVertexSizeInBytes(), - (void*)offset); + meshvertexattrib.size, + meshvertexattrib.type, + GL_FALSE, + mesh->getVertexSizeInBytes(), + (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) @@ -637,25 +638,25 @@ 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) { auto function = std::bind(MatrixPalleteCallBack, std::placeholders::_1, std::placeholders::_2, - skin->getMatrixPaletteSize(), (float*)skin->getMatrixPalette()); + 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); @@ -673,9 +674,8 @@ void EffectSprite3D::draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &tran 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); @@ -686,7 +686,7 @@ 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) { if(std::get<0>(effect) <=0) @@ -694,7 +694,7 @@ void EffectSprite3D::draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &tran CustomCommand &cc = std::get<2>(effect); cc.func = CC_CALLBACK_0(Effect3D::draw,std::get<1>(effect),transform); renderer->addCommand(&cc); - + } } @@ -702,7 +702,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); @@ -725,22 +725,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) @@ -753,7 +753,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) ); } @@ -762,7 +762,7 @@ void Sprite3DEffectTest::onTouchesEnded(const std::vector& touches, Even for (auto touch: touches) { auto location = touch->getLocation(); - + addNewSpriteWithCoords( location ); } } @@ -772,7 +772,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) ); } @@ -821,7 +821,7 @@ void Sprite3DWithSkinTest::onTouchesEnded(const std::vector& touches, Ev for (auto touch: touches) { auto location = touch->getLocation(); - + addNewSpriteWithCoords( location ); } } @@ -831,7 +831,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) ); } @@ -846,33 +846,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) @@ -884,7 +884,7 @@ void Sprite3DWithSkinOutlineTest::addNewSpriteWithCoords(Vec2 p) speed = animate->getSpeed() - 0.5 * CCRANDOM_0_1(); } animate->setSpeed(inverse ? -speed : speed); - + sprite->runAction(RepeatForever::create(animate)); } } @@ -894,24 +894,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(); } @@ -937,7 +937,7 @@ void Animate3DTest::update(float dt) if (_state == State::HURT_TO_SWIMMING) { _elapseTransTime += dt; - + if (_elapseTransTime >= Animate3D::getTransitionTime()) { _sprite->stopAction(_hurt); @@ -970,13 +970,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); @@ -1010,7 +1010,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(); @@ -1035,12 +1035,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); @@ -1062,16 +1062,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; @@ -1092,48 +1092,79 @@ 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); TTFConfig ttfConfig("fonts/arial.ttf", 20); auto label1 = Label::createWithTTF(ttfConfig,"Hair"); - auto item1 = MenuItemLabel::create(label1,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) ); + auto item1 = MenuItemLabel::create(label1,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_switchHair,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_switchGlasses,this) ); auto label3 = Label::createWithTTF(ttfConfig,"Coat"); - auto item3 = MenuItemLabel::create(label3,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) ); + auto item3 = MenuItemLabel::create(label3,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_switchCoat,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_switchPants,this) ); auto label5 = Label::createWithTTF(ttfConfig,"Shoes"); - auto item5 = MenuItemLabel::create(label5,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) ); + auto item5 = MenuItemLabel::create(label5,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_switchShoes,this) ); item1->setPosition( Vec2(VisibleRect::left().x+50, VisibleRect::bottom().y+item1->getContentSize().height*4 ) ); item2->setPosition( Vec2(VisibleRect::left().x+50, VisibleRect::bottom().y+item1->getContentSize().height *5 ) ); item3->setPosition( Vec2(VisibleRect::left().x+50, VisibleRect::bottom().y+item1->getContentSize().height*6 ) ); item4->setPosition( Vec2(VisibleRect::left().x+50, VisibleRect::bottom().y+item1->getContentSize().height *7 ) ); item5->setPosition( Vec2(VisibleRect::left().x+50, VisibleRect::bottom().y+item1->getContentSize().height *8 ) ); - item1->setUserData((void*)SkinType::HAIR); - item2->setUserData((void*)SkinType::GLASSES); - item3->setUserData((void*)SkinType::UPPER_BODY); - item4->setUserData((void*)SkinType::PANTS); - item5->setUserData((void*)SkinType::SHOES); 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) +void Sprite3DReskinTest::menuCallback_switchHair(Ref* sender) { - long index = (long)(((MenuItemLabel*)sender)->getUserData()); - if (index < (int)SkinType::MAX_TYPE) - { - _curSkin[index] = (_curSkin[index] + 1) % _skins[index].size(); - applyCurSkin(); - } + std::string str = _curSkin[SkinType::HAIR]; + if (str == "Girl_Hair01") + _curSkin[SkinType::HAIR] = "Girl_Hair02"; + else + _curSkin[SkinType::HAIR] = "Girl_Hair01"; + applyCurSkin(); +} +void Sprite3DReskinTest::menuCallback_switchGlasses(Ref* sender) +{ + std::string str = _curSkin[SkinType::GLASSES]; + if (str == "Girl_Glasses01") + _curSkin[SkinType::GLASSES] = ""; + else + _curSkin[SkinType::GLASSES] = "Girl_Glasses01"; + applyCurSkin(); +} +void Sprite3DReskinTest::menuCallback_switchCoat(Ref* sender) +{ + std::string str = _curSkin[SkinType::UPPER_BODY]; + if (str == "Girl_UpperBody01") + _curSkin[SkinType::UPPER_BODY] = "Girl_UpperBody02"; + else + _curSkin[SkinType::UPPER_BODY] = "Girl_UpperBody01"; + applyCurSkin(); +} +void Sprite3DReskinTest::menuCallback_switchPants(Ref* sender) +{ + std::string str = _curSkin[SkinType::PANTS]; + if (str == "Girl_LowerBody01") + _curSkin[SkinType::PANTS] = "Girl_LowerBody02"; + else + _curSkin[SkinType::PANTS] = "Girl_LowerBody01"; + applyCurSkin(); +} +void Sprite3DReskinTest::menuCallback_switchShoes(Ref* sender) +{ + std::string strShoes = _curSkin[SkinType::SHOES]; + if (strShoes == "Girl_Shoes01") + _curSkin[SkinType::SHOES] = "Girl_Shoes02"; + else + _curSkin[SkinType::SHOES] = "Girl_Shoes01"; + applyCurSkin(); } std::string Sprite3DReskinTest::title() const @@ -1157,41 +1188,18 @@ 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)); - + + _curSkin[SkinType::UPPER_BODY] = "Girl_UpperBody01"; + _curSkin[SkinType::PANTS] = "Girl_LowerBody01"; + _curSkin[SkinType::SHOES] = "Girl_Shoes01"; + _curSkin[SkinType::HAIR] = "Girl_Hair01"; + _curSkin[SkinType::FACE] = "Girl_Face01"; + _curSkin[SkinType::HAND] = "Girl_Hand01"; + _curSkin[SkinType::GLASSES] = ""; applyCurSkin(); } @@ -1204,8 +1212,8 @@ void Sprite3DReskinTest::applyCurSkin() 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++) { - if (mesh->getName() == _skins[j].at(_curSkin[j])) + for (auto& it : _curSkin) { + if (mesh->getName() == it.second) { isVisible = true; break; @@ -1224,7 +1232,7 @@ Sprite3DWithOBBPerfromanceTest::Sprite3DWithOBBPerfromanceTest() _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); @@ -1232,12 +1240,12 @@ Sprite3DWithOBBPerfromanceTest::Sprite3DWithOBBPerfromanceTest() decrease->setColor(Color3B(0,200,20)); auto increase = MenuItemFont::create(" + ", CC_CALLBACK_1(Sprite3DWithOBBPerfromanceTest::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)); @@ -1289,7 +1297,7 @@ void Sprite3DWithOBBPerfromanceTest::onTouchesBegan(const std::vector& t void Sprite3DWithOBBPerfromanceTest::onTouchesEnded(const std::vector& touches, Event* event) { - + } void Sprite3DWithOBBPerfromanceTest::onTouchesMoved(const std::vector& touches, Event* event) @@ -1312,23 +1320,23 @@ void Sprite3DWithOBBPerfromanceTest::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)); @@ -1367,16 +1375,16 @@ void Sprite3DWithOBBPerfromanceTest::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(Sprite3DWithOBBPerfromanceTest::reachEndCallBack, this)), nullptr); seq->setTag(100); sprite->runAction(seq); - + AABB aabb = _sprite->getAABB(); _obbt = OBB(aabb); - + _drawDebug = DrawNode3D::create(); addChild(_drawDebug); } @@ -1430,23 +1438,23 @@ void Sprite3DWithOBBPerfromanceTest::delOBBWithCount(float value) void Sprite3DWithOBBPerfromanceTest::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); } @@ -1455,26 +1463,26 @@ void Sprite3DWithOBBPerfromanceTest::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) ); @@ -1496,21 +1504,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); @@ -1519,17 +1527,115 @@ 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; } + +Sprite3DFakeShadowTest::Sprite3DFakeShadowTest() +{ + Size visibleSize = Director::getInstance()->getVisibleSize(); + auto _camera = Camera::createPerspective(60,visibleSize.width/visibleSize.height,0.1,200); + _camera->setCameraFlag(CameraFlag::USER1); + _camera->setPositionY(3); + auto plane = Sprite3D::create("Sprite3DTest/plane.c3t"); + plane->setRotation3D(Vec3(90,0,0)); + auto shader =GLProgram::createWithFilenames("Sprite3DTest/simple_shadow.vert","Sprite3DTest/simple_shadow.frag"); + auto state = GLProgramState::create(shader); + plane->setGLProgramState(state); + //pass mesh's attribute to shader + 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); + offset += meshattribute.attribSizeBytes; + } + state->setUniformMat4("u_model_matrix",plane->getNodeToWorldTransform()); + 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->addChild(plane); + + auto orc = Sprite3D::create("Sprite3DTest/orc.c3b"); + orc->setScale(0.2); + orc->setPosition3D(Vec3(0,0,-10)); + plane->getGLProgramState()->setUniformVec3("u_target_pos",orc->getPosition3D()); + this->addChild(orc); + this->addChild(_camera); + this->setCameraMask(2); +} + +std::string Sprite3DFakeShadowTest::title() const +{ + return "fake shadow effect"; +} + +std::string Sprite3DFakeShadowTest::subtitle() const +{ + return "fake shadow effect"; +} + +//the basic toon shader test +Sprite3DBasicToonShaderTest::Sprite3DBasicToonShaderTest() +{ + Size visibleSize = Director::getInstance()->getVisibleSize(); + auto _camera = Camera::createPerspective(60,visibleSize.width/visibleSize.height,0.1,200); + _camera->setCameraFlag(CameraFlag::USER1); + // create a teapot + auto teapot = Sprite3D::create("Sprite3DTest/teapot.c3b"); + //create and set our custom shader + auto shader =GLProgram::createWithFilenames("Sprite3DTest/toon.vert","Sprite3DTest/toon.frag"); + auto state = GLProgramState::create(shader); + teapot->setGLProgramState(state); + teapot->setPosition3D(Vec3(0,-5,-20)); + teapot->setRotation3D(Vec3(-90,180,0)); + auto rotate_action = RotateBy::create(3,Vec3(0,30,0)); + 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); + offset += meshattribute.attribSizeBytes; + } + addChild(teapot); + addChild(_camera); + setCameraMask(2); +} + +std::string Sprite3DBasicToonShaderTest::title() const +{ + return "basic toon shader test"; +} + +std::string Sprite3DBasicToonShaderTest::subtitle() const +{ + return " "; +} diff --git a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h index 02d8bb6c48..1425923c99 100644 --- a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h +++ b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h @@ -81,6 +81,24 @@ protected: float _shining_duraion; GLProgramState * _state; }; + +class Sprite3DFakeShadowTest : public Sprite3DTestDemo +{ +public: + CREATE_FUNC(Sprite3DFakeShadowTest); + Sprite3DFakeShadowTest(); + virtual std::string title() const override; + virtual std::string subtitle() const override; +}; + +class Sprite3DBasicToonShaderTest : public Sprite3DTestDemo +{ +public: + CREATE_FUNC(Sprite3DBasicToonShaderTest); + Sprite3DBasicToonShaderTest(); + virtual std::string title() const override; + virtual std::string subtitle() const override; +}; class EffectSprite3D; class Effect3D : public Ref @@ -120,7 +138,7 @@ protected: float _outlineWidth; //weak reference EffectSprite3D* _sprite; -#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) +#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) EventListenerCustom* _backToForegroundListener; #endif @@ -268,8 +286,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); + void menuCallback_switchHair(Ref* sender); + void menuCallback_switchGlasses(Ref* sender); + void menuCallback_switchCoat(Ref* sender); + void menuCallback_switchPants(Ref* sender); + void menuCallback_switchShoes(Ref* sender); protected: void applyCurSkin(); @@ -282,11 +303,8 @@ protected: FACE, HAND, GLASSES, - MAX_TYPE, }; - - std::vector _skins[(int)SkinType::MAX_TYPE]; //all skins - int _curSkin[(int)SkinType::MAX_TYPE]; //current skin index + std::map _curSkin; cocos2d::Sprite3D* _sprite; }; class Sprite3DWithOBBPerfromanceTest : public Sprite3DTestDemo