First render.
This commit is contained in:
		@@ -6,6 +6,8 @@
 | 
			
		||||
target_sources(${DAWN_TARGET_NAME}
 | 
			
		||||
  PRIVATE
 | 
			
		||||
    Camera.cpp
 | 
			
		||||
    Material.cpp
 | 
			
		||||
    MeshRenderer.cpp
 | 
			
		||||
)
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
# Subdirs
 | 
			
		||||
add_subdirectory(material)
 | 
			
		||||
@@ -51,7 +51,9 @@ glm::mat4 Camera::getProjection() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float_t Camera::getAspect() {
 | 
			
		||||
  return 1.0f;
 | 
			
		||||
  auto rt = this->getRenderTarget();
 | 
			
		||||
  if(rt == nullptr) rt = getGame()->renderHost.getBackBufferRenderTarget();
 | 
			
		||||
  return rt->getWidth() / rt->getHeight();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Camera::setRenderTarget(std::shared_ptr<RenderTarget> renderTarget) {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										159
									
								
								src/dawn/component/display/IRenderableComponent.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										159
									
								
								src/dawn/component/display/IRenderableComponent.hpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,159 @@
 | 
			
		||||
// Copyright (c) 2023 Dominic Masters
 | 
			
		||||
// 
 | 
			
		||||
// This software is released under the MIT License.
 | 
			
		||||
// https://opensource.org/licenses/MIT
 | 
			
		||||
 | 
			
		||||
#pragma once
 | 
			
		||||
#include "game/Game.hpp"
 | 
			
		||||
#include "scene/Scene.hpp"
 | 
			
		||||
#include "display/mesh/Mesh.hpp"
 | 
			
		||||
#include "display/shader/Shader.hpp"
 | 
			
		||||
#include "component/display/Camera.hpp"
 | 
			
		||||
#include "component/display/MeshRenderer.hpp"
 | 
			
		||||
 | 
			
		||||
namespace Dawn {
 | 
			
		||||
  struct IRenderPass {
 | 
			
		||||
    public:
 | 
			
		||||
      std::shared_ptr<Mesh> mesh;
 | 
			
		||||
 | 
			
		||||
      /**
 | 
			
		||||
       * Binds the shader for this render pass.
 | 
			
		||||
       */
 | 
			
		||||
      virtual void bind() = 0;
 | 
			
		||||
 | 
			
		||||
      /**
 | 
			
		||||
       * Sets the data for this render pass to the shader.
 | 
			
		||||
       */
 | 
			
		||||
      virtual void setData() = 0;
 | 
			
		||||
 | 
			
		||||
      /**
 | 
			
		||||
       * Uploads the data to the GPU.
 | 
			
		||||
       */
 | 
			
		||||
      virtual void upload() = 0;
 | 
			
		||||
 | 
			
		||||
      /**
 | 
			
		||||
       * Draws the mesh for this render pass.
 | 
			
		||||
       */
 | 
			
		||||
      virtual void draw() = 0;
 | 
			
		||||
 | 
			
		||||
      /**
 | 
			
		||||
       * Cleans up the render pass.
 | 
			
		||||
       */
 | 
			
		||||
      virtual ~IRenderPass() { }
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  template<class S, typename D>
 | 
			
		||||
  struct RenderPass : public IRenderPass {
 | 
			
		||||
    private:
 | 
			
		||||
      std::shared_ptr<S> shader;
 | 
			
		||||
      const D data;
 | 
			
		||||
      std::shared_ptr<Mesh> mesh;
 | 
			
		||||
      const enum MeshDrawMode drawMode;
 | 
			
		||||
      const int32_t indiceStart;
 | 
			
		||||
      const int32_t indiceCount;
 | 
			
		||||
 | 
			
		||||
    public:
 | 
			
		||||
      /**
 | 
			
		||||
       * Constructs a new RenderPass.
 | 
			
		||||
       * 
 | 
			
		||||
       * @param self Self component instance that is creating this render pass.
 | 
			
		||||
       * @param d The data to use for this render pass.
 | 
			
		||||
       * @param mesh The mesh to use for this render pass.
 | 
			
		||||
       * @param drawMode The draw mode to use for this render pass.
 | 
			
		||||
       * @param indiceStart The indice to start drawing from.
 | 
			
		||||
       * @param indiceCount The number of indices to draw.
 | 
			
		||||
       */
 | 
			
		||||
      RenderPass(
 | 
			
		||||
        SceneComponent &self,
 | 
			
		||||
        const D d,
 | 
			
		||||
        const std::shared_ptr<Mesh> mesh,
 | 
			
		||||
        const enum MeshDrawMode drawMode,
 | 
			
		||||
        const int32_t indiceStart,
 | 
			
		||||
        const int32_t indiceCount
 | 
			
		||||
      ) :
 | 
			
		||||
        data(d),
 | 
			
		||||
        mesh(mesh),
 | 
			
		||||
        drawMode(drawMode),
 | 
			
		||||
        indiceStart(indiceStart),
 | 
			
		||||
        indiceCount(indiceCount)
 | 
			
		||||
      {
 | 
			
		||||
        //Get the shader
 | 
			
		||||
        shader = (
 | 
			
		||||
          self.getGame()->renderHost.shaderManager.getShader<S>()
 | 
			
		||||
        );
 | 
			
		||||
        assertNotNull(shader, "Shader cannot be null!");
 | 
			
		||||
 | 
			
		||||
        // Need mesh?
 | 
			
		||||
        if(!this->mesh) {
 | 
			
		||||
          auto meshRenderer = self.getItem()->getComponent<MeshRenderer>();
 | 
			
		||||
          if(meshRenderer) this->mesh = meshRenderer->mesh;
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      void bind() override {
 | 
			
		||||
        shader->bind();
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      void setData() override {
 | 
			
		||||
        shader->setData(data);
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      void upload() override {
 | 
			
		||||
        shader->upload();
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      void draw() override {
 | 
			
		||||
        if(mesh) {
 | 
			
		||||
          mesh->draw(drawMode, indiceStart, indiceCount);
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  struct RenderPassContext {
 | 
			
		||||
    std::shared_ptr<Game> game;
 | 
			
		||||
    std::shared_ptr<Scene> scene;
 | 
			
		||||
    std::shared_ptr<Camera> camera;
 | 
			
		||||
    std::shared_ptr<RenderTarget> renderTarget;
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  class IRenderableComponent {
 | 
			
		||||
    public:
 | 
			
		||||
      /**
 | 
			
		||||
       * Retreive the list of render passes for this component.
 | 
			
		||||
       * 
 | 
			
		||||
       * @param ctx Context for the render pass.
 | 
			
		||||
       * @return List of render passes.
 | 
			
		||||
       */
 | 
			
		||||
      virtual std::vector<std::shared_ptr<IRenderPass>> getPasses(
 | 
			
		||||
        struct RenderPassContext &ctx
 | 
			
		||||
      ) = 0;
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Short-hand function to create a render pass.
 | 
			
		||||
   * 
 | 
			
		||||
   * @tparam S Shader type.
 | 
			
		||||
   * @tparam D Shader's data type
 | 
			
		||||
   * @param self Instance of the IRenderableComponent that is creating the pass.
 | 
			
		||||
   * @param data Data to use for the render pass.
 | 
			
		||||
   * @return Created render pass.
 | 
			
		||||
   */
 | 
			
		||||
  template<class S, typename D>
 | 
			
		||||
  std::shared_ptr<struct IRenderPass> createRenderPass(
 | 
			
		||||
    SceneComponent &self,
 | 
			
		||||
    const D data,
 | 
			
		||||
    const std::shared_ptr<Mesh> mesh = nullptr,
 | 
			
		||||
    const enum MeshDrawMode drawMode = MeshDrawMode::TRIANGLES,
 | 
			
		||||
    int32_t indiceStart = 0,
 | 
			
		||||
    int32_t indiceCount = -1
 | 
			
		||||
  ) {
 | 
			
		||||
    return std::make_shared<struct RenderPass<S,D>>(
 | 
			
		||||
      self,
 | 
			
		||||
      data,
 | 
			
		||||
      mesh,
 | 
			
		||||
      drawMode,
 | 
			
		||||
      indiceStart,
 | 
			
		||||
      indiceCount
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										10
									
								
								src/dawn/component/display/material/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								src/dawn/component/display/material/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,10 @@
 | 
			
		||||
# Copyright (c) 2023 Dominic Masters
 | 
			
		||||
# 
 | 
			
		||||
# This software is released under the MIT License.
 | 
			
		||||
# https://opensource.org/licenses/MIT
 | 
			
		||||
 | 
			
		||||
target_sources(${DAWN_TARGET_NAME}
 | 
			
		||||
  PRIVATE
 | 
			
		||||
    Material.cpp
 | 
			
		||||
    SimpleTexturedMaterial.cpp
 | 
			
		||||
)
 | 
			
		||||
@@ -5,9 +5,15 @@
 | 
			
		||||
 | 
			
		||||
#pragma once
 | 
			
		||||
#include "scene/SceneComponent.hpp"
 | 
			
		||||
#include "component/display/IRenderableComponent.hpp"
 | 
			
		||||
 | 
			
		||||
namespace Dawn {
 | 
			
		||||
  class Material : public SceneComponent {
 | 
			
		||||
  class Material :
 | 
			
		||||
    public SceneComponent,
 | 
			
		||||
    public IRenderableComponent
 | 
			
		||||
  {
 | 
			
		||||
    protected:
 | 
			
		||||
 | 
			
		||||
    public:
 | 
			
		||||
      void onInit() override;
 | 
			
		||||
      void onDispose() override;
 | 
			
		||||
@@ -0,0 +1,32 @@
 | 
			
		||||
// Copyright (c) 2023 Dominic Masters
 | 
			
		||||
// 
 | 
			
		||||
// This software is released under the MIT License.
 | 
			
		||||
// https://opensource.org/licenses/MIT
 | 
			
		||||
 | 
			
		||||
#include "SimpleTexturedMaterial.hpp"
 | 
			
		||||
 | 
			
		||||
using namespace Dawn;
 | 
			
		||||
 | 
			
		||||
struct Color SimpleTexturedMaterial::getColor() {
 | 
			
		||||
  return this->data.color;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SimpleTexturedMaterial::setColor(struct Color color) {
 | 
			
		||||
  this->data.color = color;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::vector<std::shared_ptr<IRenderPass>> SimpleTexturedMaterial::getPasses(
 | 
			
		||||
  struct RenderPassContext &ctx
 | 
			
		||||
) {
 | 
			
		||||
  this->data.model = this->getItem()->getWorldTransform();
 | 
			
		||||
  this->data.projection = ctx.camera->getProjection();
 | 
			
		||||
  this->data.view = ctx.camera->getItem()->getWorldTransform();
 | 
			
		||||
  this->data.color = COLOR_RED;
 | 
			
		||||
 | 
			
		||||
  return {
 | 
			
		||||
    createRenderPass<SimpleTexturedShader, struct SimpleTexturedShaderData>(
 | 
			
		||||
      *this,
 | 
			
		||||
      data
 | 
			
		||||
    )
 | 
			
		||||
  };
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,32 @@
 | 
			
		||||
// Copyright (c) 2023 Dominic Masters
 | 
			
		||||
// 
 | 
			
		||||
// This software is released under the MIT License.
 | 
			
		||||
// https://opensource.org/licenses/MIT
 | 
			
		||||
 | 
			
		||||
#pragma once
 | 
			
		||||
#include "component/display/material/Material.hpp"
 | 
			
		||||
#include "display/shader/SimpleTexturedShader.hpp"
 | 
			
		||||
 | 
			
		||||
namespace Dawn {
 | 
			
		||||
  class SimpleTexturedMaterial : public Material {
 | 
			
		||||
    private:
 | 
			
		||||
      struct SimpleTexturedShaderData data;
 | 
			
		||||
 | 
			
		||||
    public:
 | 
			
		||||
      /**
 | 
			
		||||
       * Returns the color of this material.
 | 
			
		||||
       */
 | 
			
		||||
      struct Color getColor();
 | 
			
		||||
 | 
			
		||||
      /**
 | 
			
		||||
       * Sets the color of this material.
 | 
			
		||||
       * 
 | 
			
		||||
       * @param color The color to set.
 | 
			
		||||
       */
 | 
			
		||||
      void setColor(struct Color color);
 | 
			
		||||
 | 
			
		||||
      std::vector<std::shared_ptr<IRenderPass>> getPasses(
 | 
			
		||||
        struct RenderPassContext &ctx
 | 
			
		||||
      ) override;
 | 
			
		||||
  };
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user