Minor - bigcube generation + using shared model - V11.4.0

This commit is contained in:
2025-06-19 22:19:33 +02:00
parent f5331444cb
commit de16b97f8e
10 changed files with 219 additions and 185 deletions

View File

@@ -13,7 +13,7 @@ public:
void Initialize(d_3d_class* d3dClassRef); // Get all the required references
object* ConstructSkybox();
object* ConstructSkybox(application_class* app);
// Variables

View File

@@ -55,6 +55,8 @@ constexpr bool full_screen = false;
constexpr float screen_depth = 1000.0f;
constexpr float screen_near = 0.3f;
static std::map<std::string, std::shared_ptr<model_class>> g_model_cache;
struct input
{
bool key_left = false;
@@ -79,8 +81,7 @@ public:
render_texture_class* get_refraction_texture() const { return refraction_texture_; };
render_texture_class* get_reflection_texture() const { return reflection_texture_; };
void create_big_cube(int side_count);
void process_terrain_generation();
bool create_big_cube(int side_count);
virtual bool initialize(int, int, HWND, bool is_vulkan);
void shutdown();
virtual bool frame(input_class*);

View File

@@ -29,6 +29,7 @@ enum class ShaderType
class object
{
public:
object(application_class& app);
object();
~object();
@@ -137,6 +138,9 @@ public :
bool m_gravityEnabled = true;
private:
application_class& m_Application;
XMMATRIX m_scaleMatrix;
XMMATRIX m_rotateMatrix;
XMMATRIX m_translateMatrix;

View File

@@ -6,6 +6,7 @@
#include "object.h"
#include <intrin.h> // Pour __cpuid
#include <dxgi.h> // Pour DXGI
#include <mutex>
#pragma comment(lib, "dxgi.lib")
class stats
@@ -36,7 +37,6 @@ public:
std::string get_gpu_driver_version(ID3D11Device* device);
private:
std::thread update_geometric_thread_;
std::thread update_display_thread_;
fps_class* fps_;

View File

@@ -23,7 +23,7 @@ void Skybox::Initialize(d_3d_class* d3dClassRef)
m_d3dClassRef = d3dClassRef;
}
object* Skybox::ConstructSkybox()
object* Skybox::ConstructSkybox(application_class* app)
{
Logger::Get().Log("Construct skybox", __FILE__, __LINE__, Logger::LogLevel::Initialize);
@@ -51,7 +51,7 @@ object* Skybox::ConstructSkybox()
}
// Create the model object
m_Skybox = new object();
m_Skybox = new object(*app);
HRESULT result = m_Skybox->Initialize(m_d3dClassRef->get_device(), m_d3dClassRef->get_device_context(), modelFilename, SkyboxTextures);
if (!result)
{

View File

@@ -505,7 +505,7 @@ bool application_class::initialize(int screenWidth, int screenHeight, HWND hwnd,
//ConstructSkyboxWithPlanes();
Skybox* skybox = new Skybox;
skybox->Initialize(direct_3d_);
skybox_.push_back(skybox->ConstructSkybox());
skybox_.push_back(skybox->ConstructSkybox(this));
culling_active_ = true;
culling_thread_ = std::thread(&application_class::culling_thread_function, this);
@@ -765,8 +765,6 @@ void application_class::shutdown()
bool application_class::frame(input_class* Input)
{
process_terrain_generation();
stats_->reset_draw_call_count();
int mouseX, mouseY, currentMouseX, currentMouseY;
@@ -1133,10 +1131,10 @@ bool application_class::render(float rotation, float x, float y, float z, float
// -------------------------------------------------------- //
// set the active camera to the sun camera for rendering the shadow map.
active_camera_ = sun_camera_;
active_camera_->render();
// Render the objects in the render queues. with depth only pass.
active_camera_->get_view_matrix(viewMatrix);
// active_camera_ = sun_camera_;
// 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);
if (!result)
{
@@ -1145,18 +1143,19 @@ bool application_class::render(float rotation, float x, float y, float z, float
}
// Reset the active camera to the main camera.
active_camera_ = camera_;
active_camera_->render();
active_camera_->get_view_matrix(viewMatrix);
// render the objects in the render queues. with standard pass.
result = render_pass(render_queues_, diffuseColor, lightPosition, ambientColor, viewMatrix, projectionMatrix);
if (!result)
{
Logger::Get().Log("Could not render the model using any shader", __FILE__, __LINE__, Logger::LogLevel::Error);
return false;
}
// active_camera_ = camera_;
// active_camera_->render();
// active_camera_->get_view_matrix(viewMatrix);
//
// // render the objects in the render queues. with standard pass.
// result = render_pass(render_queues_, diffuseColor, lightPosition, ambientColor, viewMatrix, projectionMatrix);
// if (!result)
// {
// Logger::Get().Log("Could not render the model using any shader", __FILE__, __LINE__, Logger::LogLevel::Error);
// return false;
// }
stats_->update_geometric_stats();
// Update the render count text.
result = update_render_count_string(get_render_count());
@@ -1329,7 +1328,7 @@ void application_class::generate_terrain()
for (int j = 0; j < gridSizeZ; j++)
{
// Cr<43>er un nouvel objet de terrain
object* terrain = new object();
object* terrain = new object(*this);
// Initialiser avec le mod<6F>le et les textures_ pr<70>charg<72>es
if (!terrain->Initialize(direct_3d_->get_device(), direct_3d_->get_device_context(), modelFilename, textureContainer))
@@ -1362,6 +1361,7 @@ void application_class::generate_terrain()
void application_class::add_kobject(std::wstring& filepath)
{
std::lock_guard<std::mutex> lock(objects_mutex_);
Logger::Get().Log("Adding object", __FILE__, __LINE__);
char modelFilename[128];
@@ -1382,7 +1382,7 @@ void application_class::add_kobject(std::wstring& filepath)
L"assets/Texture/BricksGLOSS2K.png"
};
object* newObject = new object();
object* newObject = new object(*this);
newObject->LoadTexturesFromPath(kobjTexture,KobjectsTextures, direct_3d_); // Load textures_ from the path
newObject->Initialize(direct_3d_->get_device(), direct_3d_->get_device_context(), modelFilename, KobjectsTextures);
newObject->SetMass(1.0f);
@@ -1404,6 +1404,7 @@ void application_class::add_kobject(std::wstring& filepath)
void application_class::add_cube()
{
std::lock_guard<std::mutex> lock(objects_mutex_);
Logger::Get().Log("Adding cube", __FILE__, __LINE__);
char modelFilename[128];
@@ -1418,7 +1419,7 @@ void application_class::add_cube()
static int cubeCount = 0;
float position = cubeCount * 2.0f;
object* newCube = new object();
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));
@@ -1429,6 +1430,7 @@ void application_class::add_cube()
void application_class::delete_kobject(int index)
{
std::lock_guard<std::mutex> lock(objects_mutex_);
Logger::Get().Log("Deleting object", __FILE__, __LINE__);
if (index < object_.size())
@@ -1439,6 +1441,7 @@ void application_class::delete_kobject(int index)
void application_class::delete_terrain()
{
std::lock_guard<std::mutex> lock(objects_mutex_);
Logger::Get().Log("Deleting terrain", __FILE__, __LINE__);
terrain_chunk_.clear();
}
@@ -2151,126 +2154,56 @@ void application_class::physics_thread_function()
}
}
void application_class::create_big_cube(int side_count)
bool application_class::create_big_cube(int side_count)
{
Logger::Get().Log("Lancement de la g<>n<EFBFBD>ration du terrain dans un thread s<>par<61>", __FILE__, __LINE__, Logger::LogLevel::Info);
// Cr<43>er un thread qui ex<65>cutera la g<>n<EFBFBD>ration du terrain
std::thread generationThread([this, side_count]() {
Logger::Get().Log("Thread de g<>n<EFBFBD>ration de terrain d<>marr<72>", __FILE__, __LINE__, Logger::LogLevel::Info);
std::string modelName = "assets/Model/TXT/cube.txt";
std::shared_ptr<model_class> sharedModel;
// V<>rifier si le mod<6F>le existe d<>j<EFBFBD>
auto it = g_model_cache.find(modelName);
if (it != g_model_cache.end()) {
sharedModel = it->second;
} else {
// copy le string en char*
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);
// Stockage temporaire pour les nouveaux objets
std::vector<object*> newTerrainChunks;
// Cr<EFBFBD>er et initialiser le mod<6F>le si non trouv<75>
auto newModel = std::make_shared<model_class>();
TextureContainer textures;
textures.diffusePaths.push_back(L"assets/Texture/Bricks2K.png");
textures.normalPaths.push_back(L"assets/Texture/BricksNRM2K.png");
textures.specularPaths.push_back(L"assets/Texture/BricksGLOSS2K.png");
newModel->PreloadTextures(direct_3d_->get_device(), direct_3d_->get_device_context(), textures);
if (!newModel->Initialize(direct_3d_->get_device(), direct_3d_->get_device_context(),model_file, textures)) {
Logger::Get().Log("Impossible d'initialiser le mod<6F>le du gros cube", __FILE__, __LINE__, Logger::LogLevel::Error);
return false;
}
g_model_cache[modelName] = newModel;
sharedModel = newModel;
}
// Dimensions du terrain
float scaleX = 1.0f;
float scaleY = 1.0f;
float scaleZ = 1.0f;
// Cr<EFBFBD>er temporairement les cubes dans un vecteur local
std::vector<object*> tempCubes;
tempCubes.reserve(side_count * side_count * side_count);
// Pr<EFBFBD>parer les donn<6E>es pour la cr<63>ation des cubes
std::vector<std::tuple<float, float, float, std::string, int>> cubeData;
int objectId = object_id_;
// G<>n<EFBFBD>rer les donn<6E>es des cubes
for (int i = 0; i < side_count; i++) {
for (int j = 0; j < side_count; j++) {
for (int k = 0; k < side_count; k++) {
// V<>rifier si nous devons arr<72>ter le thread
if (should_quit_) {
Logger::Get().Log("G<EFBFBD>n<EFBFBD>ration de terrain annul<75>e", __FILE__, __LINE__, Logger::LogLevel::Info);
return;
}
// Position et nom du cube
float posX = i * scaleX;
float posY = k * scaleY;
float posZ = j * scaleZ;
std::string name = "TerrainTile_" + std::to_string(i) + "_" + std::to_string(j) + "_" + std::to_string(k);
// Stocker les donn<6E>es du cube
cubeData.push_back(std::make_tuple(posX, posY, posZ, name, objectId++));
}
// G<EFBFBD>n<EFBFBD>rer side_count<6E> 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<float>(x),static_cast<float>(y),static_cast<float>(z)));
tempCubes.push_back(cubePart);
}
}
// Synchroniser avec le thread principal pour cr<63>er les objets
// Cette partie doit <20>tre ex<65>cut<75>e dans le thread principal (via une file d'attente ou autre m<>canisme)
std::lock_guard<std::mutex> lock(terrain_mutex_);
terrain_generation_data_ = std::move(cubeData);
terrain_generation_ready_ = true;
next_terrain_object_id_ = objectId;
Logger::Get().Log("Donn<EFBFBD>es de g<>n<EFBFBD>ration de terrain pr<70>par<61>es (" + std::to_string(terrain_generation_data_.size()) + " cubes)", __FILE__, __LINE__, Logger::LogLevel::Info);
});
// D<>tacher le thread pour qu'il s'ex<65>cute en arri<72>re-plan
generationThread.detach();
}
void application_class::process_terrain_generation()
{
// V<>rifier si des donn<6E>es de terrain sont pr<70>tes <20> <20>tre trait<69>es
if (!terrain_generation_ready_) {
return;
}
// Acqu<71>rir les donn<6E>es g<>n<EFBFBD>r<EFBFBD>es
std::vector<std::tuple<float, float, float, std::string, int>> cubeData;
{
std::lock_guard<std::mutex> lock(terrain_mutex_);
cubeData = std::move(terrain_generation_data_);
terrain_generation_data_.clear();
terrain_generation_ready_ = false;
object_id_ = next_terrain_object_id_;
}
Logger::Get().Log("Cr<EFBFBD>ation des objets terrain <20> partir des donn<6E>es pr<70>par<61>es", __FILE__, __LINE__, Logger::LogLevel::Info);
// Transf<73>rer les cubes du vecteur temporaire au vecteur membre
cubes_ = std::move(tempCubes);
terrain_chunk_.clear();
// Cr<43>er un conteneur de textures_ partag<61>
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");
// Pr<50>charger les textures_ une seule fois
model_class* sharedModel = new model_class();
sharedModel->PreloadTextures(direct_3d_->get_device(), direct_3d_->get_device_context(), textureContainer);
char modelFilename[128];
strcpy_s(modelFilename, "assets/Model/TXT/cube.txt");
// Cr<43>er les objets <20> partir des donn<6E>es
for (const auto& [posX, posY, posZ, name, id] : cubeData)
{
// Cr<43>er un nouvel objet de terrain
object* terrain = new object();
// Initialiser avec le mod<6F>le et les textures_
if (!terrain->Initialize(direct_3d_->get_device(), direct_3d_->get_device_context(), modelFilename, textureContainer))
{
Logger::Get().Log("<EFBFBD>chec de l'initialisation d'une tuile de terrain", __FILE__, __LINE__, Logger::LogLevel::Error);
delete terrain;
continue;
}
// Configurer l'objet
XMFLOAT3 position(posX, posY, posZ);
XMFLOAT3 scale(1.0f, 1.0f, 1.0f);
terrain->SetPosition(XMLoadFloat3(&position));
terrain->SetScale(XMLoadFloat3(&scale));
terrain->SetName(name);
terrain->SetType(ObjectType::Cube);
terrain->SetActiveShader(ShaderType::TEXTURE);
terrain->SetId(id);
// Ajouter <20> la liste des chunks de terrain
terrain_chunk_.push_back(terrain);
}
delete sharedModel;
Logger::Get().Log("G<EFBFBD>n<EFBFBD>ration du terrain termin<69>e avec " + std::to_string(terrain_chunk_.size()) + " cubes", __FILE__, __LINE__, Logger::LogLevel::Info);
return true;
}

View File

@@ -2,9 +2,27 @@
#include <comdef.h>
#include "application_class.h"
#include "d_3d_class.h"
object::object()// initialize the reference here
object::object(application_class& app) : m_Application(app)// initialize the reference here
{
m_scaleMatrix = XMMatrixIdentity();
m_rotateMatrix = XMMatrixIdentity();
m_translateMatrix = XMMatrixIdentity();
m_srMatrix = XMMatrixIdentity();
m_worldMatrix = XMMatrixIdentity();
m_previousPosition = XMVectorZero();
m_velocity = XMVectorZero();
m_acceleration = XMVectorZero();
m_mass = NULL;
m_isGrounded = false;
m_id = NULL;
m_boundingRadius = 1.0f;
}
object::object()
: m_Application(*static_cast<application_class*>(nullptr)) // Remplacer suivant votre logique
{
m_scaleMatrix = XMMatrixIdentity();
m_rotateMatrix = XMMatrixIdentity();
@@ -31,8 +49,25 @@ bool object::Initialize(
TextureContainer& texturesContainer
)
{
m_model_ = std::make_shared<model_class>();
return m_model_->Initialize(device, deviceContext, modelFilename, texturesContainer);
std::string filename(modelFilename);
auto it = g_model_cache.find(filename);
if (it != g_model_cache.end())
{
m_model_ = it->second;
}
else
{
auto new_model = std::make_shared<model_class>();
if (!new_model->Initialize(device, deviceContext, modelFilename, texturesContainer))
{
return false;
}
g_model_cache[filename] = new_model;
m_model_ = new_model;
}
return true;
}
void object::SetScaleMatrix(XMMATRIX scaleMatrix)

View File

@@ -26,11 +26,6 @@ bool stats::initialize(application_class* app)
Logger::Get().Log("FPS object is null", __FILE__, __LINE__, Logger::LogLevel::Error);
return false;
}
// call update() in a separate thread
std::thread update_geometric_thread(&stats::update_geometric_stats, this);
update_geometric_thread_ = std::move(update_geometric_thread); // Move the thread to the member variable
update_geometric_thread_.detach(); // Detach the thread to run independently
Logger::Get().Log("Stats initialized successfully", __FILE__, __LINE__, Logger::LogLevel::Initialize);
@@ -39,8 +34,7 @@ bool stats::initialize(application_class* app)
void stats::update_geometric_stats()
{
while (true)
{
object_vec_ = app_->get_kobjects();
cubes_vec_ = app_->get_cubes();
terrain_chunk_vec_ = app_->get_terrain_cubes();
@@ -48,10 +42,6 @@ void stats::update_geometric_stats()
total_vertex_count_ = get_total_vertex_count();
total_triangle_count_ = get_total_triangle_count();
visible_triangle_count_ = get_visible_triangle_count();
// call update_stats() every second
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
void stats::update_display_stats()