Khaotic Engine Reborn
Loading...
Searching...
No Matches
render_system.h
1#pragma once
2#include "../entity_manager.h"
3#include "../components/render_component.h"
4#include "../components/transform_component.h"
5#include "../components/shader_component.h"
6#include "shader_manager_class.h"
7#include <DirectXMath.h>
8
9namespace ecs {
10
12public:
19 RenderSystem(ID3D11DeviceContext* deviceContext, shader_manager_class* shaderManager)
20 : m_deviceContext(deviceContext), m_shaderManager(shaderManager) {}
21
38 bool RenderEntity(std::shared_ptr<Entity> entity,
39 const DirectX::XMMATRIX& viewMatrix,
40 const DirectX::XMMATRIX& projectionMatrix,
41 const DirectX::XMFLOAT4* diffuseColors,
42 const DirectX::XMFLOAT4* lightPositions,
43 const DirectX::XMFLOAT4* ambientColors,
44 const DirectX::XMFLOAT3& cameraPosition,
45 const DirectX::XMFLOAT4& sunlightDiffuse,
46 const DirectX::XMFLOAT4& sunlightAmbient,
47 const DirectX::XMFLOAT3& sunlightDirection,
48 float sunlightIntensity) {
49
50 // Vérifier si l'entité a tous les composants nécessaires
51 auto transform = entity->GetComponent<TransformComponent>();
52 auto render = entity->GetComponent<RenderComponent>();
53 auto shader = entity->GetComponent<ShaderComponent>();
54
55 if (!transform || !render || !shader || !render->GetModel())
56 return false;
57
58 // Calculer la matrice monde
59 XMMATRIX scaleMatrix = transform->GetScaleMatrix();
60 XMMATRIX rotateMatrix = transform->GetRotateMatrix();
61 XMMATRIX translateMatrix = transform->GetTranslateMatrix();
62
63 XMMATRIX worldMatrix = XMMatrixMultiply(
64 XMMatrixMultiply(scaleMatrix, rotateMatrix),
65 translateMatrix
66 );
67
68 // Rendre le modèle
69 render->Render(m_deviceContext);
70
71 // Sélectionner le shader approprié
72 switch (shader->GetActiveShader()) {
73 case ShaderType::ALPHA_MAPPING:
74 return m_shaderManager->render_alpha_map_shader(
75 m_deviceContext,
76 render->GetIndexCount(),
77 worldMatrix,
78 viewMatrix,
79 projectionMatrix,
80 render->GetTexture(TextureType::Diffuse, 0),
81 render->GetTexture(TextureType::Diffuse, 1),
82 render->GetTexture(TextureType::Alpha, 0)
83 );
84
85 case ShaderType::CEL_SHADING:
86 return m_shaderManager->render_cel_shading_shader(
87 m_deviceContext,
88 render->GetIndexCount(),
89 worldMatrix,
90 viewMatrix,
91 projectionMatrix,
92 render->GetTexture(TextureType::Diffuse, 0),
93 sunlightDiffuse,
94 sunlightAmbient,
95 sunlightDirection,
96 sunlightIntensity
97 );
98
99 case ShaderType::NORMAL_MAPPING:
100 return m_shaderManager->render_normal_map_shader(
101 m_deviceContext,
102 render->GetIndexCount(),
103 worldMatrix,
104 viewMatrix,
105 projectionMatrix,
106 render->GetTexture(TextureType::Diffuse, 0),
107 render->GetTexture(TextureType::Normal, 0),
108 sunlightDirection,
109 sunlightDiffuse
110 );
111
112 case ShaderType::SPECULAR_MAPPING:
113
114 return m_shaderManager->render_spec_map_shader(
115 m_deviceContext,
116 render->GetIndexCount(),
117 worldMatrix,
118 viewMatrix,
119 projectionMatrix,
120 render->GetTexture(TextureType::Diffuse, 0),
121 render->GetTexture(TextureType::Normal, 0),
122 render->GetTexture(TextureType::Specular, 0),
123 sunlightDirection,
124 sunlightDiffuse,
125 cameraPosition,
126 sunlightDiffuse, // Couleur speculaire (à ajuster)
127 16.0f // Puissance speculaire (à ajuster)
128 );
129
130 case ShaderType::LIGHTING:
131 {
132 // Créer des copies locales non constantes des tableaux
133 DirectX::XMFLOAT4 localDiffuseColors[4];
134 DirectX::XMFLOAT4 localLightPositions[4];
135 DirectX::XMFLOAT4 localAmbientColors[4];
136
137 // Copier les données
138 for (int i = 0; i < 4; i++) {
139 localDiffuseColors[i] = diffuseColors[i];
140 localLightPositions[i] = lightPositions[i];
141 localAmbientColors[i] = ambientColors[i];
142 }
143
144 return m_shaderManager->renderlight_shader(
145 m_deviceContext,
146 render->GetIndexCount(),
147 worldMatrix,
148 viewMatrix,
149 projectionMatrix,
150 render->GetTexture(TextureType::Diffuse, 0),
151 localDiffuseColors,
152 localLightPositions,
153 localAmbientColors
154 );
155 }
156
157 case ShaderType::SUNLIGHT:
158 return m_shaderManager->render_sunlight_shader(
159 m_deviceContext,
160 render->GetIndexCount(),
161 worldMatrix,
162 viewMatrix,
163 projectionMatrix,
164 render->GetTexture(TextureType::Diffuse, 0),
165 sunlightDiffuse,
166 sunlightAmbient,
167 sunlightDirection,
168 sunlightIntensity
169 );
170
171 case ShaderType::SKYBOX:
172 return m_shaderManager->render_skybox_shader(
173 m_deviceContext,
174 render->GetIndexCount(),
175 worldMatrix,
176 viewMatrix,
177 projectionMatrix,
178 render->GetTexture(TextureType::Diffuse, 0),
179 sunlightDiffuse,
180 sunlightAmbient,
181 sunlightDirection,
182 sunlightIntensity
183 );
184
185 case ShaderType::TEXTURE:
186 default:
187 return m_shaderManager->render_texture_shader(
188 m_deviceContext,
189 render->GetIndexCount(),
190 worldMatrix,
191 viewMatrix,
192 projectionMatrix,
193 render->GetTexture(TextureType::Diffuse, 0)
194 );
195 }
196 }
197
215 const DirectX::XMMATRIX& viewMatrix,
216 const DirectX::XMMATRIX& projectionMatrix,
217 const DirectX::XMFLOAT4* diffuseColors,
218 const DirectX::XMFLOAT4* lightPositions,
219 const DirectX::XMFLOAT4* ambientColors,
220 const DirectX::XMFLOAT3& cameraPos,
221 const DirectX::XMFLOAT4& sunlightDiffuse,
222 const DirectX::XMFLOAT4& sunlightAmbient,
223 const DirectX::XMFLOAT3& sunlightDirection,
224 float sunlightIntensity) {
225
226 int renderCount = 0;
227
228 // Récupérer toutes les entités qui ont les composants RenderComponent et TransformComponent
229 auto entities = entityManager->GetEntitiesWithComponent<RenderComponent>();
230
231 for (auto& entity : entities) {
232 auto render = entity->GetComponent<RenderComponent>();
233
234 // Vérifier si l'entité a un TransformComponent
235 auto transform = entity->GetComponent<TransformComponent>();
236 if (!transform) continue;
237
238 // Vérifier si le modèle est visible
239 if (!render->IsVisible()) continue;
240
241 // Effectuer le rendu
242 if (RenderEntity(entity, viewMatrix, projectionMatrix,
243 diffuseColors, lightPositions, ambientColors,cameraPos,
244 sunlightDiffuse, sunlightAmbient, sunlightDirection,
245 sunlightIntensity)) {
246 renderCount++;
247 }
248 }
249
250 return renderCount;
251 }
252
253private:
254 ID3D11DeviceContext* m_deviceContext;
255 shader_manager_class* m_shaderManager;
256};
257
258} // namespace ecs
std::vector< std::shared_ptr< Entity > > GetEntitiesWithComponent()
RenderSystem(ID3D11DeviceContext *deviceContext, shader_manager_class *shaderManager)
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)
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 component.h:9