CCSkybox.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. #include "base/ccMacros.h"
  22. #include "base/CCConfiguration.h"
  23. #include "base/CCDirector.h"
  24. #include "renderer/ccGLStateCache.h"
  25. #include "renderer/CCGLProgram.h"
  26. #include "renderer/CCGLProgramCache.h"
  27. #include "renderer/CCGLProgramState.h"
  28. #include "renderer/CCRenderer.h"
  29. #include "renderer/CCRenderState.h"
  30. #include "renderer/CCTextureCube.h"
  31. #include "3d/CCSkybox.h"
  32. #include "2d/CCCamera.h"
  33. NS_CC_BEGIN
  34. Skybox::Skybox()
  35. : _vao(0)
  36. , _vertexBuffer(0)
  37. , _indexBuffer(0)
  38. ,_texture(nullptr)
  39. {
  40. }
  41. Skybox::~Skybox()
  42. {
  43. glDeleteBuffers(1, &_vertexBuffer);
  44. glDeleteBuffers(1, &_indexBuffer);
  45. _vertexBuffer = 0;
  46. _indexBuffer = 0;
  47. if (Configuration::getInstance()->supportsShareableVAO())
  48. {
  49. glDeleteVertexArrays(1, &_vao);
  50. GL::bindVAO(0);
  51. _vao = 0;
  52. }
  53. _texture->release();
  54. }
  55. Skybox* Skybox::create(const std::string& positive_x, const std::string& negative_x,
  56. const std::string& positive_y, const std::string& negative_y,
  57. const std::string& positive_z, const std::string& negative_z)
  58. {
  59. auto ret = new (std::nothrow) Skybox();
  60. ret->init(positive_x, negative_x, positive_y, negative_y, positive_z, negative_z);
  61. ret->autorelease();
  62. return ret;
  63. }
  64. bool Skybox::init()
  65. {
  66. // create and set our custom shader
  67. auto shader = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_3D_SKYBOX);
  68. auto state = GLProgramState::create(shader);
  69. state->setVertexAttribPointer(GLProgram::ATTRIBUTE_NAME_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), nullptr);
  70. setGLProgramState(state);
  71. initBuffers();
  72. CHECK_GL_ERROR_DEBUG();
  73. return true;
  74. }
  75. bool Skybox::init(const std::string& positive_x, const std::string& negative_x,
  76. const std::string& positive_y, const std::string& negative_y,
  77. const std::string& positive_z, const std::string& negative_z)
  78. {
  79. auto texture = TextureCube::create(positive_x, negative_x, positive_y, negative_y, positive_z, negative_z);
  80. if (texture == nullptr)
  81. return false;
  82. init();
  83. setTexture(texture);
  84. return true;
  85. }
  86. void Skybox::initBuffers()
  87. {
  88. if (Configuration::getInstance()->supportsShareableVAO())
  89. {
  90. glGenVertexArrays(1, &_vao);
  91. GL::bindVAO(_vao);
  92. }
  93. // The skybox is rendered using a purpose-built shader which makes use of
  94. // the shader language's inherent support for cubemaps. Hence there is no
  95. // need to build a cube mesh. All that is needed is a single quad that
  96. // covers the entire screen. The vertex shader will draw the appropriate
  97. // view of the cubemap onto that quad.
  98. //
  99. // The vertex shader does not apply either the model/view matrix or the
  100. // projection matrix, so the appropriate quad is one with unit coordinates
  101. // in the x and y dimensions. Such a quad will exactly cover the screen.
  102. // To ensure that the skybox is rendered behind all other objects, z needs
  103. // to be 1.0, but the vertex shader overwrites z to 1.0, so - for the sake
  104. // of z-buffering - it is unimportant what we set it to for the vertices
  105. // of the quad.
  106. //
  107. // The quad vertex positions are also used in deriving a direction
  108. // vector for the cubemap lookup. We choose z = -1 which matches the
  109. // negative-z pointing direction of the camera and gives a field of
  110. // view of 90deg in both x and y, if not otherwise adjusted. That fov
  111. // is then adjusted to exactly match the camera by applying a prescaling
  112. // to the camera's world transformation before sending it to the shader.
  113. // init vertex buffer object
  114. Vec3 vexBuf[] =
  115. {
  116. Vec3(1, -1, -1), Vec3(1, 1, -1), Vec3(-1, 1, -1), Vec3(-1, -1, -1)
  117. };
  118. glGenBuffers(1, &_vertexBuffer);
  119. glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
  120. glBufferData(GL_ARRAY_BUFFER, sizeof(vexBuf), vexBuf, GL_STATIC_DRAW);
  121. // init index buffer object
  122. const unsigned char idxBuf[] = {0, 1, 2, 0, 2, 3};
  123. glGenBuffers(1, &_indexBuffer);
  124. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
  125. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(idxBuf), idxBuf, GL_STATIC_DRAW);
  126. if (Configuration::getInstance()->supportsShareableVAO())
  127. {
  128. glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
  129. getGLProgramState()->applyAttributes(false);
  130. GL::bindVAO(0);
  131. }
  132. }
  133. void Skybox::draw(Renderer* renderer, const Mat4& transform, uint32_t flags)
  134. {
  135. _customCommand.init(_globalZOrder);
  136. _customCommand.func = CC_CALLBACK_0(Skybox::onDraw, this, transform, flags);
  137. _customCommand.setTransparent(false);
  138. _customCommand.set3D(true);
  139. renderer->addCommand(&_customCommand);
  140. }
  141. void Skybox::onDraw(const Mat4& transform, uint32_t /*flags*/)
  142. {
  143. auto camera = Camera::getVisitingCamera();
  144. Mat4 cameraModelMat = camera->getNodeToWorldTransform();
  145. Mat4 projectionMat = camera->getProjectionMatrix();
  146. // Ignore the translation
  147. cameraModelMat.m[12] = cameraModelMat.m[13] = cameraModelMat.m[14] = 0;
  148. // prescale the matrix to account for the camera fov
  149. cameraModelMat.scale(1 / projectionMat.m[0], 1 / projectionMat.m[5], 1.0);
  150. auto state = getGLProgramState();
  151. state->apply(transform);
  152. Vec4 color(_displayedColor.r / 255.f, _displayedColor.g / 255.f, _displayedColor.b / 255.f, 1.f);
  153. state->setUniformVec4("u_color", color);
  154. state->setUniformMat4("u_cameraRot", cameraModelMat);
  155. glEnable(GL_DEPTH_TEST);
  156. RenderState::StateBlock::_defaultState->setDepthTest(true);
  157. glDepthFunc(GL_LEQUAL);
  158. RenderState::StateBlock::_defaultState->setDepthFunction(RenderState::DEPTH_LEQUAL);
  159. glEnable(GL_CULL_FACE);
  160. RenderState::StateBlock::_defaultState->setCullFace(true);
  161. glCullFace(GL_BACK);
  162. RenderState::StateBlock::_defaultState->setCullFaceSide(RenderState::CULL_FACE_SIDE_BACK);
  163. glDisable(GL_BLEND);
  164. RenderState::StateBlock::_defaultState->setBlend(false);
  165. if (Configuration::getInstance()->supportsShareableVAO())
  166. {
  167. GL::bindVAO(_vao);
  168. }
  169. else
  170. {
  171. GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION);
  172. glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
  173. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), nullptr);
  174. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
  175. }
  176. glDrawElements(GL_TRIANGLES, (GLsizei)6, GL_UNSIGNED_BYTE, nullptr);
  177. if (Configuration::getInstance()->supportsShareableVAO())
  178. {
  179. GL::bindVAO(0);
  180. }
  181. else
  182. {
  183. glBindBuffer(GL_ARRAY_BUFFER, 0);
  184. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  185. }
  186. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 4);
  187. CHECK_GL_ERROR_DEBUG();
  188. }
  189. void Skybox::setTexture(TextureCube* texture)
  190. {
  191. CCASSERT(texture != nullptr, __FUNCTION__);
  192. texture->retain();
  193. if (_texture)
  194. _texture->release();
  195. _texture = texture;
  196. getGLProgramState()->setUniformTexture("u_Env", _texture);
  197. }
  198. void Skybox::reload()
  199. {
  200. initBuffers();
  201. }
  202. NS_CC_END