2014-08-07 15:23:31 +08:00
|
|
|
/****************************************************************************
|
2014-10-16 17:38:42 +08:00
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2014-08-07 15:23:31 +08:00
|
|
|
|
|
|
|
#include "Camera3DTest.h"
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
IDC_NEXT = 100,
|
|
|
|
IDC_BACK,
|
|
|
|
IDC_RESTART
|
|
|
|
};
|
|
|
|
|
|
|
|
static int sceneIdx = -1;
|
|
|
|
|
|
|
|
static std::function<Layer*()> createFunctions[] =
|
|
|
|
{
|
2015-01-09 08:30:00 +08:00
|
|
|
CL(CameraRotationTest),
|
2014-08-07 15:23:31 +08:00
|
|
|
CL(Camera3DTestDemo),
|
2015-01-09 08:30:00 +08:00
|
|
|
CL(CameraCullingDemo)
|
2014-08-07 15:23:31 +08:00
|
|
|
};
|
|
|
|
#define MAX_LAYER (sizeof(createFunctions) / sizeof(createFunctions[0]))
|
|
|
|
|
2015-01-09 08:30:00 +08:00
|
|
|
static Layer*nextTest()
|
2014-08-07 15:23:31 +08:00
|
|
|
{
|
|
|
|
sceneIdx++;
|
|
|
|
sceneIdx = sceneIdx % MAX_LAYER;
|
|
|
|
auto layer = (createFunctions[sceneIdx])();
|
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
|
2015-01-09 08:30:00 +08:00
|
|
|
static Layer*backTest()
|
2014-08-07 15:23:31 +08:00
|
|
|
{
|
|
|
|
sceneIdx--;
|
|
|
|
int total = MAX_LAYER;
|
|
|
|
if( sceneIdx < 0 )
|
|
|
|
sceneIdx += total;
|
2014-10-16 17:31:18 +08:00
|
|
|
|
2014-08-07 15:23:31 +08:00
|
|
|
auto layer = (createFunctions[sceneIdx])();
|
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
|
2015-01-09 08:30:00 +08:00
|
|
|
static Layer* restartTest()
|
2014-08-07 15:23:31 +08:00
|
|
|
{
|
|
|
|
auto layer = (createFunctions[sceneIdx])();
|
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
|
2015-01-09 08:30:00 +08:00
|
|
|
void Camera3DTestScene::runThisTest()
|
|
|
|
{
|
|
|
|
auto layer = nextTest();
|
|
|
|
addChild(layer);
|
|
|
|
Director::getInstance()->replaceScene(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// CameraBaseTest
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
|
|
|
|
void CameraBaseTest::restartCallback(Ref* sender)
|
|
|
|
{
|
|
|
|
auto s = new (std::nothrow) Camera3DTestScene();
|
|
|
|
s->addChild(restartTest());
|
|
|
|
|
|
|
|
Director::getInstance()->replaceScene(s);
|
|
|
|
s->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CameraBaseTest::nextCallback(Ref* sender)
|
|
|
|
{
|
|
|
|
auto s = new (std::nothrow) Camera3DTestScene();
|
|
|
|
s->addChild(nextTest());
|
|
|
|
Director::getInstance()->replaceScene(s);
|
|
|
|
s->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CameraBaseTest::backCallback(Ref* sender)
|
|
|
|
{
|
|
|
|
auto s = new (std::nothrow) Camera3DTestScene();
|
|
|
|
s->addChild(backTest());
|
|
|
|
Director::getInstance()->replaceScene(s);
|
|
|
|
s->release();
|
|
|
|
}
|
|
|
|
|
2014-08-07 15:23:31 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
2015-01-09 08:30:00 +08:00
|
|
|
// Camera Rotation Test
|
2014-08-07 15:23:31 +08:00
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
2015-01-09 08:30:00 +08:00
|
|
|
CameraRotationTest::CameraRotationTest()
|
|
|
|
{
|
|
|
|
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
|
|
|
|
_camControlNode = Node::create();
|
|
|
|
_camControlNode->setNormalizedPosition(Vec2(.5,.5));
|
|
|
|
addChild(_camControlNode);
|
|
|
|
|
|
|
|
_camNode = Node::create();
|
|
|
|
_camNode->setPositionZ(Camera::getDefaultCamera()->getPosition3D().z);
|
|
|
|
_camControlNode->addChild(_camNode);
|
|
|
|
|
|
|
|
//Sprites
|
|
|
|
auto sp1 = Sprite::create("Images/grossini.png");
|
|
|
|
addChild(sp1);
|
2015-01-10 06:06:21 +08:00
|
|
|
sp1->setPosition(s.width/2, s.height/2);
|
2015-01-09 08:30:00 +08:00
|
|
|
|
|
|
|
auto sp2 = Sprite::create("Images/grossinis_sister1.png");
|
2015-01-10 06:06:21 +08:00
|
|
|
// sp2->setPosition(40, 40);
|
2015-01-09 08:30:00 +08:00
|
|
|
sp1->addChild(sp2);
|
|
|
|
|
|
|
|
//Billboards
|
|
|
|
auto bill1 = BillBoard::create("Images/Icon.png");
|
2015-01-10 06:06:21 +08:00
|
|
|
bill1->setPosition3D(Vec3(s.width/2 + 30, s.height/2 + 10, -10));
|
2015-01-09 08:30:00 +08:00
|
|
|
bill1->setColor(Color3B::RED);
|
|
|
|
bill1->setScale(0.6);
|
2015-01-10 06:06:21 +08:00
|
|
|
// addChild(bill1);
|
|
|
|
|
|
|
|
auto sp3 = Sprite::create("Images/grossinis_sister2.png");
|
|
|
|
sp3->setPosition(10, 10);
|
|
|
|
bill1->addChild(sp3);
|
2015-01-09 08:30:00 +08:00
|
|
|
|
|
|
|
auto bill2 = BillBoard::create("Images/Icon.png");
|
2015-01-10 06:06:21 +08:00
|
|
|
bill2->setPosition3D(Vec3(s.width/2 - 30, s.height/2 - 10, -10));
|
2015-01-09 08:30:00 +08:00
|
|
|
bill2->setScale(0.6);
|
2015-01-10 06:06:21 +08:00
|
|
|
// addChild(bill2);
|
2015-01-09 08:30:00 +08:00
|
|
|
|
|
|
|
//3D models
|
|
|
|
auto model = Sprite3D::create("Sprite3DTest/boss1.obj");
|
|
|
|
model->setScale(4);
|
|
|
|
model->setTexture("Sprite3DTest/boss.png");
|
|
|
|
model->setPosition3D(Vec3(s.width/2, s.height/2, 0));
|
|
|
|
addChild(model);
|
|
|
|
|
|
|
|
//Listener
|
|
|
|
_lis = EventListenerTouchOneByOne::create();
|
|
|
|
_lis->onTouchBegan = [this](Touch* t, Event* e) {
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
_lis->onTouchMoved = [this](Touch* t, Event* e) {
|
|
|
|
float dx = t->getDelta().x;
|
|
|
|
Vec3 rot = _camControlNode->getRotation3D();
|
|
|
|
rot.y += dx;
|
|
|
|
_camControlNode->setRotation3D(rot);
|
|
|
|
|
|
|
|
Vec3 worldPos;
|
|
|
|
_camNode->getNodeToWorldTransform().getTranslation(&worldPos);
|
|
|
|
|
|
|
|
Camera::getDefaultCamera()->setPosition3D(worldPos);
|
|
|
|
Camera::getDefaultCamera()->lookAt(_camControlNode->getPosition3D());
|
|
|
|
};
|
|
|
|
|
|
|
|
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(_lis, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
CameraRotationTest::~CameraRotationTest()
|
|
|
|
{
|
|
|
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_lis);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CameraRotationTest::title() const
|
|
|
|
{
|
|
|
|
return "Camera Rotation Test";
|
|
|
|
}
|
|
|
|
|
|
|
|
void CameraRotationTest::onEnter()
|
|
|
|
{
|
|
|
|
BaseTest::onEnter();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CameraRotationTest::onExit()
|
|
|
|
{
|
|
|
|
BaseTest::onExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CameraRotationTest::update(float dt)
|
|
|
|
{
|
|
|
|
}
|
2014-08-07 15:23:31 +08:00
|
|
|
|
2015-01-09 08:30:00 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Camera3DTestDemo
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
2014-08-07 15:23:31 +08:00
|
|
|
Camera3DTestDemo::Camera3DTestDemo(void)
|
2015-01-09 08:30:00 +08:00
|
|
|
: _incRot(nullptr)
|
2014-11-26 09:53:52 +08:00
|
|
|
, _camera(nullptr)
|
2014-08-11 10:55:14 +08:00
|
|
|
, _decRot(nullptr)
|
2014-11-26 09:53:52 +08:00
|
|
|
, _bZoomOut(false)
|
|
|
|
, _bZoomIn(false)
|
|
|
|
, _bRotateLeft(false)
|
|
|
|
, _bRotateRight(false)
|
2014-08-07 15:23:31 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
Camera3DTestDemo::~Camera3DTestDemo(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
void Camera3DTestDemo::reachEndCallBack()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
std::string Camera3DTestDemo::title() const
|
|
|
|
{
|
|
|
|
return "Testing Camera";
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera3DTestDemo::scaleCameraCallback(Ref* sender,float value)
|
|
|
|
{
|
2015-01-09 08:30:00 +08:00
|
|
|
if(_camera&& _cameraType!=CameraType::FirstPerson)
|
2014-08-07 15:23:31 +08:00
|
|
|
{
|
|
|
|
Vec3 cameraPos= _camera->getPosition3D();
|
|
|
|
cameraPos+= cameraPos.getNormalized()*value;
|
|
|
|
_camera->setPosition3D(cameraPos);
|
|
|
|
}
|
2014-10-16 17:31:18 +08:00
|
|
|
}
|
2014-08-07 15:23:31 +08:00
|
|
|
void Camera3DTestDemo::rotateCameraCallback(Ref* sender,float value)
|
|
|
|
{
|
2015-01-09 08:30:00 +08:00
|
|
|
if(_cameraType==CameraType::Free || _cameraType==CameraType::FirstPerson)
|
2014-08-07 15:23:31 +08:00
|
|
|
{
|
|
|
|
Vec3 rotation3D= _camera->getRotation3D();
|
|
|
|
rotation3D.y+= value;
|
|
|
|
_camera->setRotation3D(rotation3D);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void Camera3DTestDemo::SwitchViewCallback(Ref* sender, CameraType cameraType)
|
|
|
|
{
|
|
|
|
if(_cameraType==cameraType)
|
|
|
|
{
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
_cameraType = cameraType;
|
2015-01-09 08:30:00 +08:00
|
|
|
if(_cameraType==CameraType::Free)
|
2014-08-07 15:23:31 +08:00
|
|
|
{
|
2014-10-16 17:31:18 +08:00
|
|
|
_camera->setPosition3D(Vec3(0, 130, 130) + _sprite3D->getPosition3D());
|
2015-01-09 08:30:00 +08:00
|
|
|
_camera->lookAt(_sprite3D->getPosition3D());
|
2014-10-16 17:31:18 +08:00
|
|
|
|
|
|
|
_RotateRightlabel->setColor(Color3B::WHITE);
|
|
|
|
_RotateLeftlabel->setColor(Color3B::WHITE);
|
|
|
|
_ZoomInlabel->setColor(Color3B::WHITE);
|
|
|
|
_ZoomOutlabel->setColor(Color3B::WHITE);
|
2014-08-07 15:23:31 +08:00
|
|
|
}
|
2015-01-09 08:30:00 +08:00
|
|
|
else if(_cameraType==CameraType::FirstPerson)
|
2014-08-07 15:23:31 +08:00
|
|
|
{
|
2014-08-11 10:55:14 +08:00
|
|
|
Vec3 newFaceDir;
|
|
|
|
_sprite3D->getWorldToNodeTransform().getForwardVector(&newFaceDir);
|
|
|
|
newFaceDir.normalize();
|
|
|
|
_camera->setPosition3D(Vec3(0,35,0) + _sprite3D->getPosition3D());
|
2015-01-09 08:30:00 +08:00
|
|
|
_camera->lookAt(_sprite3D->getPosition3D() + newFaceDir*50);
|
2014-10-16 17:31:18 +08:00
|
|
|
|
|
|
|
_RotateRightlabel->setColor(Color3B::WHITE);
|
|
|
|
_RotateLeftlabel->setColor(Color3B::WHITE);
|
|
|
|
_ZoomInlabel->setColor(Color3B::GRAY);
|
|
|
|
_ZoomOutlabel->setColor(Color3B::GRAY);
|
2014-08-07 15:23:31 +08:00
|
|
|
}
|
2015-01-09 08:30:00 +08:00
|
|
|
else if(_cameraType==CameraType::ThirdPerson)
|
2014-08-07 15:23:31 +08:00
|
|
|
{
|
2014-08-11 10:55:14 +08:00
|
|
|
_camera->setPosition3D(Vec3(0, 130, 130) + _sprite3D->getPosition3D());
|
2015-01-09 08:30:00 +08:00
|
|
|
_camera->lookAt(_sprite3D->getPosition3D());
|
2014-10-16 17:31:18 +08:00
|
|
|
|
|
|
|
_RotateRightlabel->setColor(Color3B::GRAY);
|
|
|
|
_RotateLeftlabel->setColor(Color3B::GRAY);
|
|
|
|
_ZoomInlabel->setColor(Color3B::WHITE);
|
|
|
|
_ZoomOutlabel->setColor(Color3B::WHITE);
|
2014-08-07 15:23:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
void Camera3DTestDemo::onEnter()
|
|
|
|
{
|
|
|
|
BaseTest::onEnter();
|
|
|
|
_sprite3D=nullptr;
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
|
|
|
listener->onTouchesBegan = CC_CALLBACK_2(Camera3DTestDemo::onTouchesBegan, this);
|
|
|
|
listener->onTouchesMoved = CC_CALLBACK_2(Camera3DTestDemo::onTouchesMoved, this);
|
|
|
|
listener->onTouchesEnded = CC_CALLBACK_2(Camera3DTestDemo::onTouchesEnded, this);
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
auto layer3D=Layer::create();
|
|
|
|
addChild(layer3D,0);
|
|
|
|
_layer3D=layer3D;
|
|
|
|
_curState=State_None;
|
2014-10-09 17:19:43 +08:00
|
|
|
addNewSpriteWithCoords( Vec3(0,0,0),"Sprite3DTest/girl.c3b",true,0.2f,true);
|
2014-08-07 15:23:31 +08:00
|
|
|
TTFConfig ttfConfig("fonts/arial.ttf", 20);
|
2014-10-16 17:31:18 +08:00
|
|
|
|
|
|
|
auto containerForLabel1 = Node::create();
|
|
|
|
_ZoomOutlabel = Label::createWithTTF(ttfConfig,"zoom out");
|
|
|
|
_ZoomOutlabel->setPosition(s.width-50, VisibleRect::top().y-30);
|
|
|
|
containerForLabel1->addChild(_ZoomOutlabel);
|
|
|
|
addChild(containerForLabel1, 10);
|
|
|
|
|
|
|
|
auto listener1 = EventListenerTouchOneByOne::create();
|
|
|
|
listener1->setSwallowTouches(true);
|
|
|
|
|
|
|
|
listener1->onTouchBegan = CC_CALLBACK_2(Camera3DTestDemo::onTouchesZoomOut, this);
|
|
|
|
listener1->onTouchEnded = CC_CALLBACK_2(Camera3DTestDemo::onTouchesZoomOutEnd, this);
|
|
|
|
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, _ZoomOutlabel);
|
|
|
|
|
|
|
|
auto containerForLabel2 = Node::create();
|
|
|
|
_ZoomInlabel = Label::createWithTTF(ttfConfig,"zoom in");
|
|
|
|
_ZoomInlabel->setPosition(s.width-50, VisibleRect::top().y-100);
|
|
|
|
containerForLabel2->addChild(_ZoomInlabel);
|
|
|
|
addChild(containerForLabel2, 10);
|
|
|
|
|
|
|
|
auto listener2 = EventListenerTouchOneByOne::create();
|
|
|
|
listener2->setSwallowTouches(true);
|
|
|
|
|
|
|
|
listener2->onTouchBegan = CC_CALLBACK_2(Camera3DTestDemo::onTouchesZoomIn, this);
|
|
|
|
listener2->onTouchEnded = CC_CALLBACK_2(Camera3DTestDemo::onTouchesZoomInEnd, this);
|
|
|
|
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener2, _ZoomInlabel);
|
|
|
|
|
|
|
|
auto containerForLabel3 = Node::create();
|
|
|
|
_RotateLeftlabel = Label::createWithTTF(ttfConfig,"rotate left");
|
|
|
|
_RotateLeftlabel->setPosition(s.width-50, VisibleRect::top().y-170);
|
|
|
|
containerForLabel3->addChild(_RotateLeftlabel);
|
|
|
|
addChild(containerForLabel3, 10);
|
|
|
|
|
|
|
|
auto listener3 = EventListenerTouchOneByOne::create();
|
|
|
|
listener3->setSwallowTouches(true);
|
|
|
|
|
|
|
|
listener3->onTouchBegan = CC_CALLBACK_2(Camera3DTestDemo::onTouchesRotateLeft, this);
|
|
|
|
listener3->onTouchEnded = CC_CALLBACK_2(Camera3DTestDemo::onTouchesRotateLeftEnd, this);
|
|
|
|
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener3, _RotateLeftlabel);
|
|
|
|
|
|
|
|
auto containerForLabel4 = Node::create();
|
|
|
|
_RotateRightlabel = Label::createWithTTF(ttfConfig,"rotate right");
|
|
|
|
_RotateRightlabel->setPosition(s.width-50, VisibleRect::top().y-240);
|
|
|
|
containerForLabel4->addChild(_RotateRightlabel);
|
|
|
|
addChild(containerForLabel4, 10);
|
|
|
|
|
|
|
|
auto listener4 = EventListenerTouchOneByOne::create();
|
|
|
|
listener4->setSwallowTouches(true);
|
|
|
|
|
|
|
|
listener4->onTouchBegan = CC_CALLBACK_2(Camera3DTestDemo::onTouchesRotateRight, this);
|
|
|
|
listener4->onTouchEnded = CC_CALLBACK_2(Camera3DTestDemo::onTouchesRotateRightEnd, this);
|
|
|
|
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener4, _RotateRightlabel);
|
|
|
|
|
|
|
|
auto label1 = Label::createWithTTF(ttfConfig,"free ");
|
2015-01-09 08:30:00 +08:00
|
|
|
auto menuItem1 = MenuItemLabel::create(label1, CC_CALLBACK_1(Camera3DTestDemo::SwitchViewCallback,this,CameraType::Free));
|
2014-10-16 17:31:18 +08:00
|
|
|
auto label2 = Label::createWithTTF(ttfConfig,"third person");
|
2015-01-09 08:30:00 +08:00
|
|
|
auto menuItem2 = MenuItemLabel::create(label2, CC_CALLBACK_1(Camera3DTestDemo::SwitchViewCallback,this,CameraType::ThirdPerson));
|
2014-10-16 17:31:18 +08:00
|
|
|
auto label3 = Label::createWithTTF(ttfConfig,"first person");
|
2015-01-09 08:30:00 +08:00
|
|
|
auto menuItem3 = MenuItemLabel::create(label3, CC_CALLBACK_1(Camera3DTestDemo::SwitchViewCallback,this,CameraType::FirstPerson));
|
2014-10-16 17:31:18 +08:00
|
|
|
auto menu = Menu::create(menuItem1, menuItem2, menuItem3, nullptr);
|
|
|
|
|
2014-08-07 15:23:31 +08:00
|
|
|
menu->setPosition(Vec2::ZERO);
|
2014-10-16 17:31:18 +08:00
|
|
|
|
|
|
|
menuItem1->setPosition(VisibleRect::left().x+100, VisibleRect::top().y-50);
|
|
|
|
menuItem2->setPosition(VisibleRect::left().x+100, VisibleRect::top().y -100);
|
|
|
|
menuItem3->setPosition(VisibleRect::left().x+100, VisibleRect::top().y -150);
|
2014-08-07 15:23:31 +08:00
|
|
|
addChild(menu, 0);
|
2014-10-04 00:38:36 +08:00
|
|
|
schedule(CC_SCHEDULE_SELECTOR(Camera3DTestDemo::updateCamera), 0.0f);
|
2014-08-07 15:23:31 +08:00
|
|
|
if (_camera == nullptr)
|
|
|
|
{
|
|
|
|
_camera=Camera::createPerspective(60, (GLfloat)s.width/s.height, 1, 1000);
|
|
|
|
_camera->setCameraFlag(CameraFlag::USER1);
|
|
|
|
_layer3D->addChild(_camera);
|
|
|
|
}
|
2015-01-09 08:30:00 +08:00
|
|
|
SwitchViewCallback(this,CameraType::ThirdPerson);
|
2014-12-16 16:34:35 +08:00
|
|
|
DrawNode3D* line =DrawNode3D::create();
|
2014-08-07 15:23:31 +08:00
|
|
|
//draw x
|
|
|
|
for( int j =-20; j<=20 ;j++)
|
|
|
|
{
|
|
|
|
line->drawLine(Vec3(-100, 0, 5*j),Vec3(100,0,5*j),Color4F(1,0,0,1));
|
|
|
|
}
|
|
|
|
//draw z
|
|
|
|
for( int j =-20; j<=20 ;j++)
|
|
|
|
{
|
|
|
|
line->drawLine(Vec3(5*j, 0, -100),Vec3(5*j,0,100),Color4F(0,0,1,1));
|
|
|
|
}
|
|
|
|
//draw y
|
|
|
|
line->drawLine(Vec3(0, -50, 0),Vec3(0,0,0),Color4F(0,0.5,0,1));
|
|
|
|
line->drawLine(Vec3(0, 0, 0),Vec3(0,50,0),Color4F(0,1,0,1));
|
|
|
|
_layer3D->addChild(line);
|
|
|
|
_layer3D->setCameraMask(2);
|
|
|
|
}
|
|
|
|
void Camera3DTestDemo::onExit()
|
|
|
|
{
|
|
|
|
BaseTest::onExit();
|
|
|
|
if (_camera)
|
|
|
|
{
|
|
|
|
_camera = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void Camera3DTestDemo::addNewSpriteWithCoords(Vec3 p,std::string fileName,bool playAnimation,float scale,bool bindCamera)
|
|
|
|
{
|
2014-10-16 17:31:18 +08:00
|
|
|
|
2014-08-07 15:23:31 +08:00
|
|
|
auto sprite = Sprite3D::create(fileName);
|
|
|
|
_layer3D->addChild(sprite);
|
|
|
|
float globalZOrder=sprite->getGlobalZOrder();
|
|
|
|
sprite->setPosition3D( Vec3( p.x, p.y,p.z) );
|
|
|
|
sprite->setGlobalZOrder(globalZOrder);
|
|
|
|
if(playAnimation)
|
|
|
|
{
|
|
|
|
auto animation = Animation3D::create(fileName,"Take 001");
|
|
|
|
if (animation)
|
|
|
|
{
|
|
|
|
auto animate = Animate3D::create(animation);
|
|
|
|
sprite->runAction(RepeatForever::create(animate));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(bindCamera)
|
|
|
|
{
|
|
|
|
_sprite3D=sprite;
|
|
|
|
}
|
|
|
|
sprite->setScale(scale);
|
2014-10-16 17:31:18 +08:00
|
|
|
|
2014-08-07 15:23:31 +08:00
|
|
|
}
|
|
|
|
void Camera3DTestDemo::onTouchesBegan(const std::vector<Touch*>& touches, cocos2d::Event *event)
|
|
|
|
{
|
|
|
|
for ( auto &item: touches )
|
|
|
|
{
|
|
|
|
auto touch = item;
|
|
|
|
auto location = touch->getLocation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void Camera3DTestDemo::onTouchesMoved(const std::vector<Touch*>& touches, cocos2d::Event *event)
|
|
|
|
{
|
|
|
|
if(touches.size()==1)
|
|
|
|
{
|
|
|
|
auto touch = touches[0];
|
|
|
|
auto location = touch->getLocation();
|
|
|
|
Point newPos = touch->getPreviousLocation()-location;
|
2015-01-09 08:30:00 +08:00
|
|
|
if(_cameraType==CameraType::Free || _cameraType==CameraType::FirstPerson)
|
2014-08-07 15:23:31 +08:00
|
|
|
{
|
2014-10-16 17:31:18 +08:00
|
|
|
Vec3 cameraDir;
|
|
|
|
Vec3 cameraRightDir;
|
|
|
|
_camera->getNodeToWorldTransform().getForwardVector(&cameraDir);
|
|
|
|
cameraDir.normalize();
|
|
|
|
cameraDir.y=0;
|
|
|
|
_camera->getNodeToWorldTransform().getRightVector(&cameraRightDir);
|
|
|
|
cameraRightDir.normalize();
|
|
|
|
cameraRightDir.y=0;
|
|
|
|
Vec3 cameraPos= _camera->getPosition3D();
|
|
|
|
cameraPos+=cameraDir*newPos.y*0.1f;
|
|
|
|
cameraPos+=cameraRightDir*newPos.x*0.1f;
|
2014-08-07 15:23:31 +08:00
|
|
|
_camera->setPosition3D(cameraPos);
|
2015-01-09 08:30:00 +08:00
|
|
|
if(_sprite3D && _cameraType==CameraType::FirstPerson)
|
2014-08-07 15:23:31 +08:00
|
|
|
{
|
|
|
|
_sprite3D->setPosition3D(Vec3(_camera->getPositionX(),0,_camera->getPositionZ()));
|
|
|
|
_targetPos=_sprite3D->getPosition3D();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void Camera3DTestDemo::move3D(float elapsedTime)
|
|
|
|
{
|
|
|
|
if(_sprite3D)
|
|
|
|
{
|
|
|
|
Vec3 curPos= _sprite3D->getPosition3D();
|
|
|
|
Vec3 newFaceDir = _targetPos - curPos;
|
|
|
|
newFaceDir.y = 0.0f;
|
|
|
|
newFaceDir.normalize();
|
|
|
|
Vec3 offset = newFaceDir * 25.0f * elapsedTime;
|
|
|
|
curPos+=offset;
|
|
|
|
_sprite3D->setPosition3D(curPos);
|
|
|
|
offset.x=offset.x;
|
|
|
|
offset.z=offset.z;
|
2015-01-09 08:30:00 +08:00
|
|
|
if(_cameraType==CameraType::ThirdPerson)
|
2014-08-07 15:23:31 +08:00
|
|
|
{
|
|
|
|
Vec3 cameraPos= _camera->getPosition3D();
|
|
|
|
cameraPos.x+=offset.x;
|
|
|
|
cameraPos.z+=offset.z;
|
|
|
|
_camera->setPosition3D(cameraPos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void Camera3DTestDemo::updateState(float elapsedTime)
|
|
|
|
{
|
|
|
|
if(_sprite3D)
|
|
|
|
{
|
|
|
|
Vec3 curPos= _sprite3D->getPosition3D();
|
|
|
|
Vec3 curFaceDir;
|
|
|
|
_sprite3D->getNodeToWorldTransform().getForwardVector(&curFaceDir);
|
|
|
|
curFaceDir=-curFaceDir;
|
|
|
|
curFaceDir.normalize();
|
|
|
|
Vec3 newFaceDir = _targetPos - curPos;
|
|
|
|
newFaceDir.y = 0.0f;
|
|
|
|
newFaceDir.normalize();
|
|
|
|
float cosAngle = std::fabs(Vec3::dot(curFaceDir,newFaceDir) - 1.0f);
|
|
|
|
float dist = curPos.distanceSquared(_targetPos);
|
|
|
|
if(dist<=4.0f)
|
|
|
|
{
|
|
|
|
if(cosAngle<=0.01f)
|
|
|
|
_curState = State_Idle;
|
|
|
|
else
|
|
|
|
_curState = State_Rotate;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(cosAngle>0.01f)
|
|
|
|
_curState = State_Rotate | State_Move;
|
|
|
|
else
|
|
|
|
_curState = State_Move;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void Camera3DTestDemo::onTouchesEnded(const std::vector<Touch*>& touches, cocos2d::Event *event)
|
|
|
|
{
|
|
|
|
for ( auto &item: touches )
|
|
|
|
{
|
|
|
|
auto touch = item;
|
|
|
|
auto location = touch->getLocationInView();
|
|
|
|
if(_camera)
|
|
|
|
{
|
2015-01-09 08:30:00 +08:00
|
|
|
if(_sprite3D && _cameraType==CameraType::ThirdPerson && _bZoomOut == false && _bZoomIn == false && _bRotateLeft == false && _bRotateRight == false)
|
2014-08-07 15:23:31 +08:00
|
|
|
{
|
|
|
|
Vec3 nearP(location.x, location.y, -1.0f), farP(location.x, location.y, 1.0f);
|
|
|
|
|
|
|
|
auto size = Director::getInstance()->getWinSize();
|
|
|
|
_camera->unproject(size, &nearP, &nearP);
|
|
|
|
_camera->unproject(size, &farP, &farP);
|
|
|
|
Vec3 dir(farP - nearP);
|
|
|
|
float dist=0.0f;
|
|
|
|
float ndd = Vec3::dot(Vec3(0,1,0),dir);
|
|
|
|
if(ndd == 0)
|
|
|
|
dist=0.0f;
|
|
|
|
float ndo = Vec3::dot(Vec3(0,1,0),nearP);
|
|
|
|
dist= (0 - ndo) / ndd;
|
|
|
|
Vec3 p = nearP + dist * dir;
|
2014-10-16 17:31:18 +08:00
|
|
|
|
|
|
|
if( p.x > 100)
|
|
|
|
p.x = 100;
|
|
|
|
if( p.x < -100)
|
|
|
|
p.x = -100;
|
|
|
|
if( p.z > 100)
|
|
|
|
p.z = 100;
|
|
|
|
if( p.z < -100)
|
|
|
|
p.z = -100;
|
|
|
|
|
2014-08-07 15:23:31 +08:00
|
|
|
_targetPos=p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void onTouchesCancelled(const std::vector<Touch*>& touches, cocos2d::Event *event)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
void Camera3DTestDemo::updateCamera(float fDelta)
|
|
|
|
{
|
|
|
|
if(_sprite3D)
|
|
|
|
{
|
2015-01-09 08:30:00 +08:00
|
|
|
if( _cameraType==CameraType::ThirdPerson)
|
2014-08-07 15:23:31 +08:00
|
|
|
{
|
|
|
|
updateState(fDelta);
|
|
|
|
if(isState(_curState,State_Move))
|
|
|
|
{
|
|
|
|
move3D(fDelta);
|
|
|
|
if(isState(_curState,State_Rotate))
|
|
|
|
{
|
|
|
|
Vec3 curPos = _sprite3D->getPosition3D();
|
2014-10-16 17:31:18 +08:00
|
|
|
|
2014-08-07 15:23:31 +08:00
|
|
|
Vec3 newFaceDir = _targetPos - curPos;
|
|
|
|
newFaceDir.y = 0;
|
|
|
|
newFaceDir.normalize();
|
|
|
|
Vec3 up;
|
|
|
|
_sprite3D->getNodeToWorldTransform().getUpVector(&up);
|
|
|
|
up.normalize();
|
|
|
|
Vec3 right;
|
|
|
|
Vec3::cross(-newFaceDir,up,&right);
|
|
|
|
right.normalize();
|
|
|
|
Vec3 pos = Vec3(0,0,0);
|
|
|
|
Mat4 mat;
|
|
|
|
mat.m[0] = right.x;
|
|
|
|
mat.m[1] = right.y;
|
|
|
|
mat.m[2] = right.z;
|
|
|
|
mat.m[3] = 0.0f;
|
2014-10-16 17:31:18 +08:00
|
|
|
|
2014-08-07 15:23:31 +08:00
|
|
|
mat.m[4] = up.x;
|
|
|
|
mat.m[5] = up.y;
|
|
|
|
mat.m[6] = up.z;
|
|
|
|
mat.m[7] = 0.0f;
|
2014-10-16 17:31:18 +08:00
|
|
|
|
2014-08-07 15:23:31 +08:00
|
|
|
mat.m[8] = newFaceDir.x;
|
|
|
|
mat.m[9] = newFaceDir.y;
|
|
|
|
mat.m[10] = newFaceDir.z;
|
|
|
|
mat.m[11] = 0.0f;
|
2014-10-16 17:31:18 +08:00
|
|
|
|
2014-08-07 15:23:31 +08:00
|
|
|
mat.m[12] = pos.x;
|
|
|
|
mat.m[13] = pos.y;
|
|
|
|
mat.m[14] = pos.z;
|
|
|
|
mat.m[15] = 1.0f;
|
|
|
|
_sprite3D->setAdditionalTransform(&mat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-16 17:31:18 +08:00
|
|
|
if(_bZoomOut == true)
|
|
|
|
{
|
|
|
|
if(_camera)
|
|
|
|
{
|
2015-01-09 08:30:00 +08:00
|
|
|
if(_cameraType == CameraType::ThirdPerson)
|
2014-10-16 17:31:18 +08:00
|
|
|
{
|
|
|
|
Vec3 lookDir = _camera->getPosition3D() - _sprite3D->getPosition3D();
|
|
|
|
Vec3 cameraPos = _camera->getPosition3D();
|
|
|
|
if(lookDir.length() <= 300)
|
|
|
|
{
|
|
|
|
cameraPos += lookDir.getNormalized();
|
|
|
|
_camera->setPosition3D(cameraPos);
|
|
|
|
}
|
|
|
|
}
|
2015-01-09 08:30:00 +08:00
|
|
|
else if(_cameraType == CameraType::Free)
|
2014-10-16 17:31:18 +08:00
|
|
|
{
|
|
|
|
Vec3 cameraPos = _camera->getPosition3D();
|
|
|
|
if(cameraPos.length() <= 300)
|
|
|
|
{
|
|
|
|
cameraPos += cameraPos.getNormalized();
|
|
|
|
_camera->setPosition3D(cameraPos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(_bZoomIn == true)
|
|
|
|
{
|
|
|
|
if(_camera)
|
|
|
|
{
|
2015-01-09 08:30:00 +08:00
|
|
|
if(_cameraType == CameraType::ThirdPerson)
|
2014-10-16 17:31:18 +08:00
|
|
|
{
|
|
|
|
Vec3 lookDir = _camera->getPosition3D() - _sprite3D->getPosition3D();
|
|
|
|
Vec3 cameraPos = _camera->getPosition3D();
|
|
|
|
if(lookDir.length() >= 50)
|
|
|
|
{
|
|
|
|
cameraPos -= lookDir.getNormalized();
|
|
|
|
_camera->setPosition3D(cameraPos);
|
|
|
|
}
|
|
|
|
}
|
2015-01-09 08:30:00 +08:00
|
|
|
else if(_cameraType == CameraType::Free)
|
2014-10-16 17:31:18 +08:00
|
|
|
{
|
|
|
|
Vec3 cameraPos = _camera->getPosition3D();
|
|
|
|
if(cameraPos.length() >= 50)
|
|
|
|
{
|
|
|
|
cameraPos -= cameraPos.getNormalized();
|
|
|
|
_camera->setPosition3D(cameraPos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(_bRotateLeft == true)
|
|
|
|
{
|
2015-01-09 08:30:00 +08:00
|
|
|
if(_cameraType==CameraType::Free || _cameraType==CameraType::FirstPerson)
|
2014-10-16 17:31:18 +08:00
|
|
|
{
|
|
|
|
Vec3 rotation3D= _camera->getRotation3D();
|
|
|
|
rotation3D.y+= 1;
|
|
|
|
_camera->setRotation3D(rotation3D);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(_bRotateRight == true)
|
|
|
|
{
|
2015-01-09 08:30:00 +08:00
|
|
|
if(_cameraType==CameraType::Free || _cameraType==CameraType::FirstPerson)
|
2014-10-16 17:31:18 +08:00
|
|
|
{
|
|
|
|
Vec3 rotation3D= _camera->getRotation3D();
|
|
|
|
rotation3D.y-= 1;
|
|
|
|
_camera->setRotation3D(rotation3D);
|
|
|
|
}
|
|
|
|
}
|
2014-08-07 15:23:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
bool Camera3DTestDemo::isState(unsigned int state,unsigned int bit) const
|
|
|
|
{
|
|
|
|
return (state & bit) == bit;
|
|
|
|
}
|
2014-10-16 17:31:18 +08:00
|
|
|
bool Camera3DTestDemo::onTouchesZoomOut(Touch* touch, Event* event)
|
|
|
|
{
|
|
|
|
auto target = static_cast<Label*>(event->getCurrentTarget());
|
|
|
|
|
|
|
|
Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation());
|
|
|
|
Size s = target->getContentSize();
|
|
|
|
Rect rect = Rect(0, 0, s.width, s.height);
|
|
|
|
|
|
|
|
if (rect.containsPoint(locationInNode))
|
|
|
|
{
|
|
|
|
_bZoomOut = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
void Camera3DTestDemo::onTouchesZoomOutEnd(Touch* touch, Event* event)
|
|
|
|
{
|
|
|
|
_bZoomOut = false;
|
|
|
|
}
|
|
|
|
bool Camera3DTestDemo::onTouchesZoomIn(Touch* touch, Event* event)
|
|
|
|
{
|
|
|
|
auto target = static_cast<Label*>(event->getCurrentTarget());
|
|
|
|
|
|
|
|
Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation());
|
|
|
|
Size s = target->getContentSize();
|
|
|
|
Rect rect = Rect(0, 0, s.width, s.height);
|
|
|
|
|
|
|
|
if (rect.containsPoint(locationInNode))
|
|
|
|
{
|
|
|
|
_bZoomIn = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
void Camera3DTestDemo::onTouchesZoomInEnd(Touch* touch, Event* event)
|
|
|
|
{
|
|
|
|
_bZoomIn = false;
|
|
|
|
}
|
|
|
|
bool Camera3DTestDemo::onTouchesRotateLeft(Touch* touch, Event* event)
|
|
|
|
{
|
|
|
|
auto target = static_cast<Label*>(event->getCurrentTarget());
|
|
|
|
|
|
|
|
Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation());
|
|
|
|
Size s = target->getContentSize();
|
|
|
|
Rect rect = Rect(0, 0, s.width, s.height);
|
|
|
|
|
|
|
|
if (rect.containsPoint(locationInNode))
|
|
|
|
{
|
|
|
|
_bRotateLeft = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
void Camera3DTestDemo::onTouchesRotateLeftEnd(Touch* touch, Event* event)
|
|
|
|
{
|
|
|
|
_bRotateLeft = false;
|
|
|
|
}
|
|
|
|
bool Camera3DTestDemo::onTouchesRotateRight(Touch* touch, Event* event)
|
|
|
|
{
|
|
|
|
auto target = static_cast<Label*>(event->getCurrentTarget());
|
|
|
|
|
|
|
|
Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation());
|
|
|
|
Size s = target->getContentSize();
|
|
|
|
Rect rect = Rect(0, 0, s.width, s.height);
|
|
|
|
|
|
|
|
if (rect.containsPoint(locationInNode))
|
|
|
|
{
|
|
|
|
_bRotateRight = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
void Camera3DTestDemo::onTouchesRotateRightEnd(Touch* touch, Event* event)
|
|
|
|
{
|
|
|
|
_bRotateRight = false;
|
|
|
|
}
|
2014-12-16 16:34:35 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
2015-01-09 08:30:00 +08:00
|
|
|
// CameraCullingDemo
|
|
|
|
CameraCullingDemo::CameraCullingDemo(void)
|
|
|
|
: _layer3D(nullptr)
|
|
|
|
, _cameraType(CameraType::FirstPerson)
|
2014-12-16 16:34:35 +08:00
|
|
|
, _cameraFirst(nullptr)
|
2014-12-18 17:18:22 +08:00
|
|
|
, _cameraThird(nullptr)
|
|
|
|
, _moveAction(nullptr)
|
2014-12-16 16:34:35 +08:00
|
|
|
, _drawAABB(nullptr)
|
2014-12-18 17:18:22 +08:00
|
|
|
, _drawFrustum(nullptr)
|
2014-12-22 17:48:34 +08:00
|
|
|
, _row(3)
|
2014-12-16 16:34:35 +08:00
|
|
|
{
|
|
|
|
}
|
2015-01-09 08:30:00 +08:00
|
|
|
CameraCullingDemo::~CameraCullingDemo(void)
|
2014-12-16 16:34:35 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-01-09 08:30:00 +08:00
|
|
|
std::string CameraCullingDemo::title() const
|
2014-12-16 16:34:35 +08:00
|
|
|
{
|
2014-12-18 17:18:22 +08:00
|
|
|
return "Camera Frustum Clipping";
|
2014-12-16 16:34:35 +08:00
|
|
|
}
|
|
|
|
|
2015-01-09 08:30:00 +08:00
|
|
|
void CameraCullingDemo::onEnter()
|
2014-12-16 16:34:35 +08:00
|
|
|
{
|
|
|
|
BaseTest::onEnter();
|
2015-01-09 08:30:00 +08:00
|
|
|
schedule(schedule_selector(CameraCullingDemo::update), 0.0f);
|
2014-12-18 17:18:22 +08:00
|
|
|
|
2014-12-16 16:34:35 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
2015-01-09 08:30:00 +08:00
|
|
|
listener->onTouchesBegan = CC_CALLBACK_2(CameraCullingDemo::onTouchesBegan, this);
|
|
|
|
listener->onTouchesMoved = CC_CALLBACK_2(CameraCullingDemo::onTouchesMoved, this);
|
|
|
|
listener->onTouchesEnded = CC_CALLBACK_2(CameraCullingDemo::onTouchesEnded, this);
|
2014-12-16 16:34:35 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
2014-12-18 17:18:22 +08:00
|
|
|
auto layer3D=Layer::create();
|
|
|
|
addChild(layer3D,0);
|
|
|
|
_layer3D=layer3D;
|
2014-12-16 16:34:35 +08:00
|
|
|
|
2014-12-18 17:18:22 +08:00
|
|
|
// swich camera
|
|
|
|
MenuItemFont::setFontName("fonts/arial.ttf");
|
|
|
|
MenuItemFont::setFontSize(20);
|
2014-12-16 16:34:35 +08:00
|
|
|
|
2015-01-09 08:30:00 +08:00
|
|
|
auto menuItem1 = MenuItemFont::create("Switch Camera", CC_CALLBACK_1(CameraCullingDemo::switchViewCallback,this));
|
2014-12-18 17:18:22 +08:00
|
|
|
menuItem1->setColor(Color3B(0,200,20));
|
|
|
|
auto menu = Menu::create(menuItem1,NULL);
|
2014-12-16 16:34:35 +08:00
|
|
|
menu->setPosition(Vec2::ZERO);
|
2014-12-18 17:18:22 +08:00
|
|
|
menuItem1->setPosition(VisibleRect::left().x + 80, VisibleRect::top().y -70);
|
|
|
|
addChild(menu, 1);
|
|
|
|
|
|
|
|
// + -
|
|
|
|
MenuItemFont::setFontSize(40);
|
2015-01-09 08:30:00 +08:00
|
|
|
auto decrease = MenuItemFont::create(" - ", CC_CALLBACK_1(CameraCullingDemo::delSpriteCallback, this));
|
2014-12-18 17:18:22 +08:00
|
|
|
decrease->setColor(Color3B(0,200,20));
|
2015-01-09 08:30:00 +08:00
|
|
|
auto increase = MenuItemFont::create(" + ", CC_CALLBACK_1(CameraCullingDemo::addSpriteCallback, this));
|
2014-12-18 17:18:22 +08:00
|
|
|
increase->setColor(Color3B(0,200,20));
|
|
|
|
|
|
|
|
menu = Menu::create(decrease, increase, nullptr);
|
|
|
|
menu->alignItemsHorizontally();
|
|
|
|
menu->setPosition(Vec2(s.width - 60, VisibleRect::top().y -70));
|
|
|
|
addChild(menu, 1);
|
|
|
|
|
|
|
|
TTFConfig ttfCount("fonts/Marker Felt.ttf", 30);
|
|
|
|
_labelSprite3DCount = Label::createWithTTF(ttfCount,"0 sprits");
|
|
|
|
_labelSprite3DCount->setColor(Color3B(0,200,20));
|
|
|
|
_labelSprite3DCount->setPosition(Vec2(s.width/2, VisibleRect::top().y -70));
|
|
|
|
addChild(_labelSprite3DCount);
|
|
|
|
|
|
|
|
// aabb drawNode3D
|
|
|
|
_drawAABB = DrawNode3D::create();
|
|
|
|
_drawAABB->setCameraMask((unsigned short) CameraFlag::USER1);
|
|
|
|
addChild(_drawAABB);
|
2014-12-16 16:34:35 +08:00
|
|
|
|
2014-12-18 17:18:22 +08:00
|
|
|
// frustum drawNode3D
|
|
|
|
_drawFrustum = DrawNode3D::create();
|
|
|
|
_drawFrustum->setCameraMask((unsigned short) CameraFlag::USER1);
|
|
|
|
addChild(_drawFrustum);
|
2014-12-16 16:34:35 +08:00
|
|
|
|
2014-12-18 17:18:22 +08:00
|
|
|
// set camera
|
|
|
|
switchViewCallback(this);
|
2014-12-16 16:34:35 +08:00
|
|
|
|
2014-12-18 17:18:22 +08:00
|
|
|
// add sprite
|
|
|
|
addSpriteCallback(nullptr);
|
|
|
|
}
|
|
|
|
|
2015-01-09 08:30:00 +08:00
|
|
|
void CameraCullingDemo::onExit()
|
2014-12-18 17:18:22 +08:00
|
|
|
{
|
|
|
|
BaseTest::onExit();
|
|
|
|
if (_cameraFirst)
|
2014-12-16 16:34:35 +08:00
|
|
|
{
|
2014-12-18 17:18:22 +08:00
|
|
|
_cameraFirst = nullptr;
|
2014-12-16 16:34:35 +08:00
|
|
|
}
|
2014-12-18 17:18:22 +08:00
|
|
|
if (_cameraThird)
|
2014-12-16 16:34:35 +08:00
|
|
|
{
|
2014-12-18 17:18:22 +08:00
|
|
|
_cameraThird = nullptr;
|
2014-12-16 16:34:35 +08:00
|
|
|
}
|
2014-12-18 17:18:22 +08:00
|
|
|
}
|
|
|
|
|
2015-01-09 08:30:00 +08:00
|
|
|
void CameraCullingDemo::update(float dt)
|
2014-12-18 17:18:22 +08:00
|
|
|
{
|
|
|
|
_drawAABB->clear();
|
2014-12-16 16:34:35 +08:00
|
|
|
|
2015-01-09 08:30:00 +08:00
|
|
|
if(_cameraType == CameraType::ThirdPerson)
|
2014-12-18 17:18:22 +08:00
|
|
|
drawCameraFrustum();
|
2014-12-16 16:34:35 +08:00
|
|
|
|
2014-12-18 17:18:22 +08:00
|
|
|
Vector<Node*>& children = _layer3D->getChildren();
|
|
|
|
Vec3 corners[8];
|
2014-12-16 16:34:35 +08:00
|
|
|
|
2014-12-18 17:18:22 +08:00
|
|
|
for (const auto& iter: children)
|
|
|
|
{
|
|
|
|
const AABB& aabb = static_cast<Sprite3D*>(iter)->getAABB();
|
2014-12-26 13:37:02 +08:00
|
|
|
if (_cameraFirst->isVisibleInFrustum(&aabb))
|
2014-12-18 17:18:22 +08:00
|
|
|
{
|
|
|
|
aabb.getCorners(corners);
|
|
|
|
_drawAABB->drawCube(corners, Color4F(0, 1, 0, 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-09 08:30:00 +08:00
|
|
|
void CameraCullingDemo::reachEndCallBack()
|
2014-12-18 17:18:22 +08:00
|
|
|
{
|
|
|
|
_cameraFirst->stopActionByTag(100);
|
|
|
|
auto inverse = (MoveTo*)_moveAction->reverse();
|
|
|
|
inverse->retain();
|
|
|
|
_moveAction->release();
|
|
|
|
_moveAction = inverse;
|
|
|
|
auto rot = RotateBy::create(1.f, Vec3(0.f, 180.f, 0.f));
|
2015-01-09 08:30:00 +08:00
|
|
|
auto seq = Sequence::create(rot, _moveAction, CallFunc::create(CC_CALLBACK_0(CameraCullingDemo::reachEndCallBack, this)), nullptr);
|
2014-12-18 17:18:22 +08:00
|
|
|
seq->setTag(100);
|
|
|
|
_cameraFirst->runAction(seq);
|
|
|
|
}
|
|
|
|
|
2015-01-09 08:30:00 +08:00
|
|
|
void CameraCullingDemo::switchViewCallback(Ref* sender)
|
2014-12-18 17:18:22 +08:00
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2014-12-16 16:34:35 +08:00
|
|
|
|
2014-12-18 17:18:22 +08:00
|
|
|
if (_cameraFirst == nullptr)
|
2014-12-16 16:34:35 +08:00
|
|
|
{
|
2014-12-18 17:18:22 +08:00
|
|
|
_cameraFirst = Camera::createPerspective(30, (GLfloat)s.width/s.height, 10, 200);
|
|
|
|
_cameraFirst->setCameraFlag(CameraFlag::USER8);
|
|
|
|
_cameraFirst->setPosition3D(Vec3(-100,0,0));
|
2015-01-09 08:30:00 +08:00
|
|
|
_cameraFirst->lookAt(Vec3(1000,0,0));
|
2014-12-18 17:18:22 +08:00
|
|
|
_moveAction = MoveTo::create(4.f, Vec2(100, 0));
|
|
|
|
_moveAction->retain();
|
2015-01-09 08:30:00 +08:00
|
|
|
auto seq = Sequence::create(_moveAction, CallFunc::create(CC_CALLBACK_0(CameraCullingDemo::reachEndCallBack, this)), nullptr);
|
2014-12-18 17:18:22 +08:00
|
|
|
seq->setTag(100);
|
|
|
|
_cameraFirst->runAction(seq);
|
|
|
|
addChild(_cameraFirst);
|
2014-12-16 16:34:35 +08:00
|
|
|
}
|
|
|
|
|
2014-12-18 17:18:22 +08:00
|
|
|
if (_cameraThird == nullptr)
|
|
|
|
{
|
|
|
|
_cameraThird = Camera::createPerspective(60, (GLfloat)s.width/s.height, 1, 1000);
|
|
|
|
_cameraThird->setCameraFlag(CameraFlag::USER8);
|
|
|
|
_cameraThird->setPosition3D(Vec3(0, 130, 130));
|
2015-01-09 08:30:00 +08:00
|
|
|
_cameraThird->lookAt(Vec3(0,0,0));
|
2014-12-18 17:18:22 +08:00
|
|
|
addChild(_cameraThird);
|
2014-12-16 16:34:35 +08:00
|
|
|
}
|
|
|
|
|
2015-01-09 08:30:00 +08:00
|
|
|
if(_cameraType == CameraType::FirstPerson)
|
2014-12-16 16:34:35 +08:00
|
|
|
{
|
2015-01-09 08:30:00 +08:00
|
|
|
_cameraType = CameraType::ThirdPerson;
|
2014-12-18 17:18:22 +08:00
|
|
|
_cameraThird->setCameraFlag(CameraFlag::USER1);
|
2014-12-22 18:13:07 +08:00
|
|
|
_cameraThird->enableFrustumCulling(false, false);
|
2014-12-18 17:18:22 +08:00
|
|
|
_cameraFirst->setCameraFlag(CameraFlag::USER8);
|
|
|
|
}
|
2015-01-09 08:30:00 +08:00
|
|
|
else if(_cameraType == CameraType::ThirdPerson)
|
2014-12-18 17:18:22 +08:00
|
|
|
{
|
2015-01-09 08:30:00 +08:00
|
|
|
_cameraType = CameraType::FirstPerson;
|
2014-12-18 17:18:22 +08:00
|
|
|
_cameraFirst->setCameraFlag(CameraFlag::USER1);
|
2014-12-22 18:13:07 +08:00
|
|
|
_cameraFirst->enableFrustumCulling(true, true);
|
2014-12-18 17:18:22 +08:00
|
|
|
_cameraThird->setCameraFlag(CameraFlag::USER8);
|
|
|
|
_drawFrustum->clear();
|
2014-12-16 16:34:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-09 08:30:00 +08:00
|
|
|
void CameraCullingDemo::addSpriteCallback(Ref* sender)
|
2014-12-16 16:34:35 +08:00
|
|
|
{
|
2014-12-18 17:18:22 +08:00
|
|
|
_layer3D->removeAllChildren();
|
|
|
|
_objects.clear();
|
|
|
|
_drawAABB->clear();
|
2014-12-16 16:34:35 +08:00
|
|
|
|
2014-12-18 17:18:22 +08:00
|
|
|
++_row;
|
|
|
|
for (int x = -_row; x < _row; x++)
|
|
|
|
{
|
|
|
|
for (int z = -_row; z < _row; z++)
|
|
|
|
{
|
|
|
|
auto sprite = Sprite3D::create("Sprite3DTest/orc.c3b");
|
|
|
|
sprite->setPosition3D(Vec3(x * 30, 0, z * 30));
|
|
|
|
sprite->setRotation3D(Vec3(0,180,0));
|
|
|
|
_objects.push_back(sprite);
|
|
|
|
_layer3D->addChild(sprite);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set layer mask.
|
|
|
|
_layer3D->setCameraMask( (unsigned short) CameraFlag::USER1);
|
|
|
|
|
|
|
|
// update sprite number
|
|
|
|
char szText[16];
|
|
|
|
sprintf(szText,"%ld sprits",_layer3D->getChildrenCount());
|
|
|
|
_labelSprite3DCount->setString(szText);
|
2014-12-16 16:34:35 +08:00
|
|
|
}
|
|
|
|
|
2015-01-09 08:30:00 +08:00
|
|
|
void CameraCullingDemo::delSpriteCallback(Ref* sender)
|
2014-12-16 16:34:35 +08:00
|
|
|
{
|
2014-12-18 17:18:22 +08:00
|
|
|
if (_row == 0) return;
|
|
|
|
|
|
|
|
_layer3D->removeAllChildren();
|
|
|
|
_objects.clear();
|
|
|
|
|
|
|
|
--_row;
|
|
|
|
for (int x = -_row; x < _row; x++)
|
|
|
|
{
|
|
|
|
for (int z = -_row; z < _row; z++)
|
|
|
|
{
|
|
|
|
auto sprite = Sprite3D::create("Sprite3DTest/orc.c3b");
|
|
|
|
sprite->setPosition3D(Vec3(x * 30, 0, z * 30));
|
|
|
|
_objects.push_back(sprite);
|
|
|
|
_layer3D->addChild(sprite);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set layer mask.
|
|
|
|
_layer3D->setCameraMask((unsigned short) CameraFlag::USER1);
|
|
|
|
|
|
|
|
// update sprite number
|
|
|
|
char szText[16];
|
|
|
|
sprintf(szText,"%ld sprits",_layer3D->getChildrenCount());
|
|
|
|
_labelSprite3DCount->setString(szText);
|
2014-12-16 16:34:35 +08:00
|
|
|
}
|
|
|
|
|
2015-01-09 08:30:00 +08:00
|
|
|
void CameraCullingDemo::drawCameraFrustum()
|
2014-12-18 17:18:22 +08:00
|
|
|
{
|
|
|
|
_drawFrustum->clear();
|
|
|
|
auto size = Director::getInstance()->getWinSize();
|
|
|
|
|
|
|
|
Color4F color(1.f, 1.f, 0.f, 1);
|
|
|
|
|
|
|
|
// top-left
|
|
|
|
Vec3 tl_0,tl_1;
|
|
|
|
Vec3 src(0,0,0);
|
|
|
|
_cameraFirst->unproject(size, &src, &tl_0);
|
|
|
|
src = Vec3(0,0,1);
|
|
|
|
_cameraFirst->unproject(size, &src, &tl_1);
|
|
|
|
|
|
|
|
// top-right
|
|
|
|
Vec3 tr_0,tr_1;
|
|
|
|
src = Vec3(size.width,0,0);
|
|
|
|
_cameraFirst->unproject(size, &src, &tr_0);
|
|
|
|
src = Vec3(size.width,0,1);
|
|
|
|
_cameraFirst->unproject(size, &src, &tr_1);
|
|
|
|
|
|
|
|
// bottom-left
|
|
|
|
Vec3 bl_0,bl_1;
|
|
|
|
src = Vec3(0,size.height,0);
|
|
|
|
_cameraFirst->unproject(size, &src, &bl_0);
|
|
|
|
src = Vec3(0,size.height,1);
|
|
|
|
_cameraFirst->unproject(size, &src, &bl_1);
|
|
|
|
|
|
|
|
// bottom-right
|
|
|
|
Vec3 br_0,br_1;
|
|
|
|
src = Vec3(size.width,size.height,0);
|
|
|
|
_cameraFirst->unproject(size, &src, &br_0);
|
|
|
|
src = Vec3(size.width,size.height,1);
|
|
|
|
_cameraFirst->unproject(size, &src, &br_1);
|
|
|
|
|
|
|
|
_drawFrustum->drawLine(tl_0, tl_1, color);
|
|
|
|
_drawFrustum->drawLine(tr_0, tr_1, color);
|
|
|
|
_drawFrustum->drawLine(bl_0, bl_1, color);
|
|
|
|
_drawFrustum->drawLine(br_0, br_1, color);
|
|
|
|
|
|
|
|
_drawFrustum->drawLine(tl_0, tr_0, color);
|
|
|
|
_drawFrustum->drawLine(tr_0, br_0, color);
|
|
|
|
_drawFrustum->drawLine(br_0, bl_0, color);
|
|
|
|
_drawFrustum->drawLine(bl_0, tl_0, color);
|
|
|
|
|
|
|
|
_drawFrustum->drawLine(tl_1, tr_1, color);
|
|
|
|
_drawFrustum->drawLine(tr_1, br_1, color);
|
|
|
|
_drawFrustum->drawLine(br_1, bl_1, color);
|
|
|
|
_drawFrustum->drawLine(bl_1, tl_1, color);
|
2014-12-16 16:34:35 +08:00
|
|
|
}
|
|
|
|
|