Renderer2D.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 Technique;
  30. class VertexBuffer;
  31. struct FrameInfo;
  32. struct SourceBatch2D;
  33. /// 2D view batch info.
  34. struct ViewBatchInfo2D
  35. {
  36. /// Construct.
  37. ViewBatchInfo2D();
  38. /// Vertex buffer update frame number.
  39. unsigned vertexBufferUpdateFrameNumber_;
  40. /// Index count.
  41. unsigned indexCount_;
  42. /// Vertex count.
  43. unsigned vertexCount_;
  44. /// Vertex buffer.
  45. SharedPtr<VertexBuffer> vertexBuffer_;
  46. /// Batch updated frame number.
  47. unsigned batchUpdatedFrameNumber_;
  48. /// Source batches.
  49. PODVector<const SourceBatch2D*> sourceBatches_;
  50. /// Batch count;
  51. unsigned batchCount_;
  52. /// Materials.
  53. Vector<SharedPtr<Material> > materials_;
  54. /// Geometries.
  55. Vector<SharedPtr<Geometry> > geometries_;
  56. };
  57. /// 2D renderer component.
  58. class ATOMIC_API Renderer2D : public Drawable
  59. {
  60. OBJECT(Renderer2D);
  61. friend void CheckDrawableVisibility(const WorkItem* item, unsigned threadIndex);
  62. public:
  63. /// Construct.
  64. Renderer2D(Context* context);
  65. /// Destruct.
  66. ~Renderer2D();
  67. /// Register object factory.
  68. static void RegisterObject(Context* context);
  69. /// Process octree raycast. May be called from a worker thread.
  70. virtual void ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results);
  71. /// Calculate distance and prepare batches for rendering. May be called from worker thread(s), possibly re-entrantly.
  72. virtual void UpdateBatches(const FrameInfo& frame);
  73. /// Prepare geometry for rendering. Called from a worker thread if possible (no GPU update.)
  74. virtual void UpdateGeometry(const FrameInfo& frame);
  75. /// Return whether a geometry update is necessary, and if it can happen in a worker thread.
  76. virtual UpdateGeometryType GetUpdateGeometryType();
  77. /// Add Drawable2D.
  78. void AddDrawable(Drawable2D* drawable);
  79. /// Remove Drawable2D.
  80. void RemoveDrawable(Drawable2D* drawable);
  81. /// Return material by texture and blend mode.
  82. Material* GetMaterial(Texture2D* texture, BlendMode blendMode);
  83. /// Check visibility.
  84. bool CheckVisibility(Drawable2D* drawable) const;
  85. /// Whether this renderer uses triangles (instead of quads)
  86. void SetUseTris(bool useTris) { useTris_ = useTris; }
  87. bool GetUseTris() const { return useTris_; }
  88. private:
  89. /// Recalculate the world-space bounding box.
  90. virtual void OnWorldBoundingBoxUpdate();
  91. /// Create material by texture and blend mode.
  92. SharedPtr<Material> CreateMaterial(Texture2D* texture, BlendMode blendMode);
  93. /// Handle view update begin event. Determine Drawable2D's and their batches here.
  94. void HandleBeginViewUpdate(StringHash eventType, VariantMap& eventData);
  95. /// Get all drawables in node.
  96. void GetDrawables(PODVector<Drawable2D*>& drawables, Node* node);
  97. /// Update view batch info.
  98. void UpdateViewBatchInfo(ViewBatchInfo2D& viewBatchInfo, Camera* camera);
  99. /// Add view batch.
  100. void AddViewBatch
  101. (ViewBatchInfo2D& viewBatchInfo, Material* material, unsigned indexStart, unsigned indexCount, unsigned vertexStart,
  102. unsigned vertexCount);
  103. /// Index buffer.
  104. SharedPtr<IndexBuffer> indexBuffer_;
  105. /// Material.
  106. SharedPtr<Material> material_;
  107. /// Drawables.
  108. PODVector<Drawable2D*> drawables_;
  109. /// View frame info for current frame.
  110. FrameInfo frame_;
  111. /// View batch info.
  112. HashMap<Camera*, ViewBatchInfo2D> viewBatchInfos_;
  113. /// Frustum for current frame.
  114. const Frustum* frustum_;
  115. /// Frustum bounding box for current frame.
  116. BoundingBox frustumBoundingBox_;
  117. /// Cached materials.
  118. HashMap<Texture2D*, HashMap<int, SharedPtr<Material> > > cachedMaterials_;
  119. /// Cached techniques per blend mode.
  120. HashMap<int, SharedPtr<Technique> > cachedTechniques_;
  121. /// Whether or not the renderer containts tris (default is quads)
  122. bool useTris_;
  123. };
  124. }