Patch Update - WIP Async Cube Generation - V10.2.1
This commit is contained in:
@@ -79,6 +79,7 @@ public:
|
||||
int GetVisibleTriangleCount() const;
|
||||
|
||||
void CreateBigCube(int sideCount);
|
||||
void ProcessTerrainGeneration();
|
||||
bool Initialize(int, int, HWND, bool IsVulkan);
|
||||
void Shutdown();
|
||||
bool Frame(InputClass*);
|
||||
@@ -195,6 +196,12 @@ public :
|
||||
|
||||
private :
|
||||
|
||||
|
||||
std::mutex m_TerrainMutex;
|
||||
std::vector<std::tuple<float, float, float, std::string, int>> m_TerrainGenerationData;
|
||||
bool m_TerrainGenerationReady;
|
||||
int m_NextTerrainObjectId;
|
||||
|
||||
// ------------------------------------- //
|
||||
// ------------- DIRECT3D -------------- //
|
||||
// ------------------------------------- //
|
||||
|
@@ -710,6 +710,7 @@ void ApplicationClass::Shutdown()
|
||||
bool ApplicationClass::Frame(InputClass* Input)
|
||||
{
|
||||
|
||||
ProcessTerrainGeneration();
|
||||
ResetDrawCallCount();
|
||||
|
||||
int mouseX, mouseY, currentMouseX, currentMouseY;
|
||||
@@ -2389,74 +2390,132 @@ void ApplicationClass::ResetDrawCallCount()
|
||||
|
||||
void ApplicationClass::CreateBigCube(int sideCount)
|
||||
{
|
||||
Logger::Get().Log("G<EFBFBD>n<EFBFBD>ration du terrain avec instanciation", __FILE__, __LINE__, Logger::LogLevel::Info);
|
||||
Logger::Get().Log("Lancement de la g<EFBFBD>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, sideCount]() {
|
||||
Logger::Get().Log("Thread de g<>n<EFBFBD>ration de terrain d<>marr<72>", __FILE__, __LINE__, Logger::LogLevel::Info);
|
||||
|
||||
// Stockage temporaire pour les nouveaux objets
|
||||
std::vector<Object*> newTerrainChunks;
|
||||
|
||||
// Nettoyer les objets terrain existants pour <20>viter les fuites
|
||||
// Dimensions du terrain
|
||||
float scaleX = 1.0f;
|
||||
float scaleY = 1.0f;
|
||||
float scaleZ = 1.0f;
|
||||
|
||||
// Pr<50>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 = m_ObjectId;
|
||||
|
||||
// G<>n<EFBFBD>rer les donn<6E>es des cubes
|
||||
for (int i = 0; i < sideCount; i++) {
|
||||
for (int j = 0; j < sideCount; j++) {
|
||||
for (int k = 0; k < sideCount; k++) {
|
||||
// V<>rifier si nous devons arr<72>ter le thread
|
||||
if (m_ShouldQuit) {
|
||||
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++));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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(m_TerrainMutex);
|
||||
m_TerrainGenerationData = std::move(cubeData);
|
||||
m_TerrainGenerationReady = true;
|
||||
m_NextTerrainObjectId = objectId;
|
||||
|
||||
Logger::Get().Log("Donn<EFBFBD>es de g<>n<EFBFBD>ration de terrain pr<70>par<61>es (" + std::to_string(m_TerrainGenerationData.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 ApplicationClass::ProcessTerrainGeneration()
|
||||
{
|
||||
// V<>rifier si des donn<6E>es de terrain sont pr<70>tes <20> <20>tre trait<69>es
|
||||
if (!m_TerrainGenerationReady) {
|
||||
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(m_TerrainMutex);
|
||||
cubeData = std::move(m_TerrainGenerationData);
|
||||
m_TerrainGenerationData.clear();
|
||||
m_TerrainGenerationReady = false;
|
||||
m_ObjectId = m_NextTerrainObjectId;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
// Nettoyer les objets terrain existants
|
||||
for (auto* chunk : m_terrainChunk)
|
||||
{
|
||||
if (chunk)
|
||||
{
|
||||
if (chunk) {
|
||||
chunk->Shutdown();
|
||||
delete chunk;
|
||||
}
|
||||
}
|
||||
m_terrainChunk.clear();
|
||||
|
||||
// Dimensions du terrain
|
||||
float scaleX = 1.0f;
|
||||
float scaleY = 1.0f;
|
||||
float scaleZ = 1.0f;
|
||||
|
||||
|
||||
// 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
|
||||
ModelClass* sharedModel = new ModelClass();
|
||||
sharedModel->PreloadTextures(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), textureContainer);
|
||||
|
||||
|
||||
char modelFilename[128];
|
||||
strcpy_s(modelFilename, "assets/Model/TXT/cube.txt");
|
||||
|
||||
// G<EFBFBD>n<EFBFBD>rer les tuiles de terrain
|
||||
for (int i = 0; i < sideCount; i++)
|
||||
|
||||
// Cr<EFBFBD>er les objets <20> partir des donn<6E>es
|
||||
for (const auto& [posX, posY, posZ, name, id] : cubeData)
|
||||
{
|
||||
for (int j = 0; j < sideCount; j++)
|
||||
// Cr<43>er un nouvel objet de terrain
|
||||
Object* terrain = new Object();
|
||||
|
||||
// Initialiser avec le mod<6F>le et les textures
|
||||
if (!terrain->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, textureContainer))
|
||||
{
|
||||
|
||||
for (int k = 0; k < sideCount; k++)
|
||||
{
|
||||
// Cr<43>er un nouvel objet de terrain
|
||||
Object* terrain = new Object();
|
||||
|
||||
// Initialiser avec le mod<6F>le et les textures pr<70>charg<72>es
|
||||
if (!terrain->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, textureContainer))
|
||||
{
|
||||
Logger::Get().Log("<EFBFBD>chec de l'initialisation d'une tuile de terrain", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
delete terrain;
|
||||
continue;
|
||||
}
|
||||
|
||||
// D<>finir la position dans la grille
|
||||
XMFLOAT3 position(i * scaleX, k * scaleY, j * scaleZ);
|
||||
XMFLOAT3 scale(scaleX, scaleY, scaleZ);
|
||||
|
||||
terrain->SetPosition(XMLoadFloat3(&position));
|
||||
terrain->SetScale(XMLoadFloat3(&scale));
|
||||
|
||||
// Configurer les propri<72>t<EFBFBD>s
|
||||
terrain->SetName("TerrainTile_" + std::to_string(i) + "_" + std::to_string(j));
|
||||
terrain->SetType(ObjectType::Cube);
|
||||
terrain->SetActiveShader(ShaderType::TEXTURE);
|
||||
terrain->SetId(m_ObjectId++);
|
||||
|
||||
// Ajouter <20> la liste des chunks de terrain
|
||||
m_terrainChunk.push_back(terrain);
|
||||
}
|
||||
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
|
||||
m_terrainChunk.push_back(terrain);
|
||||
}
|
||||
|
||||
Logger::Get().Log("Terrain g<>n<EFBFBD>r<EFBFBD> avec " + std::to_string(m_terrainChunk.size()) + " tuiles", __FILE__, __LINE__, Logger::LogLevel::Info);
|
||||
|
||||
delete sharedModel;
|
||||
|
||||
Logger::Get().Log("G<EFBFBD>n<EFBFBD>ration du terrain termin<69>e avec " + std::to_string(m_terrainChunk.size()) + " cubes", __FILE__, __LINE__, Logger::LogLevel::Info);
|
||||
}
|
@@ -18,24 +18,7 @@ ModelClass::~ModelClass()
|
||||
{
|
||||
}
|
||||
|
||||
// bool ModelClass::Initialize(ID3D11Device* device, ID3D11DeviceContext* deviceContext, char* modelFilename, std::vector<ID3D11ShaderResourceView*> textures) {
|
||||
// Logger::Get().Log("Initializing model class with preloaded textures (vector)", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
//
|
||||
// bool result = Initialize(device, deviceContext, modelFilename);
|
||||
// if (!result) {
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// // Ajouter toutes les textures au container comme textures diffuses
|
||||
// for (auto& tex : textures) {
|
||||
// m_Textures.diffuse.push_back(tex);
|
||||
// }
|
||||
//
|
||||
// return true;
|
||||
// }
|
||||
|
||||
bool ModelClass::Initialize(ID3D11Device* device, ID3D11DeviceContext* deviceContext, char* modelFilename, const TextureContainer& textures) {
|
||||
Logger::Get().Log("Initializing model class with preloaded texture container", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
bool result = Initialize(device, deviceContext, modelFilename);
|
||||
if (!result) {
|
||||
@@ -48,7 +31,6 @@ bool ModelClass::Initialize(ID3D11Device* device, ID3D11DeviceContext* deviceCon
|
||||
}
|
||||
|
||||
bool ModelClass::Initialize(ID3D11Device* device, ID3D11DeviceContext* deviceContext, char* modelFilename) {
|
||||
Logger::Get().Log("Initializing model class without textures", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
bool result;
|
||||
|
||||
@@ -68,9 +50,7 @@ bool ModelClass::Initialize(ID3D11Device* device, ID3D11DeviceContext* deviceCon
|
||||
Logger::Get().Log("Failed to initialize buffers", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
Logger::Get().Log("Model class initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -103,18 +83,12 @@ int ModelClass::GetIndexCount()
|
||||
return m_indexCount;
|
||||
}
|
||||
|
||||
// ID3D11ShaderResourceView* ModelClass::GetTexture(int index) const {
|
||||
// // Pour compatibilit<69>, utilise la texture diffuse par d<>faut
|
||||
// return m_Textures.GetTexture(TextureType::Diffuse, index);
|
||||
// }
|
||||
|
||||
ID3D11ShaderResourceView* ModelClass::GetTexture(TextureType type, int index) const {
|
||||
return m_Textures.GetTexture(type, index);
|
||||
}
|
||||
|
||||
bool ModelClass::InitializeBuffers(ID3D11Device* device)
|
||||
{
|
||||
Logger::Get().Log("Initializing buffers", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
VertexType* vertices;
|
||||
unsigned long* indices;
|
||||
@@ -189,16 +163,12 @@ bool ModelClass::InitializeBuffers(ID3D11Device* device)
|
||||
|
||||
delete[] indices;
|
||||
indices = 0;
|
||||
|
||||
Logger::Get().Log("Buffers initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void ModelClass::ShutdownBuffers()
|
||||
{
|
||||
Logger::Get().Log("Shutting down buffers", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
// Release the index buffer.
|
||||
if (m_indexBuffer)
|
||||
{
|
||||
@@ -212,10 +182,7 @@ void ModelClass::ShutdownBuffers()
|
||||
m_vertexBuffer->Release();
|
||||
m_vertexBuffer = 0;
|
||||
}
|
||||
|
||||
Logger::Get().Log("Buffers shut down", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -243,17 +210,13 @@ void ModelClass::RenderBuffers(ID3D11DeviceContext* deviceContext)
|
||||
|
||||
void ModelClass::ReleaseTextures()
|
||||
{
|
||||
Logger::Get().Log("Releasing textures", __FILE__, __LINE__);
|
||||
|
||||
// Utilise la m<>thode ReleaseAll de TextureContainer
|
||||
m_Textures.ReleaseAll();
|
||||
|
||||
Logger::Get().Log("Textures released", __FILE__, __LINE__);
|
||||
}
|
||||
|
||||
bool ModelClass::LoadModel(char* filename)
|
||||
{
|
||||
Logger::Get().Log("Loading model: " + std::string(filename), __FILE__, __LINE__);
|
||||
|
||||
std::string fileStr(filename);
|
||||
std::string extension = fileStr.substr(fileStr.find_last_of(".") + 1);
|
||||
@@ -277,8 +240,7 @@ bool ModelClass::LoadModel(char* filename)
|
||||
|
||||
bool ModelClass::LoadObjModel(char* filename)
|
||||
{
|
||||
Logger::Get().Log("Loading model", __FILE__, __LINE__);
|
||||
|
||||
|
||||
std::string line;
|
||||
std::ifstream fin(filename);
|
||||
|
||||
@@ -362,16 +324,12 @@ bool ModelClass::LoadObjModel(char* filename)
|
||||
|
||||
fin.close();
|
||||
|
||||
Logger::Get().Log("Model loaded", __FILE__, __LINE__);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool ModelClass::LoadTxtModel(char* filename)
|
||||
{
|
||||
Logger::Get().Log("Loading model", __FILE__, __LINE__);
|
||||
|
||||
ifstream fin;
|
||||
char input;
|
||||
int i;
|
||||
@@ -422,16 +380,12 @@ bool ModelClass::LoadTxtModel(char* filename)
|
||||
|
||||
// Close the model file.
|
||||
fin.close();
|
||||
|
||||
Logger::Get().Log("Model loaded "+ std::string(filename), __FILE__, __LINE__);
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ModelClass::CalculateModelVectors()
|
||||
{
|
||||
Logger::Get().Log("Calculating model vectors", __FILE__, __LINE__);
|
||||
|
||||
int faceCount, i, index;
|
||||
TempVertexType vertex1, vertex2, vertex3;
|
||||
VectorType tangent, binormal;
|
||||
@@ -493,10 +447,6 @@ void ModelClass::CalculateModelVectors()
|
||||
m_model[index - 3].by = binormal.y;
|
||||
m_model[index - 3].bz = binormal.z;
|
||||
}
|
||||
|
||||
Logger::Get().Log("Model vectors calculated", __FILE__, __LINE__);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void ModelClass::CalculateTangentBinormal(TempVertexType vertex1, TempVertexType vertex2, TempVertexType vertex3, VectorType& tangent, VectorType& binormal)
|
||||
@@ -551,23 +501,16 @@ void ModelClass::CalculateTangentBinormal(TempVertexType vertex1, TempVertexType
|
||||
binormal.x = binormal.x / length;
|
||||
binormal.y = binormal.y / length;
|
||||
binormal.z = binormal.z / length;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void ModelClass::ReleaseModel()
|
||||
{
|
||||
Logger::Get().Log("Releasing model", __FILE__, __LINE__);
|
||||
|
||||
if (m_model)
|
||||
{
|
||||
delete[] m_model;
|
||||
m_model = 0;
|
||||
}
|
||||
|
||||
Logger::Get().Log("Model released", __FILE__, __LINE__);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bool ModelClass::PreloadTextures(ID3D11Device* device, ID3D11DeviceContext* deviceContext, TextureContainer& textureContainer)
|
||||
|
Reference in New Issue
Block a user