CCCameraBackgroundBrush.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. #include "2d/CCCameraBackgroundBrush.h"
  21. #include "2d/CCCamera.h"
  22. #include "base/ccMacros.h"
  23. #include "base/CCConfiguration.h"
  24. #include "base/CCDirector.h"
  25. #include "renderer/ccGLStateCache.h"
  26. #include "renderer/CCGLProgram.h"
  27. #include "renderer/CCGLProgramCache.h"
  28. #include "renderer/CCGLProgramState.h"
  29. #include "renderer/CCRenderer.h"
  30. #include "renderer/CCRenderState.h"
  31. #include "renderer/CCTextureCube.h"
  32. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  33. #include "base/CCEventCustom.h"
  34. #include "base/CCEventListenerCustom.h"
  35. #include "base/CCEventType.h"
  36. #include "base/CCEventDispatcher.h"
  37. #endif
  38. NS_CC_BEGIN
  39. CameraBackgroundBrush::CameraBackgroundBrush()
  40. : _glProgramState(nullptr)
  41. {
  42. }
  43. CameraBackgroundBrush::~CameraBackgroundBrush()
  44. {
  45. CC_SAFE_RELEASE(_glProgramState);
  46. }
  47. CameraBackgroundBrush* CameraBackgroundBrush::createNoneBrush()
  48. {
  49. auto ret = new (std::nothrow) CameraBackgroundBrush();
  50. ret->init();
  51. ret->autorelease();
  52. return ret;
  53. }
  54. CameraBackgroundColorBrush* CameraBackgroundBrush::createColorBrush(const Color4F& color, float depth)
  55. {
  56. return CameraBackgroundColorBrush::create(color, depth);
  57. }
  58. CameraBackgroundDepthBrush* CameraBackgroundBrush::createDepthBrush(float depth)
  59. {
  60. return CameraBackgroundDepthBrush::create(depth);
  61. }
  62. CameraBackgroundSkyBoxBrush* CameraBackgroundBrush::createSkyboxBrush(const std::string& positive_x, const std::string& negative_x, const std::string& positive_y, const std::string& negative_y, const std::string& positive_z, const std::string& negative_z)
  63. {
  64. return CameraBackgroundSkyBoxBrush::create(positive_x, negative_x, positive_y, negative_y, positive_z, negative_z);
  65. }
  66. //////////////////////////////////////////////////////////////////////////////////////////
  67. CameraBackgroundDepthBrush::CameraBackgroundDepthBrush()
  68. : _depth(0.f)
  69. , _clearColor(GL_FALSE)
  70. {
  71. }
  72. CameraBackgroundDepthBrush::~CameraBackgroundDepthBrush()
  73. {
  74. }
  75. CameraBackgroundDepthBrush* CameraBackgroundDepthBrush::create(float depth)
  76. {
  77. auto ret = new (std::nothrow) CameraBackgroundDepthBrush();
  78. if (nullptr != ret && ret->init())
  79. {
  80. ret->_depth = depth;
  81. ret->autorelease();
  82. }
  83. else
  84. {
  85. CC_SAFE_DELETE(ret);
  86. }
  87. return ret;
  88. }
  89. bool CameraBackgroundDepthBrush::init()
  90. {
  91. auto shader = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_CAMERA_CLEAR);
  92. _glProgramState = GLProgramState::getOrCreateWithGLProgram(shader);
  93. _glProgramState->retain();
  94. _quad.bl.vertices = Vec3(-1,-1,0);
  95. _quad.br.vertices = Vec3(1,-1,0);
  96. _quad.tl.vertices = Vec3(-1,1,0);
  97. _quad.tr.vertices = Vec3(1,1,0);
  98. _quad.bl.colors = _quad.br.colors = _quad.tl.colors = _quad.tr.colors = Color4B(0,0,0,1);
  99. _quad.bl.texCoords = Tex2F(0,0);
  100. _quad.br.texCoords = Tex2F(1,0);
  101. _quad.tl.texCoords = Tex2F(0,1);
  102. _quad.tr.texCoords = Tex2F(1,1);
  103. return true;
  104. }
  105. void CameraBackgroundDepthBrush::drawBackground(Camera* /*camera*/)
  106. {
  107. GLboolean oldDepthTest;
  108. GLint oldDepthFunc;
  109. GLboolean oldDepthMask;
  110. {
  111. glColorMask(_clearColor, _clearColor, _clearColor, _clearColor);
  112. glStencilMask(0);
  113. oldDepthTest = glIsEnabled(GL_DEPTH_TEST);
  114. glGetIntegerv(GL_DEPTH_FUNC, &oldDepthFunc);
  115. glGetBooleanv(GL_DEPTH_WRITEMASK, &oldDepthMask);
  116. glDepthMask(GL_TRUE);
  117. glEnable(GL_DEPTH_TEST);
  118. glDepthFunc(GL_ALWAYS);
  119. }
  120. //draw
  121. _glProgramState->setUniformFloat("depth", _depth);
  122. _glProgramState->apply(Mat4::IDENTITY);
  123. GLshort indices[6] = {0, 1, 2, 3, 2, 1};
  124. {
  125. GL::bindVAO(0);
  126. glBindBuffer(GL_ARRAY_BUFFER, 0);
  127. GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX);
  128. // vertices
  129. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), &_quad.tl.vertices);
  130. // colors
  131. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V3F_C4B_T2F), &_quad.tl.colors);
  132. // tex coords
  133. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), &_quad.tl.texCoords);
  134. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  135. glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
  136. }
  137. {
  138. if(GL_FALSE == oldDepthTest)
  139. {
  140. glDisable(GL_DEPTH_TEST);
  141. }
  142. glDepthFunc(oldDepthFunc);
  143. if(GL_FALSE == oldDepthMask)
  144. {
  145. glDepthMask(GL_FALSE);
  146. }
  147. /* IMPORTANT: We only need to update the states that are not restored.
  148. Since we don't know what was the previous value of the mask, we update the RenderState
  149. after setting it.
  150. The other values don't need to be updated since they were restored to their original values
  151. */
  152. glStencilMask(0xFFFFF);
  153. // RenderState::StateBlock::_defaultState->setStencilWrite(0xFFFFF);
  154. /* BUG: RenderState does not support glColorMask yet. */
  155. glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  156. }
  157. }
  158. //////////////////////////////////////////////////////////////////////////////////////////
  159. CameraBackgroundColorBrush::CameraBackgroundColorBrush()
  160. : _color(0.f, 0.f, 0.f, 0.f)
  161. {
  162. }
  163. CameraBackgroundColorBrush::~CameraBackgroundColorBrush()
  164. {
  165. }
  166. bool CameraBackgroundColorBrush::init()
  167. {
  168. CameraBackgroundDepthBrush::init();
  169. this->_clearColor = GL_TRUE;
  170. return true;
  171. }
  172. void CameraBackgroundColorBrush::setColor(const Color4F& color)
  173. {
  174. _quad.bl.colors = _quad.br.colors = _quad.tl.colors = _quad.tr.colors = Color4B(color);
  175. }
  176. CameraBackgroundColorBrush* CameraBackgroundColorBrush::create(const Color4F& color, float depth)
  177. {
  178. auto ret = new (std::nothrow) CameraBackgroundColorBrush();
  179. if (nullptr != ret && ret->init())
  180. {
  181. ret->setColor(color);
  182. ret->setDepth(depth);
  183. ret->autorelease();
  184. }
  185. else
  186. {
  187. CC_SAFE_DELETE(ret);
  188. }
  189. return ret;
  190. }
  191. /////////////////////////////////////////////////////////////////////////////////////////////
  192. CameraBackgroundSkyBoxBrush::CameraBackgroundSkyBoxBrush()
  193. : _vao(0)
  194. , _vertexBuffer(0)
  195. , _indexBuffer(0)
  196. , _texture(nullptr)
  197. , _actived(true)
  198. , _textureValid(true)
  199. {
  200. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  201. _backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED,
  202. [this](EventCustom*)
  203. {
  204. initBuffer();
  205. }
  206. );
  207. Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1);
  208. #endif
  209. }
  210. CameraBackgroundSkyBoxBrush::~CameraBackgroundSkyBoxBrush()
  211. {
  212. CC_SAFE_RELEASE(_texture);
  213. glDeleteBuffers(1, &_vertexBuffer);
  214. glDeleteBuffers(1, &_indexBuffer);
  215. _vertexBuffer = 0;
  216. _indexBuffer = 0;
  217. if (Configuration::getInstance()->supportsShareableVAO())
  218. {
  219. glDeleteVertexArrays(1, &_vao);
  220. GL::bindVAO(0);
  221. _vao = 0;
  222. }
  223. }
  224. CameraBackgroundSkyBoxBrush* CameraBackgroundSkyBoxBrush::create(
  225. const std::string& positive_x,
  226. const std::string& negative_x,
  227. const std::string& positive_y,
  228. const std::string& negative_y,
  229. const std::string& positive_z,
  230. const std::string& negative_z
  231. )
  232. {
  233. CameraBackgroundSkyBoxBrush* ret = nullptr;
  234. auto texture = TextureCube::create(positive_x,
  235. negative_x,
  236. positive_y,
  237. negative_y,
  238. positive_z,
  239. negative_z);
  240. if (texture != nullptr)
  241. {
  242. Texture2D::TexParams tRepeatParams;
  243. tRepeatParams.magFilter = GL_LINEAR;
  244. tRepeatParams.minFilter = GL_LINEAR;
  245. tRepeatParams.wrapS = GL_CLAMP_TO_EDGE;
  246. tRepeatParams.wrapT = GL_CLAMP_TO_EDGE;
  247. texture->setTexParameters(tRepeatParams);
  248. ret = new (std::nothrow) CameraBackgroundSkyBoxBrush;
  249. if (nullptr != ret && ret->init())
  250. {
  251. ret->setTexture(texture);
  252. ret->autorelease();
  253. }
  254. else
  255. {
  256. CC_SAFE_DELETE(texture);
  257. CC_SAFE_DELETE(ret);
  258. }
  259. }
  260. return ret;
  261. }
  262. CameraBackgroundSkyBoxBrush* CameraBackgroundSkyBoxBrush::create()
  263. {
  264. auto ret = new (std::nothrow) CameraBackgroundSkyBoxBrush();
  265. if (nullptr != ret && ret->init())
  266. {
  267. ret->autorelease();
  268. }
  269. else
  270. {
  271. CC_SAFE_DELETE(ret);
  272. }
  273. return ret;
  274. }
  275. void CameraBackgroundSkyBoxBrush::drawBackground(Camera* camera)
  276. {
  277. if (!_actived)
  278. return;
  279. Mat4 cameraModelMat = camera->getNodeToWorldTransform();
  280. Vec4 color(1.f, 1.f, 1.f, 1.f);
  281. _glProgramState->setUniformVec4("u_color", color);
  282. cameraModelMat.m[12] = cameraModelMat.m[13] = cameraModelMat.m[14] = 0;
  283. _glProgramState->setUniformMat4("u_cameraRot", cameraModelMat);
  284. _glProgramState->apply(Mat4::IDENTITY);
  285. glEnable(GL_DEPTH_TEST);
  286. RenderState::StateBlock::_defaultState->setDepthTest(true);
  287. glDepthMask(GL_TRUE);
  288. RenderState::StateBlock::_defaultState->setDepthWrite(true);
  289. glDepthFunc(GL_ALWAYS);
  290. RenderState::StateBlock::_defaultState->setDepthFunction(RenderState::DEPTH_ALWAYS);
  291. glEnable(GL_CULL_FACE);
  292. RenderState::StateBlock::_defaultState->setCullFace(true);
  293. glCullFace(GL_BACK);
  294. RenderState::StateBlock::_defaultState->setCullFaceSide(RenderState::CULL_FACE_SIDE_BACK);
  295. glDisable(GL_BLEND);
  296. RenderState::StateBlock::_defaultState->setBlend(false);
  297. if (Configuration::getInstance()->supportsShareableVAO())
  298. {
  299. GL::bindVAO(_vao);
  300. }
  301. else
  302. {
  303. GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION);
  304. glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
  305. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), nullptr);
  306. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
  307. }
  308. glDrawElements(GL_TRIANGLES, (GLsizei)36, GL_UNSIGNED_BYTE, nullptr);
  309. if (Configuration::getInstance()->supportsShareableVAO())
  310. {
  311. GL::bindVAO(0);
  312. }
  313. else
  314. {
  315. glBindBuffer(GL_ARRAY_BUFFER, 0);
  316. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  317. }
  318. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 8);
  319. CHECK_GL_ERROR_DEBUG();
  320. }
  321. bool CameraBackgroundSkyBoxBrush::init()
  322. {
  323. auto shader = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_3D_SKYBOX);
  324. _glProgramState = GLProgramState::create(shader);
  325. _glProgramState->setVertexAttribPointer(GLProgram::ATTRIBUTE_NAME_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), nullptr);
  326. _glProgramState->retain();
  327. initBuffer();
  328. return true;
  329. }
  330. void CameraBackgroundSkyBoxBrush::initBuffer()
  331. {
  332. if (_vertexBuffer)
  333. glDeleteBuffers(1, &_vertexBuffer);
  334. if (_indexBuffer)
  335. glDeleteBuffers(1, &_indexBuffer);
  336. if (Configuration::getInstance()->supportsShareableVAO() && _vao)
  337. {
  338. glDeleteVertexArrays(1, &_vao);
  339. GL::bindVAO(0);
  340. _vao = 0;
  341. }
  342. if (Configuration::getInstance()->supportsShareableVAO())
  343. {
  344. glGenVertexArrays(1, &_vao);
  345. GL::bindVAO(_vao);
  346. }
  347. // init vertex buffer object
  348. Vec3 vexBuf[] =
  349. {
  350. Vec3(1, -1, 1), Vec3(1, 1, 1), Vec3(-1, 1, 1), Vec3(-1, -1, 1),
  351. Vec3(1, -1, -1), Vec3(1, 1, -1), Vec3(-1, 1, -1), Vec3(-1, -1, -1)
  352. };
  353. glGenBuffers(1, &_vertexBuffer);
  354. glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
  355. glBufferData(GL_ARRAY_BUFFER, sizeof(vexBuf), vexBuf, GL_STATIC_DRAW);
  356. // init index buffer object
  357. const unsigned char idxBuf[] = { 2, 1, 0, 3, 2, 0, // font
  358. 1, 5, 4, 1, 4, 0, // right
  359. 4, 5, 6, 4, 6, 7, // back
  360. 7, 6, 2, 7, 2, 3, // left
  361. 2, 6, 5, 2, 5, 1, // up
  362. 3, 0, 4, 3, 4, 7 // down
  363. };
  364. glGenBuffers(1, &_indexBuffer);
  365. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
  366. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(idxBuf), idxBuf, GL_STATIC_DRAW);
  367. if (Configuration::getInstance()->supportsShareableVAO())
  368. {
  369. glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
  370. _glProgramState->applyAttributes(false);
  371. GL::bindVAO(0);
  372. }
  373. }
  374. void CameraBackgroundSkyBoxBrush::setTexture(TextureCube* texture)
  375. {
  376. CC_SAFE_RETAIN(texture);
  377. CC_SAFE_RELEASE(_texture);
  378. _texture = texture;
  379. _glProgramState->setUniformTexture("u_Env", _texture);
  380. }
  381. bool CameraBackgroundSkyBoxBrush::isActived() const
  382. {
  383. return _actived;
  384. }
  385. void CameraBackgroundSkyBoxBrush::setActived(bool actived)
  386. {
  387. _actived = actived;
  388. }
  389. void CameraBackgroundSkyBoxBrush::setTextureValid(bool valid)
  390. {
  391. _textureValid = valid;
  392. }
  393. bool CameraBackgroundSkyBoxBrush::isValid()
  394. {
  395. return _actived;
  396. }
  397. NS_CC_END