DrawableProxy2D.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. #include "Precompiled.h"
  23. #include "Camera.h"
  24. #include "Context.h"
  25. #include "Drawable2D.h"
  26. #include "DrawableProxy2D.h"
  27. #include "Geometry.h"
  28. #include "Log.h"
  29. #include "Material.h"
  30. #include "Node.h"
  31. #include "VertexBuffer.h"
  32. #include "Sort.h"
  33. #include "DebugNew.h"
  34. namespace Urho3D
  35. {
  36. DrawableProxy2D::DrawableProxy2D(Context* context) :
  37. Drawable(context, DRAWABLE_GEOMETRY),
  38. vertexBuffer_(new VertexBuffer(context_)),
  39. orderDirty_(true)
  40. {
  41. }
  42. DrawableProxy2D::~DrawableProxy2D()
  43. {
  44. }
  45. void DrawableProxy2D::RegisterObject(Context* context)
  46. {
  47. context->RegisterFactory<DrawableProxy2D>();
  48. }
  49. void DrawableProxy2D::UpdateBatches(const FrameInfo& frame)
  50. {
  51. unsigned count = materials_.Size();
  52. batches_.Resize(count);
  53. for (unsigned i = 0; i < count; ++i)
  54. {
  55. batches_[i].distance_ = 10.0f + (count - i) * 0.001f;
  56. batches_[i].material_ = materials_[i];
  57. batches_[i].geometry_ = geometries_[i];
  58. batches_[i].worldTransform_ = &Matrix3x4::IDENTITY;
  59. }
  60. }
  61. void DrawableProxy2D::UpdateGeometry(const FrameInfo& frame)
  62. {
  63. materials_.Clear();
  64. if (orderDirty_)
  65. {
  66. Sort(drawables_.Begin(), drawables_.End(), CompareDrawable2Ds);
  67. orderDirty_ = false;
  68. }
  69. float timeStep = frame.timeStep_;
  70. unsigned vertexCount = 0;
  71. for (unsigned i = 0; i < drawables_.Size(); ++i)
  72. vertexCount += drawables_[i]->GetVertices().Size();
  73. if (vertexCount == 0)
  74. return;
  75. vertexCount = vertexCount / 4 * 6;
  76. vertexBuffer_->SetSize(vertexCount, MASK_VERTEX2D);
  77. Vertex2D* dest = reinterpret_cast<Vertex2D*>(vertexBuffer_->Lock(0, vertexCount, true));
  78. if (dest)
  79. {
  80. unsigned start = 0;
  81. unsigned count = 0;
  82. Material* material = 0;
  83. for (unsigned d = 0; d < drawables_.Size(); ++d)
  84. {
  85. Material* currMaterial = drawables_[d]->GetUsedMaterial();
  86. if (material != currMaterial)
  87. {
  88. if (material)
  89. {
  90. AddBatch(material, start, count);
  91. start += count;
  92. count = 0;
  93. }
  94. material = currMaterial;
  95. }
  96. const Vector<Vertex2D>& vertices = drawables_[d]->GetVertices();
  97. for (unsigned i = 0; i < vertices.Size(); i += 4)
  98. {
  99. dest[0] = vertices[i + 0];
  100. dest[1] = vertices[i + 1];
  101. dest[2] = vertices[i + 2];
  102. dest[3] = vertices[i + 0];
  103. dest[4] = vertices[i + 2];
  104. dest[5] = vertices[i + 3];
  105. dest += 6;
  106. }
  107. count += vertices.Size() / 4 * 6;
  108. }
  109. if (material)
  110. AddBatch(material, start, count);
  111. vertexBuffer_->Unlock();
  112. }
  113. else
  114. LOGERROR("Failed to lock vertex buffer");
  115. }
  116. UpdateGeometryType DrawableProxy2D::GetUpdateGeometryType()
  117. {
  118. return UPDATE_WORKER_THREAD;
  119. }
  120. void DrawableProxy2D::AddDrawable(Drawable2D* drawable)
  121. {
  122. if (!drawable)
  123. return;
  124. if (drawables_.Contains(drawable))
  125. return;
  126. drawables_.Push(drawable);
  127. orderDirty_ = true;
  128. }
  129. void DrawableProxy2D::RemoveDrawable(Drawable2D* drawable)
  130. {
  131. if (!drawable)
  132. return;
  133. drawables_.Remove(drawable);
  134. orderDirty_ = true;
  135. }
  136. void DrawableProxy2D::OnWorldBoundingBoxUpdate()
  137. {
  138. boundingBox_.Clear();
  139. for (unsigned i = 0; i < drawables_.Size(); ++i)
  140. boundingBox_.Merge(drawables_[i]->GetWorldBoundingBox());
  141. worldBoundingBox_ = boundingBox_;
  142. }
  143. void DrawableProxy2D::AddBatch(Material* material, unsigned vertexStart, unsigned vertexCount)
  144. {
  145. if (!material)
  146. return;
  147. materials_.Push(SharedPtr<Material>(material));
  148. unsigned batchSize = materials_.Size();
  149. if (geometries_.Size() < batchSize)
  150. {
  151. SharedPtr<Geometry> geometry(new Geometry(context_));
  152. geometry->SetVertexBuffer(0, vertexBuffer_, MASK_VERTEX2D);
  153. geometries_.Push(geometry);
  154. }
  155. geometries_[batchSize - 1]->SetDrawRange(TRIANGLE_LIST, 0, 0, vertexStart, vertexCount);
  156. }
  157. }