CCCameraBackgroundBrush.cpp 17 KB

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