PolySceneLight.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 "PolyString.h"
  21. #include "PolyGlobals.h"
  22. #include "PolySceneEntity.h"
  23. #include "PolyCoreServices.h"
  24. #include "PolyScene.h"
  25. #include "PolyCamera.h"
  26. #include "PolyMesh.h"
  27. namespace Polycode {
  28. class Scene;
  29. class Camera;
  30. /**
  31. * 3D light source. Lights can be area or spot lights and can be set to different colors.
  32. */
  33. class _PolyExport SceneLight : public SceneEntity {
  34. public:
  35. /**
  36. * Constructs a light with parameters.
  37. * @param type Type of light to create. Can be SceneLight::AREA_LIGHT or SceneLight::SPOT_LIGHT
  38. * @param intensity Light intensity.
  39. * @param distance Light falloff disatance.
  40. * @param parentScene Scene to light. The reason this parameter is required is for shadow buffer generation.
  41. */
  42. SceneLight(int type, Number intensity, Number distance, Scene *parentScene);
  43. virtual ~SceneLight();
  44. /*
  45. * Returns the light's intensity.
  46. */
  47. Number getIntensity();
  48. /*
  49. * Returns the light's falloff distance.
  50. */
  51. Number getDistance();
  52. /*
  53. * Returns the light's type.
  54. */
  55. int getType();
  56. void renderDepthMap(Scene *scene);
  57. void Render();
  58. Matrix4 getLightViewMatrix();
  59. static const int AREA_LIGHT = 0;
  60. static const int SPOT_LIGHT = 1;
  61. Texture *getZBufferTexture();
  62. /**
  63. * Color of the light.
  64. */
  65. Color lightColor;
  66. /**
  67. * Sets the light color.
  68. * @param r Red value 0-1.
  69. * @param g Green value 0-1
  70. * @param b Blue value 0-1
  71. * @param a Alpha value 0-1
  72. */
  73. void setLightColor(Number r, Number g, Number b) { lightColor.r = r; lightColor.g = g; lightColor.b = b; }
  74. /**
  75. * If this is called with 'true', the light will generate a shadow map.
  76. * @param val If set to true, enables this light to cast shadows.
  77. * @param resolution Resolution of the shadow map. (defaults to 256x256).
  78. */
  79. void enableShadows(bool val, Number resolution=256);
  80. /**
  81. * 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.
  82. * @param fov New field of view value.
  83. */
  84. void setShadowMapFOV(Number fov);
  85. /**
  86. * Returns true if shadows are enabled.
  87. */
  88. bool areShadowsEnabled();
  89. /**
  90. * Returns the light type.
  91. */
  92. int getLightType() { return type; }
  93. private:
  94. int type;
  95. Number intensity;
  96. Camera *spotCamera;
  97. Texture *zBufferTexture;
  98. Scene *parentScene;
  99. Matrix4 lightViewMatrix;
  100. Number shadowMapRes;
  101. Number shadowMapFOV;
  102. bool shadowsEnabled;
  103. Number distance;
  104. Mesh *lightMesh;
  105. };
  106. }