CCLight.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /****************************************************************************
  2. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "2d/CCLight.h"
  21. #include "2d/CCScene.h"
  22. NS_CC_BEGIN
  23. void BaseLight::setIntensity(float intensity)
  24. {
  25. CC_ASSERT(intensity >= 0);
  26. _intensity = intensity;
  27. }
  28. void BaseLight::onEnter()
  29. {
  30. auto scene = getScene();
  31. if (scene)
  32. {
  33. auto &lights = scene->_lights;
  34. auto iter = std::find(lights.begin(), lights.end(), this);
  35. if (iter == lights.end())
  36. lights.push_back(this);
  37. }
  38. Node::onEnter();
  39. }
  40. void BaseLight::onExit()
  41. {
  42. auto scene = getScene();
  43. if (scene)
  44. {
  45. auto &lights = scene->_lights;
  46. auto iter = std::find(lights.begin(), lights.end(), this);
  47. if (iter != lights.end())
  48. lights.erase(iter);
  49. }
  50. Node::onExit();
  51. }
  52. void BaseLight::setRotationFromDirection( const Vec3 &direction )
  53. {
  54. float projLen = sqrt(direction.x * direction.x + direction.z * direction.z);
  55. float rotY = CC_RADIANS_TO_DEGREES(atan2f(-direction.x, -direction.z));
  56. float rotX = -CC_RADIANS_TO_DEGREES(atan2f(-direction.y, projLen));
  57. setRotation3D(Vec3(rotX, rotY, 0.0f));
  58. }
  59. BaseLight::BaseLight()
  60. : _intensity(1.0f)
  61. , _lightFlag(LightFlag::LIGHT0)
  62. , _enabled(true)
  63. {
  64. }
  65. BaseLight::~BaseLight()
  66. {
  67. }
  68. ////////////////////////////////////////////////////////////////////
  69. DirectionLight* DirectionLight::create(const Vec3 &direction, const Color3B &color)
  70. {
  71. auto light = new (std::nothrow) DirectionLight();
  72. light->setRotationFromDirection(direction);
  73. light->setColor(color);
  74. light->autorelease();
  75. return light;
  76. }
  77. void DirectionLight::setDirection(const Vec3 &dir)
  78. {
  79. setRotationFromDirection(dir);
  80. }
  81. Vec3 DirectionLight::getDirection() const
  82. {
  83. Mat4 mat = getNodeToParentTransform();
  84. return Vec3(-mat.m[8], -mat.m[9], -mat.m[10]);
  85. }
  86. Vec3 DirectionLight::getDirectionInWorld() const
  87. {
  88. Mat4 mat = getNodeToWorldTransform();
  89. return Vec3(-mat.m[8], -mat.m[9], -mat.m[10]);
  90. }
  91. DirectionLight::DirectionLight()
  92. {
  93. }
  94. DirectionLight::~DirectionLight()
  95. {
  96. }
  97. //////////////////////////////////////////////////////////////////
  98. PointLight* PointLight::create(const Vec3 &position, const Color3B &color, float range)
  99. {
  100. auto light = new (std::nothrow) PointLight();
  101. light->setPosition3D(position);
  102. light->setColor(color);
  103. light->_range = range;
  104. light->autorelease();
  105. return light;
  106. }
  107. PointLight::PointLight()
  108. {
  109. }
  110. PointLight::~PointLight()
  111. {
  112. }
  113. //////////////////////////////////////////////////////////////
  114. SpotLight* SpotLight::create(const Vec3 &direction, const Vec3 &position, const Color3B &color, float innerAngle, float outerAngle, float range)
  115. {
  116. auto light = new (std::nothrow) SpotLight();
  117. light->setRotationFromDirection(direction);
  118. light->setPosition3D(position);
  119. light->setColor(color);
  120. light->setInnerAngle(innerAngle);
  121. light->setOuterAngle(outerAngle);
  122. light->_range = range;
  123. light->autorelease();
  124. return light;
  125. }
  126. void SpotLight::setDirection(const Vec3 &dir)
  127. {
  128. setRotationFromDirection(dir);
  129. }
  130. Vec3 SpotLight::getDirection() const
  131. {
  132. Mat4 mat = getNodeToParentTransform();
  133. return Vec3(-mat.m[8], -mat.m[9], -mat.m[10]);
  134. }
  135. Vec3 SpotLight::getDirectionInWorld() const
  136. {
  137. Mat4 mat = getNodeToWorldTransform();
  138. return Vec3(-mat.m[8], -mat.m[9], -mat.m[10]);
  139. }
  140. void SpotLight::setInnerAngle(float angle)
  141. {
  142. _innerAngle = angle;
  143. _cosInnerAngle = cosf(angle);
  144. }
  145. void SpotLight::setOuterAngle(float angle)
  146. {
  147. _outerAngle = angle;
  148. _cosOuterAngle = cosf(angle);
  149. }
  150. SpotLight::SpotLight()
  151. {
  152. }
  153. SpotLight::~SpotLight()
  154. {
  155. }
  156. /////////////////////////////////////////////////////////////
  157. AmbientLight* AmbientLight::create( const Color3B &color )
  158. {
  159. auto light = new (std::nothrow) AmbientLight();
  160. light->setColor(color);
  161. light->autorelease();
  162. return light;
  163. }
  164. AmbientLight::AmbientLight()
  165. {
  166. }
  167. AmbientLight::~AmbientLight()
  168. {
  169. }
  170. NS_CC_END