PolySceneLight.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include "PolyGlobals.h"
  21. #include "PolyEntity.h"
  22. namespace Polycode {
  23. class Scene;
  24. class Camera;
  25. class Mesh;
  26. class Texture;
  27. // class ScenePrimitive;
  28. /**
  29. * 3D light source. Lights can be point or spot lights and can be set to different colors.
  30. */
  31. class _PolyExport SceneLight : public Entity {
  32. public:
  33. /**
  34. * Constructs a light with parameters.
  35. * @param type Type of light to create. Can be SceneLight::POINT_LIGHT or SceneLight::SPOT_LIGHT
  36. * @param parentScene Scene to light.
  37. * @param intensity Light color intensity
  38. * @param constantAttenuation Constant falloff attenuation value
  39. * @param linearAttenuation Linear falloff attenuation value
  40. * @param quadraticAttenuation Quadratic falloff attenuation value
  41. */
  42. SceneLight(int type, Scene *parentScene, Number intensity, Number constantAttenuation=1, Number linearAttenuation=1, Number quadraticAttenuation=1);
  43. virtual ~SceneLight();
  44. /*
  45. * Returns the light's intensity.
  46. */
  47. Number getIntensity() const;
  48. /**
  49. * Sets the light's intensity
  50. * @param newIntensity New intensity value.
  51. */
  52. void setIntensity(Number newIntensity);
  53. /**
  54. * Sets the attenuation values for the light.
  55. * @param constantAttenuation Constant falloff attenuation value
  56. * @param linearAttenuation Linear falloff attenuation value
  57. * @param quadraticAttenuation Quadratic falloff attenuation value
  58. *
  59. */
  60. void setAttenuation(Number constantAttenuation, Number linearAttenuation, Number quadraticAttenuation);
  61. Number getConstantAttenuation() const { return constantAttenuation; }
  62. Number getLinearAttenuation() const { return linearAttenuation; }
  63. Number getQuadraticAttenuation() const { return quadraticAttenuation; }
  64. /*
  65. * Returns the light's type.
  66. */
  67. int getType() const;
  68. void renderDepthMap(Scene *scene);
  69. void Render();
  70. const Matrix4& getLightViewMatrix() const;
  71. static const int POINT_LIGHT = 0;
  72. static const int SPOT_LIGHT = 1;
  73. Texture *getZBufferTexture() const;
  74. /**
  75. * Color of the light.
  76. */
  77. Color specularLightColor;
  78. /**
  79. * Sets the light color.
  80. * @param r Red value 0-1.
  81. * @param g Green value 0-1
  82. * @param b Blue value 0-1
  83. * @param a Alpha value 0-1
  84. */
  85. void setSpecularLightColor(Number r, Number g, Number b, Number a) { specularLightColor.r = r; specularLightColor.g = g; specularLightColor.b = b; specularLightColor.a = a; }
  86. /**
  87. * Color of the light.
  88. */
  89. Color lightColor;
  90. /**
  91. * Sets the light color.
  92. * @param r Red value 0-1.
  93. * @param g Green value 0-1
  94. * @param b Blue value 0-1
  95. * @param a Alpha value 0-1
  96. */
  97. void setDiffuseLightColor(Number r, Number g, Number b) { lightColor.r = r; lightColor.g = g; lightColor.b = b; }
  98. /**
  99. * Sets both the specular and diffust light colors. Use setDiffuseLightColor and setSpecularLightColor to set the individual light colors.
  100. * @param r Red value 0-1.
  101. * @param g Green value 0-1
  102. * @param b Blue value 0-1
  103. * @param a Alpha value 0-1
  104. */
  105. void setLightColor(Number r, Number g, Number b, Number a=1.0) {
  106. setDiffuseLightColor(r,g,b);
  107. setSpecularLightColor(r,g,b,a);
  108. }
  109. /**
  110. * Sets the spotlight properties. These control the shape of the spotlight beam.
  111. * @param spotlightExponent Spotlight exponent size
  112. * @param spotlightCutoff Spotlight furstrum cutoff.
  113. */
  114. void setSpotlightProperties(Number spotlightCutoff, Number spotlightExponent) {
  115. this->spotlightCutoff = spotlightCutoff;
  116. this->spotlightExponent = spotlightExponent;
  117. }
  118. Number getSpotlightCutoff() const { return spotlightCutoff; }
  119. Number getSpotlightExponent() const { return spotlightExponent; }
  120. /**
  121. * If this is called with 'true', the light will generate a shadow map.
  122. * @param val If set to true, enables this light to cast shadows.
  123. * @param resolution Resolution of the shadow map. (defaults to 256x256).
  124. */
  125. void enableShadows(bool val, unsigned int resolution=256);
  126. /**
  127. * This sets the shadow map field of view. The larger the field of view, the more of the scene it encompasses, but the more quality it loses.
  128. * @param fov New field of view value.
  129. */
  130. void setShadowMapFOV(Number fov);
  131. /**
  132. * Returns the light's shadow map field of view.
  133. */
  134. Number getShadowMapFOV() const;
  135. unsigned int getShadowMapResolution() const;
  136. /**
  137. * Returns true if shadows are enabled.
  138. */
  139. bool areShadowsEnabled() const;
  140. /**
  141. * Returns the light type.
  142. */
  143. int getLightType() const { return type; }
  144. void setLightImportance(int newImportance);
  145. int getLightImportance() const;
  146. void setLightType(int lightType);
  147. virtual Entity *Clone(bool deepClone, bool ignoreEditorOnly) const;
  148. virtual void applyClone(Entity *clone, bool deepClone, bool ignoreEditorOnly) const;
  149. Scene *getParentScene() const;
  150. void setParentScene(Scene *scene);
  151. Camera *getSpotlightCamera();
  152. protected:
  153. Number spotlightExponent;
  154. Number spotlightCutoff;
  155. int lightImportance;
  156. Number constantAttenuation;
  157. Number linearAttenuation;
  158. Number quadraticAttenuation;
  159. int type;
  160. Number intensity;
  161. Camera *spotCamera;
  162. Texture *zBufferTexture;
  163. Scene *parentScene;
  164. Matrix4 lightViewMatrix;
  165. unsigned int shadowMapRes;
  166. Number shadowMapFOV;
  167. bool shadowsEnabled;
  168. Number distance;
  169. Mesh *lightMesh;
  170. };
  171. }