Light.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #include "Base.h"
  2. #include "Light.h"
  3. #include "DAESceneEncoder.h"
  4. namespace gameplay
  5. {
  6. Light::Light(void) :
  7. _lightType(0),
  8. _constantAttenuation(0.0f),
  9. _linearAttenuation(0.0f),
  10. _quadraticAttenuation(0.0f),
  11. _falloffAngle(0.0f),
  12. _falloffExponent(0.0f),
  13. _range(-1.0f),
  14. _innerAngle(-1.0f),
  15. _outerAngle(0.0f)
  16. {
  17. fillArray(_color, 0.0f, COLOR_SIZE);
  18. }
  19. Light::~Light(void)
  20. {
  21. }
  22. unsigned int Light::getTypeId(void) const
  23. {
  24. return LIGHT_ID;
  25. }
  26. const char* Light::getElementName(void) const
  27. {
  28. return "Light";
  29. }
  30. float Light::computeRange(float constantAttenuation, float linearAttenuation, float quadraticAttenuation)
  31. {
  32. if (constantAttenuation == 0.0f && linearAttenuation == 0.0f && quadraticAttenuation == 0.0f)
  33. {
  34. return 0.0f;
  35. }
  36. // Constant Attenuation is currently not supported.
  37. if (constantAttenuation == 1.0f)
  38. {
  39. return 1.0f;
  40. }
  41. const float step = 0.01f;
  42. float range = 0.01f;
  43. float att = 1.0f;
  44. while (att > 0.01f)
  45. {
  46. att = 1 / (constantAttenuation + (range * linearAttenuation) + (range * range * quadraticAttenuation));
  47. range += step;
  48. }
  49. return range;
  50. }
  51. float lerpstep( float lower, float upper, float s)
  52. {
  53. float lerpstep = ( s - lower ) / ( upper - lower );
  54. if (lerpstep < 0.0f)
  55. lerpstep = 0.0f;
  56. else if (lerpstep > 1.0f)
  57. lerpstep = 1.0f;
  58. return lerpstep;
  59. }
  60. float Light::computeInnerAngle(float outerAngle)
  61. {
  62. const float epsilon = 0.15f;
  63. // Set inner angle to half of outer angle.
  64. float innerAngle = outerAngle / 2.0f;
  65. // Try to find the inner angle by sampling the attenuation with steps of angle.
  66. for (float angle = 0.0; angle < outerAngle; angle += epsilon)
  67. {
  68. float outerCosAngle = cos(MATH_DEG_TO_RAD(outerAngle));
  69. float innerCosAngle = cos(MATH_DEG_TO_RAD(innerAngle));
  70. float cosAngle = cos(MATH_DEG_TO_RAD(angle));
  71. float att = lerpstep(outerCosAngle, innerCosAngle, cosAngle);
  72. if (att < 1.0f)
  73. {
  74. innerAngle = angle;
  75. break;
  76. }
  77. }
  78. return innerAngle;
  79. }
  80. void Light::writeBinary(FILE* file)
  81. {
  82. Object::writeBinary(file);
  83. write(_lightType, file);
  84. write(_color, COLOR_SIZE, file);
  85. // Compute an approximate light range with Collada's attenuation parameters.
  86. // This facilitates bringing in the light nodes directly from maya to gameplay.
  87. if (_range == -1.0f)
  88. {
  89. _range = computeRange(_constantAttenuation, _linearAttenuation, _quadraticAttenuation);
  90. }
  91. if (_lightType == SpotLight)
  92. {
  93. // Compute an approximate inner angle of the spot light using Collada's outer angle.
  94. _outerAngle = _falloffAngle / 2.0f;
  95. if (_innerAngle == -1.0f)
  96. {
  97. _innerAngle = computeInnerAngle(_outerAngle);
  98. }
  99. write(_range, file);
  100. write(MATH_DEG_TO_RAD(_innerAngle), file);
  101. write(MATH_DEG_TO_RAD(_outerAngle), file);
  102. }
  103. else if (_lightType == PointLight)
  104. {
  105. write(_range, file);
  106. }
  107. }
  108. void Light::writeText(FILE* file)
  109. {
  110. fprintElementStart(file);
  111. fprintfElement(file, "lightType", _lightType);
  112. fprintfElement(file, "color", _color, COLOR_SIZE);
  113. // Compute an approximate light range with Collada's attenuation parameters.
  114. // This facilitates bringing in the light nodes directly from maya to gameplay.
  115. if (_range == -1.0f)
  116. {
  117. _range = computeRange(_constantAttenuation, _linearAttenuation, _quadraticAttenuation);
  118. }
  119. if (_lightType == SpotLight)
  120. {
  121. // Compute an approximate inner angle of the spot light using Collada's outer angle.
  122. _outerAngle = _falloffAngle / 2.0f;
  123. if (_innerAngle == -1.0f)
  124. {
  125. _innerAngle = computeInnerAngle(_outerAngle);
  126. }
  127. fprintfElement(file, "range", _range);
  128. fprintfElement(file, "innerAngle", MATH_DEG_TO_RAD(_innerAngle));
  129. fprintfElement(file, "outerAngle", MATH_DEG_TO_RAD(_outerAngle));
  130. }
  131. else if (_lightType == PointLight)
  132. {
  133. fprintfElement(file, "range", _range);
  134. }
  135. fprintElementEnd(file);
  136. }
  137. float Light::getRed() const
  138. {
  139. return _color[0];
  140. }
  141. float Light::getGreen() const
  142. {
  143. return _color[1];
  144. }
  145. float Light::getBlue() const
  146. {
  147. return _color[2];
  148. }
  149. bool Light::isAmbient() const
  150. {
  151. return _lightType == AmbientLight;
  152. }
  153. void Light::setAmbientLight()
  154. {
  155. _lightType = AmbientLight;
  156. }
  157. void Light::setDirectionalLight()
  158. {
  159. _lightType = DirectionalLight;
  160. }
  161. void Light::setPointLight()
  162. {
  163. _lightType = PointLight;
  164. }
  165. void Light::setSpotLight()
  166. {
  167. _lightType = SpotLight;
  168. }
  169. void Light::setColor(float r, float g, float b)
  170. {
  171. _color[0] = r;
  172. _color[1] = g;
  173. _color[2] = b;
  174. }
  175. void Light::setConstantAttenuation(float value)
  176. {
  177. _constantAttenuation = value;
  178. }
  179. void Light::setLinearAttenuation(float value)
  180. {
  181. _linearAttenuation = value;
  182. }
  183. void Light::setQuadraticAttenuation(float value)
  184. {
  185. _quadraticAttenuation = value;
  186. }
  187. void Light::setFalloffAngle(float value)
  188. {
  189. _falloffAngle = value;
  190. }
  191. void Light::setFalloffExponent(float value)
  192. {
  193. _falloffExponent = value;
  194. if ( value != 1.0)
  195. {
  196. printf("Warning: spot light falloff_exponent must be 1.0. \n");
  197. }
  198. }
  199. }