CCCamera.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /****************************************************************************
  2. Copyright (c) 2014-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. Code based GamePlay3D's Camera: http://gameplay3d.org
  21. ****************************************************************************/
  22. #ifndef _CCCAMERA_H__
  23. #define _CCCAMERA_H__
  24. #include "2d/CCNode.h"
  25. #include "3d/CCFrustum.h"
  26. #include "renderer/CCQuadCommand.h"
  27. #include "renderer/CCCustomCommand.h"
  28. #include "renderer/CCFrameBuffer.h"
  29. NS_CC_BEGIN
  30. class Scene;
  31. class CameraBackgroundBrush;
  32. /**
  33. * Note:
  34. * Scene creates a default camera. And the default camera mask of Node is 1, therefore it can be seen by the default camera.
  35. * During rendering the scene, it draws the objects seen by each camera in the added order except default camera. The default camera is the last one being drawn with.
  36. * It's usually a good idea to render 3D objects in a separate camera.
  37. * And set the 3d camera flag to CameraFlag::USER1 or anything else except DEFAULT. Dedicate The DEFAULT camera for UI, because it is rendered at last.
  38. * You can change the camera order to get different result when depth test is not enabled.
  39. * For each camera, transparent 3d sprite is rendered after opaque 3d sprite and other 2d objects.
  40. */
  41. enum class CameraFlag
  42. {
  43. DEFAULT = 1,
  44. USER1 = 1 << 1,
  45. USER2 = 1 << 2,
  46. USER3 = 1 << 3,
  47. USER4 = 1 << 4,
  48. USER5 = 1 << 5,
  49. USER6 = 1 << 6,
  50. USER7 = 1 << 7,
  51. USER8 = 1 << 8,
  52. };
  53. /**
  54. * Defines a camera .
  55. */
  56. class CC_DLL Camera :public Node
  57. {
  58. friend class Scene;
  59. friend class Director;
  60. friend class EventDispatcher;
  61. public:
  62. /**
  63. * The type of camera.
  64. */
  65. enum class Type
  66. {
  67. PERSPECTIVE = 1,
  68. ORTHOGRAPHIC = 2
  69. };
  70. public:
  71. /**
  72. * Creates a perspective camera.
  73. *
  74. * @param fieldOfView The field of view for the perspective camera (normally in the range of 40-60 degrees).
  75. * @param aspectRatio The aspect ratio of the camera (normally the width of the viewport divided by the height of the viewport).
  76. * @param nearPlane The near plane distance.
  77. * @param farPlane The far plane distance.
  78. */
  79. static Camera* createPerspective(float fieldOfView, float aspectRatio, float nearPlane, float farPlane);
  80. /**
  81. * Creates an orthographic camera.
  82. *
  83. * @param zoomX The zoom factor along the X-axis of the orthographic projection (the width of the ortho projection).
  84. * @param zoomY The zoom factor along the Y-axis of the orthographic projection (the height of the ortho projection).
  85. * @param nearPlane The near plane distance.
  86. * @param farPlane The far plane distance.
  87. */
  88. static Camera* createOrthographic(float zoomX, float zoomY, float nearPlane, float farPlane);
  89. /** create default camera, the camera type depends on Director::getProjection, the depth of the default camera is 0 */
  90. static Camera* create();
  91. /**
  92. * Get the visiting camera , the visiting camera shall be set on Scene::render
  93. */
  94. static const Camera* getVisitingCamera();
  95. static const experimental::Viewport& getDefaultViewport();
  96. static void setDefaultViewport(const experimental::Viewport& vp);
  97. /**
  98. * Get the default camera of the current running scene.
  99. */
  100. static Camera* getDefaultCamera();
  101. /**
  102. * Gets the type of camera.
  103. *
  104. * @return The camera type.
  105. */
  106. Camera::Type getType() const { return _type; }
  107. /**get & set Camera flag*/
  108. CameraFlag getCameraFlag() const { return (CameraFlag)_cameraFlag; }
  109. void setCameraFlag(CameraFlag flag) { _cameraFlag = (unsigned short)flag; }
  110. /**
  111. * Make Camera looks at target
  112. *
  113. * @param target The target camera is point at
  114. * @param up The up vector, usually it's Y axis
  115. */
  116. virtual void lookAt(const Vec3& target, const Vec3& up = Vec3::UNIT_Y);
  117. /**
  118. * Gets the camera's projection matrix.
  119. *
  120. * @return The camera projection matrix.
  121. */
  122. const Mat4& getProjectionMatrix() const;
  123. /**
  124. * Gets the camera's view matrix.
  125. *
  126. * @return The camera view matrix.
  127. */
  128. const Mat4& getViewMatrix() const;
  129. /**get view projection matrix*/
  130. const Mat4& getViewProjectionMatrix() const;
  131. /* convert the specified point in 3D world-space coordinates into the screen-space coordinates.
  132. *
  133. * Origin point at left top corner in screen-space.
  134. * @param src The world-space position.
  135. * @return The screen-space position.
  136. */
  137. Vec2 project(const Vec3& src) const;
  138. /* convert the specified point in 3D world-space coordinates into the GL-screen-space coordinates.
  139. *
  140. * Origin point at left bottom corner in GL-screen-space.
  141. * @param src The 3D world-space position.
  142. * @return The GL-screen-space position.
  143. */
  144. Vec2 projectGL(const Vec3& src) const;
  145. /**
  146. * Convert the specified point of screen-space coordinate into the 3D world-space coordinate.
  147. *
  148. * Origin point at left top corner in screen-space.
  149. * @param src The screen-space position.
  150. * @return The 3D world-space position.
  151. */
  152. Vec3 unproject(const Vec3& src) const;
  153. /**
  154. * Convert the specified point of GL-screen-space coordinate into the 3D world-space coordinate.
  155. *
  156. * Origin point at left bottom corner in GL-screen-space.
  157. * @param src The GL-screen-space position.
  158. * @return The 3D world-space position.
  159. */
  160. Vec3 unprojectGL(const Vec3& src) const;
  161. /**
  162. * Convert the specified point of screen-space coordinate into the 3D world-space coordinate.
  163. *
  164. * Origin point at left top corner in screen-space.
  165. * @param size The window size to use.
  166. * @param src The screen-space position.
  167. * @param dst The 3D world-space position.
  168. */
  169. void unproject(const Size& size, const Vec3* src, Vec3* dst) const;
  170. /**
  171. * Convert the specified point of GL-screen-space coordinate into the 3D world-space coordinate.
  172. *
  173. * Origin point at left bottom corner in GL-screen-space.
  174. * @param size The window size to use.
  175. * @param src The GL-screen-space position.
  176. * @param dst The 3D world-space position.
  177. */
  178. void unprojectGL(const Size& size, const Vec3* src, Vec3* dst) const;
  179. /**
  180. * Is this aabb visible in frustum
  181. */
  182. bool isVisibleInFrustum(const AABB* aabb) const;
  183. /**
  184. * Get object depth towards camera
  185. */
  186. float getDepthInView(const Mat4& transform) const;
  187. /**
  188. * set depth, camera with larger depth is drawn on top of camera with smaller depth, the depth of camera with CameraFlag::DEFAULT is 0, user defined camera is -1 by default
  189. */
  190. void setDepth(int8_t depth);
  191. /**
  192. * get depth, camera with larger depth is drawn on top of camera with smaller depth, the depth of camera with CameraFlag::DEFAULT is 0, user defined camera is -1 by default
  193. */
  194. int8_t getDepth() const { return _depth; }
  195. /**
  196. get rendered order
  197. */
  198. int getRenderOrder() const;
  199. /**
  200. * Get the frustum's far plane.
  201. */
  202. float getFarPlane() const { return _farPlane; }
  203. /**
  204. * Get the frustum's near plane.
  205. */
  206. float getNearPlane() const { return _nearPlane; }
  207. //override
  208. virtual void onEnter() override;
  209. virtual void onExit() override;
  210. /**
  211. Before rendering scene with this camera, the background need to be cleared. It clears the depth buffer with max depth by default. Use setBackgroundBrush to modify the default behavior
  212. */
  213. void clearBackground();
  214. /**
  215. Apply the FBO, RenderTargets and viewport.
  216. */
  217. void apply();
  218. /**
  219. Restore the FBO, RenderTargets and viewport.
  220. */
  221. void restore();
  222. /**
  223. Set FBO, which will attach several render target for the rendered result.
  224. */
  225. void setFrameBufferObject(experimental::FrameBuffer* fbo);
  226. /**
  227. Set Viewport for camera.
  228. */
  229. void setViewport(const experimental::Viewport& vp);
  230. /**
  231. * Whether or not the viewprojection matrix was updated since the last frame.
  232. * @return True if the viewprojection matrix was updated since the last frame.
  233. */
  234. bool isViewProjectionUpdated() const {return _viewProjectionUpdated;}
  235. /**
  236. * set the background brush. See CameraBackgroundBrush for more information.
  237. * @param clearBrush Brush used to clear the background
  238. */
  239. void setBackgroundBrush(CameraBackgroundBrush* clearBrush);
  240. /**
  241. * Get clear brush
  242. */
  243. CameraBackgroundBrush* getBackgroundBrush() const { return _clearBrush; }
  244. virtual void visit(Renderer* renderer, const Mat4 &parentTransform, uint32_t parentFlags) override;
  245. bool isBrushValid();
  246. CC_CONSTRUCTOR_ACCESS:
  247. Camera();
  248. ~Camera();
  249. /**
  250. * Set the scene,this method shall not be invoke manually
  251. */
  252. void setScene(Scene* scene);
  253. /**set additional matrix for the projection matrix, it multiplies mat to projection matrix when called, used by WP8*/
  254. void setAdditionalProjection(const Mat4& mat);
  255. /** init camera */
  256. bool initDefault();
  257. bool initPerspective(float fieldOfView, float aspectRatio, float nearPlane, float farPlane);
  258. bool initOrthographic(float zoomX, float zoomY, float nearPlane, float farPlane);
  259. void applyFrameBufferObject();
  260. void applyViewport();
  261. void restoreFrameBufferObject();
  262. void restoreViewport();
  263. protected:
  264. static Camera* _visitingCamera;
  265. static experimental::Viewport _defaultViewport;
  266. Scene* _scene; //Scene camera belongs to
  267. Mat4 _projection;
  268. mutable Mat4 _view;
  269. mutable Mat4 _viewInv;
  270. mutable Mat4 _viewProjection;
  271. Vec3 _up;
  272. Camera::Type _type;
  273. float _fieldOfView;
  274. float _zoom[2];
  275. float _aspectRatio;
  276. float _nearPlane;
  277. float _farPlane;
  278. mutable bool _viewProjectionDirty;
  279. bool _viewProjectionUpdated; //Whether or not the viewprojection matrix was updated since the last frame.
  280. unsigned short _cameraFlag; // camera flag
  281. mutable Frustum _frustum; // camera frustum
  282. mutable bool _frustumDirty;
  283. int8_t _depth; //camera depth, the depth of camera with CameraFlag::DEFAULT flag is 0 by default, a camera with larger depth is drawn on top of camera with smaller depth
  284. CameraBackgroundBrush* _clearBrush; //brush used to clear the back ground
  285. experimental::Viewport _viewport;
  286. experimental::FrameBuffer* _fbo;
  287. GLint _oldViewport[4];
  288. };
  289. NS_CC_END
  290. #endif// __CCCAMERA_H_