CCCameraBackgroundBrush.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /****************************************************************************
  2. Copyright (c) 2015-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #ifndef _CCCAMERA_BACKGROUND_BRUSH_H__
  21. #define _CCCAMERA_BACKGROUND_BRUSH_H__
  22. #include "base/ccTypes.h"
  23. #include "base/CCRef.h"
  24. #include "3d/CCFrustum.h"
  25. #include "renderer/CCQuadCommand.h"
  26. #include "renderer/CCCustomCommand.h"
  27. #include "renderer/CCFrameBuffer.h"
  28. NS_CC_BEGIN
  29. class CameraBackgroundColorBrush;
  30. class CameraBackgroundDepthBrush;
  31. class CameraBackgroundSkyBoxBrush;
  32. class GLProgramState;
  33. class Camera;
  34. /**
  35. * Defines a brush to clear the background of camera.
  36. * 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.
  37. */
  38. class CC_DLL CameraBackgroundBrush : public Ref
  39. {
  40. public:
  41. /**
  42. * Brush types. There are 4 types of brush. See CameraBackgroundDepthBrush, CameraBackgroundColorBrush, CameraBackgroundSkyBoxBrush for more information.
  43. */
  44. enum class BrushType
  45. {
  46. NONE, //none brush
  47. DEPTH, // depth brush. See CameraBackgroundDepthBrush
  48. COLOR, // color brush. See CameraBackgroundColorBrush
  49. SKYBOX, // skybox brush. See CameraBackgroundSkyBoxBrush
  50. };
  51. /**
  52. * get brush type
  53. * @return BrushType
  54. */
  55. virtual BrushType getBrushType() const { return BrushType::NONE; }
  56. /**
  57. * Creates a none brush, it does nothing when clear the background
  58. * @return Created brush.
  59. */
  60. static CameraBackgroundBrush* createNoneBrush();
  61. /**
  62. * Creates a depth brush, which clears depth buffer with a given depth.
  63. * @param depth Depth used to clear depth buffer
  64. * @return Created brush
  65. */
  66. static CameraBackgroundDepthBrush* createDepthBrush(float depth = 1.f);
  67. /**
  68. * Creates a color brush
  69. * @param color Color of brush
  70. * @param depth Depth used to clear depth buffer
  71. * @return Created brush
  72. */
  73. static CameraBackgroundColorBrush* createColorBrush(const Color4F& color, float depth);
  74. /** Creates a Skybox brush with 6 textures.
  75. @param positive_x texture for the right side of the texture cube face.
  76. @param negative_x texture for the up side of the texture cube face.
  77. @param positive_y texture for the top side of the texture cube face
  78. @param negative_y texture for the bottom side of the texture cube face
  79. @param positive_z texture for the forward side of the texture cube face.
  80. @param negative_z texture for the rear side of the texture cube face.
  81. @return A new brush inited with given parameters.
  82. */
  83. static CameraBackgroundSkyBoxBrush* createSkyboxBrush(const std::string& positive_x, const std::string& negative_x,
  84. const std::string& positive_y, const std::string& negative_y,
  85. const std::string& positive_z, const std::string& negative_z);
  86. /**
  87. * draw the background
  88. */
  89. virtual void drawBackground(Camera* /*camera*/) {}
  90. virtual bool isValid() { return true; }
  91. CC_CONSTRUCTOR_ACCESS :
  92. CameraBackgroundBrush();
  93. virtual ~CameraBackgroundBrush();
  94. virtual bool init() { return true; }
  95. protected:
  96. GLProgramState* _glProgramState;
  97. };
  98. /**
  99. * Depth brush clear depth buffer with given depth
  100. */
  101. class CC_DLL CameraBackgroundDepthBrush : public CameraBackgroundBrush
  102. {
  103. public:
  104. /**
  105. * Create a depth brush
  106. * @param depth Depth used to clear the depth buffer
  107. * @return Created brush
  108. */
  109. static CameraBackgroundDepthBrush* create(float depth);
  110. /**
  111. * Get brush type. Should be BrushType::DEPTH
  112. * @return brush type
  113. */
  114. virtual BrushType getBrushType() const override { return BrushType::DEPTH; }
  115. /**
  116. * Draw background
  117. */
  118. virtual void drawBackground(Camera* camera) override;
  119. /**
  120. * Set depth
  121. * @param depth Depth used to clear depth buffer
  122. */
  123. void setDepth(float depth) { _depth = depth; }
  124. CC_CONSTRUCTOR_ACCESS:
  125. CameraBackgroundDepthBrush();
  126. virtual ~CameraBackgroundDepthBrush();
  127. virtual bool init() override;
  128. protected:
  129. float _depth;
  130. GLboolean _clearColor;
  131. V3F_C4B_T2F_Quad _quad;
  132. };
  133. /**
  134. * Color brush clear buffer with given depth and color
  135. */
  136. class CC_DLL CameraBackgroundColorBrush : public CameraBackgroundDepthBrush
  137. {
  138. public:
  139. /**
  140. * Get brush type. Should be BrushType::COLOR
  141. * @return brush type
  142. */
  143. virtual BrushType getBrushType() const override { return BrushType::COLOR; }
  144. /**
  145. * Create a color brush
  146. * @param color Color used to clear the color buffer
  147. * @param depth Depth used to clear the depth buffer
  148. * @return Created brush
  149. */
  150. static CameraBackgroundColorBrush* create(const Color4F& color, float depth);
  151. /**
  152. * Set clear color
  153. * @param color Color used to clear the color buffer
  154. */
  155. void setColor(const Color4F& color);
  156. CC_CONSTRUCTOR_ACCESS:
  157. CameraBackgroundColorBrush();
  158. virtual ~CameraBackgroundColorBrush();
  159. virtual bool init() override;
  160. protected:
  161. Color4F _color;
  162. };
  163. class TextureCube;
  164. class GLProgramState;
  165. class EventListenerCustom;
  166. /**
  167. * Skybox brush clear buffer with a skybox
  168. */
  169. class CC_DLL CameraBackgroundSkyBoxBrush : public CameraBackgroundBrush
  170. {
  171. public:
  172. /**
  173. * Get brush type. Should be BrushType::SKYBOX
  174. * @return brush type
  175. */
  176. virtual BrushType getBrushType() const override { return BrushType::SKYBOX; }
  177. /** Creates a Skybox brush with 6 textures.
  178. @param positive_x texture for the right side of the texture cube face.
  179. @param negative_x texture for the up side of the texture cube face.
  180. @param positive_y texture for the top side of the texture cube face
  181. @param negative_y texture for the bottom side of the texture cube face
  182. @param positive_z texture for the forward side of the texture cube face.
  183. @param negative_z texture for the rear side of the texture cube face.
  184. @return A new brush inited with given parameters.
  185. */
  186. static CameraBackgroundSkyBoxBrush* create(const std::string& positive_x, const std::string& negative_x,
  187. const std::string& positive_y, const std::string& negative_y,
  188. const std::string& positive_z, const std::string& negative_z);
  189. /** Creates a Skybox brush with 6 textures.
  190. */
  191. static CameraBackgroundSkyBoxBrush* create();
  192. /**
  193. * Set skybox texture
  194. * @param texture Skybox texture
  195. */
  196. void setTexture(TextureCube* texture);
  197. /**
  198. * Draw background
  199. */
  200. virtual void drawBackground(Camera* camera) override;
  201. bool isActived() const;
  202. void setActived(bool actived);
  203. virtual void setTextureValid(bool valid);
  204. virtual bool isValid()override;
  205. CC_CONSTRUCTOR_ACCESS :
  206. CameraBackgroundSkyBoxBrush();
  207. virtual ~CameraBackgroundSkyBoxBrush();
  208. /**
  209. * init Skybox.
  210. */
  211. virtual bool init() override;
  212. protected:
  213. void initBuffer();
  214. GLuint _vao;
  215. GLuint _vertexBuffer;
  216. GLuint _indexBuffer;
  217. TextureCube* _texture;
  218. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  219. EventListenerCustom* _backToForegroundListener;
  220. #endif
  221. private:
  222. bool _actived;
  223. bool _textureValid;
  224. };
  225. NS_CC_END
  226. #endif// _CCCAMERA_BACKGROUND_BRUSH_H__