Finished UI Render Context
This commit is contained in:
		@@ -5,9 +5,8 @@
 | 
			
		||||
 | 
			
		||||
#include "UICanvas.hpp"
 | 
			
		||||
#include "display/pass/RenderPass.hpp"
 | 
			
		||||
#include "display/shader/UIShader.hpp"
 | 
			
		||||
 | 
			
		||||
#include "display/mesh/QuadMesh.hpp"
 | 
			
		||||
#include "ui/UIComponent.hpp"
 | 
			
		||||
 | 
			
		||||
using namespace Dawn;
 | 
			
		||||
 | 
			
		||||
@@ -35,32 +34,70 @@ void UICanvas::onDispose() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::vector<std::shared_ptr<IRenderPass>> UICanvas::getPasses(
 | 
			
		||||
  struct RenderPassContext &ctx
 | 
			
		||||
struct RenderPassContext &ctx
 | 
			
		||||
) {
 | 
			
		||||
  std::vector<std::shared_ptr<IRenderPass>> passes;
 | 
			
		||||
  std::unordered_map<shadertexturebinding_t, std::shared_ptr<Texture>> textures;
 | 
			
		||||
  UIShaderData data = {
 | 
			
		||||
    .projection = ctx.camera->getProjection(),
 | 
			
		||||
    .view = ctx.camera->getItem()->getWorldTransform(),
 | 
			
		||||
    .model = this->getItem()->getWorldTransform()
 | 
			
		||||
  };
 | 
			
		||||
  
 | 
			
		||||
  size_t quadCount = 0;
 | 
			
		||||
  if(this->components.empty()) return {};
 | 
			
		||||
 | 
			
		||||
  data.projection = ctx.camera->getProjection();
 | 
			
		||||
  data.view = ctx.camera->getItem()->getWorldTransform();
 | 
			
		||||
  data.model = this->getItem()->getWorldTransform();
 | 
			
		||||
 | 
			
		||||
  this->passes.clear();
 | 
			
		||||
  this->textureBindings.clear();
 | 
			
		||||
  this->textures.clear();
 | 
			
		||||
  quadCount = 0;
 | 
			
		||||
  nextBinding = 0;
 | 
			
		||||
 | 
			
		||||
  auto itComponents = components.begin();
 | 
			
		||||
  auto self = std::ref(*this);
 | 
			
		||||
  while(itComponents != components.end()) {
 | 
			
		||||
    auto component = *itComponents;
 | 
			
		||||
 | 
			
		||||
    // Get this components' quads.
 | 
			
		||||
    auto quads = component->getQuads({ 0, 0 });
 | 
			
		||||
    for(auto quad : quads) {
 | 
			
		||||
      data.quads[quadCount++] = quad;
 | 
			
		||||
      assertTrue(quadCount <= UI_SHADER_QUAD_COUNT, "Too many UI quads!");
 | 
			
		||||
    }
 | 
			
		||||
    component->getQuads({ 0, 0 }, self);
 | 
			
		||||
    ++itComponents;
 | 
			
		||||
  }
 | 
			
		||||
  flushPass();
 | 
			
		||||
 | 
			
		||||
  // No render passes if no quads.
 | 
			
		||||
  if(quadCount == 0) return passes;
 | 
			
		||||
  return passes;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void UICanvas::addQuad(
 | 
			
		||||
  const glm::vec4 quad,
 | 
			
		||||
  const glm::vec4 uvs,
 | 
			
		||||
  const struct Color color,
 | 
			
		||||
  const std::shared_ptr<Texture> text
 | 
			
		||||
) {
 | 
			
		||||
  float_t fTexture = -1;
 | 
			
		||||
  if(text == nullptr) {
 | 
			
		||||
    fTexture = -1;
 | 
			
		||||
  } else {
 | 
			
		||||
    shadertexturebinding_t texture;
 | 
			
		||||
    auto bindingIt = textureBindings.find(text);
 | 
			
		||||
    if(bindingIt == textureBindings.end()) {
 | 
			
		||||
      if(nextBinding >= UI_SHADER_TEXTURE_COUNT) {
 | 
			
		||||
        flushPass();
 | 
			
		||||
      }
 | 
			
		||||
      textureBindings[text] = nextBinding;
 | 
			
		||||
      textures[nextBinding] = text;
 | 
			
		||||
      data.textures[nextBinding] = nextBinding;
 | 
			
		||||
      texture = nextBinding++;
 | 
			
		||||
    } else {
 | 
			
		||||
      texture = bindingIt->second;
 | 
			
		||||
    }
 | 
			
		||||
    fTexture = (float_t)texture;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  data.quads[quadCount] = {
 | 
			
		||||
    quad,
 | 
			
		||||
    uvs,
 | 
			
		||||
    color,
 | 
			
		||||
    fTexture
 | 
			
		||||
  };
 | 
			
		||||
  quadCount++;
 | 
			
		||||
  if(quadCount == UI_SHADER_QUAD_COUNT) flushPass();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void UICanvas::flushPass() {
 | 
			
		||||
  if(quadCount == 0) return;
 | 
			
		||||
 | 
			
		||||
  auto pass = createRenderPass<UIShader, UIShaderData>(
 | 
			
		||||
    std::ref(*this),
 | 
			
		||||
@@ -73,6 +110,8 @@ std::vector<std::shared_ptr<IRenderPass>> UICanvas::getPasses(
 | 
			
		||||
  );
 | 
			
		||||
  passes.push_back(pass);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
  return passes;
 | 
			
		||||
  quadCount = 0;
 | 
			
		||||
  nextBinding = 0;
 | 
			
		||||
  textures.clear();
 | 
			
		||||
  textureBindings.clear();
 | 
			
		||||
}
 | 
			
		||||
@@ -6,25 +6,47 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
#include "scene/SceneItem.hpp"
 | 
			
		||||
#include "component/display/IRenderableComponent.hpp"
 | 
			
		||||
#include "ui/UIComponent.hpp"
 | 
			
		||||
#include "display/shader/UIShader.hpp"
 | 
			
		||||
 | 
			
		||||
namespace Dawn {
 | 
			
		||||
  class UIComponent;
 | 
			
		||||
 | 
			
		||||
  class UICanvas :
 | 
			
		||||
    public SceneComponent,
 | 
			
		||||
    public IRenderableComponent
 | 
			
		||||
  {
 | 
			
		||||
    protected:
 | 
			
		||||
    private:
 | 
			
		||||
      std::shared_ptr<Mesh> mesh;
 | 
			
		||||
      UIShaderData data;
 | 
			
		||||
 | 
			
		||||
      size_t quadCount = 0;
 | 
			
		||||
      shadertexturebinding_t nextBinding = 0;
 | 
			
		||||
      std::unordered_map<
 | 
			
		||||
        shadertexturebinding_t, std::shared_ptr<Texture>
 | 
			
		||||
      > textures;
 | 
			
		||||
      std::map<
 | 
			
		||||
        std::shared_ptr<Texture>, shadertexturebinding_t
 | 
			
		||||
      > textureBindings;
 | 
			
		||||
      
 | 
			
		||||
      std::vector<std::shared_ptr<IRenderPass>> passes;
 | 
			
		||||
      
 | 
			
		||||
    protected:
 | 
			
		||||
      virtual void onInit() override;
 | 
			
		||||
      virtual void onDispose() override;
 | 
			
		||||
      void flushPass();
 | 
			
		||||
 | 
			
		||||
    public:
 | 
			
		||||
      std::vector<std::shared_ptr<UIComponent>> components;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
      std::vector<std::shared_ptr<IRenderPass>> getPasses(
 | 
			
		||||
        struct RenderPassContext &ctx
 | 
			
		||||
      ) override;
 | 
			
		||||
 | 
			
		||||
      void addQuad(
 | 
			
		||||
        const glm::vec4 quad,
 | 
			
		||||
        const glm::vec4 uvs,
 | 
			
		||||
        const struct Color color,
 | 
			
		||||
        const std::shared_ptr<Texture> texture
 | 
			
		||||
      );
 | 
			
		||||
  };
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user