PuzzleSDK
LightTest.cpp
浏览该文件的文档.
1/****************************************************************************
2 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
3
4 http://www.cocos2d-x.org
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 ****************************************************************************/
24
25#include "LightTest.h"
26
28
29LightTests::LightTests()
30{
32}
33
35 : _directionalLight(nullptr)
36 , _pointLight(nullptr)
37 , _spotLight(nullptr)
38{
39 addSprite();
40 addLights();
41 scheduleUpdate();
42
43 auto s = Director::getInstance()->getWinSize();
44 auto camera = Camera::createPerspective(60, (float)s.width/s.height, 1.0f, 1000.0f);
45 camera->setCameraFlag(CameraFlag::USER1);
46 camera->setPosition3D(Vec3(0.0f, 100.0f, 100.0f));
47 camera->lookAt(Vec3(0.0f, 0.0f, 0.0f), Vec3(0.0f, 1.0f, 0.0f));
48 addChild(camera);
49
50 TTFConfig ttfConfig("fonts/arial.ttf", 15);
51 _ambientLightLabel = Label::createWithTTF(ttfConfig,"Ambient Light ON");
52 _ambientLightLabel->retain();
53 auto menuItem0 = MenuItemLabel::create(_ambientLightLabel, CC_CALLBACK_1(LightTest::SwitchLight,this,LightType::AMBIENT));
54 _directionalLightLabel = Label::createWithTTF(ttfConfig,"Directional Light OFF");
55 _directionalLightLabel->retain();
56 auto menuItem1 = MenuItemLabel::create(_directionalLightLabel, CC_CALLBACK_1(LightTest::SwitchLight,this,LightType::DIRECTIONAL));
57 _pointLightLabel = Label::createWithTTF(ttfConfig,"Point Light OFF");
58 _pointLightLabel->retain();
59 auto menuItem2 = MenuItemLabel::create(_pointLightLabel, CC_CALLBACK_1(LightTest::SwitchLight,this,LightType::POINT));
60 _spotLightLabel = Label::createWithTTF(ttfConfig,"Spot Light OFF");
61 _spotLightLabel->retain();
62 auto menuItem3 = MenuItemLabel::create(_spotLightLabel, CC_CALLBACK_1(LightTest::SwitchLight,this,LightType::SPOT));
63 auto menu = Menu::create(menuItem0, menuItem1, menuItem2, menuItem3, nullptr);
64 menu->setPosition(Vec2::ZERO);
65 menuItem0->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
66 menuItem0->setPosition( Vec2(VisibleRect::left().x, VisibleRect::top().y-50) );
67 menuItem1->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
68 menuItem1->setPosition( Vec2(VisibleRect::left().x, VisibleRect::top().y-100) );
69 menuItem2->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
70 menuItem2->setPosition( Vec2(VisibleRect::left().x, VisibleRect::top().y -150));
71 menuItem3->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
72 menuItem3->setPosition( Vec2(VisibleRect::left().x, VisibleRect::top().y -200));
73 addChild(menu);
74}
75
76
78{
80 _spotLightLabel->release();
81
83 _pointLightLabel->release();
84
86 _directionalLightLabel->release();
87
88 if (_spotLight)
89 _spotLight->release();
90
91 if (_pointLight)
92 _pointLight->release();
93
95 _directionalLight->release();
96
97 if (_ambientLight)
98 _ambientLight->release();
99}
100
101std::string LightTest::title() const
102{
103 return "Light Test";
104}
105
107{
108 auto s = Director::getInstance()->getWinSize();
109
110 //{
111 // std::string fileName = "Sprite3DTest/plane.c3b";
112 // auto sprite = Sprite3D::create(fileName);
113 // sprite->setRotation3D(Vec3(-90.0, 0.0, 0.0));
114 // sprite->setScale(5.0f);
115 // sprite->setPosition(Vec2(0.0, -50.0));
116 // addChild(sprite);
117 // sprite->setCameraMask(2);
118 //}
119
120 {
121 std::string fileName = "Sprite3DTest/orc.c3b";
122 auto sprite = Sprite3D::create(fileName);
123 sprite->setRotation3D(Vec3(0.0f, 180.0f, 0.0f));
124 sprite->setPosition(Vec2(0.0f, 0.0f));
125 sprite->setScale(2.0);
126 auto sp = Sprite3D::create("Sprite3DTest/axe.c3b");
127 sprite->getAttachNode("Bip001 R Hand")->addChild(sp);
128 auto animation = Animation3D::create(fileName);
129 if (animation)
130 {
131 auto animate = Animate3D::create(animation);
132 sprite->runAction(RepeatForever::create(animate));
133 }
134
135 addChild(sprite);
136 sprite->setCameraMask(2);
137 }
138
139 {
140 std::string fileName = "Sprite3DTest/sphere.c3b";
141 auto sprite = Sprite3D::create(fileName);
142 sprite->setPosition(Vec2(30.0f, 0.0f));
143 addChild(sprite);
144 sprite->setCameraMask(2);
145 }
146
147 {
148 std::string fileName = "Sprite3DTest/sphere.c3b";
149 auto sprite = Sprite3D::create(fileName);
150 sprite->setScale(0.5f);
151 sprite->setPosition(Vec2(-50.0f, 0.0f));
152 addChild(sprite);
153 sprite->setCameraMask(2);
154 }
155
156 {
157 std::string fileName = "Sprite3DTest/sphere.c3b";
158 auto sprite = Sprite3D::create(fileName);
159 sprite->setScale(0.5f);
160 sprite->setPosition(Vec2(-30.0f, 10.0f));
161 addChild(sprite);
162 sprite->setCameraMask(2);
163 }
164}
165
167{
168 auto s = Director::getInstance()->getWinSize();
169 _ambientLight = AmbientLight::create(Color3B(200, 200, 200));
170 _ambientLight->retain();
171 _ambientLight->setEnabled(true);
172 addChild(_ambientLight);
173 _ambientLight->setCameraMask(2);
174
175 _directionalLight = DirectionLight::create(Vec3(-1.0f, -1.0f, 0.0f), Color3B(200, 200, 200));
176 _directionalLight->retain();
177 _directionalLight->setEnabled(false);
178 addChild(_directionalLight);
179 _directionalLight->setCameraMask(2);
180
181 _pointLight = PointLight::create(Vec3(0.0f, 0.0f, 0.0f), Color3B(200, 200, 200), 10000.0f);
182 _pointLight->retain();
183 _pointLight->setEnabled(false);
184 addChild(_pointLight);
185 _pointLight->setCameraMask(2);
186
187 _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);
188 _spotLight->retain();
189 _spotLight->setEnabled(false);
190 addChild(_spotLight);
191 _spotLight->setCameraMask(2);
192
193 {
194 auto tintto1 = TintTo::create(4, 0, 0, 255);
195 auto tintto2 = TintTo::create(4, 0, 255, 0);
196 auto tintto3 = TintTo::create(4, 255, 0, 0);
197 auto tintto4 = TintTo::create(4, 255, 255, 255);
198 auto seq = Sequence::create(tintto1,tintto2, tintto3, tintto4, nullptr);
199 _ambientLight->runAction(RepeatForever::create(seq));
200 }
201
202 {
203 auto tintto1 = TintTo::create(4, 255, 0, 0);
204 auto tintto2 = TintTo::create(4, 0, 255, 0);
205 auto tintto3 = TintTo::create(4, 0, 0, 255);
206 auto tintto4 = TintTo::create(4, 255, 255, 255);
207 auto seq = Sequence::create(tintto1,tintto2, tintto3, tintto4, nullptr);
208 _directionalLight->runAction(RepeatForever::create(seq));
209 }
210
211 {
212 auto tintto1 = TintTo::create(4, 255, 0, 0);
213 auto tintto2 = TintTo::create(4, 0, 255, 0);
214 auto tintto3 = TintTo::create(4, 0, 0, 255);
215 auto tintto4 = TintTo::create(4, 255, 255, 255);
216 auto seq = Sequence::create(tintto2, tintto1, tintto3, tintto4, nullptr);
217 _pointLight->runAction(RepeatForever::create(seq));
218 }
219
220 {
221 auto tintto1 = TintTo::create(4, 255, 0, 0);
222 auto tintto2 = TintTo::create(4, 0, 255, 0);
223 auto tintto3 = TintTo::create(4, 0, 0, 255);
224 auto tintto4 = TintTo::create(4, 255, 255, 255);
225 auto seq = Sequence::create(tintto3, tintto2, tintto1, tintto4, nullptr);
226 _spotLight->runAction(RepeatForever::create(seq));
227 }
228}
229
230void LightTest::update( float delta )
231{
232 static float angleDelta = 0.0;
233
235 {
236 _directionalLight->setRotation3D(Vec3(-45.0, -CC_RADIANS_TO_DEGREES(angleDelta), 0.0f));
237 }
238
239 if (_pointLight)
240 {
241 _pointLight->setPositionX(100.0f * cosf(angleDelta + 2.0 * delta));
242 _pointLight->setPositionY(100.0f);
243 _pointLight->setPositionZ(100.0f * sinf(angleDelta + 2.0 * delta));
244 }
245
246 if (_spotLight)
247 {
248 _spotLight->setPositionX(100.0f * cosf(angleDelta + 4.0 * delta));
249 _spotLight->setPositionY(100.0f);
250 _spotLight->setPositionZ(100.0f * sinf(angleDelta + 4.0 * delta));
251 _spotLight->setDirection(-Vec3(cosf(angleDelta + 4.0 * delta), 1.0, sinf(angleDelta + 4.0 * delta)));
252 }
253
254 angleDelta += delta;
255
256 TestCase::update(delta);
257}
258
259void LightTest::SwitchLight( Ref* sender, LightType lightType )
260{
261 switch (lightType)
262 {
263 case LightType::AMBIENT:
264 {
265 char str[32];
266 bool isON = !_ambientLight->isEnabled();
267 sprintf(str, "Ambient Light %s", isON == true? "ON":"OFF");
268 _ambientLight->setEnabled(isON);
269 _ambientLightLabel->setString(str);
270 }
271 break;
272
273 case LightType::DIRECTIONAL:
274 {
275 char str[32];
276 bool isON = !_directionalLight->isEnabled();
277 sprintf(str, "Directional Light %s", isON == true? "ON":"OFF");
278 _directionalLight->setEnabled(isON);
279 _directionalLightLabel->setString(str);
280 }
281 break;
282
283 case LightType::POINT:
284 {
285 char str[32];
286 bool isON = !_pointLight->isEnabled();
287 sprintf(str, "Point Light %s", isON == true? "ON":"OFF");
288 _pointLight->setEnabled(isON);
289 _pointLightLabel->setString(str);
290 }
291 break;
292
293 case LightType::SPOT:
294 {
295 char str[32];
296 bool isON = !_spotLight->isEnabled();
297 sprintf(str, "Spot Light %s", isON == true? "ON":"OFF");
298 _spotLight->setEnabled(isON);
299 _spotLightLabel->setString(str);
300 }
301 break;
302
303 default:
304 break;
305 }
306}
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
USING_NS_CC
Definition: LightTest.cpp:27
cocos2d::Label * _pointLightLabel
Definition: LightTest.h:60
void SwitchLight(cocos2d::Ref *sender, cocos2d::LightType lightType)
Definition: LightTest.cpp:259
cocos2d::Label * _directionalLightLabel
Definition: LightTest.h:59
cocos2d::Label * _spotLightLabel
Definition: LightTest.h:61
cocos2d::DirectionLight * _directionalLight
Definition: LightTest.h:54
cocos2d::PointLight * _pointLight
Definition: LightTest.h:55
cocos2d::Label * _ambientLightLabel
Definition: LightTest.h:58
void addSprite()
Definition: LightTest.cpp:106
virtual ~LightTest()
Definition: LightTest.cpp:77
virtual void update(float delta) override
Definition: LightTest.cpp:230
cocos2d::AmbientLight * _ambientLight
Definition: LightTest.h:53
void addLights()
Definition: LightTest.cpp:166
cocos2d::SpotLight * _spotLight
Definition: LightTest.h:56
virtual std::string title() const override
Definition: LightTest.cpp:101
static cocos2d::Vec2 top()
Definition: VisibleRect.cpp:57
static cocos2d::Vec2 left()
Definition: VisibleRect.cpp:45