2014-08-17 21:33:33 +08:00
|
|
|
#include "LightTestDemo.h"
|
|
|
|
|
|
|
|
static int sceneIdx = -1;
|
|
|
|
|
|
|
|
|
|
|
|
static std::function<Layer*()> createFunctions[] =
|
|
|
|
{
|
2014-08-19 16:32:49 +08:00
|
|
|
//CL(DirectionalLightTestDemo),
|
|
|
|
//CL(PointLightTestDemo),
|
|
|
|
//CL(SpotLightTestDemo)
|
|
|
|
CL(LightTestDemo)
|
2014-08-17 21:33:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#define MAX_LAYER (sizeof(createFunctions) / sizeof(createFunctions[0]))
|
|
|
|
|
|
|
|
static Layer* nextSpriteTestAction()
|
|
|
|
{
|
2014-08-18 17:10:07 +08:00
|
|
|
sceneIdx++;
|
|
|
|
sceneIdx = sceneIdx % MAX_LAYER;
|
2014-08-17 21:33:33 +08:00
|
|
|
|
2014-08-18 17:10:07 +08:00
|
|
|
auto layer = (createFunctions[sceneIdx])();
|
|
|
|
return layer;
|
2014-08-17 21:33:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static Layer* backSpriteTestAction()
|
|
|
|
{
|
2014-08-18 17:10:07 +08:00
|
|
|
sceneIdx--;
|
|
|
|
int total = MAX_LAYER;
|
|
|
|
if( sceneIdx < 0 )
|
|
|
|
sceneIdx += total;
|
2014-08-17 21:33:33 +08:00
|
|
|
|
2014-08-18 17:10:07 +08:00
|
|
|
auto layer = (createFunctions[sceneIdx])();
|
|
|
|
return layer;
|
2014-08-17 21:33:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static Layer* restartSpriteTestAction()
|
|
|
|
{
|
2014-08-18 17:10:07 +08:00
|
|
|
auto layer = (createFunctions[sceneIdx])();
|
|
|
|
return layer;
|
2014-08-17 21:33:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LightTestDemo::LightTestDemo()
|
2014-08-19 16:32:49 +08:00
|
|
|
: _directionalLight(nullptr)
|
2014-08-19 19:24:19 +08:00
|
|
|
, _pointLight(nullptr)
|
|
|
|
, _spotLight(nullptr)
|
2014-08-17 21:33:33 +08:00
|
|
|
{
|
2014-08-19 16:32:49 +08:00
|
|
|
addSprite();
|
|
|
|
addLights();
|
|
|
|
scheduleUpdate();
|
|
|
|
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
auto camera = Camera::createPerspective(60, (GLfloat)s.width/s.height, 1.0f, 1000.0f);
|
|
|
|
camera->setCameraFlag(CameraFlag::USER1);
|
|
|
|
camera->setPosition3D(Vec3(0.0, 100, 100));
|
|
|
|
camera->lookAt(Vec3(0.0, 0.0, 0.0), Vec3(0.0, 1.0, 0.0));
|
|
|
|
addChild(camera);
|
|
|
|
|
|
|
|
TTFConfig ttfConfig("fonts/arial.ttf", 10);
|
|
|
|
auto label1 = Label::createWithTTF(ttfConfig,"Directional Light");
|
|
|
|
auto menuItem1 = MenuItemLabel::create(label1, CC_CALLBACK_1(LightTestDemo::SwitchLight,this,Light3D::DIRECTIONAL));
|
|
|
|
auto label2 = Label::createWithTTF(ttfConfig,"Point Light");
|
|
|
|
auto menuItem2 = MenuItemLabel::create(label2, CC_CALLBACK_1(LightTestDemo::SwitchLight,this,Light3D::POINT));
|
|
|
|
auto label3 = Label::createWithTTF(ttfConfig,"Spot Light");
|
|
|
|
auto menuItem3 = MenuItemLabel::create(label3, CC_CALLBACK_1(LightTestDemo::SwitchLight,this,Light3D::SPOT));
|
|
|
|
auto menu = Menu::create(menuItem1,menuItem2,menuItem3,NULL);
|
|
|
|
menu->setPosition(Vec2::ZERO);
|
|
|
|
menuItem1->setPosition( Vec2(VisibleRect::left().x + 50, VisibleRect::top().y-50) );
|
|
|
|
menuItem2->setPosition( Vec2(VisibleRect::left().x + 50, VisibleRect::top().y -70));
|
|
|
|
menuItem3->setPosition( Vec2(VisibleRect::left().x + 50, VisibleRect::top().y -90));
|
|
|
|
addChild(menu);
|
2014-08-17 21:33:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LightTestDemo::~LightTestDemo()
|
|
|
|
{
|
2014-08-19 16:32:49 +08:00
|
|
|
if (_directionalLight)
|
|
|
|
_directionalLight->release();
|
2014-08-19 15:51:30 +08:00
|
|
|
|
2014-08-19 16:32:49 +08:00
|
|
|
if (_pointLight)
|
|
|
|
_pointLight->release();
|
2014-08-19 15:51:30 +08:00
|
|
|
|
2014-08-19 16:32:49 +08:00
|
|
|
if (_spotLight)
|
|
|
|
_spotLight->release();
|
2014-08-17 21:33:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string LightTestDemo::title() const
|
|
|
|
{
|
2014-08-19 15:51:30 +08:00
|
|
|
return "Light Test";
|
2014-08-17 21:33:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string LightTestDemo::subtitle() const
|
|
|
|
{
|
2014-08-18 17:10:07 +08:00
|
|
|
return "";
|
2014-08-17 21:33:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LightTestDemo::restartCallback( Ref* sender )
|
|
|
|
{
|
2014-08-18 17:10:07 +08:00
|
|
|
auto s = new LightTestScene();
|
|
|
|
s->addChild(restartSpriteTestAction());
|
|
|
|
Director::getInstance()->replaceScene(s);
|
|
|
|
s->release();
|
2014-08-17 21:33:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LightTestDemo::nextCallback( Ref* sender )
|
|
|
|
{
|
2014-08-18 17:10:07 +08:00
|
|
|
auto s = new LightTestScene();
|
|
|
|
s->addChild( nextSpriteTestAction() );
|
|
|
|
Director::getInstance()->replaceScene(s);
|
|
|
|
s->release();
|
2014-08-17 21:33:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LightTestDemo::backCallback( Ref* sender )
|
|
|
|
{
|
2014-08-18 17:10:07 +08:00
|
|
|
auto s = new LightTestScene();
|
|
|
|
s->addChild( backSpriteTestAction() );
|
|
|
|
Director::getInstance()->replaceScene(s);
|
|
|
|
s->release();
|
2014-08-17 21:33:33 +08:00
|
|
|
}
|
|
|
|
|
2014-08-18 17:10:07 +08:00
|
|
|
void LightTestDemo::onEnter()
|
2014-08-17 21:33:33 +08:00
|
|
|
{
|
2014-08-18 17:10:07 +08:00
|
|
|
BaseTest::onEnter();
|
|
|
|
}
|
2014-08-17 21:33:33 +08:00
|
|
|
|
2014-08-18 17:10:07 +08:00
|
|
|
void LightTestDemo::onExit()
|
|
|
|
{
|
|
|
|
BaseTest::onExit();
|
|
|
|
}
|
2014-08-17 21:33:33 +08:00
|
|
|
|
2014-08-19 15:51:30 +08:00
|
|
|
void LightTestDemo::addSprite()
|
|
|
|
{
|
2014-08-19 16:32:49 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
|
|
|
|
{
|
|
|
|
std::string fileName = "Sprite3DTest/plane.c3b";
|
|
|
|
auto sprite = Sprite3D::create(fileName);
|
|
|
|
sprite->setRotation3D(Vec3(-90.0, 0.0, 0.0));
|
|
|
|
sprite->setScale(5.0f);
|
|
|
|
sprite->setPosition(Vec2(0.0, -50.0));
|
|
|
|
addChild(sprite);
|
|
|
|
sprite->setCameraMask(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
std::string fileName = "Sprite3DTest/sphere.c3b";
|
|
|
|
auto sprite = Sprite3D::create(fileName);
|
|
|
|
//sprite->setPosition(Vec2(s.width/2, s.height/2));
|
|
|
|
addChild(sprite);
|
|
|
|
sprite->setCameraMask(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
std::string fileName = "Sprite3DTest/sphere.c3b";
|
|
|
|
auto sprite = Sprite3D::create(fileName);
|
|
|
|
sprite->setScale(2.0f);
|
|
|
|
sprite->setPosition(Vec2(50.0, 0.0));
|
|
|
|
addChild(sprite);
|
|
|
|
sprite->setCameraMask(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
std::string fileName = "Sprite3DTest/sphere.c3b";
|
|
|
|
auto sprite = Sprite3D::create(fileName);
|
|
|
|
sprite->setScale(0.5f);
|
|
|
|
sprite->setPosition(Vec2(-50.0, 0.0));
|
|
|
|
addChild(sprite);
|
|
|
|
sprite->setCameraMask(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
std::string fileName = "Sprite3DTest/sphere.c3b";
|
|
|
|
auto sprite = Sprite3D::create(fileName);
|
|
|
|
sprite->setScale(0.5f);
|
|
|
|
sprite->setPosition(Vec2(-30.0, 10.0));
|
|
|
|
addChild(sprite);
|
|
|
|
sprite->setCameraMask(2);
|
|
|
|
}
|
2014-08-19 15:51:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LightTestDemo::addLights()
|
|
|
|
{
|
2014-08-19 19:24:19 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2014-08-20 16:20:22 +08:00
|
|
|
_directionalLight = Light3D::CreateDirectionalLight(Vec3(-1.0f, -1.0f, 0.0f), Color3B(200, 200, 200));
|
2014-08-19 19:24:19 +08:00
|
|
|
_directionalLight->retain();
|
|
|
|
addChild(_directionalLight);
|
2014-08-19 16:32:49 +08:00
|
|
|
|
2014-08-19 19:24:19 +08:00
|
|
|
_pointLight = Light3D::CreatePointLight(Vec3(100.0, 100.0, 100.0), Color3B(200, 200, 200), 1000.0f);
|
|
|
|
_pointLight->retain();
|
|
|
|
_pointLight->setEnabled(false);
|
|
|
|
addChild(_pointLight);
|
2014-08-19 16:32:49 +08:00
|
|
|
|
2014-08-20 16:20:22 +08:00
|
|
|
_spotLight = Light3D::CreateSpotLight(Vec3(-1.0f, -1.0f, 0.0f), Vec3(100.0, 100.0, 0.0f), Color3B(200, 200, 200), 0.0, 0.5, 1000.0f);
|
2014-08-19 16:32:49 +08:00
|
|
|
_spotLight->retain();
|
|
|
|
_spotLight->setEnabled(false);
|
|
|
|
addChild(_spotLight);
|
2014-08-19 15:51:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LightTestDemo::update( float delta )
|
|
|
|
{
|
2014-08-19 16:32:49 +08:00
|
|
|
static float angleDelta = 0.0;
|
2014-08-19 19:24:19 +08:00
|
|
|
if (_directionalLight)
|
2014-08-20 16:20:22 +08:00
|
|
|
_directionalLight->setRotation3D(Vec3(-45.0, -CC_RADIANS_TO_DEGREES(angleDelta), 0.0f));
|
2014-08-19 19:24:19 +08:00
|
|
|
|
|
|
|
if (_pointLight)
|
|
|
|
{
|
|
|
|
_pointLight->setPositionX(100.0f * cosf(angleDelta));
|
|
|
|
_pointLight->setPositionY(100.0f);
|
|
|
|
_pointLight->setPositionZ(100.0f * sinf(angleDelta));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_spotLight)
|
|
|
|
{
|
|
|
|
_spotLight->setPositionX(100.0f * cosf(angleDelta));
|
|
|
|
_spotLight->setPositionY(100.0f);
|
|
|
|
_spotLight->setPositionZ(100.0f * sinf(angleDelta));
|
2014-08-20 16:20:22 +08:00
|
|
|
_spotLight->setDirection(-Vec3(cosf(angleDelta), 1.0, sinf(angleDelta)));
|
2014-08-19 19:24:19 +08:00
|
|
|
}
|
2014-08-19 15:51:30 +08:00
|
|
|
|
2014-08-19 16:32:49 +08:00
|
|
|
angleDelta += delta;
|
2014-08-19 15:51:30 +08:00
|
|
|
|
2014-08-19 16:32:49 +08:00
|
|
|
BaseTest::update(delta);
|
2014-08-19 15:51:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LightTestDemo::SwitchLight( Ref* sender,Light3D::LightType lightType )
|
|
|
|
{
|
2014-08-19 16:32:49 +08:00
|
|
|
switch (lightType)
|
|
|
|
{
|
|
|
|
case Light3D::DIRECTIONAL:
|
|
|
|
{
|
2014-08-19 19:24:19 +08:00
|
|
|
if (_directionalLight)
|
|
|
|
_directionalLight->setEnabled(true);
|
|
|
|
if (_pointLight)
|
|
|
|
_pointLight->setEnabled(false);
|
|
|
|
if (_spotLight)
|
|
|
|
_spotLight->setEnabled(false);
|
2014-08-19 16:32:49 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Light3D::POINT:
|
|
|
|
{
|
2014-08-19 19:24:19 +08:00
|
|
|
if (_directionalLight)
|
|
|
|
_directionalLight->setEnabled(false);
|
|
|
|
if (_pointLight)
|
|
|
|
_pointLight->setEnabled(true);
|
|
|
|
if (_spotLight)
|
|
|
|
_spotLight->setEnabled(false);
|
2014-08-19 16:32:49 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Light3D::SPOT:
|
|
|
|
{
|
2014-08-19 19:24:19 +08:00
|
|
|
if (_directionalLight)
|
|
|
|
_directionalLight->setEnabled(false);
|
|
|
|
if (_pointLight)
|
|
|
|
_pointLight->setEnabled(false);
|
|
|
|
if (_spotLight)
|
|
|
|
_spotLight->setEnabled(true);
|
2014-08-19 16:32:49 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-08-19 15:51:30 +08:00
|
|
|
}
|
|
|
|
|
2014-08-18 17:10:07 +08:00
|
|
|
void LightTestScene::runThisTest()
|
|
|
|
{
|
|
|
|
auto layer = nextSpriteTestAction();
|
|
|
|
addChild(layer);
|
2014-08-17 21:33:33 +08:00
|
|
|
|
2014-08-18 17:10:07 +08:00
|
|
|
Director::getInstance()->replaceScene(this);
|
2014-08-17 21:33:33 +08:00
|
|
|
}
|
|
|
|
|
2014-08-18 17:10:07 +08:00
|
|
|
LightTestScene::LightTestScene()
|
2014-08-17 21:33:33 +08:00
|
|
|
{
|
2014-08-18 17:10:07 +08:00
|
|
|
setAmbientColor(Color4F(0.2f, 0.2f, 0.2f, 1.0f));
|
2014-08-19 16:32:49 +08:00
|
|
|
}
|