BillboardSet.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  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. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "Color.h"
  25. #include "Drawable.h"
  26. #include "Rect.h"
  27. class IndexBuffer;
  28. class Graphics;
  29. class VertexBuffer;
  30. /// One billboard in the billboard set
  31. struct Billboard
  32. {
  33. /// Position
  34. Vector3 position_;
  35. /// Two-dimensional size
  36. Vector2 size_;
  37. /// UV coordinates
  38. Rect uv_;
  39. /// Color
  40. Color color_;
  41. /// Rotation
  42. float rotation_;
  43. /// Enabled flag
  44. bool enabled_;
  45. /// Sort distance
  46. float sortDistance_;
  47. };
  48. /// Billboard component
  49. class BillboardSet : public Drawable
  50. {
  51. OBJECT(BillboardSet);
  52. public:
  53. /// Construct
  54. BillboardSet(Context* context);
  55. /// Destruct
  56. virtual ~BillboardSet();
  57. /// Register object factory
  58. static void RegisterObject(Context* context);
  59. /// Handle attribute write access
  60. virtual void OnSetAttribute(const AttributeInfo& attr, const Variant& value);
  61. /// Handle attribute read access
  62. virtual Variant OnGetAttribute(const AttributeInfo& attr);
  63. /// Calculate distance for rendering
  64. virtual void UpdateDistance(const FrameInfo& frame);
  65. /// Prepare geometry for rendering
  66. virtual void UpdateGeometry(const FrameInfo& frame);
  67. /// Return number of batches
  68. virtual unsigned GetNumBatches();
  69. /// Return rendering batch
  70. virtual void GetBatch(const FrameInfo& frame, unsigned batchIndex, Batch& batch);
  71. /// Set material
  72. void SetMaterial(Material* material);
  73. /// Set number of billboards
  74. void SetNumBillboards(unsigned num);
  75. /// Set whether billboards are relative to the scene node
  76. void SetRelative(bool enable);
  77. /// Set whether scene node scale affects billboards' size
  78. void SetScaled(bool enable);
  79. /// Set whether billboards are sorted by distance
  80. void SetSorted(bool enable);
  81. /// Set animation LOD bias
  82. void SetAnimationLodBias(float bias);
  83. /// Call after changing the billboards
  84. void Updated();
  85. /// Return material
  86. Material* GetMaterial() const { return material_; }
  87. /// Return number of billboards
  88. unsigned GetNumBillboards() const { return billboards_.size(); }
  89. /// Return all billboards
  90. std::vector<Billboard>& GetBillboards() { return billboards_; }
  91. /// Return billboard by index
  92. Billboard* GetBillboard(unsigned index);
  93. /// Return whether billboards are relative to the scene node
  94. bool GetRelative() const { return relative_; }
  95. /// Return whether scene node scale affects billboards' size
  96. bool GetScaled() const { return scaled_; }
  97. /// Return whether billboards are sorted
  98. bool GetSorted() const { return sorted_; }
  99. /// Return animation LOD bias
  100. float GetAnimationLodBias() const { return animationLodBias_; }
  101. protected:
  102. /// Transform has changed. Mark billboards dirty if necessary
  103. virtual void OnMarkedDirty(Node* node);
  104. /// Update world-space bounding box
  105. virtual void OnWorldBoundingBoxUpdate();
  106. /// Mark billboard vertex buffer to need an update
  107. void MarkPositionsDirty();
  108. /// Billboards
  109. std::vector<Billboard> billboards_;
  110. /// Animation LOD bias
  111. float animationLodBias_;
  112. /// Animation LOD timer
  113. float animationLodTimer_;
  114. /// Billboards relative flag
  115. bool relative_;
  116. /// Scale affects billboard scale flag
  117. bool scaled_;
  118. /// Billboards sorted flag
  119. bool sorted_;
  120. private:
  121. /// Resize billboard vertex and index buffers
  122. void UpdateBufferSize();
  123. /// Rewrite billboard vertex buffer
  124. void UpdateVertexBuffer(const FrameInfo& frame);
  125. /// Geometry
  126. SharedPtr<Geometry> geometry_;
  127. /// Material
  128. SharedPtr<Material> material_;
  129. /// Vertex buffer
  130. SharedPtr<VertexBuffer> vertexBuffer_;
  131. /// Index buffer
  132. SharedPtr<IndexBuffer> indexBuffer_;
  133. /// Buffers need resize flag
  134. bool bufferSizeDirty_;
  135. /// Vertex buffer needs rewrite flag
  136. bool bufferDirty_;
  137. /// Force update flag (ignore animation LOD momentarily)
  138. bool forceUpdate_;
  139. /// Frame number on which was last sorted
  140. unsigned sortFrameNumber_;
  141. /// Previous offset to camera for determining whether sorting is necessary
  142. Vector3 previousOffset_;
  143. /// Billboard pointers for sorting
  144. std::vector<Billboard*> sortedBillboards_;
  145. };