CCCameraBackgroundBrush.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /****************************************************************************
  2. Copyright (c) 2015-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. ****************************************************************************/
  21. #ifndef _CCCAMERA_BACKGROUND_BRUSH_H__
  22. #define _CCCAMERA_BACKGROUND_BRUSH_H__
  23. #include "base/ccTypes.h"
  24. #include "base/CCRef.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 CameraBackgroundColorBrush;
  31. class CameraBackgroundDepthBrush;
  32. class CameraBackgroundSkyBoxBrush;
  33. class GLProgramState;
  34. class Camera;
  35. /**
  36. * Defines a brush to clear the background of camera.
  37. * There are 4 types of brush. None brush do nothing, Depth brush clear background with given depth, Color brush clear background with given color and depth, Skybox brush clear the background with a skybox. Camera uses depth brush by default.
  38. */
  39. class CC_DLL CameraBackgroundBrush : public Ref
  40. {
  41. public:
  42. /**
  43. * Brush types. There are 4 types of brush. See CameraBackgroundDepthBrush, CameraBackgroundColorBrush, CameraBackgroundSkyBoxBrush for more information.
  44. */
  45. enum class BrushType
  46. {
  47. NONE, //none brush
  48. DEPTH, // depth brush. See CameraBackgroundDepthBrush
  49. COLOR, // color brush. See CameraBackgroundColorBrush
  50. SKYBOX, // skybox brush. See CameraBackgroundSkyBoxBrush
  51. };
  52. /**
  53. * get brush type
  54. * @return BrushType
  55. */
  56. virtual BrushType getBrushType() const { return BrushType::NONE; }
  57. /**
  58. * Creates a none brush, it does nothing when clear the background
  59. * @return Created brush.
  60. */
  61. static CameraBackgroundBrush* createNoneBrush();
  62. /**
  63. * Creates a depth brush, which clears depth buffer with a given depth.
  64. * @param depth Depth used to clear depth buffer
  65. * @return Created brush
  66. */
  67. static CameraBackgroundDepthBrush* createDepthBrush(float depth = 1.f);
  68. /**
  69. * Creates a color brush
  70. * @param color Color of brush
  71. * @param depth Depth used to clear depth buffer
  72. * @return Created brush
  73. */
  74. static CameraBackgroundColorBrush* createColorBrush(const Color4F& color, float depth);
  75. /** Creates a Skybox brush with 6 textures.
  76. @param positive_x texture for the right side of the texture cube face.
  77. @param negative_x texture for the up side of the texture cube face.
  78. @param positive_y texture for the top side of the texture cube face
  79. @param negative_y texture for the bottom side of the texture cube face
  80. @param positive_z texture for the forward side of the texture cube face.
  81. @param negative_z texture for the rear side of the texture cube face.
  82. @return A new brush inited with given parameters.
  83. */
  84. static CameraBackgroundSkyBoxBrush* createSkyboxBrush(const std::string& positive_x, const std::string& negative_x,
  85. const std::string& positive_y, const std::string& negative_y,
  86. const std::string& positive_z, const std::string& negative_z);
  87. /**
  88. * draw the background
  89. */
  90. virtual void drawBackground(Camera* /*camera*/) {}
  91. virtual bool isValid() { return true; }
  92. CC_CONSTRUCTOR_ACCESS :
  93. CameraBackgroundBrush();
  94. virtual ~CameraBackgroundBrush();
  95. virtual bool init() { return true; }
  96. protected:
  97. GLProgramState* _glProgramState;
  98. };
  99. /**
  100. * Depth brush clear depth buffer with given depth
  101. */
  102. class CC_DLL CameraBackgroundDepthBrush : public CameraBackgroundBrush
  103. {
  104. public:
  105. /**
  106. * Create a depth brush
  107. * @param depth Depth used to clear the depth buffer
  108. * @return Created brush
  109. */
  110. static CameraBackgroundDepthBrush* create(float depth);
  111. /**
  112. * Get brush type. Should be BrushType::DEPTH
  113. * @return brush type
  114. */
  115. virtual BrushType getBrushType() const override { return BrushType::DEPTH; }
  116. /**
  117. * Draw background
  118. */
  119. virtual void drawBackground(Camera* camera) override;
  120. /**
  121. * Set depth
  122. * @param depth Depth used to clear depth buffer
  123. */
  124. void setDepth(float depth) { _depth = depth; }
  125. CC_CONSTRUCTOR_ACCESS:
  126. CameraBackgroundDepthBrush();
  127. virtual ~CameraBackgroundDepthBrush();
  128. virtual bool init() override;
  129. protected:
  130. float _depth;
  131. GLboolean _clearColor;
  132. V3F_C4B_T2F_Quad _quad;
  133. GLuint _vao;
  134. GLuint _vertexBuffer;
  135. GLuint _indexBuffer;
  136. };
  137. /**
  138. * Color brush clear buffer with given depth and color
  139. */
  140. class CC_DLL CameraBackgroundColorBrush : public CameraBackgroundDepthBrush
  141. {
  142. public:
  143. /**
  144. * Get brush type. Should be BrushType::COLOR
  145. * @return brush type
  146. */
  147. virtual BrushType getBrushType() const override { return BrushType::COLOR; }
  148. /**
  149. * Create a color brush
  150. * @param color Color used to clear the color buffer
  151. * @param depth Depth used to clear the depth buffer
  152. * @return Created brush
  153. */
  154. static CameraBackgroundColorBrush* create(const Color4F& color, float depth);
  155. /**
  156. * Draw background
  157. */
  158. virtual void drawBackground(Camera* camera) override;
  159. /**
  160. * Set clear color
  161. * @param color Color used to clear the color buffer
  162. */
  163. void setColor(const Color4F& color);
  164. CC_CONSTRUCTOR_ACCESS:
  165. CameraBackgroundColorBrush();
  166. virtual ~CameraBackgroundColorBrush();
  167. virtual bool init() override;
  168. protected:
  169. Color4F _color;
  170. };
  171. class TextureCube;
  172. class GLProgramState;
  173. class EventListenerCustom;
  174. /**
  175. * Skybox brush clear buffer with a skybox
  176. */
  177. class CC_DLL CameraBackgroundSkyBoxBrush : public CameraBackgroundBrush
  178. {
  179. public:
  180. /**
  181. * Get brush type. Should be BrushType::SKYBOX
  182. * @return brush type
  183. */
  184. virtual BrushType getBrushType() const override { return BrushType::SKYBOX; }
  185. /** Creates a Skybox brush with 6 textures.
  186. @param positive_x texture for the right side of the texture cube face.
  187. @param negative_x texture for the up side of the texture cube face.
  188. @param positive_y texture for the top side of the texture cube face
  189. @param negative_y texture for the bottom side of the texture cube face
  190. @param positive_z texture for the forward side of the texture cube face.
  191. @param negative_z texture for the rear side of the texture cube face.
  192. @return A new brush inited with given parameters.
  193. */
  194. static CameraBackgroundSkyBoxBrush* create(const std::string& positive_x, const std::string& negative_x,
  195. const std::string& positive_y, const std::string& negative_y,
  196. const std::string& positive_z, const std::string& negative_z);
  197. /** Creates a Skybox brush with 6 textures.
  198. */
  199. static CameraBackgroundSkyBoxBrush* create();
  200. /**
  201. * Set skybox texture
  202. * @param texture Skybox texture
  203. */
  204. void setTexture(TextureCube* texture);
  205. /**
  206. * Draw background
  207. */
  208. virtual void drawBackground(Camera* camera) override;
  209. bool isActived() const;
  210. void setActived(bool actived);
  211. virtual void setTextureValid(bool valid);
  212. virtual bool isValid()override;
  213. CC_CONSTRUCTOR_ACCESS :
  214. CameraBackgroundSkyBoxBrush();
  215. virtual ~CameraBackgroundSkyBoxBrush();
  216. /**
  217. * init Skybox.
  218. */
  219. virtual bool init() override;
  220. protected:
  221. void initBuffer();
  222. GLuint _vao;
  223. GLuint _vertexBuffer;
  224. GLuint _indexBuffer;
  225. TextureCube* _texture;
  226. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  227. EventListenerCustom* _backToForegroundListener;
  228. #endif
  229. private:
  230. bool _actived;
  231. bool _textureValid;
  232. };
  233. NS_CC_END
  234. #endif// _CCCAMERA_BACKGROUND_BRUSH_H__