Patch Update - Fix Memory Leak From terrain genneration and use Instancing to generate big terrain - V10.1.3
This commit is contained in:
@@ -98,7 +98,8 @@ public:
|
||||
std::string ObjectTypeToString(ObjectType objectType);
|
||||
|
||||
void LaunchObject();
|
||||
void LoadTexturesFromPath(std::vector<std::wstring>& texturePaths, TextureContainer& texturesContainer, D3DClass* m_Direct3D);
|
||||
bool LoadTexturesFromPath(std::vector<std::wstring>& texturePaths, TextureContainer& texturesContainer,
|
||||
D3DClass* m_Direct3D);
|
||||
void SetAlpha(float alpha) { m_alpha = alpha; }
|
||||
float GetAlpha() const { return m_alpha; }
|
||||
void SetInitialStretch(float initialStretch) { m_initialStretch = initialStretch; }
|
||||
|
@@ -1192,50 +1192,74 @@ int ApplicationClass::GetScreenHeight() const
|
||||
|
||||
void ApplicationClass::GenerateTerrain()
|
||||
{
|
||||
Logger::Get().Log("Generating terrain", __FILE__, __LINE__);
|
||||
Logger::Get().Log("G<EFBFBD>n<EFBFBD>ration du terrain avec instanciation", __FILE__, __LINE__, Logger::LogLevel::Info);
|
||||
|
||||
char modelFilename[128];
|
||||
TextureContainer TerrainTextures;
|
||||
XMMATRIX scaleMatrix;
|
||||
float scaleX, scaleY, scaleZ;
|
||||
// Nettoyer les objets terrain existants pour <20>viter les fuites
|
||||
for (auto* chunk : m_terrainChunk)
|
||||
{
|
||||
if (chunk)
|
||||
{
|
||||
chunk->Shutdown();
|
||||
delete chunk;
|
||||
}
|
||||
}
|
||||
m_terrainChunk.clear();
|
||||
|
||||
scaleX = 10.0f;
|
||||
scaleY = 1.0f;
|
||||
scaleZ = 10.0f;
|
||||
// Dimensions du terrain
|
||||
float scaleX = 10.0f;
|
||||
float scaleY = 1.0f;
|
||||
float scaleZ = 10.0f;
|
||||
int gridSizeX = 20;
|
||||
int gridSizeZ = 20;
|
||||
|
||||
scaleMatrix = XMMatrixScaling(scaleX, scaleY, scaleZ);
|
||||
// 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");
|
||||
|
||||
// Set the file name of the model.
|
||||
strcpy_s(modelFilename, "assets/Model/OBJ/plane.obj");
|
||||
// Pr<50>charger les textures une seule fois
|
||||
ModelClass* sharedModel = new ModelClass();
|
||||
sharedModel->PreloadTextures(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), textureContainer);
|
||||
|
||||
// Liste des fichiers de texture
|
||||
std::vector<std::wstring> terrainTexture = {
|
||||
L"assets/Texture/Bricks2K.png",
|
||||
L"assets/Texture/BricksNRM2K.png",
|
||||
L"assets/Texture/BricksGLOSS2K.png"
|
||||
};
|
||||
char modelFilename[128];
|
||||
strcpy_s(modelFilename, "assets/Model/OBJ/plane.obj");
|
||||
|
||||
std::filesystem::path p(modelFilename);
|
||||
std::string filenameWithoutExtension = p.stem().string();
|
||||
|
||||
// for loop to generate terrain chunks for a 10x10 grid
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
for (int j = 0; j < 20; j++)
|
||||
{
|
||||
Object* newTerrain = new Object();
|
||||
newTerrain->LoadTexturesFromPath(terrainTexture, TerrainTextures, m_Direct3D); // Load textures from the path
|
||||
newTerrain->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, TerrainTextures);
|
||||
newTerrain->SetScaleMatrix(scaleMatrix);
|
||||
newTerrain->SetTranslateMatrix(XMMatrixTranslation(i / 2 * scaleX , -12.0f, j * scaleZ));
|
||||
newTerrain->SetName(filenameWithoutExtension);
|
||||
newTerrain->SetType(ObjectType::Cube);
|
||||
newTerrain->SetActiveShader(ShaderType::SUNLIGHT);
|
||||
m_terrainChunk.push_back(newTerrain);
|
||||
// G<>n<EFBFBD>rer les tuiles de terrain
|
||||
for (int i = 0; i < gridSizeX; i++)
|
||||
{
|
||||
for (int j = 0; j < gridSizeZ; j++)
|
||||
{
|
||||
// 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, -12.0f, 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::SUNLIGHT);
|
||||
terrain->SetId(m_ObjectId++);
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
void ApplicationClass::AddKobject(std::wstring& filepath)
|
||||
|
@@ -354,7 +354,8 @@ void Object::LaunchObject()
|
||||
OutputDebugStringA(buffer);
|
||||
}
|
||||
|
||||
void Object::LoadTexturesFromPath(std::vector<std::wstring>& texturePaths, TextureContainer& texturesContainer, D3DClass* m_Direct3D)
|
||||
bool Object::LoadTexturesFromPath(std::vector<std::wstring>& texturePaths, TextureContainer& texturesContainer,
|
||||
D3DClass* m_Direct3D)
|
||||
{
|
||||
|
||||
HRESULT result;
|
||||
@@ -380,10 +381,12 @@ void Object::LoadTexturesFromPath(std::vector<std::wstring>& texturePaths, Textu
|
||||
"\nError: " + std::to_string(result) +
|
||||
"\nDescription: " + str,
|
||||
__FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return ; // Assurez-vous de retourner false ou de g<>rer l'erreur de mani<6E>re appropri<72>e
|
||||
return false; // Assurez-vous de retourner false ou de g<>rer l'erreur de mani<6E>re appropri<72>e
|
||||
}
|
||||
texturesContainer.AssignTexture(texturesContainer, texture,texturePath , i);
|
||||
i++;
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
Reference in New Issue
Block a user