axmol/tests/cpp-tests/Classes/Camera3DTest/Camera3DTest.cpp

1455 lines
46 KiB
C++
Raw Normal View History

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"
#include <algorithm>
#include "../testResource.h"
enum
{
IDC_NEXT = 100,
IDC_BACK,
IDC_RESTART
};
static int sceneIdx = -1;
static std::function<Layer*()> createFunctions[] =
{
CL(Camera3DTestDemo),
2015-01-07 11:47:34 +08:00
CL(CameraClippingDemo),
CL(CameraArcBallDemo),
CL(FogTestDemo)
2014-08-07 15:23:31 +08:00
};
#define MAX_LAYER (sizeof(createFunctions) / sizeof(createFunctions[0]))
static Layer* nextSpriteTestAction()
{
sceneIdx++;
sceneIdx = sceneIdx % MAX_LAYER;
auto layer = (createFunctions[sceneIdx])();
return layer;
}
static Layer* backSpriteTestAction()
{
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;
}
static Layer* restartSpriteTestAction()
{
auto layer = (createFunctions[sceneIdx])();
return layer;
}
//------------------------------------------------------------------
//
// SpriteTestDemo
//
//------------------------------------------------------------------
Camera3DTestDemo::Camera3DTestDemo(void)
: BaseTest()
, _incRot(nullptr)
, _camera(nullptr)
, _decRot(nullptr)
, _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";
}
std::string Camera3DTestDemo::subtitle() const
{
return "";
}
void Camera3DTestDemo::scaleCameraCallback(Ref* sender,float value)
{
if(_camera&& _cameraType!=CameraType::FirstCamera)
{
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)
{
if(_cameraType==CameraType::FreeCamera || _cameraType==CameraType::FirstCamera)
{
Vec3 rotation3D= _camera->getRotation3D();
rotation3D.y+= value;
_camera->setRotation3D(rotation3D);
}
}
void Camera3DTestDemo::SwitchViewCallback(Ref* sender, CameraType cameraType)
{
if(_cameraType==cameraType)
{
return ;
}
_cameraType = cameraType;
if(_cameraType==CameraType::FreeCamera)
{
2014-10-16 17:31:18 +08:00
_camera->setPosition3D(Vec3(0, 130, 130) + _sprite3D->getPosition3D());
_camera->lookAt(_sprite3D->getPosition3D(), Vec3(0,1,0));
_RotateRightlabel->setColor(Color3B::WHITE);
_RotateLeftlabel->setColor(Color3B::WHITE);
_ZoomInlabel->setColor(Color3B::WHITE);
_ZoomOutlabel->setColor(Color3B::WHITE);
2014-08-07 15:23:31 +08:00
}
else if(_cameraType==CameraType::FirstCamera)
{
Vec3 newFaceDir;
_sprite3D->getWorldToNodeTransform().getForwardVector(&newFaceDir);
newFaceDir.normalize();
_camera->setPosition3D(Vec3(0,35,0) + _sprite3D->getPosition3D());
_camera->lookAt(_sprite3D->getPosition3D() + newFaceDir*50, Vec3(0, 1, 0));
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
}
else if(_cameraType==CameraType::ThirdCamera)
{
_camera->setPosition3D(Vec3(0, 130, 130) + _sprite3D->getPosition3D());
_camera->lookAt(_sprite3D->getPosition3D(), Vec3(0,1,0));
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 ");
auto menuItem1 = MenuItemLabel::create(label1, CC_CALLBACK_1(Camera3DTestDemo::SwitchViewCallback,this,CameraType::FreeCamera));
auto label2 = Label::createWithTTF(ttfConfig,"third person");
auto menuItem2 = MenuItemLabel::create(label2, CC_CALLBACK_1(Camera3DTestDemo::SwitchViewCallback,this,CameraType::ThirdCamera));
auto label3 = Label::createWithTTF(ttfConfig,"first person");
auto menuItem3 = MenuItemLabel::create(label3, CC_CALLBACK_1(Camera3DTestDemo::SwitchViewCallback,this,CameraType::FirstCamera));
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);
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);
}
SwitchViewCallback(this,CameraType::ThirdCamera);
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);
2015-01-07 11:47:34 +08:00
2014-08-07 15:23:31 +08:00
_layer3D->setCameraMask(2);
}
void Camera3DTestDemo::onExit()
{
BaseTest::onExit();
if (_camera)
{
_camera = nullptr;
}
}
void Camera3DTestDemo::restartCallback(Ref* sender)
{
auto s = new (std::nothrow) Camera3DTestScene();
2014-08-07 15:23:31 +08:00
s->addChild(restartSpriteTestAction());
2014-10-16 17:31:18 +08:00
2014-08-07 15:23:31 +08:00
Director::getInstance()->replaceScene(s);
s->release();
}
void Camera3DTestDemo::nextCallback(Ref* sender)
{
auto s = new (std::nothrow) Camera3DTestScene();
2014-08-07 15:23:31 +08:00
s->addChild( nextSpriteTestAction() );
Director::getInstance()->replaceScene(s);
s->release();
}
void Camera3DTestDemo::backCallback(Ref* sender)
{
auto s = new (std::nothrow) Camera3DTestScene();
2014-08-07 15:23:31 +08:00
s->addChild( backSpriteTestAction() );
Director::getInstance()->replaceScene(s);
s->release();
}
2015-01-07 11:47:34 +08:00
2014-08-07 15:23:31 +08:00
void Camera3DTestDemo::addNewSpriteWithCoords(Vec3 p,std::string fileName,bool playAnimation,float scale,bool bindCamera)
{
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;
}
2015-01-07 11:47:34 +08:00
sprite->setScale(scale);
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;
if(_cameraType==CameraType::FreeCamera || _cameraType==CameraType::FirstCamera)
{
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);
2014-10-16 17:31:18 +08:00
if(_sprite3D && _cameraType==CameraType::FirstCamera)
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;
if(_cameraType==CameraType::ThirdCamera)
{
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)
{
2014-10-16 17:31:18 +08:00
if(_sprite3D && _cameraType==CameraType::ThirdCamera && _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)
{
if( _cameraType==CameraType::ThirdCamera)
{
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)
{
if(_cameraType == CameraType::ThirdCamera)
{
Vec3 lookDir = _camera->getPosition3D() - _sprite3D->getPosition3D();
Vec3 cameraPos = _camera->getPosition3D();
if(lookDir.length() <= 300)
{
cameraPos += lookDir.getNormalized();
_camera->setPosition3D(cameraPos);
}
}
else if(_cameraType == CameraType::FreeCamera)
{
Vec3 cameraPos = _camera->getPosition3D();
if(cameraPos.length() <= 300)
{
cameraPos += cameraPos.getNormalized();
_camera->setPosition3D(cameraPos);
}
}
}
}
if(_bZoomIn == true)
{
if(_camera)
{
if(_cameraType == CameraType::ThirdCamera)
{
Vec3 lookDir = _camera->getPosition3D() - _sprite3D->getPosition3D();
Vec3 cameraPos = _camera->getPosition3D();
if(lookDir.length() >= 50)
{
cameraPos -= lookDir.getNormalized();
_camera->setPosition3D(cameraPos);
}
}
else if(_cameraType == CameraType::FreeCamera)
{
Vec3 cameraPos = _camera->getPosition3D();
if(cameraPos.length() >= 50)
{
cameraPos -= cameraPos.getNormalized();
_camera->setPosition3D(cameraPos);
}
}
}
}
if(_bRotateLeft == true)
{
if(_cameraType==CameraType::FreeCamera || _cameraType==CameraType::FirstCamera)
{
Vec3 rotation3D= _camera->getRotation3D();
rotation3D.y+= 1;
_camera->setRotation3D(rotation3D);
}
}
if(_bRotateRight == true)
{
if(_cameraType==CameraType::FreeCamera || _cameraType==CameraType::FirstCamera)
{
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
////////////////////////////////////////////////////////////
// CameraClippingDemo
CameraClippingDemo::CameraClippingDemo(void)
2014-12-16 16:34:35 +08:00
: BaseTest()
, _layer3D(nullptr)
, _cameraType(CameraType::FirstCamera)
2014-12-16 16:34:35 +08:00
, _cameraFirst(nullptr)
, _cameraThird(nullptr)
, _moveAction(nullptr)
2014-12-16 16:34:35 +08:00
, _drawAABB(nullptr)
, _drawFrustum(nullptr)
, _row(3)
2014-12-16 16:34:35 +08:00
{
}
CameraClippingDemo::~CameraClippingDemo(void)
2014-12-16 16:34:35 +08:00
{
}
std::string CameraClippingDemo::title() const
2014-12-16 16:34:35 +08:00
{
return "Camera Frustum Clipping";
2014-12-16 16:34:35 +08:00
}
void CameraClippingDemo::restartCallback(Ref* sender)
2014-12-16 16:34:35 +08:00
{
auto s = new (std::nothrow) Camera3DTestScene();
s->addChild(restartSpriteTestAction());
2014-12-16 16:34:35 +08:00
Director::getInstance()->replaceScene(s);
s->release();
2014-12-16 16:34:35 +08:00
}
void CameraClippingDemo::nextCallback(Ref* sender)
2014-12-16 16:34:35 +08:00
{
auto s = new (std::nothrow) Camera3DTestScene();
s->addChild( nextSpriteTestAction() );
Director::getInstance()->replaceScene(s);
s->release();
2014-12-16 16:34:35 +08:00
}
void CameraClippingDemo::backCallback(Ref* sender)
2014-12-16 16:34:35 +08:00
{
auto s = new (std::nothrow) Camera3DTestScene();
s->addChild( backSpriteTestAction() );
Director::getInstance()->replaceScene(s);
s->release();
2014-12-16 16:34:35 +08:00
}
void CameraClippingDemo::onEnter()
2014-12-16 16:34:35 +08:00
{
BaseTest::onEnter();
schedule(schedule_selector(CameraClippingDemo::update), 0.0f);
2014-12-16 16:34:35 +08:00
auto s = Director::getInstance()->getWinSize();
auto listener = EventListenerTouchAllAtOnce::create();
listener->onTouchesBegan = CC_CALLBACK_2(CameraClippingDemo::onTouchesBegan, this);
listener->onTouchesMoved = CC_CALLBACK_2(CameraClippingDemo::onTouchesMoved, this);
listener->onTouchesEnded = CC_CALLBACK_2(CameraClippingDemo::onTouchesEnded, this);
2014-12-16 16:34:35 +08:00
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
auto layer3D=Layer::create();
addChild(layer3D,0);
_layer3D=layer3D;
2014-12-16 16:34:35 +08:00
// swich camera
MenuItemFont::setFontName("fonts/arial.ttf");
MenuItemFont::setFontSize(20);
2014-12-16 16:34:35 +08:00
auto menuItem1 = MenuItemFont::create("Switch Camera", CC_CALLBACK_1(CameraClippingDemo::switchViewCallback,this));
menuItem1->setColor(Color3B(0,200,20));
auto menu = Menu::create(menuItem1,NULL);
2014-12-16 16:34:35 +08:00
menu->setPosition(Vec2::ZERO);
menuItem1->setPosition(VisibleRect::left().x + 80, VisibleRect::top().y -70);
addChild(menu, 1);
// + -
MenuItemFont::setFontSize(40);
auto decrease = MenuItemFont::create(" - ", CC_CALLBACK_1(CameraClippingDemo::delSpriteCallback, this));
decrease->setColor(Color3B(0,200,20));
auto increase = MenuItemFont::create(" + ", CC_CALLBACK_1(CameraClippingDemo::addSpriteCallback, this));
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
// frustum drawNode3D
_drawFrustum = DrawNode3D::create();
_drawFrustum->setCameraMask((unsigned short) CameraFlag::USER1);
addChild(_drawFrustum);
2014-12-16 16:34:35 +08:00
// set camera
switchViewCallback(this);
2014-12-16 16:34:35 +08:00
// add sprite
addSpriteCallback(nullptr);
}
void CameraClippingDemo::onExit()
{
BaseTest::onExit();
if (_cameraFirst)
2014-12-16 16:34:35 +08:00
{
_cameraFirst = nullptr;
2014-12-16 16:34:35 +08:00
}
if (_cameraThird)
2014-12-16 16:34:35 +08:00
{
_cameraThird = nullptr;
2014-12-16 16:34:35 +08:00
}
}
void CameraClippingDemo::update(float dt)
{
_drawAABB->clear();
2014-12-16 16:34:35 +08:00
if(_cameraType == CameraType::ThirdCamera)
drawCameraFrustum();
2014-12-16 16:34:35 +08:00
Vector<Node*>& children = _layer3D->getChildren();
Vec3 corners[8];
2014-12-16 16:34:35 +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))
{
aabb.getCorners(corners);
_drawAABB->drawCube(corners, Color4F(0, 1, 0, 1));
}
}
}
void CameraClippingDemo::reachEndCallBack()
{
_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));
auto seq = Sequence::create(rot, _moveAction, CallFunc::create(CC_CALLBACK_0(CameraClippingDemo::reachEndCallBack, this)), nullptr);
seq->setTag(100);
_cameraFirst->runAction(seq);
}
void CameraClippingDemo::switchViewCallback(Ref* sender)
{
auto s = Director::getInstance()->getWinSize();
2014-12-16 16:34:35 +08:00
if (_cameraFirst == nullptr)
2014-12-16 16:34:35 +08:00
{
_cameraFirst = Camera::createPerspective(30, (GLfloat)s.width/s.height, 10, 200);
_cameraFirst->setCameraFlag(CameraFlag::USER8);
_cameraFirst->setPosition3D(Vec3(-100,0,0));
_cameraFirst->lookAt(Vec3(1000,0,0), Vec3(0, 1, 0));
_moveAction = MoveTo::create(4.f, Vec2(100, 0));
_moveAction->retain();
auto seq = Sequence::create(_moveAction, CallFunc::create(CC_CALLBACK_0(CameraClippingDemo::reachEndCallBack, this)), nullptr);
seq->setTag(100);
_cameraFirst->runAction(seq);
addChild(_cameraFirst);
2014-12-16 16:34:35 +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));
_cameraThird->lookAt(Vec3(0,0,0), Vec3(0, 1, 0));
addChild(_cameraThird);
2014-12-16 16:34:35 +08:00
}
if(_cameraType == CameraType::FirstCamera)
2014-12-16 16:34:35 +08:00
{
_cameraType = CameraType::ThirdCamera;
_cameraThird->setCameraFlag(CameraFlag::USER1);
2014-12-22 18:13:07 +08:00
_cameraThird->enableFrustumCulling(false, false);
_cameraFirst->setCameraFlag(CameraFlag::USER8);
}
else if(_cameraType == CameraType::ThirdCamera)
{
_cameraType = CameraType::FirstCamera;
_cameraFirst->setCameraFlag(CameraFlag::USER1);
2014-12-22 18:13:07 +08:00
_cameraFirst->enableFrustumCulling(true, true);
_cameraThird->setCameraFlag(CameraFlag::USER8);
_drawFrustum->clear();
2014-12-16 16:34:35 +08:00
}
}
void CameraClippingDemo::addSpriteCallback(Ref* sender)
2014-12-16 16:34:35 +08:00
{
_layer3D->removeAllChildren();
_objects.clear();
_drawAABB->clear();
2014-12-16 16:34:35 +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
}
void CameraClippingDemo::delSpriteCallback(Ref* sender)
2014-12-16 16:34:35 +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
}
void CameraClippingDemo::drawCameraFrustum()
{
_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
}
2015-01-07 11:47:34 +08:00
////////////////////////////////////////////////////////////
// CameraArcBallDemo
CameraArcBallDemo::CameraArcBallDemo(void)
: BaseTest()
, _layer3D(nullptr)
, _cameraType(CameraType::FreeCamera)
, _camera(nullptr)
,_drawGrid(nullptr)
,_sprite3D1(nullptr)
,_sprite3D2(nullptr)
,_fRadius(1.0f)
,_distanceX(0.0f)
,_distanceZ(50.0f)
,_operate(OperateCamType::RotateCamera)
,_center(Vec3(0,0,0))
,_target(0)
{
}
CameraArcBallDemo::~CameraArcBallDemo(void)
{
}
std::string CameraArcBallDemo::title() const
{
return "Camera ArcBall Moving";
}
void CameraArcBallDemo::restartCallback(Ref* sender)
{
auto s = new (std::nothrow) Camera3DTestScene();
s->addChild(restartSpriteTestAction());
Director::getInstance()->replaceScene(s);
s->release();
}
void CameraArcBallDemo::nextCallback(Ref* sender)
{
auto s = new (std::nothrow) Camera3DTestScene();
s->addChild( nextSpriteTestAction() );
Director::getInstance()->replaceScene(s);
s->release();
}
void CameraArcBallDemo::backCallback(Ref* sender)
{
auto s = new (std::nothrow) Camera3DTestScene();
s->addChild( backSpriteTestAction() );
Director::getInstance()->replaceScene(s);
s->release();
}
void CameraArcBallDemo::onEnter()
{
BaseTest::onEnter();
_rotationQuat.set(0.0f, 0.0f, 0.0f, 1.0f);
schedule(schedule_selector(CameraArcBallDemo::update), 0.0f);
auto s = Director::getInstance()->getWinSize();
auto listener = EventListenerTouchAllAtOnce::create();
listener->onTouchesMoved = CC_CALLBACK_2(CameraArcBallDemo::onTouchsMoved, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
// swich camera
MenuItemFont::setFontName("fonts/arial.ttf");
MenuItemFont::setFontSize(20);
auto menuItem1 = MenuItemFont::create("Switch Operation", CC_CALLBACK_1(CameraArcBallDemo::switchOperateCallback,this));
menuItem1->setColor(Color3B(0,200,20));
auto menuItem2 = MenuItemFont::create("Switch Target", CC_CALLBACK_1(CameraArcBallDemo::switchTargetCallback,this));
menuItem2->setColor(Color3B(0,200,20));
auto menu = Menu::create(menuItem1,menuItem2,NULL);
menu->setPosition(Vec2::ZERO);
menuItem1->setPosition(VisibleRect::left().x + 80, VisibleRect::top().y -70);
menuItem2->setPosition(VisibleRect::left().x + 80, VisibleRect::top().y -100);
addChild(menu, 1);
auto layer3D=Layer::create();
addChild(layer3D,0);
_layer3D=layer3D;
if (_camera == nullptr)
{
_camera=Camera::createPerspective(60, (GLfloat)s.width/s.height, 1, 1000);
_camera->setCameraFlag(CameraFlag::USER1);
_camera->setPosition3D(Vec3(0, 100, 50));
_camera->lookAt(Vec3(0, 0, 0), Vec3(0, 1, 0));
_camera->retain();
_layer3D->addChild(_camera);
}
_sprite3D1 = Sprite3D::create("Sprite3DTest/orc.c3b");
_sprite3D1->setScale(0.5);
_sprite3D1->setRotation3D(Vec3(0,180,0));
_sprite3D1->setPosition3D(Vec3(0,0,0));
_layer3D->addChild(_sprite3D1);
_sprite3D2 = Sprite3D::create("Sprite3DTest/boss.c3b");
_sprite3D2->setScale(0.6);
_sprite3D2->setRotation3D(Vec3(-90,0,0));
_sprite3D2->setPosition3D(Vec3(20,0,0));
_layer3D->addChild(_sprite3D2);
_drawGrid =DrawNode3D::create();
//draw x
for( int j =-20; j<=20 ;j++)
{
_drawGrid->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++)
{
_drawGrid->drawLine(Vec3(5*j, 0, -100),Vec3(5*j,0,100),Color4F(0,0,1,1));
}
//draw y
_drawGrid->drawLine(Vec3(0, 0, 0),Vec3(0,50,0),Color4F(0,1,0,1));
_layer3D->addChild(_drawGrid);
_layer3D->setCameraMask(2);
updateCameraTransform();
}
void CameraArcBallDemo::onExit()
{
BaseTest::onExit();
if (_camera)
{
_camera = nullptr;
}
}
void CameraArcBallDemo::onTouchsMoved( const std::vector<Touch*> &touchs, Event *event )
{
if (!touchs.empty())
{
if(_operate == OperateCamType::RotateCamera)
{
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 prelocation = touchs[0]->getPreviousLocationInView();
Vec2 location = touchs[0]->getLocationInView();
location.x = 2.0 * (location.x) / (visibleSize.width) - 1.0f;
location.y = 2.0 * (visibleSize.height - location.y) / (visibleSize.height) - 1.0f;
prelocation.x = 2.0 * (prelocation.x) / (visibleSize.width) - 1.0f;
prelocation.y = 2.0 * (visibleSize.height - prelocation.y) / (visibleSize.height) - 1.0f;
Vec3 axes;
float angle;
calculateArcBall(axes, angle, prelocation.x, prelocation.y, location.x, location.y);
Quaternion quat(axes, angle);
_rotationQuat = quat * _rotationQuat;
updateCameraTransform();
}
else if(_operate == OperateCamType::MoveCamera)
{
Point newPos = touchs[0]->getPreviousLocation() - touchs[0]->getLocation();
_distanceZ -= newPos.y*0.1f;
_distanceZ += newPos.x*0.1f;
updateCameraTransform();
}
}
}
void CameraArcBallDemo::calculateArcBall( cocos2d::Vec3 & axis, float & angle, float p1x, float p1y, float p2x, float p2y )
{
Mat4 rotation_matrix;
Mat4::createRotation(_rotationQuat, &rotation_matrix);
Vec3 uv = rotation_matrix * Vec3(0.0f,1.0f,0.0f);
Vec3 sv = rotation_matrix * Vec3(1.0f,0.0f,0.0f);
Vec3 lv = rotation_matrix * Vec3(0.0f,0.0f,-1.0f);
Vec3 p1 = sv * p1x + uv * p1y - lv * projectToSphere(_fRadius, p1x, p1y);
Vec3 p2 = sv * p2x + uv * p2y - lv * projectToSphere(_fRadius, p2x, p2y);
Vec3::cross(p2, p1, &axis);
axis.normalize();
float t = (p2 - p1).length() / (2.0 * _fRadius);
if (t > 1.0) t = 1.0;
if (t < -1.0) t = -1.0;
angle = asin(t);
}
float CameraArcBallDemo::projectToSphere( float r, float x, float y )
{
float d, t, z;
d = sqrt(x*x + y*y);
if (d < r * 0.70710678118654752440)
{
z = sqrt(r*r - d*d);
}
else
{
t = r / 1.41421356237309504880;
z = t*t / d;
}
return z;
}
void CameraArcBallDemo::updateCameraTransform()
{
Mat4 trans, rot, center;
Mat4::createTranslation(Vec3(_distanceX, 0.0f, _distanceZ), &trans);
Mat4::createRotation(_rotationQuat, &rot);
Mat4::createTranslation(_center, &center);
Mat4 result = center * rot * trans;
_camera->setNodeToParentTransform(result);
}
void CameraArcBallDemo::switchOperateCallback(Ref* sender)
{
if(_operate == OperateCamType::MoveCamera)
{
_operate = OperateCamType::RotateCamera;
}
else if(_operate == OperateCamType::RotateCamera)
{
_operate = OperateCamType::MoveCamera;
}
}
void CameraArcBallDemo::switchTargetCallback(Ref* sender)
{
if(_target == 0)
{
_target = 1;
_center = _sprite3D2->getPosition3D();
updateCameraTransform();
}
else if(_target == 1)
{
_target = 0;
_center = _sprite3D1->getPosition3D();
updateCameraTransform();
}
}
void CameraArcBallDemo::update(float dt)
{
//updateCameraTransform();
}
////////////////////////////////////////////////////////////
// FogTestDemo
FogTestDemo::FogTestDemo(void)
: BaseTest()
, _layer3D(nullptr)
, _cameraType(CameraType::FreeCamera)
, _camera(nullptr)
, _shader(nullptr)
, _state(nullptr)
{
}
FogTestDemo::~FogTestDemo(void)
{
}
std::string FogTestDemo::title() const
{
return "Fog Test Demo";
}
void FogTestDemo::restartCallback(Ref* sender)
{
auto s = new (std::nothrow) Camera3DTestScene();
s->addChild(restartSpriteTestAction());
Director::getInstance()->replaceScene(s);
s->release();
}
void FogTestDemo::nextCallback(Ref* sender)
{
auto s = new (std::nothrow) Camera3DTestScene();
s->addChild( nextSpriteTestAction() );
Director::getInstance()->replaceScene(s);
s->release();
}
void FogTestDemo::backCallback(Ref* sender)
{
auto s = new (std::nothrow) Camera3DTestScene();
s->addChild( backSpriteTestAction() );
Director::getInstance()->replaceScene(s);
s->release();
}
void FogTestDemo::onEnter()
{
BaseTest::onEnter();
schedule(schedule_selector(FogTestDemo::update), 0.0f);
auto s = Director::getInstance()->getWinSize();
auto listener = EventListenerTouchAllAtOnce::create();
listener->onTouchesBegan = CC_CALLBACK_2(FogTestDemo::onTouchesBegan, this);
listener->onTouchesMoved = CC_CALLBACK_2(FogTestDemo::onTouchesMoved, this);
listener->onTouchesEnded = CC_CALLBACK_2(FogTestDemo::onTouchesEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
// swich fog type
TTFConfig ttfConfig("fonts/arial.ttf", 20);
auto label1 = Label::createWithTTF(ttfConfig,"Linear ");
auto menuItem1 = MenuItemLabel::create(label1, CC_CALLBACK_1(FogTestDemo::switchTypeCallback,this,0));
auto label2 = Label::createWithTTF(ttfConfig,"Exp");
auto menuItem2 = MenuItemLabel::create(label2, CC_CALLBACK_1(FogTestDemo::switchTypeCallback,this,1));
auto label3 = Label::createWithTTF(ttfConfig,"Exp2");
auto menuItem3 = MenuItemLabel::create(label3, CC_CALLBACK_1(FogTestDemo::switchTypeCallback,this,2));
auto menu = Menu::create(menuItem1, menuItem2, menuItem3, nullptr);
menu->setPosition(Vec2::ZERO);
menuItem1->setPosition(VisibleRect::left().x+60, VisibleRect::top().y-50);
menuItem2->setPosition(VisibleRect::left().x+60, VisibleRect::top().y -100);
menuItem3->setPosition(VisibleRect::left().x+60, VisibleRect::top().y -150);
addChild(menu, 0);
auto layer3D=Layer::create();
addChild(layer3D,0);
_layer3D=layer3D;
glClearColor(0.5,0.5,0.5,1.0);
_shader =GLProgram::createWithFilenames("Sprite3DTest/fog.vert","Sprite3DTest/fog.frag");
_state = GLProgramState::create(_shader);
_sprite3D1 = Sprite3D::create("Sprite3DTest/teapot.c3b");
_sprite3D2 = Sprite3D::create("Sprite3DTest/teapot.c3b");
_sprite3D1->setGLProgramState(_state);
_sprite3D2->setGLProgramState(_state);
//pass mesh's attribute to shader
long offset = 0;
auto attributeCount = _sprite3D1->getMesh()->getMeshVertexAttribCount();
for (auto i = 0; i < attributeCount; i++) {
auto meshattribute = _sprite3D1->getMesh()->getMeshVertexAttribute(i);
_state->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib],
meshattribute.size,
meshattribute.type,
GL_FALSE,
_sprite3D1->getMesh()->getVertexSizeInBytes(),
(GLvoid*)offset);
offset += meshattribute.attribSizeBytes;
}
long offset1 = 0;
auto attributeCount1 = _sprite3D2->getMesh()->getMeshVertexAttribCount();
for (auto i = 0; i < attributeCount1; i++) {
auto meshattribute = _sprite3D2->getMesh()->getMeshVertexAttribute(i);
_state->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib],
meshattribute.size,
meshattribute.type,
GL_FALSE,
_sprite3D2->getMesh()->getVertexSizeInBytes(),
(GLvoid*)offset1);
offset1 += meshattribute.attribSizeBytes;
}
_state->setUniformVec4("fogColor", Vec4(0.5,0.5,0.5,1.0));
_state->setUniformFloat("fStart",10);
_state->setUniformFloat("fEnd",60);
_state->setUniformInt("iEquation" ,0);
_layer3D->addChild(_sprite3D1);
_sprite3D1->setPosition3D( Vec3( 0, 0,0 ) );
_sprite3D1->setScale(2.0f);
_sprite3D1->setRotation3D(Vec3(-90,180,0));
_layer3D->addChild(_sprite3D2);
_sprite3D2->setPosition3D( Vec3( 0, 0,-20 ) );
_sprite3D2->setScale(2.0f);
_sprite3D2->setRotation3D(Vec3(-90,180,0));
if (_camera == nullptr)
{
_camera=Camera::createPerspective(60, (GLfloat)s.width/s.height, 1, 1000);
_camera->setCameraFlag(CameraFlag::USER1);
_camera->setPosition3D(Vec3(0, 30, 40));
_camera->lookAt(Vec3(0,0,0), Vec3(0, 1, 0));
_layer3D->addChild(_camera);
}
_layer3D->setCameraMask(2);
}
void FogTestDemo::switchTypeCallback(Ref* sender,int type)
{
if(type == 0)
{
_state->setUniformVec4("fogColor", Vec4(0.5,0.5,0.5,1.0));
_state->setUniformFloat("fStart",10);
_state->setUniformFloat("fEnd",60);
_state->setUniformInt("iEquation" ,0);
_sprite3D1->setGLProgramState(_state);
_sprite3D2->setGLProgramState(_state);
}
else if(type == 1)
{
_state->setUniformVec4("fogColor", Vec4(0.5,0.5,0.5,1.0));
_state->setUniformFloat("fogDensity",0.03);
_state->setUniformInt("iEquation" ,1);
_sprite3D1->setGLProgramState(_state);
_sprite3D2->setGLProgramState(_state);
}
else if(type == 2)
{
_state->setUniformVec4("fogColor", Vec4(0.5,0.5,0.5,1.0));
_state->setUniformFloat("fogDensity",0.03);
_state->setUniformInt("iEquation" ,2);
_sprite3D1->setGLProgramState(_state);
_sprite3D2->setGLProgramState(_state);
}
}
void FogTestDemo::onExit()
{
BaseTest::onExit();
glClearColor(0.0,0.0,0.0,1.0);
if (_camera)
{
_camera = nullptr;
}
}
void FogTestDemo::update(float dt)
{
}
void FogTestDemo::onTouchesBegan(const std::vector<Touch*>& touches, cocos2d::Event *event)
{
for ( auto &item: touches )
{
auto touch = item;
auto location = touch->getLocation();
}
}
void FogTestDemo::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;
if(_cameraType==CameraType::FreeCamera)
{
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;
_camera->setPosition3D(cameraPos);
}
}
}
void FogTestDemo::onTouchesEnded(const std::vector<Touch*>& touches, cocos2d::Event *event)
{
for ( auto &item: touches )
{
auto touch = item;
auto location = touch->getLocationInView();
}
}
2014-08-07 15:23:31 +08:00
void Camera3DTestScene::runThisTest()
{
auto layer = nextSpriteTestAction();
addChild(layer);
Director::getInstance()->replaceScene(this);
}