(11) Cocos2d-x3.3RC0之LightTest分析

来源:互联网 发布:jsp中嵌入java代码 编辑:程序博客网 时间:2024/05/17 01:05

1、头文件

[cpp] view plaincopy
  1. //LightTest.h  
  2. class LightTest : public Layer  
  3. {  
  4. public:  
  5.     CREATE_FUNC(LightTest);  
  6.     LightTest();  
  7.     virtual ~LightTest();  
  8.       
  9.     virtual void update(float delta);  
  10.     void SwitchLight(Ref* sender, LightType lightType);//转换光源  
  11.       
  12. private:  
  13.       
  14.     void addSprite();//添加精灵  
  15.     void addLights();//添加光源  
  16.       
  17. private:  
  18.       
  19.     AmbientLight *_ambientLight;       //环境光,想四周发射光线,模拟一个屋子的环境,光线来源方向不定。  
  20.     DirectionLight *_directionalLight; //方向光,表示一个非常远的光源,投射光线是平行的,用来模拟太阳光  
  21.     PointLight *_pointLight;           //点光,从空间某点向四周均匀发射有限长的光线,用来模拟向四周发射状的光  
  22.     SpotLight *_spotLight;             //聚光,从某点发射一个圆锥形状,用来模拟台灯  
  23.       
  24.     Label *_ambientLightLabel;  
  25.     Label *_directionalLightLabel;  
  26.     Label *_pointLightLabel;  
  27.     Label *_spotLightLabel;  
  28. };  

2、Cpp文件

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include "LightTest.h"  
  2. //构造函数,初始化除环境光AmbientLight之外的三种光为nullptr  
  3. LightTest::LightTest()  
  4. : _directionalLight(nullptr)  
  5. , _pointLight(nullptr)  
  6. , _spotLight(nullptr)  
  7. {  
  8.     addSprite();//添加精灵  
  9.     addLights();//添加光源  
  10.     scheduleUpdate();//帧更新  
  11.       
  12.     //设置摄像机位置  
  13.     auto s = Director::getInstance()->getWinSize();  
  14.     //参数1:透视相机的视野,通常在40-60度范围  
  15.     //参数2:相机的长宽比,通常视窗的宽度除以视窗的高度  
  16.     //参数3:近点平面距离  
  17.     //参数4:原点平面距离  
  18.     auto camera = Camera::createPerspective(60, (GLfloat)s.width/s.height, 1.0f, 1000.0f);  
  19.     camera->setCameraFlag(CameraFlag::USER1);//设置相机Flag  
  20.     camera->setPosition3D(Vec3(0.0, 100, 100));//设置相机3D坐标  
  21.     //参数1:目标的中心位置,参数2:向上的向量  
  22.     camera->lookAt(Vec3(0.0, 0.0, 0.0), Vec3(0.0, 1.0, 0.0));  
  23.     addChild(camera);  
  24.       
  25.     //添加相机转换的开光,Label控件  
  26.     TTFConfig ttfConfig("fonts/arial.ttf", 30);  
  27.     _ambientLightLabel = Label::createWithTTF(ttfConfig,"Ambient Light ON");  
  28.     _ambientLightLabel->retain();  
  29.     auto menuItem0 = MenuItemLabel::create(_ambientLightLabel, CC_CALLBACK_1(LightTest::SwitchLight,this,LightType::AMBIENT));  
  30.     _directionalLightLabel = Label::createWithTTF(ttfConfig,"Directional Light OFF");  
  31.     _directionalLightLabel->retain();  
  32.     auto menuItem1 = MenuItemLabel::create(_directionalLightLabel, CC_CALLBACK_1(LightTest::SwitchLight,this,LightType::DIRECTIONAL));  
  33.     _pointLightLabel = Label::createWithTTF(ttfConfig,"Point Light OFF");  
  34.     _pointLightLabel->retain();  
  35.     auto menuItem2 = MenuItemLabel::create(_pointLightLabel, CC_CALLBACK_1(LightTest::SwitchLight,this,LightType::POINT));  
  36.     _spotLightLabel = Label::createWithTTF(ttfConfig,"Spot Light OFF");  
  37.     _spotLightLabel->retain();  
  38.     auto menuItem3 = MenuItemLabel::create(_spotLightLabel, CC_CALLBACK_1(LightTest::SwitchLight,this,LightType::SPOT));  
  39.     auto menu = Menu::create(menuItem0, menuItem1, menuItem2, menuItem3, nullptr);  
  40.     menu->setPosition(Vec2::ZERO);  
  41.     menuItem0->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);  
  42.     menuItem0->setPosition( Vec2(VisibleRect::left().x, VisibleRect::top().y-50) );  
  43.     menuItem1->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);  
  44.     menuItem1->setPosition( Vec2(VisibleRect::left().x, VisibleRect::top().y-100) );  
  45.     menuItem2->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);  
  46.     menuItem2->setPosition( Vec2(VisibleRect::left().x, VisibleRect::top().y -150));  
  47.     menuItem3->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);  
  48.     menuItem3->setPosition( Vec2(VisibleRect::left().x, VisibleRect::top().y -200));  
  49.     addChild(menu);  
  50. }  
  51.   
  52. //析构资源  
  53. LightTest::~LightTest()  
  54. {  
  55.     if (_spotLightLabel)  
  56.         _spotLightLabel->release();  
  57.       
  58.     if (_pointLightLabel)  
  59.         _pointLightLabel->release();  
  60.       
  61.     if (_directionalLightLabel)  
  62.         _directionalLightLabel->release();  
  63.       
  64.     if (_spotLight)  
  65.         _spotLight->release();  
  66.       
  67.     if (_pointLight)  
  68.         _pointLight->release();  
  69.       
  70.     if (_directionalLight)  
  71.         _directionalLight->release();  
  72.       
  73.     if (_ambientLight)  
  74.         _ambientLight->release();  
  75. }  
  76. //添加精灵  
  77. void LightTest::addSprite()  
  78. {  
  79.     auto s = Director::getInstance()->getWinSize();  
  80.       
  81.     {  
  82.         //创建3D对象,c3b是二进制文件,通过FBX模型文件进行二次转换得到。  
  83.         //obj:FBX是一种通用导出格式,可通过maya和max导出,maya和max默认导出obj格式文件。  
  84.         //c3t:通过FBX模型文件转换后生成的Json文件,体积大载入慢,不提倡使用  
  85.         //c3b:二进制文件,数据内容与c3t一样,体积小,速度快,提倡使用  
  86.         //注意:.c3b文件的使用需和它对应的贴图拷贝到Resource目录下,否则,运行时找不到资源。  
  87.         std::string fileName = "orc.c3b";  
  88.         //创建3D精灵对象  
  89.         auto sprite = Sprite3D::create(fileName);  
  90.         sprite->setRotation3D(Vec3(0.0, 180.0, 0.0));  
  91.         sprite->setPosition(Vec2(0.0, 0.0));  
  92.         sprite->setScale(2.0);  
  93.         //创建3D精灵对象  
  94.         auto sp = Sprite3D::create("axe.c3b");  
  95.         //通过骨骼名称获得连接点  
  96.         sprite->getAttachNode("Bip001 R Hand")->addChild(sp);  
  97.         //创建3D动画  
  98.         auto animation = Animation3D::create(fileName);  
  99.         if (animation)  
  100.         {  
  101.             auto animate = Animate3D::create(animation);  
  102.             sprite->runAction(RepeatForever::create(animate));//3D精灵执行3D动画  
  103.         }  
  104.           
  105.         addChild(sprite);  
  106.         //设置精灵的Mask,该值与相机的flag进行&运算为真,则对相机可见  
  107.         sprite->setCameraMask(2);  
  108.     }  
  109.       
  110.     {  
  111.         //同上  
  112.         std::string fileName = "sphere.c3b";  
  113.         auto sprite = Sprite3D::create(fileName);  
  114.         sprite->setPosition(Vec2(30.0, 0.0));  
  115.         addChild(sprite);  
  116.         sprite->setCameraMask(2);  
  117.     }  
  118.       
  119.     {  
  120.         //同上  
  121.         std::string fileName = "sphere.c3b";  
  122.         auto sprite = Sprite3D::create(fileName);  
  123.         sprite->setScale(0.5f);  
  124.         sprite->setPosition(Vec2(-50.0, 0.0));  
  125.         addChild(sprite);  
  126.         sprite->setCameraMask(2);  
  127.     }  
  128.       
  129.     {  
  130.         //同上  
  131.         std::string fileName = "sphere.c3b";  
  132.         auto sprite = Sprite3D::create(fileName);  
  133.         sprite->setScale(0.5f);  
  134.         sprite->setPosition(Vec2(-30.0, 10.0));  
  135.         addChild(sprite);  
  136.         sprite->setCameraMask(2);  
  137.     }  
  138. }  
  139. //添加光源  
  140. void LightTest::addLights()  
  141. {  
  142.     auto s = Director::getInstance()->getWinSize();  
  143.    
  144.     //环境光  
  145.     _ambientLight = AmbientLight::create(Color3B(200, 200, 200));  
  146.     _ambientLight->retain();  
  147.     _ambientLight->setEnabled(true);//开启光源  
  148.     addChild(_ambientLight);  
  149.     _ambientLight->setCameraMask(2);//确保相机可见  
  150.       
  151.     //方向光,参数1:光源方向.参数2:光源颜色  
  152.     _directionalLight = DirectionLight::create(Vec3(-1.0f, -1.0f, 0.0f), Color3B(200, 200, 200));  
  153.     _directionalLight->retain();  
  154.     _directionalLight->setEnabled(false);//光源关闭  
  155.     addChild(_directionalLight);  
  156.     _directionalLight->setCameraMask(2);//确保相机可见  
  157.       
  158.     //点光,参数1:光源的位置,参数2:光源的颜色。参数3:光源范围  
  159.     _pointLight = PointLight::create(Vec3(0.0f, 0.0f, 0.0f), Color3B(200, 200, 200), 10000.0f);  
  160.     _pointLight->retain();  
  161.     _pointLight->setEnabled(false);//光源关闭  
  162.     addChild(_pointLight);  
  163.     _pointLight->setCameraMask(2);//确保相机可见  
  164.       
  165.     //聚光,参数1:方向。参数2:位置。参数3:颜色,参数4:内圆弧度。参数5:外圆弧度。参数6:光源范围  
  166.     _spotLight = SpotLight::create(Vec3(-1.0f, -1.0f, 0.0f), Vec3(0.0f, 0.0f, 0.0f), Color3B(200, 200, 200), 0.0, 0.5, 10000.0f);  
  167.     _spotLight->retain();  
  168.     _spotLight->setEnabled(false);//光源关闭  
  169.     addChild(_spotLight);  
  170.     _spotLight->setCameraMask(2);//确保相机可见  
  171.       
  172.     {  
  173.         auto tintto1 = TintTo::create(4, 0, 0, 255);  
  174.         auto tintto2 = TintTo::create(4, 0, 255, 0);  
  175.         auto tintto3 = TintTo::create(4, 255, 0, 0);  
  176.         auto tintto4 = TintTo::create(4, 255, 255, 255);  
  177.         auto seq = Sequence::create(tintto1,tintto2, tintto3, tintto4, nullptr);  
  178.         _ambientLight->runAction(RepeatForever::create(seq));//光源执行渐隐动作  
  179.     }  
  180.       
  181.     {  
  182.         auto tintto1 = TintTo::create(4, 255, 0, 0);  
  183.         auto tintto2 = TintTo::create(4, 0, 255, 0);  
  184.         auto tintto3 = TintTo::create(4, 0, 0, 255);  
  185.         auto tintto4 = TintTo::create(4, 255, 255, 255);  
  186.         auto seq = Sequence::create(tintto1,tintto2, tintto3, tintto4, nullptr);  
  187.         _directionalLight->runAction(RepeatForever::create(seq));//光源执行渐隐动作  
  188.     }  
  189.       
  190.     {  
  191.         auto tintto1 = TintTo::create(4, 255, 0, 0);  
  192.         auto tintto2 = TintTo::create(4, 0, 255, 0);  
  193.         auto tintto3 = TintTo::create(4, 0, 0, 255);  
  194.         auto tintto4 = TintTo::create(4, 255, 255, 255);  
  195.         auto seq = Sequence::create(tintto2, tintto1, tintto3, tintto4, nullptr);  
  196.         _pointLight->runAction(RepeatForever::create(seq));//光源执行渐隐动作  
  197.     }  
  198.       
  199.     {  
  200.         auto tintto1 = TintTo::create(4, 255, 0, 0);  
  201.         auto tintto2 = TintTo::create(4, 0, 255, 0);  
  202.         auto tintto3 = TintTo::create(4, 0, 0, 255);  
  203.         auto tintto4 = TintTo::create(4, 255, 255, 255);  
  204.         auto seq = Sequence::create(tintto3, tintto2, tintto1, tintto4, nullptr);  
  205.         _spotLight->runAction(RepeatForever::create(seq));//光源执行渐隐动作  
  206.     }  
  207. }  
  208. //帧更新  
  209. void LightTest::update( float delta )  
  210. {  
  211.     static float angleDelta = 0.0;  
  212.       
  213.     if (_directionalLight)  
  214.     {  
  215.         _directionalLight->setRotation3D(Vec3(-45.0, -CC_RADIANS_TO_DEGREES(angleDelta), 0.0f));//设置旋转角度,宏是弧度转化为角度  
  216.     }  
  217.     //cosf与sinf:float版的cos和sin  
  218.     if (_pointLight)  
  219.     {  
  220.         _pointLight->setPositionX(100.0f * cosf(angleDelta + 2.0 * delta));  
  221.         _pointLight->setPositionY(100.0f);  
  222.         _pointLight->setPositionZ(100.0f * sinf(angleDelta + 2.0 * delta));  
  223.     }  
  224.       
  225.     if (_spotLight)  
  226.     {  
  227.         _spotLight->setPositionX(100.0f * cosf(angleDelta + 4.0 * delta));  
  228.         _spotLight->setPositionY(100.0f);  
  229.         _spotLight->setPositionZ(100.0f * sinf(angleDelta + 4.0 * delta));  
  230.         _spotLight->setDirection(-Vec3(cosf(angleDelta + 4.0 * delta), 1.0, sinf(angleDelta + 4.0 * delta)));  
  231.     }  
  232.       
  233.     angleDelta += delta;  
  234.       
  235. //    update(delta);  
  236. }  
  237. //光源开关  
  238. void LightTest::SwitchLight( Ref* sender, LightType lightType )  
  239. {  
  240.     switch (lightType)  
  241.     {  
  242.         case LightType::AMBIENT:  
  243.         {  
  244.             char str[32];  
  245.             bool isON = !_ambientLight->isEnabled();  
  246.             sprintf(str, "Ambient Light %s", isON == true"ON":"OFF");  
  247.             _ambientLight->setEnabled(isON);  
  248.             _ambientLightLabel->setString(str);  
  249.         }  
  250.             break;  
  251.               
  252.         case LightType::DIRECTIONAL:  
  253.         {  
  254.             char str[32];  
  255.             bool isON = !_directionalLight->isEnabled();  
  256.             sprintf(str, "Directional Light %s", isON == true"ON":"OFF");  
  257.             _directionalLight->setEnabled(isON);  
  258.             _directionalLightLabel->setString(str);  
  259.         }  
  260.             break;  
  261.               
  262.         case LightType::POINT:  
  263.         {  
  264.             char str[32];  
  265.             bool isON = !_pointLight->isEnabled();  
  266.             sprintf(str, "Point Light %s", isON == true"ON":"OFF");  
  267.             _pointLight->setEnabled(isON);  
  268.             _pointLightLabel->setString(str);  
  269.         }  
  270.             break;  
  271.               
  272.         case LightType::SPOT:  
  273.         {  
  274.             char str[32];  
  275.             bool isON = !_spotLight->isEnabled();  
  276.             sprintf(str, "Spot Light %s", isON == true"ON":"OFF");  
  277.             _spotLight->setEnabled(isON);  
  278.             _spotLightLabel->setString(str);  
  279.         }  
  280.             break;  
  281.               
  282.         default:  
  283.             break;  
  284.     }  
  285. }  

3、使用

在HelloWorldScene.cpp中的createScene()中,创建LightTest的对象,加入Scene
0 0
原创粉丝点击