1
0

CCSkybox.cpp 8.1 KB

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