From bf1b5d78e556715e34ca6290b440277d1729c5cf Mon Sep 17 00:00:00 2001 From: CatChow0 Date: Tue, 24 Jun 2025 16:13:56 +0200 Subject: [PATCH] Minor - ECS use in other method pt.1 - V12.2.0 --- .../src/inc/system/application_class.h | 14 +- .../src/src/system/application_class.cpp | 401 ++++++++++-------- 2 files changed, 225 insertions(+), 190 deletions(-) diff --git a/enginecustom/src/inc/system/application_class.h b/enginecustom/src/inc/system/application_class.h index 357de4d..07d44fb 100644 --- a/enginecustom/src/inc/system/application_class.h +++ b/enginecustom/src/inc/system/application_class.h @@ -7,7 +7,6 @@ /////////////////////// #include "d_3d_class.h" #include "camera_class.h" -#include "object.h" #include "light_class.h" #include "bitmap_class.h" @@ -106,13 +105,8 @@ public: void set_speed(const float speed) { this->speed_ = speed; }; void add_cube(); + void delete_entity_by_id(int entity_id); void delete_kobject(int index); - size_t get_cube_count() const { return cubes_.size(); }; - size_t get_terrain_cube_count() const { return terrain_chunk_.size(); }; - std::vector get_cubes() const { return cubes_; }; - std::vector get_terrain_cubes() const { return terrain_chunk_; }; - std::vector get_kobjects() const { return object_; }; - void set_kobjects(std::vector kobjects) { object_ = kobjects; }; void add_kobject(std::wstring& filepath); void set_path(WCHAR* path) { path_ = path; }; void set_w_folder(const std::filesystem::path& w_folder) { w_folder_ = w_folder; }; @@ -188,7 +182,7 @@ private: bool render_scene_to_texture(float); bool render_refraction_to_texture(); bool render_reflection_to_texture(); - bool render_pass(const std::vector>>& RenderQueues, XMFLOAT4* diffuse, XMFLOAT4* position, XMFLOAT4* ambient, XMMATRIX view, XMMATRIX projection); + bool render_pass(XMFLOAT4* diffuse, XMFLOAT4* position, XMFLOAT4* ambient, XMMATRIX view, XMMATRIX projection); void update_skybox_position(); void culling_thread_function(); @@ -246,14 +240,10 @@ private : std::unique_ptr entity_manager_; object* selected_object_; - std::vector cubes_; - std::vector terrain_chunk_; - std::vector object_; float speed_ = 0.1f; // speed for the demo spinning object std::vector imported_object_; int object_id_ = 0; - std::vector>> render_queues_; std::vector skybox_; // ----------------------------------- // diff --git a/enginecustom/src/src/system/application_class.cpp b/enginecustom/src/src/system/application_class.cpp index 8b9d8ce..0a3e87a 100644 --- a/enginecustom/src/src/system/application_class.cpp +++ b/enginecustom/src/src/system/application_class.cpp @@ -28,10 +28,6 @@ application_class::application_class() : should_quit_(false) reflection_texture_ = nullptr; scene_texture_ = nullptr; physics_ = nullptr; - cubes_.clear(); - terrain_chunk_.clear(); - object_.clear(); - render_queues_.clear(); skybox_.clear(); lights_.clear(); sun_light_ = nullptr; @@ -86,11 +82,6 @@ bool application_class::initialize(int screenWidth, int screenHeight, HWND hwnd, bool result; HRESULT Hresult; - render_queues_.push_back(std::ref(skybox_)); // Skybox doit être rendu en premier pour être derrière touts les autres objets - render_queues_.push_back(std::ref(object_)); - render_queues_.push_back(std::ref(cubes_)); - render_queues_.push_back(std::ref(terrain_chunk_)); - // create entity manager entity_manager_ = std::make_unique(); @@ -191,14 +182,14 @@ bool application_class::initialize(int screenWidth, int screenHeight, HWND hwnd, result = display_plane_->Initialize(direct_3d_->get_device(), 1.0f, 1.0f); if (!result) - { + { Logger::Get().Log("Could not initialize the display plane object", __FILE__, __LINE__, Logger::LogLevel::Error); return false; } // Set the sprite info file we will be using. - // + // strcpy_s(spriteFilename, "sprite_data_01.txt"); // Create and initialize the sprite object. @@ -252,7 +243,7 @@ bool application_class::initialize(int screenWidth, int screenHeight, HWND hwnd, }; TextureContainer CubeTextures; - + for (const auto& textureFilename : textureFilenames) { ID3D11ShaderResourceView* texture = nullptr; @@ -267,7 +258,7 @@ bool application_class::initialize(int screenWidth, int screenHeight, HWND hwnd, // Create and initialize the model object. model_ = new model_class; - + result = model_->Initialize(direct_3d_->get_device(), direct_3d_->get_device_context(), modelFilename, CubeTextures); if (!result) @@ -370,7 +361,7 @@ bool application_class::initialize(int screenWidth, int screenHeight, HWND hwnd, }; TextureContainer BathTextures; - + textures.clear(); for (const auto& textureFilename : bathTextures) { @@ -521,7 +512,7 @@ bool application_class::initialize(int screenWidth, int screenHeight, HWND hwnd, } Logger::Get().Log("Application class initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize); - + return true; } @@ -529,7 +520,7 @@ bool application_class::initialize(int screenWidth, int screenHeight, HWND hwnd, void application_class::shutdown() { Logger::Get().Log("Shutting down application class", __FILE__, __LINE__, Logger::LogLevel::Shutdown); - + // Release the shader manager object. if (shader_manager_) { @@ -769,7 +760,7 @@ void application_class::shutdown() bool application_class::frame(input_class* Input) { stats_->reset_draw_call_count(); - + int mouseX, mouseY, currentMouseX, currentMouseY; bool result, leftMouseDown, rightMouseDown, buttonQ, buttonD, buttonZ, buttonS, buttonA, buttonE, scrollUp, scrollDown; float rotationY, rotationX, positionX, positionY, positionZ; @@ -834,12 +825,12 @@ bool application_class::frame(input_class* Input) buttonE = Input->IsEPressed(); position_->MoveCamera(buttonZ, buttonS, buttonQ, buttonD, buttonE, buttonA, scrollUp, scrollDown, rightMouseDown); position_->GetPosition(positionX, positionY, positionZ); - + XMFLOAT3 dir = sun_light_->GetDirection(); float pitch = asinf(-dir.y) * (180.0f / XM_PI); // en degrés float yaw = atan2f(dir.x, dir.z) * (180.0f / XM_PI); // en degrés float roll = 0.0f; - + if (Input->is_key_pressed(DIK_TAB)) { if (!tab_was_pressed_) { // Alterner la caméra active @@ -985,7 +976,7 @@ bool application_class::render_refraction_to_texture() // render the bath model using the refraction shader. bath_model_->Render(direct_3d_->get_device_context()); - result = shader_manager_->render_refraction_shader(direct_3d_->get_device_context(), bath_model_->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, + result = shader_manager_->render_refraction_shader(direct_3d_->get_device_context(), bath_model_->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, bath_model_->GetTexture(TextureType::Diffuse,0), lights_[0]->GetDirection(), ambientColor, diffuseColor, lightPosition, clipPlane); if (!result) { @@ -1002,7 +993,7 @@ bool application_class::render_refraction_to_texture() bool application_class::render_reflection_to_texture() { XMMATRIX reflectionViewMatrix; - + // Set the render target to be the reflection render to texture and clear it. reflection_texture_->SetRenderTarget(direct_3d_->get_device_context()); reflection_texture_->ClearRenderTarget(direct_3d_->get_device_context(), 0.0f, 0.0f, 0.0f, 1.0f); @@ -1012,7 +1003,7 @@ bool application_class::render_reflection_to_texture() // Get the camera reflection view matrix instead of the normal view matrix. camera_->get_reflection_view_matrix(reflectionViewMatrix); - + // Reset the render target back to the original back buffer and not the render to texture anymore. And reset the viewport back to the original. direct_3d_->set_back_buffer_render_target(); direct_3d_->reset_viewport(); @@ -1031,7 +1022,7 @@ bool application_class::render_scene_to_texture(float rotation) // Set the position of the camera for viewing the cube. camera_->render(); - + camera_->get_view_matrix(viewMatrix); render_texture_->GetProjectionMatrix(projectionMatrix); @@ -1099,14 +1090,14 @@ bool application_class::render(float rotation, float x, float y, float z, float scene_texture_->Shutdown(); scene_texture_->Initialize(direct_3d_->get_device(), (int)window_size_.x, (int)window_size_.y, screen_depth, screen_near, 1); } - + direct_3d_->set_back_buffer_render_target(); direct_3d_->reset_viewport(); } /// CPT ALED C'EST ICI QUE CA SE PASSE /// - + //scene_texture_->SetRenderTarget(direct_3d_->get_device_context()); //scene_texture_->ClearRenderTarget(direct_3d_->get_device_context(), 0.0f, 0.0f, 0.0f, 0.0f); @@ -1126,7 +1117,7 @@ bool application_class::render(float rotation, float x, float y, float z, float diffuseColor, lightPosition, ambientColor); update_skybox_position(); // Update the position of the skybox to match the camera position. - + direct_3d_->turn_z_buffer_on(); // Enable the Z buffer after rendering the skybox. // -------------------------------------------------------- // @@ -1138,7 +1129,7 @@ bool application_class::render(float rotation, float x, float y, float z, float // active_camera_->render(); // // Render the objects in the render queues. with depth only pass. // active_camera_->get_view_matrix(viewMatrix); - result = render_pass(render_queues_, diffuseColor, lightPosition, ambientColor, viewMatrix, projectionMatrix); + result = render_pass(diffuseColor, lightPosition, ambientColor, viewMatrix, projectionMatrix); if (!result) { Logger::Get().Log("Could not render the model using any shader", __FILE__, __LINE__, Logger::LogLevel::Error); @@ -1159,7 +1150,7 @@ bool application_class::render(float rotation, float x, float y, float z, float // } // stats_->update_geometric_stats(); - + // Update the render count text. result = update_render_count_string(get_render_count()); if (!result) @@ -1268,19 +1259,19 @@ bool application_class::render(float rotation, float x, float y, float z, float // turn on the Z buffer direct_3d_->turn_z_buffer_on(); - + // Get the light properties. for (i = 0; i < num_lights_; i++) { // Create the diffuse color array from the four light colors. diffuseColor[i] = lights_[i]->GetDiffuseColor(); - + // Create the light position array from the four light positions. lightPosition[i] = lights_[i]->GetPosition(); } // Réinitialiser la cible de rendu au back buffer. direct_3d_->set_back_buffer_render_target(); - + return true; } @@ -1301,10 +1292,11 @@ int application_class::get_screen_height() const void application_class::generate_terrain() { - Logger::Get().Log("Génération du terrain avec instanciation", __FILE__, __LINE__, Logger::LogLevel::Info); - - terrain_chunk_.clear(); + Logger::Get().Log("Génération du terrain avec ECS", __FILE__, __LINE__, Logger::LogLevel::Info); + // delete previous terrain if it exists + delete_terrain(); + // Dimensions du terrain float scaleX = 10.0f; float scaleY = 1.0f; @@ -1312,63 +1304,82 @@ void application_class::generate_terrain() int gridSizeX = 20; int gridSizeZ = 20; - // Créer un conteneur de textures_ partagé - TextureContainer textureContainer; - textureContainer.diffusePaths.push_back(L"assets/Texture/Bricks2K.png"); - textureContainer.normalPaths.push_back(L"assets/Texture/BricksNRM2K.png"); - textureContainer.specularPaths.push_back(L"assets/Texture/BricksGLOSS2K.png"); + // Vérifier si le modèle existe déjà dans le cache + std::string modelName = "assets/Model/OBJ/plane.obj"; + std::shared_ptr sharedModel; - // Précharger les textures_ une seule fois - model_class* sharedModel = new model_class(); - sharedModel->PreloadTextures(direct_3d_->get_device(), direct_3d_->get_device_context(), textureContainer); + auto it = g_model_cache.find(modelName); + if (it != g_model_cache.end()) { + // Utiliser le modèle existant du cache + Logger::Get().Log("Using cached model for terrain: " + modelName, __FILE__, __LINE__); + sharedModel = it->second; + } + else { + // Créer un conteneur de textures partagé + TextureContainer textureContainer; + textureContainer.diffusePaths.push_back(L"assets/Texture/Bricks2K.png"); + textureContainer.normalPaths.push_back(L"assets/Texture/BricksNRM2K.png"); + textureContainer.specularPaths.push_back(L"assets/Texture/BricksGLOSS2K.png"); - char modelFilename[128]; - strcpy_s(modelFilename, "assets/Model/OBJ/plane.obj"); + // Créer et initialiser le modèle si non trouvé + char modelFilename[128]; + strcpy_s(modelFilename, modelName.c_str()); - // Générer les tuiles de terrain - for (int i = 0; i < gridSizeX; i++) - { - for (int j = 0; j < gridSizeZ; j++) - { - // Créer un nouvel objet de terrain - object* terrain = new object(*this); + auto newModel = std::make_shared(); + newModel->PreloadTextures(direct_3d_->get_device(), direct_3d_->get_device_context(), textureContainer); - // Initialiser avec le modèle et les textures_ préchargées - if (!terrain->Initialize(direct_3d_->get_device(), direct_3d_->get_device_context(), modelFilename, textureContainer)) - { - Logger::Get().Log("Échec de l'initialisation d'une tuile de terrain", __FILE__, __LINE__, Logger::LogLevel::Error); - delete terrain; - continue; + if (!newModel->Initialize(direct_3d_->get_device(), direct_3d_->get_device_context(), modelFilename, textureContainer)) { + Logger::Get().Log("Impossible d'initialiser le modèle du terrain", __FILE__, __LINE__, Logger::LogLevel::Error); + return; + } + + // Ajouter le modèle au cache + g_model_cache[modelName] = newModel; + sharedModel = newModel; + Logger::Get().Log("Added terrain model to cache: " + modelName, __FILE__, __LINE__); + } + + // Vérifier si l'entity manager est disponible + if (entity_manager_) { + // Générer les tuiles de terrain avec le système ECS + for (int i = 0; i < gridSizeX; i++) { + for (int j = 0; j < gridSizeZ; j++) { + // Créer une entité + auto entity = entity_manager_->CreateEntity(); + + // Ajouter un composant d'identité + auto identity = entity->AddComponent(object_id_++); + identity->SetName("TerrainTile_" + std::to_string(i) + "_" + std::to_string(j)); + identity->SetType(ecs::ObjectType::Terrain); + + // Ajouter un composant de transformation + auto transform = entity->AddComponent(); + transform->SetPosition(XMVectorSet(i * scaleX, -12.0f, j * scaleZ, 0.0f)); + transform->SetScale(XMVectorSet(scaleX, scaleY, scaleZ, 0.0f)); + transform->UpdateWorldMatrix(); + + // Ajouter un composant de rendu + auto render = entity->AddComponent(); + render->InitializeWithModel(sharedModel); + + // Ajouter un composant de shader + auto shader = entity->AddComponent(); + shader->SetActiveShader(ecs::ShaderType::SUNLIGHT); } - - // Définir la position dans la grille - XMFLOAT3 position(i * scaleX, -12.0f, j * scaleZ); - XMFLOAT3 scale(scaleX, scaleY, scaleZ); - - terrain->SetPosition(XMLoadFloat3(&position)); - terrain->SetScale(XMLoadFloat3(&scale)); - - // Configurer les propriétés - terrain->SetName("TerrainTile_" + std::to_string(i) + "_" + std::to_string(j)); - terrain->SetType(ObjectType::Cube); - terrain->SetActiveShader(ShaderType::SUNLIGHT); - terrain->SetId(object_id_++); - - // Ajouter à la liste des chunks de terrain - terrain_chunk_.push_back(terrain); - - update_stats_after_modification(); } } - Logger::Get().Log("Terrain généré avec " + std::to_string(terrain_chunk_.size()) + " tuiles", __FILE__, __LINE__, Logger::LogLevel::Info); + update_stats_after_modification(); + + int totalTiles = gridSizeX * gridSizeZ; + Logger::Get().Log("Terrain généré avec " + std::to_string(totalTiles) + " tuiles", __FILE__, __LINE__, Logger::LogLevel::Info); } void application_class::add_kobject(std::wstring& filepath) { std::lock_guard lock(objects_mutex_); Logger::Get().Log("Adding object", __FILE__, __LINE__); - + char modelFilename[128]; TextureContainer KobjectsTextures; filesystem::path p(filepath); @@ -1378,14 +1389,14 @@ void application_class::add_kobject(std::wstring& filepath) (void)wcstombs_s(&convertedChars, modelFilename, sizeof(modelFilename), filepath.c_str(), _TRUNCATE); filesystem::current_path(w_folder_); - + // Liste des fichiers de texture std::vector kobjTexture = { L"assets/Texture/Bricks2K.png", L"assets/Texture/BricksNRM2K.png", L"assets/Texture/BricksGLOSS2K.png" }; - + // Configurer les chemins des textures dans le conteneur KobjectsTextures.diffusePaths.push_back(kobjTexture[0]); if (kobjTexture.size() > 1) KobjectsTextures.normalPaths.push_back(kobjTexture[1]); @@ -1394,53 +1405,53 @@ void application_class::add_kobject(std::wstring& filepath) // Vérifier si le modèle existe déjà dans le cache std::string modelKey = std::string(modelFilename); std::shared_ptr sharedModel; - + auto it = g_model_cache.find(modelKey); if (it != g_model_cache.end()) { // Utiliser le modèle existant du cache Logger::Get().Log("Using cached model for " + modelKey, __FILE__, __LINE__); sharedModel = it->second; - } + } else { // Créer un nouveau modèle sharedModel = std::make_shared(); - + // Précharger les textures sharedModel->PreloadTextures(direct_3d_->get_device(), direct_3d_->get_device_context(), KobjectsTextures); - + // Initialiser le modèle if (!sharedModel->Initialize(direct_3d_->get_device(), direct_3d_->get_device_context(), modelFilename, KobjectsTextures)) { Logger::Get().Log("Failed to initialize model for object: " + modelKey, __FILE__, __LINE__, Logger::LogLevel::Error); return; } - + // Ajouter le modèle au cache g_model_cache[modelKey] = sharedModel; Logger::Get().Log("Added model to cache: " + modelKey, __FILE__, __LINE__); } - + // Créer une nouvelle entité auto entity = entity_manager_->CreateEntity(); - + // Ajouter un composant d'identité auto identity = entity->AddComponent(object_id_++); identity->SetName(filename); identity->SetType(ecs::ObjectType::Unknown); - + // Ajouter un composant de transformation auto transform = entity->AddComponent(); transform->SetPosition(XMVectorSet(0.0f, 50.0f, 0.0f, 0.0f)); transform->SetScale(XMVectorSet(1.0f, 1.0f, 1.0f, 0.0f)); transform->UpdateWorldMatrix(); - + // Ajouter un composant de rendu avec le modèle partagé auto render = entity->AddComponent(); render->InitializeWithModel(sharedModel); - + // Ajouter un composant de shader auto shader = entity->AddComponent(); shader->SetActiveShader(ecs::ShaderType::LIGHTING); - + // Stocker le chemin du modèle auto modelPath = entity->AddComponent(); modelPath->SetPath(filepath); @@ -1455,37 +1466,80 @@ void application_class::add_cube() std::lock_guard lock(objects_mutex_); Logger::Get().Log("Adding cube", __FILE__, __LINE__); - char modelFilename[128]; - TextureContainer CubeTexture; - // Set the file name of the model. - strcpy_s(modelFilename, "assets/Model/TXT/cube.txt"); + std::string model_name = "assets/Model/TXT/cube.txt"; + std::shared_ptr sharedModel; - // Liste des fichiers de texture - std::vector cubeTexture = { - L"assets/Texture/Bricks2K.png" - }; + auto it = g_model_cache.find(model_name); + if (it != g_model_cache.end()) + { + Logger::Get().Log("Using cached model: " + model_name, __FILE__, __LINE__); + sharedModel = it->second; + } + else + { + TextureContainer cube_textures; + cube_textures.diffusePaths.push_back(L"assets/Texture/Bricks2K.png"); + cube_textures.normalPaths.push_back(L"assets/Texture/BricksNRM2K.png"); + cube_textures.specularPaths.push_back(L"assets/Texture/BricksGLOSS2K.png"); - static int cubeCount = 0; - float position = cubeCount * 2.0f; - object* newCube = new object(*this); - newCube->LoadTexturesFromPath(cubeTexture, CubeTexture, direct_3d_); // Load textures_ from the path - newCube->Initialize(direct_3d_->get_device(), direct_3d_->get_device_context(), modelFilename, CubeTexture); - newCube->SetTranslateMatrix(XMMatrixTranslation(position, 0.0f, 0.0f)); + char model_filename[128]; + strcpy_s(model_filename, model_name.c_str()); - cubes_.push_back(newCube); + auto newModel = std::make_shared(); + newModel->PreloadTextures(direct_3d_->get_device(), direct_3d_->get_device_context(), cube_textures); + if (!newModel->Initialize(direct_3d_->get_device(), direct_3d_->get_device_context(), model_filename, cube_textures)) { + Logger::Get().Log("Failed to initialize cube model", __FILE__, __LINE__, Logger::LogLevel::Error); + return; + } + + g_model_cache[model_name] = newModel; + sharedModel = newModel; + Logger::Get().Log("Added cube model to cache: " + model_name, __FILE__, __LINE__); + + + if (entity_manager_) + { + auto entity = entity_manager_->CreateEntity(); + + auto identity = entity->AddComponent(object_id_++); + identity->SetName("Cube"); + identity->SetType(ecs::ObjectType::Cube); + + auto transform = entity->AddComponent(); + transform->SetPosition(XMVectorSet(0.0f, 10.0f, 0.0f, 0.0f)); + transform->SetScale(XMVectorSet(1.0f, 1.0f, 1.0f, 0.0f)); + transform->UpdateWorldMatrix(); + + auto render = entity->AddComponent(); + render->InitializeWithModel(sharedModel); + + auto shader = entity->AddComponent(); + shader->SetActiveShader(ecs::ShaderType::TEXTURE); + } + } update_stats_after_modification(); + Logger::Get().Log("Cube added successfully", __FILE__, __LINE__, Logger::LogLevel::Info); } -void application_class::delete_kobject(int index) +void application_class::delete_entity_by_id(int entity_id) { std::lock_guard lock(objects_mutex_); - Logger::Get().Log("Deleting object", __FILE__, __LINE__); + Logger::Get().Log("Deleting entity with ID: " + std::to_string(entity_id), __FILE__, __LINE__, Logger::LogLevel::Info); - if (index < object_.size()) - { - object_.erase(object_.begin() + index); + if (entity_manager_) { + // Rechercher l'entité avec l'ID spécifié via le composant IdentityComponent + auto entities_with_identity = entity_manager_->GetEntitiesWithComponent(); + for (auto& entity : entities_with_identity) { + auto identity = entity->GetComponent(); + if (identity && identity->GetId() == entity_id) { + // Supprimer l'entité + entity_manager_->DestroyEntity(entity->GetID()); + Logger::Get().Log("Entity with ID " + std::to_string(entity_id) + " successfully deleted", __FILE__, __LINE__, Logger::LogLevel::Info); + break; + } + } } update_stats_after_modification(); @@ -1494,8 +1548,19 @@ void application_class::delete_kobject(int index) void application_class::delete_terrain() { std::lock_guard lock(objects_mutex_); - Logger::Get().Log("Deleting terrain", __FILE__, __LINE__); - terrain_chunk_.clear(); + Logger::Get().Log("Deleting terrain", __FILE__, __LINE__, Logger::LogLevel::Info); + + // Get all entities with the Terrain type + auto entities_with_terrain = entity_manager_->GetEntitiesWithComponent(); + for (auto& entity : entities_with_terrain) { + auto identity = entity->GetComponent(); + if (identity && identity->GetType() == ecs::ObjectType::Terrain) { + // Destroy the entity + entity_manager_->DestroyEntity(entity->GetID()); + Logger::Get().Log("Terrain entity with ID " + std::to_string(identity->GetId()) + " successfully deleted", __FILE__, __LINE__, Logger::LogLevel::Info); + } + } + update_stats_after_modification(); } @@ -1569,7 +1634,7 @@ bool application_class::update_fps() fps_->Frame(); stats_->update_display_stats(); // Update the stats display with the latest information from fps class. - + // Get the current fps. fps = fps_->GetFps(); @@ -1733,9 +1798,9 @@ void application_class::culling_thread_function() for (auto& queueRef : queues) { std::vector& queue = queueRef.get(); - + std::lock_guard lock(objects_mutex_); - + for (auto* object : queue) { if (!object) continue; @@ -1753,10 +1818,10 @@ void application_class::culling_thread_function() // Vérification du frustum bool visible = frustum_culling_.CheckCube(x, y, z, radius, get_frustum_tolerance()); object->SetVisible(visible); - + } } - + // Pause pour éviter de surcharger le CPU std::this_thread::sleep_for(std::chrono::milliseconds(16)); // ~60 Hz } @@ -1766,7 +1831,7 @@ void application_class::culling_thread_function() -bool application_class::render_pass(const std::vector>>& RenderQueues, XMFLOAT4* diffuse, XMFLOAT4* position, XMFLOAT4* ambient, XMMATRIX view, XMMATRIX projection) +bool application_class::render_pass(XMFLOAT4* diffuse, XMFLOAT4* position, XMFLOAT4* ambient, XMMATRIX view, XMMATRIX projection) { std::lock_guard lock(objects_mutex_); XMMATRIX scaleMatrix, rotateMatrix, translateMatrix; @@ -1822,37 +1887,37 @@ bool application_class::render_pass(const std::vectorturn_z_buffer_on(); // Réactiver le Z-buffer après le rendu de la skybox - + break; - + } // Rendu des entités du système ECS s'il est disponible if (entity_manager_) { // Créer le système de rendu pour les entités ecs::RenderSystem renderSystem(direct_3d_->get_device_context(), shader_manager_); - + // Données pour le système de rendu XMFLOAT4 sunlightDiffuse = sun_light_->GetDiffuseColor(); XMFLOAT4 sunlightAmbient = sun_light_->GetAmbientColor(); XMFLOAT3 sunlightDirection = sun_light_->GetDirection(); float sunlightIntensity = sun_light_->GetIntensity(); - + // Effectuer le rendu de toutes les entités int entitiesRendered = renderSystem.RenderAllEntities( entity_manager_.get(), - view, - projection, - diffuse, - position, + view, + projection, + diffuse, + position, ambient, camera_->get_position(), sunlightDiffuse, - sunlightAmbient, + sunlightAmbient, sunlightDirection, sunlightIntensity ); - + renderCount += entitiesRendered; } @@ -1894,7 +1959,7 @@ void application_class::update_skybox_position() } skybox_[0]->SetTranslateMatrix(XMMatrixTranslation(active_camera_->get_position().x, active_camera_->get_position().y, active_camera_->get_position().z)); - + } bool application_class::render_physics(bool key_left, bool key_right, bool key_up, bool key_down, float delta_time) { @@ -2018,7 +2083,7 @@ bool application_class::create_big_cube(int side_count) char model_file[128]; size_t convertedChars = 0; (void)wcstombs_s(&convertedChars, model_file, sizeof(model_file), std::wstring(modelName.begin(), modelName.end()).c_str(), _TRUNCATE); - + // Créer et initialiser le modèle si non trouvé auto newModel = std::make_shared(); TextureContainer textures; @@ -2034,57 +2099,36 @@ bool application_class::create_big_cube(int side_count) sharedModel = newModel; } - // Version ECS - Créer les entités pour le cube - if (entity_manager_) { - for (int x = 0; x < side_count; x++) { - for (int y = 0; y < side_count; y++) { - for (int z = 0; z < side_count; z++) { - // Créer une entité - auto entity = entity_manager_->CreateEntity(); - - // Ajouter un composant d'identité - auto identity = entity->AddComponent(object_id_++); - identity->SetName("CubePart_" + std::to_string(x) + "_" + std::to_string(y) + "_" + std::to_string(z)); - identity->SetType(ecs::ObjectType::Cube); - - // Ajouter un composant de transformation - auto transform = entity->AddComponent(); - transform->SetPosition(XMVectorSet(static_cast(x), static_cast(y), static_cast(z), 0.0f)); - transform->SetScale(XMVectorSet(1.0f, 1.0f, 1.0f, 0.0f)); - transform->UpdateWorldMatrix(); - - // Ajouter un composant de rendu - auto render = entity->AddComponent(); - render->InitializeWithModel(sharedModel); - - // Ajouter un composant de shader - auto shader = entity->AddComponent(); - shader->SetActiveShader(ecs::ShaderType::LIGHTING); - } - } - } - } else { - // Ancien système - Créer temporairement les cubes dans un vecteur local - std::vector tempCubes; - tempCubes.reserve(side_count * side_count * side_count); + for (int x = 0; x < side_count; x++) { + for (int y = 0; y < side_count; y++) { + for (int z = 0; z < side_count; z++) { + // Créer une entité + auto entity = entity_manager_->CreateEntity(); - // Générer side_count³ cubes - for (int x = 0; x < side_count; x++) { - for (int y = 0; y < side_count; y++) { - for (int z = 0; z < side_count; z++) { - object* cubePart = new object(*this); - cubePart->SetModel(sharedModel); - cubePart->SetTranslateMatrix(XMMatrixTranslation(static_cast(x), static_cast(y), static_cast(z))); - tempCubes.push_back(cubePart); - } - } - } - // Transférer les cubes du vecteur temporaire au vecteur membre - cubes_ = std::move(tempCubes); - } + // Ajouter un composant d'identité + auto identity = entity->AddComponent(object_id_++); + identity->SetName("CubePart_" + std::to_string(x) + "_" + std::to_string(y) + "_" + std::to_string(z)); + identity->SetType(ecs::ObjectType::Terrain); + + // Ajouter un composant de transformation + auto transform = entity->AddComponent(); + transform->SetPosition(XMVectorSet(static_cast(x), static_cast(y), static_cast(z), 0.0f)); + transform->SetScale(XMVectorSet(1.0f, 1.0f, 1.0f, 0.0f)); + transform->UpdateWorldMatrix(); + + // Ajouter un composant de rendu + auto render = entity->AddComponent(); + render->InitializeWithModel(sharedModel); + + // Ajouter un composant de shader + auto shader = entity->AddComponent(); + shader->SetActiveShader(ecs::ShaderType::LIGHTING); + } + } + } update_stats_after_modification(); - + return true; } @@ -2095,3 +2139,4 @@ void application_class::update_stats_after_modification() stats_ -> update_geometric_stats(); } } +