PolyCamera.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. #include "PolyVector4.h"
  24. namespace Polycode {
  25. class Scene;
  26. class Material;
  27. class ShaderBinding;
  28. class Texture;
  29. /**
  30. * 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.
  31. */
  32. class _PolyExport Camera : public Entity {
  33. public:
  34. /** ProjectionMode: Orthographic projection, with manually set size. */
  35. static const int ORTHO_SIZE_MANUAL = 0;
  36. /** ProjectionMode: Orthographic projection, with height specified used and width scaled proportionally . */
  37. static const int ORTHO_SIZE_LOCK_HEIGHT = 1;
  38. /** ProjectionMode: Orthographic projection, with width specified used and height scaled proportionally. */
  39. static const int ORTHO_SIZE_LOCK_WIDTH = 2;
  40. /** ProjectionMode: Orthographic projection, scaled to viewport backing resolution. */
  41. static const int ORTHO_SIZE_VIEWPORT = 3;
  42. /** ProjectionMode: Perspective projection, with field of view specified. */
  43. static const int PERSPECTIVE_FOV = 4;
  44. /** ProjectionMode: Perspective projection, with bounds set by edges of frustum. */
  45. static const int PERSPECTIVE_FRUSTUM = 5;
  46. /** ProjectionMode: Manual matrix projection. Use setProjectionMatrix to set the matrix. */
  47. static const int MANUAL_MATRIX = 6;
  48. /**
  49. * Constructor.
  50. * @param parentScene Scene to add the camera to.
  51. */
  52. explicit Camera(Scene *parentScene);
  53. virtual ~Camera();
  54. /**
  55. * Builds the frustum clipping planes for this camera using the current render modelview and the camera's projection matrices.
  56. */
  57. void buildFrustumPlanes();
  58. /**
  59. * Checks if the camera can see a sphere.
  60. * @param pos Position of the sphere to check.
  61. * @param fRadius Radius of the sphere.
  62. * @return Returns true if the sphere is within the camera's frustum, or false if it isn't.
  63. * @see canSee()
  64. */
  65. bool isSphereInFrustum(const Vector3 &pos, Number fRadius);
  66. /**
  67. * Checks if an Axis-aligned bounding box is visible to the camera.
  68. * @param aabb An axis-aligned bounding box
  69. * @return Returns true if the AABB is within the camera's frustum, false if it isn't.
  70. */
  71. bool isAABBInFrustum(const AABB &aabb);
  72. /**
  73. * Toggles orthographic projection mode for camera.
  74. * @param mode If true, sets the camera into orthographic projection mode.
  75. * @param orthoSizeX Width of the orthographic frustum (defaults to 1.0)
  76. * @param orthoSizeY Height of the orthographic frustum (defaults to 1.0)
  77. */
  78. void setOrthoMode(bool mode);
  79. /**
  80. * Sets the orthographic size of the camera.
  81. * @param orthoSizeX Orthographic width
  82. * @param orthoSizeY Orthographic height
  83. */
  84. void setOrthoSize(Number orthoSizeX, Number orthoSizeY);
  85. /** Switches into frustum mode and sets up the planes. */
  86. void setFrustumMode(Number left, Number right, Number bottom, Number top, Number front, Number back);
  87. /**
  88. * Returns true if camera is in orthographic projection mode.
  89. * @return True if camera is orthographic, false if otherwise.
  90. */
  91. bool getOrthoMode() {
  92. return projectionMode == ORTHO_SIZE_MANUAL ||
  93. projectionMode == ORTHO_SIZE_LOCK_HEIGHT ||
  94. projectionMode == ORTHO_SIZE_LOCK_WIDTH ||
  95. projectionMode == ORTHO_SIZE_VIEWPORT;
  96. }
  97. /**
  98. * Returns the width of the camera's orthographic frustum.
  99. * @return Width of the camera's orthographic frustum.
  100. */
  101. Number getOrthoSizeX();
  102. /**
  103. * Returns the height of the camera's orthographic frustum.
  104. * @return Height of the camera's orthographic frustum.
  105. */
  106. Number getOrthoSizeY();
  107. /**
  108. * 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.
  109. * @param fov The new FOV value.
  110. */
  111. void setFOV(Number fov);
  112. /**
  113. * Returns the current FOV value for the camera.
  114. * @return Current FOV value for the camera.
  115. */
  116. Number getFOV() {
  117. return fov;
  118. }
  119. /**
  120. * Sets the clipping planes for the camera.
  121. * @param nearClipPlane Near clipping plane.
  122. * @param farClipPlane Far clipping plane.
  123. */
  124. void setClippingPlanes(Number nearClipPlane, Number farClipPlane);
  125. /**
  126. * Returns the near clipping plane of the camera.
  127. * @return Near clipping plane of the camera.
  128. */
  129. Number getNearClippingPlane();
  130. /**
  131. * Returns the far clipping plane of the camera.
  132. * @return Far clipping plane of the camera.
  133. */
  134. Number getFarClippingPlane();
  135. /**
  136. * Sets the parent scene of the camera.
  137. * @param parentScene New parent scene.
  138. */
  139. void setParentScene(Scene *parentScene);
  140. /**
  141. * Returns the camera's parent scene.
  142. * @return The camera's parent scene.
  143. */
  144. Scene *getParentScene() const;
  145. /**
  146. * Sets the renderer viewport and projection/modelview matrices based on the camera's setting and transform.
  147. */
  148. void doCameraTransform();
  149. /**
  150. * Check if camera has a post filter material applied
  151. * @return True if the camera has a filter material applied.
  152. */
  153. bool hasFilterShader();
  154. /**
  155. * Binds target buffers and renders the scene in multiple passes based on the post filter material.
  156. */
  157. void drawFilter(Texture *targetTexture = NULL, Number targetTextureWidth = 0.0, Number targetTextureHeight = 0.0, Texture *targetColorTexture = NULL, Texture *targetZTexture = NULL);
  158. /**
  159. * Sets the exposure for the camera. The exposure value is passed automatically to post material shaders using an "exposure" uniform.
  160. * @param level The new exposure value.
  161. */
  162. void setExposureLevel(Number level) {
  163. exposureLevel = level;
  164. }
  165. /**
  166. * Returns the camera's exposure value.
  167. * @return Current exposure value.
  168. */
  169. Number getExposureLevel() {
  170. return exposureLevel;
  171. }
  172. /**
  173. * Sets the post-processing shader for the camera.
  174. * @param shaderMaterial Post processing shader material.
  175. */
  176. void setPostFilter(Material *shaderMaterial);
  177. /**
  178. * Sets the post-processing shader for the camera by name. The material needs have been added as a resource.
  179. * @param materialName The material name of the post-processing filter.
  180. */
  181. void setPostFilterByName(const String& shaderName);
  182. /**
  183. * Removes the currently assigned post filter.
  184. */
  185. void removePostFilter();
  186. /**
  187. * Returns the local shader options for the camera post processing material.
  188. */
  189. std::vector<ShaderBinding*> getLocalShaderOptions() { return localShaderOptions; }
  190. /**
  191. * Returns the number of local material shader options.
  192. * @return Number of local material shader options.
  193. */
  194. unsigned int getNumLocalShaderOptions() const;
  195. /**
  196. * Returns the shader option binding at the specified shader index.
  197. * @param Specified shader index.
  198. * @return Shader binding at specified shader index or NULL if index is out of bounds.
  199. */
  200. ShaderBinding* getLocalShaderOption(unsigned int index) const;
  201. /**
  202. * Returns the shader material applied to the camera.
  203. */
  204. Material *getScreenShaderMaterial() { return filterShaderMaterial; }
  205. /**
  206. * Clones the camera.
  207. */
  208. virtual Entity *Clone(bool deepClone, bool ignoreEditorOnly) const;
  209. /**
  210. * Applies clone parameters to the camera.
  211. */
  212. virtual void applyClone(Entity *clone, bool deepClone, bool ignoreEditorOnly) const;
  213. /**
  214. * Returns the camera's projection matrix.
  215. * @return Projection matrix.
  216. */
  217. Matrix4 getProjectionMatrix();
  218. /**
  219. * Manually sets the camera's projection matrix. Projection mode must be set to MANUAL_MATRIX.
  220. * @param matrix Custom projection matrix.
  221. * @see setProjectionMode
  222. */
  223. void setProjectionMatrix(Matrix4 matrix);
  224. /**
  225. * Return's the camera's pixel viewport based on the last render pass.
  226. */
  227. Polycode::Rectangle getViewport();
  228. /**
  229. * Toggles the frustum culling of the camera. (Defaults to true).
  230. */
  231. bool frustumCulling;
  232. /**
  233. * If set to true, the orthographic projection will be set with the 0,0 coordinate in the top left corner of the viewport. Otherwise, the 0,0 coordinate is in the center.
  234. */
  235. bool topLeftOrtho;
  236. /**
  237. * Shifts camera frustum by factor of the frustum size. (x=-1 will shift the frustum to the left by a whole screen width).
  238. */
  239. Vector2 cameraShift;
  240. /** @deprecated use setProjectionMode(ProjectionMode mode) */
  241. void setOrthoSizeMode(int orthoSizeMode) { setProjectionMode(orthoSizeMode); }
  242. /** @deprecated use getProjectionMode() */
  243. int getOrthoSizeMode() const { return projectionMode; }
  244. /**
  245. * Sets the projection mode of the camera. Possible values are ORTHO_SIZE_MANUAL, ORTHO_SIZE_LOCK_HEIGHT,ORTHO_SIZE_LOCK_WIDTH, ORTHO_SIZE_LOCK_WIDTH, PERSPECTIVE_FOV, PERSPECTIVE_FRUSTUM and MANUAL_MATRIX.
  246. See the documentation of each individual mode for details.
  247. * @param mode New projection mode.
  248. */
  249. void setProjectionMode(int mode);
  250. /**
  251. * Returns the current projection mode.
  252. * @return Current projection mode.
  253. */
  254. int getProjectionMode() const { return projectionMode; }
  255. protected:
  256. int projectionMode;
  257. Matrix4 projectionMatrix;
  258. Polycode::Rectangle viewport;
  259. Number orthoSizeX;
  260. Number orthoSizeY;
  261. Number nearClipPlane;
  262. Number farClipPlane;
  263. Number exposureLevel;
  264. Number fov;
  265. Number leftFrustum,rightFrustum,topFrustum,bottomFrustum;
  266. Vector4 frustumPlanes[6];
  267. Scene *parentScene;
  268. Material *filterShaderMaterial;
  269. Texture *originalSceneTexture;
  270. Texture *zBufferSceneTexture;
  271. std::vector<ShaderBinding*> localShaderOptions;
  272. bool _hasFilterShader;
  273. };
  274. }