Renderer2D.h 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // Copyright (c) 2008-2014 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 "Drawable.h"
  24. namespace Urho3D
  25. {
  26. class Drawable2D;
  27. class IndexBuffer;
  28. class Material;
  29. class VertexBuffer;
  30. /// 2D renderer components.
  31. class URHO3D_API Renderer2D : public Drawable
  32. {
  33. OBJECT(Renderer2D);
  34. public:
  35. /// Construct.
  36. Renderer2D(Context* context);
  37. /// Destruct.
  38. ~Renderer2D();
  39. /// Register object factory.
  40. static void RegisterObject(Context* context);
  41. /// Calculate distance and prepare batches for rendering. May be called from worker thread(s), possibly re-entrantly.
  42. virtual void UpdateBatches(const FrameInfo& frame);
  43. /// Prepare geometry for rendering. Called from a worker thread if possible (no GPU update.)
  44. virtual void UpdateGeometry(const FrameInfo& frame);
  45. /// Return whether a geometry update is necessary, and if it can happen in a worker thread.
  46. virtual UpdateGeometryType GetUpdateGeometryType();
  47. /// Check visibility.
  48. bool CheckVisibility(Drawable2D* drawable) const;
  49. private:
  50. /// Recalculate the world-space bounding box.
  51. virtual void OnWorldBoundingBoxUpdate();
  52. /// Handle view update begin event. Determine Drawable2D's and their batches here.
  53. void HandleBeginViewUpdate(StringHash eventType, VariantMap& eventData);
  54. /// Get all drawables in node.
  55. void GetDrawables(PODVector<Drawable2D*>& drawables, Node* node);
  56. /// Return material by texture and blend mode.
  57. Material* GetMaterial(Texture2D* texture, BlendMode blendMode);
  58. /// Create new material by texture and blend mode.
  59. Material* CreateMaterial(Texture2D* Texture, BlendMode blendMode);
  60. /// Add batch.
  61. void AddBatch(Material* material, unsigned indexStart, unsigned indexCount, unsigned vertexStart, unsigned vertexCount);
  62. /// Index buffer.
  63. SharedPtr<IndexBuffer> indexBuffer_;
  64. /// Vertex buffer.
  65. SharedPtr<VertexBuffer> vertexBuffer_;
  66. /// Drawables.
  67. PODVector<Drawable2D*> drawables_;
  68. /// Materials.
  69. Vector<SharedPtr<Material> > materials_;
  70. /// Geometries.
  71. Vector<SharedPtr<Geometry> > geometries_;
  72. /// Frustum for current frame.
  73. const Frustum* frustum_;
  74. /// Frustum bounding box for current frame.
  75. BoundingBox frustumBoundingBox_;
  76. /// Total index count for the current frame.
  77. unsigned indexCount_;
  78. /// Total vertex count for the current frame.
  79. unsigned vertexCount_;
  80. /// Cached materials.
  81. HashMap<Texture2D*, HashMap<int, SharedPtr<Material> > > cachedMaterials_;
  82. };
  83. }