Khaotic Engine Reborn
Loading...
Searching...
No Matches
model_class Class Reference

Classes

struct  Face
 
struct  ModelType
 
struct  Normal
 
struct  TempVertexType
 
struct  Texture
 
struct  VectorType
 
struct  Vertex
 
struct  VertexType
 

Public Member Functions

 model_class (const model_class &)=delete
 
model_classoperator= (const model_class &)=delete
 
bool Initialize (ID3D11Device *, ID3D11DeviceContext *, char *, const TextureContainer &)
 
bool Initialize (ID3D11Device *, ID3D11DeviceContext *, char *)
 
void Shutdown ()
 
void Render (ID3D11DeviceContext *)
 
int GetIndexCount ()
 
int GetVertexCount () const
 
ID3D11ShaderResourceView * GetTexture (TextureType type, int index) const
 
bool ChangeTexture (ID3D11Device *device, ID3D11DeviceContext *deviceContext, std::wstring filename, TextureType type, int index)
 
bool AddTexture (ID3D11Device *device, ID3D11DeviceContext *deviceContext, std::wstring filename, TextureType type)
 
bool AddTexture (ID3D11ShaderResourceView *texture, TextureType type)
 
void SetTextureContainer (TextureContainer &texturesContainer)
 
TextureContainer GetTextureContainer () const
 
bool PreloadTextures (ID3D11Device *device, ID3D11DeviceContext *deviceContext, TextureContainer &textureContainer)
 

Protected Attributes

int m_vertexCount
 
int m_indexCount
 
ID3D11Buffer * m_vertexBuffer
 
ID3D11Buffer * m_indexBuffer
 

Detailed Description

Definition at line 150 of file model_class.h.

Constructor & Destructor Documentation

◆ model_class()

model_class::model_class ( )

Definition at line 4 of file model_class.cpp.

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}

◆ ~model_class()

model_class::~model_class ( )

Definition at line 21 of file model_class.cpp.

22{
23
24 // Destructor
25 Shutdown();
26}

Member Function Documentation

◆ AddTexture() [1/2]

bool model_class::AddTexture ( ID3D11Device * device,
ID3D11DeviceContext * deviceContext,
std::wstring filename,
TextureType type )

Definition at line 646 of file model_class.cpp.

646 {
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}
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

◆ AddTexture() [2/2]

bool model_class::AddTexture ( ID3D11ShaderResourceView * texture,
TextureType type )

Definition at line 683 of file model_class.cpp.

683 {
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}

◆ ChangeTexture()

bool model_class::ChangeTexture ( ID3D11Device * device,
ID3D11DeviceContext * deviceContext,
std::wstring filename,
TextureType type,
int index )

Definition at line 585 of file model_class.cpp.

585 {
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}

◆ GetIndexCount()

int model_class::GetIndexCount ( )

Definition at line 88 of file model_class.cpp.

89{
90 return m_indexCount;
91}

◆ GetTexture()

ID3D11ShaderResourceView * model_class::GetTexture ( TextureType type,
int index ) const

Definition at line 93 of file model_class.cpp.

93 {
94 return m_Textures.GetTexture(type, index);
95}

◆ GetTextureContainer()

TextureContainer model_class::GetTextureContainer ( ) const
inline

Definition at line 232 of file model_class.h.

232{ return m_Textures; }

◆ GetVertexCount()

int model_class::GetVertexCount ( ) const
inline

Definition at line 217 of file model_class.h.

217{ return m_vertexCount; }

◆ Initialize() [1/2]

bool model_class::Initialize ( ID3D11Device * device,
ID3D11DeviceContext * deviceContext,
char * modelFilename )

Definition at line 40 of file model_class.cpp.

40 {
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}

◆ Initialize() [2/2]

bool model_class::Initialize ( ID3D11Device * device,
ID3D11DeviceContext * deviceContext,
char * modelFilename,
const TextureContainer & textures )

Definition at line 28 of file model_class.cpp.

28 {
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}

◆ PreloadTextures()

bool model_class::PreloadTextures ( ID3D11Device * device,
ID3D11DeviceContext * deviceContext,
TextureContainer & textureContainer )

Definition at line 526 of file model_class.cpp.

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}

◆ Render()

void model_class::Render ( ID3D11DeviceContext * deviceContext)

Definition at line 79 of file model_class.cpp.

80{
81 // Put the vertex and index buffers on the graphics pipeline to prepare them for drawing.
82 RenderBuffers(deviceContext);
83
84 return;
85}

◆ SetTextureContainer()

void model_class::SetTextureContainer ( TextureContainer & texturesContainer)
inline

Definition at line 231 of file model_class.h.

231{ m_Textures = texturesContainer; }

◆ Shutdown()

void model_class::Shutdown ( )

Definition at line 64 of file model_class.cpp.

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}

Member Data Documentation

◆ m_indexBuffer

ID3D11Buffer * model_class::m_indexBuffer
protected

Definition at line 238 of file model_class.h.

◆ m_indexCount

int model_class::m_indexCount
protected

Definition at line 237 of file model_class.h.

◆ m_vertexBuffer

ID3D11Buffer* model_class::m_vertexBuffer
protected

Definition at line 238 of file model_class.h.

◆ m_vertexCount

int model_class::m_vertexCount
protected

Definition at line 237 of file model_class.h.


The documentation for this class was generated from the following files: