CmGUIWidget.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include "CmGUIWidget.h"
  2. #include "CmGUIManager.h"
  3. #include "CmGUISkin.h"
  4. #include "CmGUILabel.h"
  5. #include "CmDeferredRenderContext.h"
  6. #include "CmMaterial.h"
  7. #include "CmPass.h"
  8. #include "CmMesh.h"
  9. #include "CmCamera.h"
  10. #include "CmViewport.h"
  11. #include "CmSceneObject.h"
  12. namespace CamelotEngine
  13. {
  14. GUISkin GUIWidget::DefaultSkin;
  15. GUIWidget::GUIWidget(const HSceneObject& parent)
  16. :Overlay(parent), mSkin(nullptr)
  17. {
  18. GUIManager::instance().registerWidget(this);
  19. }
  20. GUIWidget::~GUIWidget()
  21. {
  22. GUIManager::instance().unregisterWidget(this);
  23. for(auto& elem : mElements)
  24. {
  25. CM_DELETE(elem, GUIElement, GUIAlloc);
  26. }
  27. mElements.clear();
  28. }
  29. GUILabel* GUIWidget::addLabel(const String& text, UINT32 fixedWidth, UINT32 fixedHeight, bool wordWrap, TextHorzAlign horzAlign, TextVertAlign vertAlign)
  30. {
  31. GUILabel* label = CM_NEW(GUILabel, GUIAlloc) GUILabel(this, text, getGUISkin(), fixedWidth, fixedHeight, wordWrap, horzAlign, vertAlign);
  32. mElements.push_back(label);
  33. return label;
  34. }
  35. GUILabel* GUIWidget::addLabel(const String& text, TextHorzAlign horzAlign, TextVertAlign vertAlign)
  36. {
  37. return addLabel(text, 0, 0, false, horzAlign, vertAlign);
  38. }
  39. void GUIWidget::setSkin(const GUISkin* skin)
  40. {
  41. mSkin = skin;
  42. }
  43. const GUISkin* GUIWidget::getGUISkin() const
  44. {
  45. if(mSkin != nullptr)
  46. return mSkin;
  47. else
  48. return &DefaultSkin;
  49. }
  50. void GUIWidget::updateMeshes() const
  51. {
  52. struct TempMeshData
  53. {
  54. TempMeshData()
  55. :numQuads(0), quadOffset(0), vertices(nullptr),
  56. uvs(nullptr), indices(nullptr)
  57. { }
  58. UINT32 numQuads;
  59. UINT32 quadOffset;
  60. Vector2* vertices;
  61. Vector2* uvs;
  62. UINT32* indices;
  63. HMaterial material;
  64. std::shared_ptr<MeshData> meshData;
  65. };
  66. std::unordered_map<UINT64, TempMeshData> meshDataPerRenderElement;
  67. // Group meshes based on used materials
  68. // Determine mesh sizes per group
  69. for(auto& elem : mElements)
  70. {
  71. UINT32 numRenderElems = elem->getNumRenderElements();
  72. for(UINT32 i = 0; i < numRenderElems; i++)
  73. {
  74. const HMaterial& mat = elem->getMaterial(i);
  75. UINT64 meshGroup = mat->getInternalID(); // TODO - I group based on material ID. So if two widgets used exact copies of the same material
  76. // this system won't detect it. Find a better way of determining material similarity?
  77. UINT32 numQuads = elem->getNumQuads(i);
  78. meshDataPerRenderElement[meshGroup].numQuads += numQuads;
  79. meshDataPerRenderElement[meshGroup].material = mat;
  80. }
  81. }
  82. // Allocate buffers for each group
  83. UINT32 numMeshes = 0;
  84. for(auto& renderElem : meshDataPerRenderElement)
  85. {
  86. renderElem.second.meshData = std::shared_ptr<MeshData>(CM_NEW(MeshData, PoolAlloc) MeshData(),
  87. &MemAllocDeleter<MeshData, PoolAlloc>::deleter);
  88. renderElem.second.vertices = renderElem.second.meshData->addPositionsVec2(renderElem.second.numQuads * 4);
  89. renderElem.second.uvs = renderElem.second.meshData->addUV0(renderElem.second.numQuads * 4);
  90. renderElem.second.indices = renderElem.second.meshData->addIndices32(renderElem.second.numQuads * 6);
  91. numMeshes++;
  92. }
  93. // Fill buffers for each group
  94. for(auto& elem : mElements)
  95. {
  96. UINT32 numRenderElems = elem->getNumRenderElements();
  97. for(UINT32 i = 0; i < numRenderElems; i++)
  98. {
  99. const HMaterial& mat = elem->getMaterial(i);
  100. UINT64 meshGroup = mat->getInternalID();
  101. Vector2* vertices = meshDataPerRenderElement[meshGroup].vertices;
  102. Vector2* uvs = meshDataPerRenderElement[meshGroup].uvs;
  103. UINT32* indices = meshDataPerRenderElement[meshGroup].indices;
  104. UINT32 startingQuad = meshDataPerRenderElement[meshGroup].quadOffset;
  105. UINT32 maxNumQuads = meshDataPerRenderElement[meshGroup].numQuads;
  106. elem->fillBuffer(vertices, uvs, indices, startingQuad, maxNumQuads, i);
  107. UINT32 numQuads = elem->getNumQuads(i);
  108. meshDataPerRenderElement[meshGroup].quadOffset += numQuads;
  109. }
  110. }
  111. // Update meshes
  112. for(UINT32 i = (UINT32)mCachedMeshes.size(); i < numMeshes; i++)
  113. {
  114. HMesh newMesh = Mesh::create();
  115. mCachedMeshes.push_back(newMesh);
  116. newMesh.waitUntilLoaded();
  117. }
  118. while((UINT32)mCachedMeshes.size() > numMeshes && (UINT32)mCachedMeshes.size() > 0)
  119. {
  120. mCachedMeshes.erase(mCachedMeshes.end() - 1); // TODO: Destroying meshes as soon as they're not used might be a perf. penalty?
  121. // Maybe instead pool the meshes and only actually remove them when a certain number of unused ones exists.
  122. }
  123. mCachedMaterials.resize(numMeshes);
  124. UINT32 meshIdx = 0;
  125. for(auto& renderElem : meshDataPerRenderElement)
  126. {
  127. mCachedMeshes[meshIdx]->setMeshData(renderElem.second.meshData);
  128. mCachedMaterials[meshIdx] = renderElem.second.material;
  129. meshIdx++;
  130. }
  131. }
  132. void GUIWidget::render(const Camera* camera, DeferredRenderContextPtr& renderContext) const
  133. {
  134. SO()->setPosition(Vector3(200, 100, 0));
  135. // Mesh is re-created every frame. There might be a better approach that only recreates it upon change,
  136. // but for now it seems like too much hassle for something like GUI that is pretty dynamic anyway.
  137. updateMeshes();
  138. // Render the meshes
  139. UINT32 meshIdx = 0;
  140. for(auto& mesh : mCachedMeshes)
  141. {
  142. HMaterial material = mCachedMaterials[meshIdx];
  143. // TODO - Possible optimization. I currently divide by width/height inside the shader, while it
  144. // might be more optimal to just scale the mesh as the resolution changes?
  145. material->setFloat("halfViewportWidth", camera->getViewport()->getWidth() * 0.5f);
  146. material->setFloat("halfViewportHeight", camera->getViewport()->getHeight() * 0.5f);
  147. material->setMat4("worldTransform", SO()->getWorldTfrm());
  148. if(material == nullptr || !material.isLoaded())
  149. continue;
  150. if(mesh == nullptr || !mesh.isLoaded())
  151. continue;
  152. for(UINT32 i = 0; i < material->getNumPasses(); i++)
  153. {
  154. PassPtr pass = material->getPass(i);
  155. pass->activate(renderContext);
  156. PassParametersPtr paramsPtr = material->getPassParameters(i);
  157. pass->bindParameters(renderContext, paramsPtr);
  158. renderContext->render(mesh->getRenderOperation());
  159. }
  160. meshIdx++;
  161. }
  162. }
  163. }