CCBillBoard.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /****************************************************************************
  2. Copyright (c) 2014-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 "3d/CCBillBoard.h"
  21. #include "2d/CCSpriteFrameCache.h"
  22. #include "base/CCDirector.h"
  23. #include "2d/CCCamera.h"
  24. #include "renderer/CCRenderer.h"
  25. #include "renderer/CCGLProgramCache.h"
  26. NS_CC_BEGIN
  27. BillBoard::BillBoard()
  28. : _mode(Mode::VIEW_POINT_ORIENTED)
  29. , _modeDirty(false)
  30. {
  31. Node::setAnchorPoint(Vec2(0.5f,0.5f));
  32. }
  33. BillBoard::~BillBoard()
  34. {
  35. }
  36. BillBoard* BillBoard::createWithTexture(Texture2D *texture, Mode mode)
  37. {
  38. BillBoard *billboard = new (std::nothrow) BillBoard();
  39. if (billboard && billboard->initWithTexture(texture))
  40. {
  41. billboard->_mode = mode;
  42. billboard->autorelease();
  43. return billboard;
  44. }
  45. CC_SAFE_DELETE(billboard);
  46. return nullptr;
  47. }
  48. BillBoard* BillBoard::create(const std::string& filename, Mode mode)
  49. {
  50. BillBoard *billboard = new (std::nothrow) BillBoard();
  51. if (billboard && billboard->initWithFile(filename))
  52. {
  53. billboard->_mode = mode;
  54. billboard->autorelease();
  55. return billboard;
  56. }
  57. CC_SAFE_DELETE(billboard);
  58. return nullptr;
  59. }
  60. BillBoard* BillBoard::create(const std::string& filename, const Rect& rect, Mode mode)
  61. {
  62. BillBoard *billboard = new (std::nothrow) BillBoard();
  63. if (billboard && billboard->initWithFile(filename, rect))
  64. {
  65. billboard->_mode = mode;
  66. billboard->autorelease();
  67. return billboard;
  68. }
  69. CC_SAFE_DELETE(billboard);
  70. return nullptr;
  71. }
  72. BillBoard* BillBoard::create(Mode mode)
  73. {
  74. BillBoard *billboard = new (std::nothrow) BillBoard();
  75. if (billboard && billboard->init())
  76. {
  77. billboard->_mode = mode;
  78. billboard->autorelease();
  79. return billboard;
  80. }
  81. CC_SAFE_DELETE(billboard);
  82. return nullptr;
  83. }
  84. void BillBoard::visit(Renderer *renderer, const Mat4& parentTransform, uint32_t parentFlags)
  85. {
  86. // quick return if not visible. children won't be drawn.
  87. if (!_visible)
  88. {
  89. return;
  90. }
  91. bool visibleByCamera = isVisitableByVisitingCamera();
  92. uint32_t flags = processParentFlags(parentTransform, parentFlags);
  93. //Add 3D flag so all the children will be rendered as 3D object
  94. flags |= FLAGS_RENDER_AS_3D;
  95. //Update Billboard transform
  96. bool dirty = calculateBillboardTransform();
  97. if(dirty)
  98. {
  99. flags |= FLAGS_TRANSFORM_DIRTY;
  100. }
  101. Director* director = Director::getInstance();
  102. director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  103. director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);
  104. int i = 0;
  105. if(!_children.empty())
  106. {
  107. sortAllChildren();
  108. // draw children zOrder < 0
  109. for(auto size = _children.size(); i < size; ++i)
  110. {
  111. auto node = _children.at(i);
  112. if (node && node->getLocalZOrder() < 0)
  113. node->visit(renderer, _modelViewTransform, flags);
  114. else
  115. break;
  116. }
  117. // self draw
  118. if (visibleByCamera)
  119. this->draw(renderer, _modelViewTransform, flags);
  120. for(auto it=_children.cbegin()+i, itCend = _children.cend(); it != itCend; ++it)
  121. (*it)->visit(renderer, _modelViewTransform, flags);
  122. }
  123. else if (visibleByCamera)
  124. {
  125. this->draw(renderer, _modelViewTransform, flags);
  126. }
  127. director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  128. }
  129. bool BillBoard::calculateBillboardTransform()
  130. {
  131. //Get camera world position
  132. auto camera = Camera::getVisitingCamera();
  133. const Mat4& camWorldMat = camera->getNodeToWorldTransform();
  134. //TODO: use math lib to calculate math lib Make it easier to read and maintain
  135. if (memcmp(_camWorldMat.m, camWorldMat.m, sizeof(float) * 16) != 0 || memcmp(_mvTransform.m, _modelViewTransform.m, sizeof(float) * 16) != 0 || _modeDirty || true)
  136. {
  137. //Rotate based on anchor point
  138. Vec3 anchorPoint(_anchorPointInPoints.x , _anchorPointInPoints.y , 0.0f);
  139. Mat4 localToWorld = _modelViewTransform;
  140. localToWorld.translate(anchorPoint);
  141. //Decide billboard mode
  142. Vec3 camDir;
  143. switch (_mode)
  144. {
  145. case Mode::VIEW_POINT_ORIENTED:
  146. camDir.set(localToWorld.m[12] - camWorldMat.m[12], localToWorld.m[13] - camWorldMat.m[13], localToWorld.m[14] - camWorldMat.m[14]);
  147. break;
  148. case Mode::VIEW_PLANE_ORIENTED:
  149. camWorldMat.transformVector(Vec3(0.0f, 0.0f, -1.0f), &camDir);
  150. break;
  151. default:
  152. CCASSERT(false, "invalid billboard mode");
  153. break;
  154. }
  155. _modeDirty = false;
  156. if (camDir.length() < MATH_TOLERANCE)
  157. {
  158. camDir.set(camWorldMat.m[8], camWorldMat.m[9], camWorldMat.m[10]);
  159. }
  160. camDir.normalize();
  161. Quaternion rotationQuaternion;
  162. this->getNodeToWorldTransform().getRotation(&rotationQuaternion);
  163. Mat4 rotationMatrix;
  164. rotationMatrix.setIdentity();
  165. Vec3 upAxis(rotationMatrix.m[4],rotationMatrix.m[5],rotationMatrix.m[6]);
  166. Vec3 x, y;
  167. camWorldMat.transformVector(upAxis, &y);
  168. Vec3::cross(camDir, y, &x);
  169. x.normalize();
  170. Vec3::cross(x, camDir, &y);
  171. y.normalize();
  172. float xlen = sqrtf(localToWorld.m[0] * localToWorld.m[0] + localToWorld.m[1] * localToWorld.m[1] + localToWorld.m[2] * localToWorld.m[2]);
  173. float ylen = sqrtf(localToWorld.m[4] * localToWorld.m[4] + localToWorld.m[5] * localToWorld.m[5] + localToWorld.m[6] * localToWorld.m[6]);
  174. float zlen = sqrtf(localToWorld.m[8] * localToWorld.m[8] + localToWorld.m[9] * localToWorld.m[9] + localToWorld.m[10] * localToWorld.m[10]);
  175. Mat4 billboardTransform;
  176. billboardTransform.m[0] = x.x * xlen; billboardTransform.m[1] = x.y * xlen; billboardTransform.m[2] = x.z * xlen;
  177. billboardTransform.m[4] = y.x * ylen; billboardTransform.m[5] = y.y * ylen; billboardTransform.m[6] = y.z * ylen;
  178. billboardTransform.m[8] = -camDir.x * zlen; billboardTransform.m[9] = -camDir.y * zlen; billboardTransform.m[10] = -camDir.z * zlen;
  179. billboardTransform.m[12] = localToWorld.m[12]; billboardTransform.m[13] = localToWorld.m[13]; billboardTransform.m[14] = localToWorld.m[14];
  180. billboardTransform.translate(-anchorPoint);
  181. _mvTransform = _modelViewTransform = billboardTransform;
  182. _camWorldMat = camWorldMat;
  183. return true;
  184. }
  185. return false;
  186. }
  187. bool BillBoard::calculateBillbaordTransform()
  188. {
  189. return calculateBillboardTransform();
  190. }
  191. void BillBoard::draw(Renderer *renderer, const Mat4 &/*transform*/, uint32_t flags)
  192. {
  193. //FIXME: frustum culling here
  194. flags |= Node::FLAGS_RENDER_AS_3D;
  195. _trianglesCommand.init(0, _texture->getName(), getGLProgramState(), _blendFunc, _polyInfo.triangles, _modelViewTransform, flags);
  196. _trianglesCommand.setTransparent(true);
  197. _trianglesCommand.set3D(true);
  198. renderer->addCommand(&_trianglesCommand);
  199. }
  200. void BillBoard::setMode( Mode mode )
  201. {
  202. _mode = mode;
  203. _modeDirty = true;
  204. }
  205. BillBoard::Mode BillBoard::getMode() const
  206. {
  207. return _mode;
  208. }
  209. NS_CC_END