Khaotic Engine Reborn
Loading...
Searching...
No Matches
model_class.cpp
1#include "model_class.h"
2
3
4model_class::model_class()
5{
6 m_vertexBuffer = 0;
7 m_indexBuffer = 0;
8 m_model = 0;
9 m_vertexCount = 0;
10 m_indexCount = 0;
11 m_Textures.diffuse.clear();
12 m_Textures.normal.clear();
13 m_Textures.specular.clear();
14 m_Textures.alpha.clear();
15 m_Textures.diffusePaths.clear();
16 m_Textures.normalPaths.clear();
17 m_Textures.specularPaths.clear();
18 m_Textures.alphaPaths.clear();
19}
20
21model_class::~model_class()
22{
23
24 // Destructor
25 Shutdown();
26}
27
28bool model_class::Initialize(ID3D11Device* device, ID3D11DeviceContext* deviceContext, char* modelFilename, const TextureContainer& textures) {
29
30 bool result = Initialize(device, deviceContext, modelFilename);
31 if (!result) {
32 return false;
33 }
34
35 m_Textures = textures; // Copie de la structure de textures
36
37 return true;
38}
39
40bool model_class::Initialize(ID3D11Device* device, ID3D11DeviceContext* deviceContext, char* modelFilename) {
41
42 bool result;
43
44 // Load in the model data.
45 result = LoadModel(modelFilename);
46 if (!result) {
47 Logger::Get().Log("Failed to load model data", __FILE__, __LINE__, Logger::LogLevel::Error);
48 return false;
49 }
50
51 // Calculate the tangent and binormal vectors for the model.
52 CalculateModelVectors();
53
54 // Initialize the vertex and index buffers.
55 result = InitializeBuffers(device);
56 if (!result) {
57 Logger::Get().Log("Failed to initialize buffers", __FILE__, __LINE__, Logger::LogLevel::Error);
58 return false;
59 }
60
61 return true;
62}
63
64void model_class::Shutdown()
65{
66 // Release the model textures.
67 ReleaseTextures();
68
69 // Shutdown the vertex and index buffers.
70 ShutdownBuffers();
71
72 // Release the model data.
73 ReleaseModel();
74
75 return;
76}
77
78
79void model_class::Render(ID3D11DeviceContext* deviceContext)
80{
81 // Put the vertex and index buffers on the graphics pipeline to prepare them for drawing.
82 RenderBuffers(deviceContext);
83
84 return;
85}
86
87
88int model_class::GetIndexCount()
89{
90 return m_indexCount;
91}
92
93ID3D11ShaderResourceView* model_class::GetTexture(TextureType type, int index) const {
94 return m_Textures.GetTexture(type, index);
95}
96
97bool model_class::InitializeBuffers(ID3D11Device* device)
98{
99
100 VertexType* vertices;
101 unsigned long* indices;
102 D3D11_BUFFER_DESC vertexBufferDesc, indexBufferDesc;
103 D3D11_SUBRESOURCE_DATA vertexData, indexData;
104 HRESULT result;
105 int i;
106
107 // Create the vertex array.
108 vertices = new VertexType[m_vertexCount];
109
110 // Create the index array.
111 indices = new unsigned long[m_indexCount];
112
113 // Load the vertex array and index array with data.
114 for (i = 0; i < m_vertexCount; i++)
115 {
116 vertices[i].position = XMFLOAT3(m_model[i].x, m_model[i].y, m_model[i].z);
117 vertices[i].texture = XMFLOAT2(m_model[i].tu, m_model[i].tv);
118 vertices[i].normal = XMFLOAT3(m_model[i].nx, m_model[i].ny, m_model[i].nz);
119 vertices[i].tangent = XMFLOAT3(m_model[i].tx, m_model[i].ty, m_model[i].tz);
120 vertices[i].binormal = XMFLOAT3(m_model[i].bx, m_model[i].by, m_model[i].bz);
121
122 indices[i] = i;
123 }
124
125 // Set up the description of the static vertex buffer.
126 vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
127 vertexBufferDesc.ByteWidth = sizeof(VertexType) * m_vertexCount;
128 vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
129 vertexBufferDesc.CPUAccessFlags = 0;
130 vertexBufferDesc.MiscFlags = 0;
131 vertexBufferDesc.StructureByteStride = 0;
132
133 // Give the subresource structure a pointer to the vertex data.
134 vertexData.pSysMem = vertices;
135 vertexData.SysMemPitch = 0;
136 vertexData.SysMemSlicePitch = 0;
137
138 // Now create the vertex buffer.
139 result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &m_vertexBuffer);
140 if (FAILED(result))
141 {
142 Logger::Get().Log("Failed to create vertex buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
143 return false;
144 }
145
146 // Set up the description of the static index buffer.
147 indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
148 indexBufferDesc.ByteWidth = sizeof(unsigned long) * m_indexCount;
149 indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
150 indexBufferDesc.CPUAccessFlags = 0;
151 indexBufferDesc.MiscFlags = 0;
152 indexBufferDesc.StructureByteStride = 0;
153
154 // Give the subresource structure a pointer to the index data.
155 indexData.pSysMem = indices;
156 indexData.SysMemPitch = 0;
157 indexData.SysMemSlicePitch = 0;
158
159 // Create the index buffer.
160 result = device->CreateBuffer(&indexBufferDesc, &indexData, &m_indexBuffer);
161 if (FAILED(result))
162 {
163 Logger::Get().Log("Failed to create index buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
164 return false;
165 }
166
167 // Release the arrays now that the vertex and index buffers have been created and loaded.
168 delete[] vertices;
169 vertices = 0;
170
171 delete[] indices;
172 indices = 0;
173 return true;
174}
175
176
177void model_class::ShutdownBuffers()
178{
179 if (m_indexBuffer) { m_indexBuffer->Release(); m_indexBuffer = nullptr; }
180 if (m_vertexBuffer) { m_vertexBuffer->Release(); m_vertexBuffer = nullptr; }
181}
182
183void model_class::RenderBuffers(ID3D11DeviceContext* deviceContext)
184{
185 unsigned int stride;
186 unsigned int offset;
187
188
189 // Set vertex buffer stride and offset.
190 stride = sizeof(VertexType);
191 offset = 0;
192
193 // Set the vertex buffer to active in the input assembler so it can be rendered.
194 deviceContext->IASetVertexBuffers(0, 1, &m_vertexBuffer, &stride, &offset);
195
196 // Set the index buffer to active in the input assembler so it can be rendered.
197 deviceContext->IASetIndexBuffer(m_indexBuffer, DXGI_FORMAT_R32_UINT, 0);
198
199 // Set the type of primitive that should be rendered from this vertex buffer, in this case triangles.
200 deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
201
202 return;
203}
204
205void model_class::ReleaseTextures()
206{
207
208 // Utilise la méthode ReleaseAll de TextureContainer
209 m_Textures.ReleaseAll();
210}
211
212bool model_class::LoadModel(char* filename)
213{
214
215 std::string fileStr(filename);
216 std::string extension = fileStr.substr(fileStr.find_last_of(".") + 1);
217
218 if (extension == "obj")
219 {
220 // Load .obj file
221 return LoadObjModel(filename);
222 }
223 else if (extension == "txt")
224 {
225 // Load .txt file
226 return LoadTxtModel(filename);
227 }
228 else
229 {
230 Logger::Get().Log("Unsupported file format", __FILE__, __LINE__, Logger::LogLevel::Error);
231 return false;
232 }
233}
234
235bool model_class::LoadObjModel(char* filename)
236{
237 // Lecture optimisée du fichier en mode binaire
238 std::ifstream fin(filename, std::ios::in | std::ios::binary);
239 if (!fin)
240 {
241 Logger::Get().Log("Échec d'ouverture du fichier modèle", __FILE__, __LINE__, Logger::LogLevel::Error);
242 return false;
243 }
244
245 // Lecture du fichier entier d'un coup (évite la lecture ligne par ligne)
246 fin.seekg(0, std::ios::end);
247 const size_t fileSize = fin.tellg();
248 fin.seekg(0, std::ios::beg);
249
250 std::string fileContent;
251 fileContent.resize(fileSize);
252 fin.read(&fileContent[0], fileSize);
253 fin.close();
254
255 // Pré-allocation des vecteurs (évite les réallocations)
256 const size_t estimatedVertices = fileSize / 150;
257 std::vector<XMFLOAT3> temp_positions;
258 std::vector<XMFLOAT2> temp_texcoords;
259 std::vector<XMFLOAT3> temp_normals;
260 std::vector<ModelType> temp_model;
261
262 temp_positions.reserve(estimatedVertices);
263 temp_texcoords.reserve(estimatedVertices);
264 temp_normals.reserve(estimatedVertices);
265 temp_model.reserve(estimatedVertices * 3);
266
267 // Analyse du contenu
268 std::istringstream iss(fileContent);
269 std::string line;
270
271 while (std::getline(iss, line))
272 {
273 if (line.empty() || line[0] == '#') continue;
274
275 // Analyse plus rapide basée sur le premier caractère
276 if (line[0] == 'v')
277 {
278 if (line[1] == ' ') // Position de sommet
279 {
280 XMFLOAT3 pos;
281 sscanf_s(line.c_str() + 2, "%f %f %f", &pos.x, &pos.y, &pos.z);
282 temp_positions.push_back(pos);
283 }
284 else if (line[1] == 't') // Coordonnées de texture
285 {
286 XMFLOAT2 tex;
287 sscanf_s(line.c_str() + 3, "%f %f", &tex.x, &tex.y);
288 temp_texcoords.push_back(tex);
289 }
290 else if (line[1] == 'n') // Normales
291 {
292 XMFLOAT3 norm;
293 sscanf_s(line.c_str() + 3, "%f %f %f", &norm.x, &norm.y, &norm.z);
294 temp_normals.push_back(norm);
295 }
296 }
297 else if (line[0] == 'f')
298 {
299 int posIndex[3], texIndex[3], normIndex[3];
300 const char* linePtr = line.c_str() + 2; // Sauter "f "
301
302 for (int i = 0; i < 3; i++)
303 {
304 // Analyse rapide du format v/vt/vn
305 sscanf_s(linePtr, "%d/%d/%d", &posIndex[i], &texIndex[i], &normIndex[i]);
306
307 // Avancer au prochain ensemble d'indices
308 while (*linePtr && *linePtr != ' ') linePtr++;
309 while (*linePtr == ' ') linePtr++;
310
311 if (posIndex[i] < 0) posIndex[i] += temp_positions.size() + 1;
312 if (texIndex[i] < 0) texIndex[i] += temp_texcoords.size() + 1;
313 if (normIndex[i] < 0) normIndex[i] += temp_normals.size() + 1;
314 }
315
316 for (int i = 0; i < 3; i++)
317 {
318 ModelType vertex{};
319 vertex.x = temp_positions[posIndex[i] - 1].x;
320 vertex.y = temp_positions[posIndex[i] - 1].y;
321 vertex.z = temp_positions[posIndex[i] - 1].z;
322 vertex.tu = temp_texcoords[texIndex[i] - 1].x;
323 vertex.tv = temp_texcoords[texIndex[i] - 1].y;
324 vertex.nx = temp_normals[normIndex[i] - 1].x;
325 vertex.ny = temp_normals[normIndex[i] - 1].y;
326 vertex.nz = temp_normals[normIndex[i] - 1].z;
327 temp_model.push_back(vertex);
328 }
329 }
330 }
331
332 // Allocation et copie efficace du modèle final
333 m_vertexCount = temp_model.size();
334 m_indexCount = temp_model.size();
335 m_model = new ModelType[m_vertexCount];
336 std::memcpy(m_model, temp_model.data(), m_vertexCount * sizeof(ModelType));
337
338 return true;
339}
340
341bool model_class::LoadTxtModel(char* filename)
342{
343 ifstream fin;
344 char input;
345 int i;
346
347
348 // Open the model file.
349 fin.open(filename);
350
351 // If it could not open the file then exit.
352 if (fin.fail())
353 {
354 Logger::Get().Log("Failed to open model file", __FILE__, __LINE__, Logger::LogLevel::Error);
355 return false;
356 }
357
358 // Read up to the value of vertex count.
359 fin.get(input);
360 while (input != ':')
361 {
362 fin.get(input);
363 }
364
365 // Read in the vertex count.
366 fin >> m_vertexCount;
367
368 // Set the number of indices to be the same as the vertex count.
369 m_indexCount = m_vertexCount;
370
371 // Create the model using the vertex count that was read in.
372 m_model = new ModelType[m_vertexCount];
373
374 // Read up to the beginning of the data.
375 fin.get(input);
376 while (input != ':')
377 {
378 fin.get(input);
379 }
380 fin.get(input);
381 fin.get(input);
382
383 // Read in the vertex data.
384 for (i = 0; i < m_vertexCount; i++)
385 {
386 fin >> m_model[i].x >> m_model[i].y >> m_model[i].z;
387 fin >> m_model[i].tu >> m_model[i].tv;
388 fin >> m_model[i].nx >> m_model[i].ny >> m_model[i].nz;
389 }
390
391 // Close the model file.
392 fin.close();
393
394 return true;
395}
396
397void model_class::CalculateModelVectors()
398{
399 int faceCount, i, index;
400 TempVertexType vertex1, vertex2, vertex3;
401 VectorType tangent, binormal;
402
403
404 // Calculate the number of faces in the model.
405 faceCount = m_vertexCount / 3;
406
407 // Initialize the index to the model data.
408 index = 0;
409
410 // Go through all the faces and calculate the the tangent and binormal vectors.
411 for (i = 0; i < faceCount; i++)
412 {
413 // Get the three vertices for this face from the model.
414 vertex1.x = m_model[index].x;
415 vertex1.y = m_model[index].y;
416 vertex1.z = m_model[index].z;
417 vertex1.tu = m_model[index].tu;
418 vertex1.tv = m_model[index].tv;
419 index++;
420
421 vertex2.x = m_model[index].x;
422 vertex2.y = m_model[index].y;
423 vertex2.z = m_model[index].z;
424 vertex2.tu = m_model[index].tu;
425 vertex2.tv = m_model[index].tv;
426 index++;
427
428 vertex3.x = m_model[index].x;
429 vertex3.y = m_model[index].y;
430 vertex3.z = m_model[index].z;
431 vertex3.tu = m_model[index].tu;
432 vertex3.tv = m_model[index].tv;
433 index++;
434
435 // Calculate the tangent and binormal of that face.
436 CalculateTangentBinormal(vertex1, vertex2, vertex3, tangent, binormal);
437
438 // Store the tangent and binormal for this face back in the model structure.
439 m_model[index - 1].tx = tangent.x;
440 m_model[index - 1].ty = tangent.y;
441 m_model[index - 1].tz = tangent.z;
442 m_model[index - 1].bx = binormal.x;
443 m_model[index - 1].by = binormal.y;
444 m_model[index - 1].bz = binormal.z;
445
446 m_model[index - 2].tx = tangent.x;
447 m_model[index - 2].ty = tangent.y;
448 m_model[index - 2].tz = tangent.z;
449 m_model[index - 2].bx = binormal.x;
450 m_model[index - 2].by = binormal.y;
451 m_model[index - 2].bz = binormal.z;
452
453 m_model[index - 3].tx = tangent.x;
454 m_model[index - 3].ty = tangent.y;
455 m_model[index - 3].tz = tangent.z;
456 m_model[index - 3].bx = binormal.x;
457 m_model[index - 3].by = binormal.y;
458 m_model[index - 3].bz = binormal.z;
459 }
460}
461
462void model_class::CalculateTangentBinormal(TempVertexType vertex1, TempVertexType vertex2, TempVertexType vertex3, VectorType& tangent, VectorType& binormal)
463{
464
465 float vector1[3], vector2[3];
466 float tuVector[2], tvVector[2];
467 float den;
468 float length;
469
470
471 // Calculate the two vectors for this face.
472 vector1[0] = vertex2.x - vertex1.x;
473 vector1[1] = vertex2.y - vertex1.y;
474 vector1[2] = vertex2.z - vertex1.z;
475
476 vector2[0] = vertex3.x - vertex1.x;
477 vector2[1] = vertex3.y - vertex1.y;
478 vector2[2] = vertex3.z - vertex1.z;
479
480 // Calculate the tu and tv texture space vectors.
481 tuVector[0] = vertex2.tu - vertex1.tu;
482 tvVector[0] = vertex2.tv - vertex1.tv;
483
484 tuVector[1] = vertex3.tu - vertex1.tu;
485 tvVector[1] = vertex3.tv - vertex1.tv;
486
487 // Calculate the denominator of the tangent/binormal equation.
488 den = 1.0f / (tuVector[0] * tvVector[1] - tuVector[1] * tvVector[0]);
489
490 // Calculate the cross products and multiply by the coefficient to get the tangent and binormal.
491 tangent.x = (tvVector[1] * vector1[0] - tvVector[0] * vector2[0]) * den;
492 tangent.y = (tvVector[1] * vector1[1] - tvVector[0] * vector2[1]) * den;
493 tangent.z = (tvVector[1] * vector1[2] - tvVector[0] * vector2[2]) * den;
494
495 binormal.x = (tuVector[0] * vector2[0] - tuVector[1] * vector1[0]) * den;
496 binormal.y = (tuVector[0] * vector2[1] - tuVector[1] * vector1[1]) * den;
497 binormal.z = (tuVector[0] * vector2[2] - tuVector[1] * vector1[2]) * den;
498
499 // Calculate the length of this normal.
500 length = sqrt((tangent.x * tangent.x) + (tangent.y * tangent.y) + (tangent.z * tangent.z));
501
502 // Normalize the normal and then store it
503 tangent.x = tangent.x / length;
504 tangent.y = tangent.y / length;
505 tangent.z = tangent.z / length;
506
507 // Calculate the length of this normal.
508 length = sqrt((binormal.x * binormal.x) + (binormal.y * binormal.y) + (binormal.z * binormal.z));
509
510 // Normalize the normal and then store it
511 binormal.x = binormal.x / length;
512 binormal.y = binormal.y / length;
513 binormal.z = binormal.z / length;
514}
515
516void model_class::ReleaseModel()
517{
518
519 if (m_model)
520 {
521 delete[] m_model;
522 m_model = 0;
523 }
524}
525
526bool model_class::PreloadTextures(ID3D11Device* device, ID3D11DeviceContext* deviceContext, TextureContainer& textureContainer)
527{
528 HRESULT hResult;
529
530 // Charger les textures diffuses
531 for (const auto& texturePath : textureContainer.diffusePaths)
532 {
533 ID3D11ShaderResourceView* texture = nullptr;
534 hResult = DirectX::CreateWICTextureFromFile(device, deviceContext, texturePath.c_str(), nullptr, &texture);
535 if (FAILED(hResult))
536 {
537 Logger::Get().Log("Échec du chargement de la texture diffuse: " + std::string(texturePath.begin(), texturePath.end()), __FILE__, __LINE__, Logger::LogLevel::Error);
538 return false;
539 }
540 textureContainer.diffuse.push_back(texture);
541 }
542
543 // Charger les textures normales
544 for (const auto& texturePath : textureContainer.normalPaths)
545 {
546 ID3D11ShaderResourceView* texture = nullptr;
547 hResult = DirectX::CreateWICTextureFromFile(device, deviceContext, texturePath.c_str(), nullptr, &texture);
548 if (FAILED(hResult))
549 {
550 Logger::Get().Log("Échec du chargement de la texture normale: " + std::string(texturePath.begin(), texturePath.end()), __FILE__, __LINE__, Logger::LogLevel::Error);
551 return false;
552 }
553 textureContainer.normal.push_back(texture);
554 }
555
556 // Charger les textures spéculaires
557 for (const auto& texturePath : textureContainer.specularPaths)
558 {
559 ID3D11ShaderResourceView* texture = nullptr;
560 hResult = DirectX::CreateWICTextureFromFile(device, deviceContext, texturePath.c_str(), nullptr, &texture);
561 if (FAILED(hResult))
562 {
563 Logger::Get().Log("Échec du chargement de la texture spéculaire: " + std::string(texturePath.begin(), texturePath.end()), __FILE__, __LINE__, Logger::LogLevel::Error);
564 return false;
565 }
566 textureContainer.specular.push_back(texture);
567 }
568
569 // Charger les textures alpha
570 for (const auto& texturePath : textureContainer.alphaPaths)
571 {
572 ID3D11ShaderResourceView* texture = nullptr;
573 hResult = DirectX::CreateWICTextureFromFile(device, deviceContext, texturePath.c_str(), nullptr, &texture);
574 if (FAILED(hResult))
575 {
576 Logger::Get().Log("Échec du chargement de la texture alpha: " + std::string(texturePath.begin(), texturePath.end()), __FILE__, __LINE__, Logger::LogLevel::Error);
577 return false;
578 }
579 textureContainer.alpha.push_back(texture);
580 }
581
582 return true;
583}
584
585bool model_class::ChangeTexture(ID3D11Device* device, ID3D11DeviceContext* deviceContext, std::wstring filename, TextureType type, int index) {
586 Logger::Get().Log("Changing texture", __FILE__, __LINE__, Logger::LogLevel::Initialize);
587
588 HRESULT result;
589 ID3D11ShaderResourceView* newTexture = nullptr;
590
591 // Charger la nouvelle texture
592 result = DirectX::CreateWICTextureFromFile(device, deviceContext, filename.c_str(), nullptr, &newTexture);
593 if (FAILED(result)) {
594 Logger::Get().Log("Failed to load texture: " + std::string(filename.begin(), filename.end()), __FILE__, __LINE__, Logger::LogLevel::Error);
595 return false;
596 }
597
598 // Récupérer le vecteur correspondant au type de texture
599 auto& textureVector = m_Textures.Get(type);
600
601 // Si l'index est hors limites, redimensionner le vecteur
602 if (index >= textureVector.size()) {
603 textureVector.resize(index + 1, nullptr);
604 }
605
606 // Libérer l'ancienne texture si elle existe
607 if (textureVector[index]) {
608 textureVector[index]->Release();
609 }
610
611 // Assigner la nouvelle texture
612 textureVector[index] = newTexture;
613
614 // Mettre à jour le chemin dans le conteneur approprié selon le type
615 switch (type) {
616 case TextureType::Diffuse:
617 if (index >= m_Textures.diffusePaths.size()) {
618 m_Textures.diffusePaths.resize(index + 1, L"");
619 }
620 m_Textures.diffusePaths[index] = filename;
621 break;
622 case TextureType::Normal:
623 if (index >= m_Textures.normalPaths.size()) {
624 m_Textures.normalPaths.resize(index + 1, L"");
625 }
626 m_Textures.normalPaths[index] = filename;
627 break;
628 case TextureType::Specular:
629 if (index >= m_Textures.specularPaths.size()) {
630 m_Textures.specularPaths.resize(index + 1, L"");
631 }
632 m_Textures.specularPaths[index] = filename;
633 break;
634 case TextureType::Alpha:
635 if (index >= m_Textures.alphaPaths.size()) {
636 m_Textures.alphaPaths.resize(index + 1, L"");
637 }
638 m_Textures.alphaPaths[index] = filename;
639 break;
640 }
641
642 Logger::Get().Log("Texture changed successfully", __FILE__, __LINE__, Logger::LogLevel::Initialize);
643 return true;
644}
645
646bool model_class::AddTexture(ID3D11Device* device, ID3D11DeviceContext* deviceContext, std::wstring filename, TextureType type) {
647 Logger::Get().Log("Adding texture", __FILE__, __LINE__, Logger::LogLevel::Initialize);
648
649 HRESULT result;
650 ID3D11ShaderResourceView* newTexture = nullptr;
651
652 // Charger la nouvelle texture
653 result = DirectX::CreateWICTextureFromFile(device, deviceContext, filename.c_str(), nullptr, &newTexture);
654 if (FAILED(result)) {
655 Logger::Get().Log("Failed to load texture: " + std::string(filename.begin(), filename.end()), __FILE__, __LINE__, Logger::LogLevel::Error);
656 return false;
657 }
658
659 // Ajouter la texture au vecteur approprié selon le type
660 auto& textureVector = m_Textures.Get(type);
661 textureVector.push_back(newTexture);
662
663 // Ajouter le chemin de la texture au vecteur approprié selon le type
664 switch (type) {
665 case TextureType::Diffuse:
666 m_Textures.diffusePaths.push_back(filename);
667 break;
668 case TextureType::Normal:
669 m_Textures.normalPaths.push_back(filename);
670 break;
671 case TextureType::Specular:
672 m_Textures.specularPaths.push_back(filename);
673 break;
674 case TextureType::Alpha:
675 m_Textures.alphaPaths.push_back(filename);
676 break;
677 }
678
679 Logger::Get().Log("Texture added successfully", __FILE__, __LINE__, Logger::LogLevel::Initialize);
680 return true;
681}
682
683bool model_class::AddTexture(ID3D11ShaderResourceView* texture, TextureType type) {
684 if (!texture) {
685 Logger::Get().Log("Cannot add null texture", __FILE__, __LINE__, Logger::LogLevel::Error);
686 return false;
687 }
688
689 // Ajouter la texture au vecteur approprié
690 auto& textureVector = m_Textures.Get(type);
691 textureVector.push_back(texture);
692
693 // Ajouter un chemin vide ou générique pour maintenir la synchronisation
694 switch (type) {
695 case TextureType::Diffuse:
696 m_Textures.diffusePaths.push_back(L"[texture préchargée]");
697 break;
698 case TextureType::Normal:
699 m_Textures.normalPaths.push_back(L"[texture préchargée]");
700 break;
701 case TextureType::Specular:
702 m_Textures.specularPaths.push_back(L"[texture préchargée]");
703 break;
704 case TextureType::Alpha:
705 m_Textures.alphaPaths.push_back(L"[texture préchargée]");
706 break;
707 }
708 return true;
709}
710
static Logger & Get()
Definition Logger.h:20
void Log(const std::string &message, const std::string &fileName, int lineNumber, LogLevel level=LogLevel::Info)
Definition Logger.h:158