Renderer2D.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. //
  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. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Graphics/Drawable.h"
  24. namespace Atomic
  25. {
  26. class Drawable2D;
  27. class IndexBuffer;
  28. class Material;
  29. class VertexBuffer;
  30. struct FrameInfo;
  31. struct SourceBatch2D;
  32. /// 2D view batch info.
  33. struct ViewBatchInfo2D
  34. {
  35. /// Construct.
  36. ViewBatchInfo2D();
  37. /// Vertex buffer update frame number.
  38. unsigned vertexBufferUpdateFrameNumber_;
  39. /// Index count.
  40. unsigned indexCount_;
  41. /// Vertex count.
  42. unsigned vertexCount_;
  43. /// Vertex buffer.
  44. SharedPtr<VertexBuffer> vertexBuffer_;
  45. /// Batch updated frame number.
  46. unsigned batchUpdatedFrameNumber_;
  47. /// Source batches.
  48. PODVector<const SourceBatch2D*> sourceBatches_;
  49. /// Batch count;
  50. unsigned batchCount_;
  51. /// Materials.
  52. Vector<SharedPtr<Material> > materials_;
  53. /// Geometries.
  54. Vector<SharedPtr<Geometry> > geometries_;
  55. };
  56. /// 2D renderer components.
  57. class ATOMIC_API Renderer2D : public Drawable
  58. {
  59. OBJECT(Renderer2D);
  60. friend void CheckDrawableVisibility(const WorkItem* item, unsigned threadIndex);
  61. public:
  62. /// Construct.
  63. Renderer2D(Context* context);
  64. /// Destruct.
  65. ~Renderer2D();
  66. /// Register object factory.
  67. static void RegisterObject(Context* context);
  68. /// Process octree raycast. May be called from a worker thread.
  69. virtual void ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results);
  70. /// Calculate distance and prepare batches for rendering. May be called from worker thread(s), possibly re-entrantly.
  71. virtual void UpdateBatches(const FrameInfo& frame);
  72. /// Prepare geometry for rendering. Called from a worker thread if possible (no GPU update.)
  73. virtual void UpdateGeometry(const FrameInfo& frame);
  74. /// Return whether a geometry update is necessary, and if it can happen in a worker thread.
  75. virtual UpdateGeometryType GetUpdateGeometryType();
  76. /// Add Drawable2D.
  77. void AddDrawable(Drawable2D* drawable);
  78. /// Remove Drawable2D.
  79. void RemoveDrawable(Drawable2D* drawable);
  80. /// Return material by texture and blend mode.
  81. Material* GetMaterial(Texture2D* texture, BlendMode blendMode);
  82. /// Check visibility.
  83. bool CheckVisibility(Drawable2D* drawable) const;
  84. private:
  85. /// Recalculate the world-space bounding box.
  86. virtual void OnWorldBoundingBoxUpdate();
  87. /// Create material by texture and blend mode.
  88. SharedPtr<Material> CreateMaterial(Texture2D* texture, BlendMode blendMode);
  89. /// Handle view update begin event. Determine Drawable2D's and their batches here.
  90. void HandleBeginViewUpdate(StringHash eventType, VariantMap& eventData);
  91. /// Get all drawables in node.
  92. void GetDrawables(PODVector<Drawable2D*>& drawables, Node* node);
  93. /// Update view batch info.
  94. void UpdateViewBatchInfo(ViewBatchInfo2D& viewBatchInfo, Camera* camera);
  95. /// Add view batch.
  96. void AddViewBatch(ViewBatchInfo2D& viewBatchInfo, Material* material, unsigned indexStart, unsigned indexCount, unsigned vertexStart, unsigned vertexCount);
  97. /// Index buffer.
  98. SharedPtr<IndexBuffer> indexBuffer_;
  99. /// Material.
  100. SharedPtr<Material> material_;
  101. /// Drawables.
  102. PODVector<Drawable2D*> drawables_;
  103. /// View frame info for current frame.
  104. FrameInfo frame_;
  105. /// View batch info.
  106. HashMap<Camera*, ViewBatchInfo2D> viewBatchInfos_;
  107. /// Frustum for current frame.
  108. const Frustum* frustum_;
  109. /// Frustum bounding box for current frame.
  110. BoundingBox frustumBoundingBox_;
  111. /// Cached materials.
  112. HashMap<Texture2D*, HashMap<int, SharedPtr<Material> > > cachedMaterials_;
  113. };
  114. }