PolyCamera.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. #include "PolyVector2.h"
  23. namespace Polycode {
  24. class Scene;
  25. class Material;
  26. class ShaderBinding;
  27. class Texture;
  28. /**
  29. * Camera in a 3D scene. Cameras can be added to a scene and changed between dynamically. You can also set a shader to a camera that will run as a screen shader for post-processing effects.
  30. */
  31. class _PolyExport Camera : public Entity {
  32. public:
  33. enum ProjectionMode {
  34. ORTHO_SIZE_MANUAL = 0,
  35. ORTHO_SIZE_LOCK_HEIGHT = 1,
  36. ORTHO_SIZE_LOCK_WIDTH = 2,
  37. ORTHO_SIZE_VIEWPORT = 3,
  38. PERSPECTIVE_FOV = 4,
  39. PERSPECTIVE_FRUSTUM = 5
  40. };
  41. /**
  42. * Constructor.
  43. * @param parentScene Scene to add the camera to.
  44. */
  45. Camera(Scene *parentScene);
  46. virtual ~Camera();
  47. void buildFrustumPlanes();
  48. /**
  49. * Checks if the camera can see a sphere.
  50. * @param pos Position of the sphere to check.
  51. * @param fRadius Radius of the sphere.
  52. * @return Returns true if the sphere is within the camera's frustum, or false if it isn't.
  53. * @see canSee()
  54. */
  55. bool isSphereInFrustum(Vector3 pos, Number fRadius);
  56. /**
  57. * Checks if the camera can see an entity based on its bounding radius.
  58. * @param entity Entity to check.
  59. * @return Returns true if the entity's bounding radius is within the camera's frustum, or false if it isn't.
  60. * @see isSphereInFrustum()
  61. */
  62. bool canSee(Entity *entity);
  63. /**
  64. * Toggles orthographic projection mode for camera.
  65. * @param mode If true, sets the camera into orthographic projection mode.
  66. * @param orthoSizeX Width of the orthographic frustum (defaults to 1.0)
  67. * @param orthoSizeY Height of the orthographic frustum (defaults to 1.0)
  68. */
  69. void setOrthoMode(bool mode);
  70. void setOrthoSize(Number orthoSizeX, Number orthoSizeY);
  71. /** Switches into frustum mode and sets up the planes. */
  72. void setFrustumMode(Number left, Number right, Number bottom, Number top, Number front, Number back);
  73. /**
  74. * Returns true if camera is in orthographic projection mode.
  75. * @return True if camera is orthographic, false if otherwise.
  76. */
  77. bool getOrthoMode() {
  78. return projectionMode == ORTHO_SIZE_MANUAL ||
  79. projectionMode == ORTHO_SIZE_LOCK_HEIGHT ||
  80. projectionMode == ORTHO_SIZE_LOCK_WIDTH ||
  81. projectionMode == ORTHO_SIZE_VIEWPORT;
  82. }
  83. /**
  84. * Returns the width of the camera's orthographic frustum.
  85. * @return Width of the camera's orthographic frustum.
  86. */
  87. Number getOrthoSizeX();
  88. /**
  89. * Returns the height of the camera's orthographic frustum.
  90. * @return Height of the camera's orthographic frustum.
  91. */
  92. Number getOrthoSizeY();
  93. /**
  94. * Sets the field of view (FOV) for the camera. The larger the field of view, the more the camera can see, the smaller it is, the more zoomed in it is.
  95. * @param fov The new FOV value.
  96. */
  97. void setFOV(Number fov);
  98. /**
  99. * Returns the current FOV value for the camera.
  100. * @return Current FOV value for the camera.
  101. */
  102. Number getFOV() {
  103. return fov;
  104. }
  105. void setClippingPlanes(Number nearClipPlane, Number farClipPlane);
  106. Number getNearClippingPlane();
  107. Number getFarClippingPlane();
  108. void setParentScene(Scene *parentScene);
  109. Scene *getParentScene() const;
  110. void doCameraTransform();
  111. void setLightDepthTexture(Texture *texture);
  112. bool hasFilterShader();
  113. void drawFilter(Texture *targetTexture = NULL, Number targetTextureWidth = 0.0, Number targetTextureHeight = 0.0, Texture *targetColorTexture = NULL, Texture *targetZTexture = NULL);
  114. /**
  115. * Sets the exposure for the camera. The exposure value can be passed to a shader for HDR rendering.
  116. * @param level The new exposure value.
  117. */
  118. void setExposureLevel(Number level) {
  119. exposureLevel = level;
  120. }
  121. /**
  122. * Returns the camera's exposure value.
  123. * @return Current exposure value.
  124. */
  125. Number getExposureLevel() {
  126. return exposureLevel;
  127. }
  128. /**
  129. * Sets the post-processing shader for the camera.
  130. * @param shaderMaterial Post processing shader material.
  131. */
  132. void setPostFilter(Material *shaderMaterial);
  133. /**
  134. * Sets the post-processing shader for the camera by name. The material needs have been added as a resource.
  135. * @param materialName The material name of the post-processing filter.
  136. */
  137. void setPostFilterByName(const String& shaderName);
  138. /**
  139. * Removes the currently assigned post filter.
  140. */
  141. void removePostFilter();
  142. /**
  143. * Returns the local shader options for the camera post processing material.
  144. */
  145. std::vector<ShaderBinding*> getLocalShaderOptions() { return localShaderOptions; }
  146. /**
  147. * Returns the shader material applied to the camera.
  148. */
  149. Material *getScreenShaderMaterial() { return filterShaderMaterial; }
  150. virtual Entity *Clone(bool deepClone, bool ignoreEditorOnly) const;
  151. virtual void applyClone(Entity *clone, bool deepClone, bool ignoreEditorOnly) const;
  152. Matrix4 getProjectionMatrix();
  153. Polycode::Rectangle getViewport();
  154. /**
  155. * Toggles the frustum culling of the camera. (Defaults to true).
  156. */
  157. bool frustumCulling;
  158. bool topLeftOrtho;
  159. /**
  160. * Shifts camera frustum by factor of the frustum size. (x=-1 will shift the frustum to the left by a whole screen width).
  161. */
  162. Vector2 cameraShift;
  163. /** @deprecated use setProjectionMode(ProjectionMode mode) */
  164. void setOrthoSizeMode(ProjectionMode orthoSizeMode) { setProjectionMode(orthoSizeMode); }
  165. /** @deprecated use getProjectionMode() */
  166. ProjectionMode getOrthoSizeMode() const { return projectionMode; }
  167. void setProjectionMode(ProjectionMode mode);
  168. ProjectionMode getProjectionMode() const { return projectionMode; }
  169. protected:
  170. ProjectionMode projectionMode;
  171. Matrix4 projectionMatrix;
  172. Polycode::Rectangle viewport;
  173. Number orthoSizeX;
  174. Number orthoSizeY;
  175. Number nearClipPlane;
  176. Number farClipPlane;
  177. Number exposureLevel;
  178. Number fov;
  179. Number leftFrustum,rightFrustum,topFrustum,bottomFrustum;
  180. Number frustumPlanes[6][4];
  181. Scene *parentScene;
  182. Material *filterShaderMaterial;
  183. Texture *originalSceneTexture;
  184. Texture *zBufferSceneTexture;
  185. std::vector<ShaderBinding*> localShaderOptions;
  186. bool _hasFilterShader;
  187. };
  188. }