CCBillBoard.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #ifndef __CCBILLBOARD_H__
  21. #define __CCBILLBOARD_H__
  22. #include "2d/CCSprite.h"
  23. NS_CC_BEGIN
  24. /**
  25. * @addtogroup _3d
  26. * @{
  27. */
  28. /**
  29. * @brief Inherit from Sprite, achieve BillBoard.
  30. */
  31. class CC_DLL BillBoard : public Sprite
  32. {
  33. public:
  34. enum class Mode
  35. {
  36. VIEW_POINT_ORIENTED, // orient to the camera
  37. VIEW_PLANE_ORIENTED // orient to the XOY plane of camera
  38. };
  39. /// @name Creators
  40. /**
  41. * Creates an empty BillBoard without texture. You can call setTexture method subsequently.
  42. *
  43. * @return An autoreleased BillBoard object.
  44. */
  45. static BillBoard* create(Mode mode = Mode::VIEW_POINT_ORIENTED);
  46. /**
  47. * Creates a BillBoard with an image filename.
  48. *
  49. * After creation, the rect of BillBoard will be the size of the image,
  50. * and the offset will be (0,0).
  51. *
  52. * @param filename A path to image file, e.g., "scene1/monster.png"
  53. * @return An autoreleased BillBoard object.
  54. */
  55. static BillBoard* create(const std::string& filename, Mode mode = Mode::VIEW_POINT_ORIENTED);
  56. /**
  57. * Creates a BillBoard with an image filename and a rect.
  58. *
  59. * @param filename A path to image file, e.g., "scene1/monster.png"
  60. * @param rect A subrect of the image file
  61. * @return An autoreleased BillBoard object
  62. */
  63. static BillBoard* create(const std::string& filename, const Rect& rect, Mode mode = Mode::VIEW_POINT_ORIENTED);
  64. /**
  65. * Creates a BillBoard with a Texture2D object.
  66. *
  67. * After creation, the rect will be the size of the texture, and the offset will be (0,0).
  68. *
  69. * @param texture A pointer to a Texture2D object.
  70. * @return An autoreleased BillBoard object
  71. */
  72. static BillBoard* createWithTexture(Texture2D *texture, Mode mode = Mode::VIEW_POINT_ORIENTED);
  73. /** Set the billboard rotation mode. */
  74. void setMode(Mode mode);
  75. /** Get the billboard rotation mode. */
  76. Mode getMode() const;
  77. //override
  78. /** update billboard's transform and turn it towards camera */
  79. virtual void visit(Renderer *renderer, const Mat4& parentTransform, uint32_t parentFlags) override;
  80. /**
  81. * draw BillBoard object.
  82. *
  83. * @lua NA
  84. */
  85. virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override;
  86. CC_CONSTRUCTOR_ACCESS:
  87. BillBoard();
  88. virtual ~BillBoard();
  89. protected:
  90. /**
  91. * calculate a model matrix which keep original translate & scaling but always face to the camera
  92. */
  93. bool calculateBillboardTransform();
  94. /** @deprecated Use calculateBillboardTransform instead. */
  95. CC_DEPRECATED_ATTRIBUTE bool calculateBillbaordTransform();
  96. Mat4 _camWorldMat;
  97. Mat4 _mvTransform;
  98. Mode _mode;
  99. bool _modeDirty;
  100. private:
  101. CC_DISALLOW_COPY_AND_ASSIGN(BillBoard);
  102. };
  103. // end of 3d group
  104. /// @}
  105. NS_CC_END
  106. #endif // __CCBILLBOARD_H__