Wildvine Engine
Referencia Doxygen del codigo propio de Wildvine Engine.
Cargando...
Buscando...
Nada coincide
SceneGraph.cpp
Ir a la documentación de este archivo.
1
8#include "ECS\Entity.h"
9#include "ECS\Transform.h"
10#include "ECS\LightComponent.h"
12#include "DeviceContext.h"
14#include "Rendering/Material.h"
17
19 m_entities.clear();
20}
21
23 for (Entity* e : m_entities)
24 {
25 if (!e) continue;
26 auto h = e->getComponent<HierarchyComponent>();
27 if (h)
28 {
29 h->m_parent = nullptr;
30 h->m_children.clear();
31 }
32 }
33
34 m_entities.clear();
35}
36
37void
39 if (!e) {
40 return;
41 }
42 if (isRegistered(e)) {
43 return;
44 }
45
46 // // Validar que existen los componentes minimos
47 if (!e->getComponent<Transform>()) {
48 e->addComponent(EU::MakeShared<Transform>());
50 }
52 e->addComponent(EU::MakeShared<HierarchyComponent>());
54 }
55
56 m_entities.push_back(e);
57}
58
59void
61 if (!e) return;
62 if (!isRegistered(e)) return;
63
64 // 1) Detach de su padre (si tiene)
65 detach(e);
66
67 // 2) Reparent de hijos a null (roots) o detach total
68 auto h = e->getComponent<HierarchyComponent>();
69 if (h)
70 {
71 // Copia local para no invalidar mientras iteras
72 auto childrenCopy = h->m_children;
73 for (Entity* c : childrenCopy)
74 {
75 if (!c) continue;
76 // detach del padre (que es e)
77 auto hc = c->getComponent<HierarchyComponent>();
78 if (hc && hc->m_parent == e)
79 hc->m_parent = nullptr;
80
81 // quitar referencia en e
82 h->removeChild(c);
83
84 // marcar dirty para recalcular world
85 auto wt = c->getComponent<Transform>();
86 //if (wt) wt->dirty = true;
87 //markWorldDirtyRecursive(wt);
88 }
89
90 h->m_children.clear();
91 }
92
93 // 3) eliminar del registro
94 m_entities.erase(std::remove(m_entities.begin(), m_entities.end(), e), m_entities.end());
95}
96
97bool
98SceneGraph::isAncestor(Entity* possibleAncestor, Entity* node) const {
99 // Recorre hacia arriba desde node: si encuentra possibleAncestor, hay ciclo
100 if (!possibleAncestor || !node) return false;
101
102 auto h = node->getComponent<HierarchyComponent>();
103 while (h && h->m_parent)
104 {
105 if (h->m_parent == possibleAncestor) return true;
106 node = h->m_parent;
108 if (node)
109 h = node->getComponent<HierarchyComponent>();
110
111 }
112 return false;
113}
114
115bool
117
118 if (!e) return false;
119 auto h = e->getComponent<HierarchyComponent>();
120 return (!h || h->m_parent == nullptr);
121}
122
123bool
125 return std::find(m_entities.begin(), m_entities.end(), e) != m_entities.end();
126}
127
128bool
130{
131 if (!child || !parent) return false;
132 if (child == parent) return false;
133
134 // Registro automático
135 addEntity(child);
136 addEntity(parent);
137
138 // Evita ciclos: parent no puede estar debajo de child
139 if (isAncestor(child, parent)) return false;
140
141 // Si child ya tiene padre, detach
142 detach(child);
143
144 auto hc = child->getComponent<HierarchyComponent>();
145 auto hp = parent->getComponent<HierarchyComponent>();
146 if (!hc || !hp) return false;
147
148 hc->m_parent = parent;
149 hp->addChild(child);
150
151 //markWorldDirtyRecursive(wt);
152 return true;
153}
154
155bool
157 if (!child) return false;
158
159 auto hc = child->getComponent<HierarchyComponent>();
160 if (!hc) return false;
161
162 Entity* parent = hc->m_parent;
163 if (!parent) return true; // ya estaba root
164
165 auto hp = parent->getComponent<HierarchyComponent>();
166 if (hp) hp->removeChild(child);
167
168 hc->m_parent = nullptr;
169
170 //markWorldDirtyRecursive(wt);
171 return true;
172}
173
174void
175SceneGraph::update(float deltaTime, DeviceContext& deviceContext) {
176 // Actualiza todas las entidades
177 for (Entity* e : m_entities)
178 {
179 if (!e) continue;
180 e->update(deltaTime, deviceContext);
181 }
182
183 // 2) Propagación World: procesa roots
184 for (Entity* e : m_entities)
185 {
186 if (!e) continue;
187 if (isRoot(e))
188 {
189 updateWorldRecursive(e, XMMatrixIdentity());
190 }
191 }
192}
193
194void
195SceneGraph::updateWorldRecursive(Entity* node, const XMMATRIX& parentWorld) {
196 auto t = node->getComponent<Transform>();
197 // Dirty Matrix?
198 auto h = node->getComponent<HierarchyComponent>();
199
200 if (!t || !h) {
201 return;
202 }
203 // Tu Transform::matrix es LOCAL (S*R*T)
204 // World = Local * ParentWorld
205 auto worldMatrix = t->matrix * parentWorld;
206 t->worldMatrix = worldMatrix;
207
208 for (Entity* c : h->m_children) {
209 updateWorldRecursive(c, worldMatrix);
210 }
211}
212
213void SceneGraph::render(DeviceContext& deviceContext) {
214 // Render all entities
215 for (auto& e : m_entities) {
216 if (e) {
217 e->render(deviceContext);
218 }
219 }
220}
221
222void
224 for (Entity* entity : m_entities)
225 {
226 if (!entity) {
227 continue;
228 }
229
230 auto lightComponent = entity->getComponent<LightComponent>();
231 if (lightComponent) {
232 outScene.directionalLights.push_back(lightComponent->getLightData());
233 }
234
235 auto meshRenderer = entity->getComponent<MeshRendererComponent>();
236 auto transform = entity->getComponent<Transform>();
237 if (!meshRenderer || !transform || !meshRenderer->isVisible()) {
238 continue;
239 }
240
241 RenderObject renderObject{};
242 renderObject.mesh = meshRenderer->getMesh();
243 renderObject.materialInstance = meshRenderer->getMaterialInstance();
244 renderObject.materialInstances = meshRenderer->getMaterialInstances();
245 renderObject.world = transform->worldMatrix;
246 renderObject.castShadow = meshRenderer->canCastShadow();
247
248 EU::Vector3 cameraPos = camera.getPosition();
249 XMFLOAT4X4 worldMatrix{};
250 XMStoreFloat4x4(&worldMatrix, transform->worldMatrix);
251 EU::Vector3 objectPos = EU::Vector3(worldMatrix._41, worldMatrix._42, worldMatrix._43);
252 float dx = objectPos.x - cameraPos.x;
253 float dy = objectPos.y - cameraPos.y;
254 float dz = objectPos.z - cameraPos.z;
255 renderObject.distanceToCamera = dx * dx + dy * dy + dz * dz;
256
258 if (renderObject.materialInstance &&
259 renderObject.materialInstance->getMaterial()) {
260 domain = renderObject.materialInstance->getMaterial()->getDomain();
261 }
262
263 renderObject.transparent = (domain == MaterialDomain::Transparent);
264 if (renderObject.transparent) {
265 outScene.transparentObjects.push_back(renderObject);
266 }
267 else {
268 outScene.opaqueObjects.push_back(renderObject);
269 }
270 }
271}
272
273
274
Declara la API de Camera dentro del subsistema Utilities.
Declara la API de DeviceContext dentro del subsistema Core.
Declara la API de Entity dentro del subsistema ECS.
Declara la API de HierarchyComponent dentro del subsistema SceneGraph.
Declara la API de LightComponent dentro del subsistema ECS.
Declara la API de MaterialInstance dentro del subsistema Rendering.
Declara la API de Material dentro del subsistema Rendering.
Declara la API de MeshRendererComponent dentro del subsistema ECS.
Declara la API de RenderScene dentro del subsistema Rendering.
MaterialDomain
Definition RenderTypes.h:13
Declara la API de SceneGraph dentro del subsistema SceneGraph.
Declara la API de Transform dentro del subsistema ECS.
EU::Vector3 getPosition() const
Obtiene la posición en mundo.
Definition Camera.h:45
Clase TSharedPointer para manejar la gestión de memoria compartida.
A 3D vector class.
Definition Vector3.h:45
float x
The x-coordinate of the vector.
Definition Vector3.h:47
float z
The z-coordinate of the vector.
Definition Vector3.h:49
float y
The y-coordinate of the vector.
Definition Vector3.h:48
void addComponent(EU::TSharedPointer< T > component)
Agrega un componente a la entidad.
Definition Entity.h:61
EU::TSharedPointer< T > getComponent()
Obtiene un componente de la entidad por su tipo.
Definition Entity.h:73
std::vector< Entity * > m_children
void addChild(Entity *child)
void removeChild(Entity *child)
Contenedor temporal con los elementos visibles de un frame.
Definition RenderScene.h:20
std::vector< RenderObject > transparentObjects
Objetos transparentes ordenables por distancia.
Definition RenderScene.h:29
std::vector< LightData > directionalLights
Luces direccionales activas en la escena.
Definition RenderScene.h:30
std::vector< RenderObject > opaqueObjects
Objetos opacos listos para renderizar.
Definition RenderScene.h:28
void gatherRenderScene(RenderScene &outScene, const Camera &camera)
void destroy()
bool isRegistered(Entity *e) const
bool isRoot(Entity *e) const
void init()
void addEntity(Entity *e)
Registra una entidad dentro del grafo.
void render(DeviceContext &deviceContext)
void updateWorldRecursive(Entity *node, const XMMATRIX &parentWorld)
bool detach(Entity *child)
void update(float deltaTime, DeviceContext &deviceContext)
void removeEntity(Entity *e)
Elimina una entidad del grafo si esta registrada.
std::vector< Entity * > m_entities
Entidades registradas en el grafo.
Definition SceneGraph.h:77
bool attach(Entity *child, Entity *parent)
bool isAncestor(Entity *possibleAncestor, Entity *node) const