Light.cpp 5.4 KB

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