CCLight.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include "2d/CCLight.h"
  2. #include "2d/CCScene.h"
  3. NS_CC_BEGIN
  4. void BaseLight::setIntensity(float intensity)
  5. {
  6. CC_ASSERT(intensity >= 0);
  7. _intensity = intensity;
  8. }
  9. void BaseLight::onEnter()
  10. {
  11. auto scene = getScene();
  12. if (scene)
  13. {
  14. auto &lights = scene->_lights;
  15. auto iter = std::find(lights.begin(), lights.end(), this);
  16. if (iter == lights.end())
  17. lights.push_back(this);
  18. }
  19. Node::onEnter();
  20. }
  21. void BaseLight::onExit()
  22. {
  23. auto scene = getScene();
  24. if (scene)
  25. {
  26. auto &lights = scene->_lights;
  27. auto iter = std::find(lights.begin(), lights.end(), this);
  28. if (iter != lights.end())
  29. lights.erase(iter);
  30. }
  31. Node::onExit();
  32. }
  33. void BaseLight::setRotationFromDirection( const Vec3 &direction )
  34. {
  35. float projLen = sqrt(direction.x * direction.x + direction.z * direction.z);
  36. float rotY = CC_RADIANS_TO_DEGREES(atan2f(-direction.x, -direction.z));
  37. float rotX = -CC_RADIANS_TO_DEGREES(atan2f(-direction.y, projLen));
  38. setRotation3D(Vec3(rotX, rotY, 0.0f));
  39. }
  40. BaseLight::BaseLight()
  41. : _intensity(1.0f)
  42. , _lightFlag(LightFlag::LIGHT0)
  43. , _enabled(true)
  44. {
  45. }
  46. BaseLight::~BaseLight()
  47. {
  48. }
  49. ////////////////////////////////////////////////////////////////////
  50. DirectionLight* DirectionLight::create(const Vec3 &direction, const Color3B &color)
  51. {
  52. auto light = new (std::nothrow) DirectionLight();
  53. light->setRotationFromDirection(direction);
  54. light->setColor(color);
  55. light->autorelease();
  56. return light;
  57. }
  58. void DirectionLight::setDirection(const Vec3 &dir)
  59. {
  60. setRotationFromDirection(dir);
  61. }
  62. Vec3 DirectionLight::getDirection() const
  63. {
  64. Mat4 mat = getNodeToParentTransform();
  65. return Vec3(-mat.m[8], -mat.m[9], -mat.m[10]);
  66. }
  67. Vec3 DirectionLight::getDirectionInWorld() const
  68. {
  69. Mat4 mat = getNodeToWorldTransform();
  70. return Vec3(-mat.m[8], -mat.m[9], -mat.m[10]);
  71. }
  72. DirectionLight::DirectionLight()
  73. {
  74. }
  75. DirectionLight::~DirectionLight()
  76. {
  77. }
  78. //////////////////////////////////////////////////////////////////
  79. PointLight* PointLight::create(const Vec3 &position, const Color3B &color, float range)
  80. {
  81. auto light = new (std::nothrow) PointLight();
  82. light->setPosition3D(position);
  83. light->setColor(color);
  84. light->_range = range;
  85. light->autorelease();
  86. return light;
  87. }
  88. PointLight::PointLight()
  89. {
  90. }
  91. PointLight::~PointLight()
  92. {
  93. }
  94. //////////////////////////////////////////////////////////////
  95. SpotLight* SpotLight::create(const Vec3 &direction, const Vec3 &position, const Color3B &color, float innerAngle, float outerAngle, float range)
  96. {
  97. auto light = new (std::nothrow) SpotLight();
  98. light->setRotationFromDirection(direction);
  99. light->setPosition3D(position);
  100. light->setColor(color);
  101. light->setInnerAngle(innerAngle);
  102. light->setOuterAngle(outerAngle);
  103. light->_range = range;
  104. light->autorelease();
  105. return light;
  106. }
  107. void SpotLight::setDirection(const Vec3 &dir)
  108. {
  109. setRotationFromDirection(dir);
  110. }
  111. Vec3 SpotLight::getDirection() const
  112. {
  113. Mat4 mat = getNodeToParentTransform();
  114. return Vec3(-mat.m[8], -mat.m[9], -mat.m[10]);
  115. }
  116. Vec3 SpotLight::getDirectionInWorld() const
  117. {
  118. Mat4 mat = getNodeToWorldTransform();
  119. return Vec3(-mat.m[8], -mat.m[9], -mat.m[10]);
  120. }
  121. void SpotLight::setInnerAngle(float angle)
  122. {
  123. _innerAngle = angle;
  124. _cosInnerAngle = cosf(angle);
  125. }
  126. void SpotLight::setOuterAngle(float angle)
  127. {
  128. _outerAngle = angle;
  129. _cosOuterAngle = cosf(angle);
  130. }
  131. SpotLight::SpotLight()
  132. {
  133. }
  134. SpotLight::~SpotLight()
  135. {
  136. }
  137. /////////////////////////////////////////////////////////////
  138. AmbientLight* AmbientLight::create( const Color3B &color )
  139. {
  140. auto light = new (std::nothrow) AmbientLight();
  141. light->setColor(color);
  142. light->autorelease();
  143. return light;
  144. }
  145. AmbientLight::AmbientLight()
  146. {
  147. }
  148. AmbientLight::~AmbientLight()
  149. {
  150. }
  151. NS_CC_END