Renderer2D.h 5.2 KB

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