#ifndef _MODELCLASS_H_ #define _MODELCLASS_H_ ////////////// // INCLUDES // ////////////// #include "Logger.h" #include #include #include #include #include #include #include using namespace DirectX; using namespace std; /////////////////////// // MY CLASS INCLUDES // /////////////////////// #include "textureclass.h" enum class TextureType { Diffuse, Normal, Specular, Alpha }; struct TextureContainer { // Textures std::vector diffuse; std::vector normal; std::vector specular; std::vector alpha; // Textures Paths std::vector diffusePaths; std::vector normalPaths; std::vector specularPaths; std::vector alphaPaths; // Get the vector of textures based on the type std::vector& Get(TextureType type) const { switch (type) { case TextureType::Diffuse: return const_cast&>(diffuse); case TextureType::Normal: return const_cast&>(normal); case TextureType::Specular: return const_cast&>(specular); case TextureType::Alpha: return const_cast&>(alpha); default: return const_cast&>(diffuse); } } // Get the vector of textures paths based on the type std::vector GetPaths(TextureType type) const { switch (type) { case TextureType::Diffuse: return std::vector(diffusePaths); case TextureType::Normal: return std::vector(normalPaths); case TextureType::Specular: return std::vector(specularPaths); case TextureType::Alpha: return std::vector(alphaPaths); default: return std::vector(diffusePaths); } } // Get The texture based on the type and index ID3D11ShaderResourceView* GetTexture(TextureType type, int index) const { auto& vec = Get(type); if (index >= 0 && index < vec.size()) return vec[index]; return nullptr; } // Get The texture path based on the type and index std::wstring GetTexturePath(TextureType type, int index) const { std::vector path = GetPaths(type); if (index >= 0 && index < path.size()) return path[index]; return L""; } // Release all textures and textures paths void ReleaseAll() { ReleaseVector(diffuse); ReleaseVector(normal); ReleaseVector(specular); ReleaseVector(alpha); ReleaseVector(diffusePaths); ReleaseVector(normalPaths); ReleaseVector(specularPaths); ReleaseVector(alphaPaths); } // Assign a texture and its path to the appropriate vector based on the index void AssignTexture(TextureContainer& textContainer, ID3D11ShaderResourceView* texture , const std::wstring paths, int index) { switch (index) { case 0: textContainer.diffuse.push_back(texture); textContainer.diffusePaths.push_back(paths); break; case 1: textContainer.normal.push_back(texture); textContainer.normalPaths.push_back(paths); break; case 2: textContainer.specular.push_back(texture); textContainer.specularPaths.push_back(paths); break; case 3: textContainer.alpha.push_back(texture); textContainer.alphaPaths.push_back(paths); break; default: textContainer.diffuse.push_back(texture); textContainer.diffusePaths.push_back(paths); break; } } private: void ReleaseVector(std::vector& vec) { for (auto& tex : vec) { if (tex) { tex->Release(); tex = nullptr; } } vec.clear(); } void ReleaseVector(std::vector& vec) { vec.clear(); } }; //////////////////////////////////////////////////////////////////////////////// // Class name: ModelClass //////////////////////////////////////////////////////////////////////////////// class ModelClass { protected: struct VertexType { XMFLOAT3 position; XMFLOAT2 texture; XMFLOAT3 normal; XMFLOAT3 tangent; XMFLOAT3 binormal; }; struct ModelType { float x, y, z; float tu, tv; float nx, ny, nz; float tx, ty, tz; float bx, by, bz; }; struct Vertex { float x, y, z; }; struct Texture { float u, v; }; struct Normal { float nx, ny, nz; }; struct TempVertexType { float x, y, z; float tu, tv; float nx, ny, nz; }; struct VectorType { float x, y, z; }; struct Face { int v1, v2, v3; int t1, t2, t3; int n1, n2, n3; }; public: ModelClass(); ModelClass(const ModelClass&) = delete; ModelClass& operator=(const ModelClass&) = delete; ~ModelClass(); //bool Initialize(ID3D11Device*, ID3D11DeviceContext*, char*, std::vector); // Nouvelle surcharge avec TextureContainer bool Initialize(ID3D11Device*, ID3D11DeviceContext*, char*, const TextureContainer&); // Nouvelle méthode - initialisation sans textures bool Initialize(ID3D11Device*, ID3D11DeviceContext*, char*); void Shutdown(); void Render(ID3D11DeviceContext*); int GetIndexCount(); int GetVertexCount() const { return m_vertexCount; } // TEXTURE // //ID3D11ShaderResourceView* GetTexture(int index) const; ID3D11ShaderResourceView* GetTexture(TextureType type, int index) const; //bool ChangeTexture(ID3D11Device* device, ID3D11DeviceContext* deviceContext, std::wstring filename, int index); bool ChangeTexture(ID3D11Device* device, ID3D11DeviceContext* deviceContext,std::wstring filename, TextureType type, int index); // Méthodes pour ajouter une nouvelle texture bool AddTexture(ID3D11Device* device, ID3D11DeviceContext* deviceContext,std::wstring filename, TextureType type); bool AddTexture(ID3D11ShaderResourceView* texture, TextureType type); void SetTextureContainer (TextureContainer& texturesContainer) { m_Textures = texturesContainer; } TextureContainer GetTextureContainer() const { return m_Textures; } bool PreloadTextures(ID3D11Device* device, ID3D11DeviceContext* deviceContext, TextureContainer& textureContainer); protected: int m_vertexCount, m_indexCount; ID3D11Buffer* m_vertexBuffer, * m_indexBuffer; private: bool InitializeBuffers(ID3D11Device*); void ShutdownBuffers(); void RenderBuffers(ID3D11DeviceContext*); bool LoadTextures(ID3D11Device*, ID3D11DeviceContext*, vector filename); void ReleaseTextures(); bool LoadModel(char*); bool LoadObjModel(char*); bool LoadTxtModel(char*); void ReleaseModel(); void CalculateModelVectors(); void CalculateTangentBinormal(TempVertexType, TempVertexType, TempVertexType, VectorType&, VectorType&); TextureContainer m_Textures; ModelType* m_model; }; #endif