# Khaotic Engine Reborn: enginecustom/src/inc/system/ecs/systems/render_system.h Source File

Loading...
Searching...
No Matches
render_system.h
#pragma once
#include "../entity_manager.h"
#include "../components/render_component.h"
#include "../components/transform_component.h"
#include "../components/shader_component.h"
#include "shader_manager_class.h"
#include <DirectXMath.h>
namespace ecs {
class RenderSystem {
public:
RenderSystem(ID3D11DeviceContext* deviceContext, shader_manager_class* shaderManager)
: m_deviceContext(deviceContext), m_shaderManager(shaderManager) {}
bool RenderEntity(std::shared_ptr<Entity> entity,
const DirectX::XMMATRIX& viewMatrix,
const DirectX::XMMATRIX& projectionMatrix,
const DirectX::XMFLOAT4* diffuseColors,
const DirectX::XMFLOAT4* lightPositions,
const DirectX::XMFLOAT4* ambientColors,
const DirectX::XMFLOAT3& cameraPosition,
const DirectX::XMFLOAT4& sunlightDiffuse,
const DirectX::XMFLOAT4& sunlightAmbient,
const DirectX::XMFLOAT3& sunlightDirection,
float sunlightIntensity) {
// Vérifier si l'entité a tous les composants nécessaires
auto transform = entity->GetComponent<TransformComponent>();
auto render = entity->GetComponent<RenderComponent>();
auto shader = entity->GetComponent<ShaderComponent>();
if (!transform || !render || !shader || !render->GetModel())
return false;
// Calculer la matrice monde
XMMATRIX scaleMatrix = transform->GetScaleMatrix();
XMMATRIX rotateMatrix = transform->GetRotateMatrix();
XMMATRIX translateMatrix = transform->GetTranslateMatrix();
XMMATRIX worldMatrix = XMMatrixMultiply(
XMMatrixMultiply(scaleMatrix, rotateMatrix),
translateMatrix
);
// Rendre le modèle
render->Render(m_deviceContext);
// Sélectionner le shader approprié
switch (shader->GetActiveShader()) {
case ShaderType::ALPHA_MAPPING:
return m_shaderManager->render_alpha_map_shader(
m_deviceContext,
render->GetIndexCount(),
worldMatrix,
viewMatrix,
projectionMatrix,
render->GetTexture(TextureType::Diffuse, 0),
render->GetTexture(TextureType::Diffuse, 1),
render->GetTexture(TextureType::Alpha, 0)
);
case ShaderType::CEL_SHADING:
return m_shaderManager->render_cel_shading_shader(
m_deviceContext,
render->GetIndexCount(),
worldMatrix,
viewMatrix,
projectionMatrix,
render->GetTexture(TextureType::Diffuse, 0),
sunlightDiffuse,
sunlightAmbient,
sunlightDirection,
sunlightIntensity
);
case ShaderType::NORMAL_MAPPING:
return m_shaderManager->render_normal_map_shader(
m_deviceContext,
render->GetIndexCount(),
worldMatrix,
viewMatrix,
projectionMatrix,
render->GetTexture(TextureType::Diffuse, 0),
render->GetTexture(TextureType::Normal, 0),
sunlightDirection,
sunlightDiffuse
);
case ShaderType::SPECULAR_MAPPING:
return m_shaderManager->render_spec_map_shader(
m_deviceContext,
render->GetIndexCount(),
worldMatrix,
viewMatrix,
projectionMatrix,
render->GetTexture(TextureType::Diffuse, 0),
render->GetTexture(TextureType::Normal, 0),
render->GetTexture(TextureType::Specular, 0),
sunlightDirection,
sunlightDiffuse,
cameraPosition,
sunlightDiffuse, // Couleur speculaire (à ajuster)
16.0f // Puissance speculaire (à ajuster)
);
case ShaderType::LIGHTING:
{
// Créer des copies locales non constantes des tableaux
DirectX::XMFLOAT4 localDiffuseColors[4];
DirectX::XMFLOAT4 localLightPositions[4];
DirectX::XMFLOAT4 localAmbientColors[4];
// Copier les données
for (int i = 0; i < 4; i++) {
localDiffuseColors[i] = diffuseColors[i];
localLightPositions[i] = lightPositions[i];
localAmbientColors[i] = ambientColors[i];
}
return m_shaderManager->renderlight_shader(
m_deviceContext,
render->GetIndexCount(),
worldMatrix,
viewMatrix,
projectionMatrix,
render->GetTexture(TextureType::Diffuse, 0),
localDiffuseColors,
localLightPositions,
localAmbientColors
);
}
case ShaderType::SUNLIGHT:
return m_shaderManager->render_sunlight_shader(
m_deviceContext,
render->GetIndexCount(),
worldMatrix,
viewMatrix,
projectionMatrix,
render->GetTexture(TextureType::Diffuse, 0),
sunlightDiffuse,
sunlightAmbient,
sunlightDirection,
sunlightIntensity
);
case ShaderType::SKYBOX:
return m_shaderManager->render_skybox_shader(
m_deviceContext,
render->GetIndexCount(),
worldMatrix,
viewMatrix,
projectionMatrix,
render->GetTexture(TextureType::Diffuse, 0),
sunlightDiffuse,
sunlightAmbient,
sunlightDirection,
sunlightIntensity
);
case ShaderType::TEXTURE:
default:
return m_shaderManager->render_texture_shader(
m_deviceContext,
render->GetIndexCount(),
worldMatrix,
viewMatrix,
projectionMatrix,
render->GetTexture(TextureType::Diffuse, 0)
);
}
}
int RenderAllEntities(EntityManager* entityManager,
const DirectX::XMMATRIX& viewMatrix,
const DirectX::XMMATRIX& projectionMatrix,
const DirectX::XMFLOAT4* diffuseColors,
const DirectX::XMFLOAT4* lightPositions,
const DirectX::XMFLOAT4* ambientColors,
const DirectX::XMFLOAT3& cameraPos,
const DirectX::XMFLOAT4& sunlightDiffuse,
const DirectX::XMFLOAT4& sunlightAmbient,
const DirectX::XMFLOAT3& sunlightDirection,
float sunlightIntensity) {
int renderCount = 0;
// Récupérer toutes les entités qui ont les composants RenderComponent et TransformComponent
auto entities = entityManager->GetEntitiesWithComponent<RenderComponent>();
for (auto& entity : entities) {
auto render = entity->GetComponent<RenderComponent>();
// Vérifier si l'entité a un TransformComponent
auto transform = entity->GetComponent<TransformComponent>();
if (!transform) continue;
// Vérifier si le modèle est visible
if (!render->IsVisible()) continue;
// Effectuer le rendu
if (RenderEntity(entity, viewMatrix, projectionMatrix,
diffuseColors, lightPositions, ambientColors,cameraPos,
sunlightDiffuse, sunlightAmbient, sunlightDirection,
sunlightIntensity)) {
renderCount++;
}
}
return renderCount;
}
private:
ID3D11DeviceContext* m_deviceContext;
shader_manager_class* m_shaderManager;
};
} // namespace ecs
ecs::EntityManager
Definition entity_manager.h:9
ecs::EntityManager::GetEntitiesWithComponent
std::vector< std::shared_ptr< Entity > > GetEntitiesWithComponent()
Definition entity_manager.h:95
ecs::RenderComponent
Definition render_component.h:29
ecs::RenderSystem
Definition render_system.h:11
ecs::RenderSystem::RenderSystem
RenderSystem(ID3D11DeviceContext *deviceContext, shader_manager_class *shaderManager)
Definition render_system.h:19
ecs::RenderSystem::RenderAllEntities
int RenderAllEntities(EntityManager *entityManager, const DirectX::XMMATRIX &viewMatrix, const DirectX::XMMATRIX &projectionMatrix, const DirectX::XMFLOAT4 *diffuseColors, const DirectX::XMFLOAT4 *lightPositions, const DirectX::XMFLOAT4 *ambientColors, const DirectX::XMFLOAT3 &cameraPos, const DirectX::XMFLOAT4 &sunlightDiffuse, const DirectX::XMFLOAT4 &sunlightAmbient, const DirectX::XMFLOAT3 &sunlightDirection, float sunlightIntensity)
Definition render_system.h:214
ecs::RenderSystem::RenderEntity
bool RenderEntity(std::shared_ptr< Entity > entity, const DirectX::XMMATRIX &viewMatrix, const DirectX::XMMATRIX &projectionMatrix, const DirectX::XMFLOAT4 *diffuseColors, const DirectX::XMFLOAT4 *lightPositions, const DirectX::XMFLOAT4 *ambientColors, const DirectX::XMFLOAT3 &cameraPosition, const DirectX::XMFLOAT4 &sunlightDiffuse, const DirectX::XMFLOAT4 &sunlightAmbient, const DirectX::XMFLOAT3 &sunlightDirection, float sunlightIntensity)
Definition render_system.h:38
ecs::ShaderComponent
Definition shader_component.h:24
ecs::TransformComponent
Definition transform_component.h:9
shader_manager_class
Definition shader_manager_class.h:28
ecs
Definition component.h:9