Khaotic Engine Reborn
Loading...
Searching...
No Matches
model_class.h
1#ifndef _MODELCLASS_H_
2#define _MODELCLASS_H_
3
4
6// INCLUDES //
8#include "Logger.h"
9
10#include <d3d11.h>
11#include <directxmath.h>
12#include <fstream>
13#include <sstream>
14#include <vector>
15#include <string>
16#include <WICTextureLoader.h>
17using namespace DirectX;
18using namespace std;
19
21// MY CLASS INCLUDES //
23#include "texture_class.h"
24
25enum class TextureType
26{
27 Diffuse,
28 Normal,
29 Specular,
30 Alpha
31};
32
34{
35
36 // Textures
37 std::vector<ID3D11ShaderResourceView*> diffuse;
38 std::vector<ID3D11ShaderResourceView*> normal;
39 std::vector<ID3D11ShaderResourceView*> specular;
40 std::vector<ID3D11ShaderResourceView*> alpha;
41
42 // Textures Paths
43 std::vector<std::wstring> diffusePaths;
44 std::vector<std::wstring> normalPaths;
45 std::vector<std::wstring> specularPaths;
46 std::vector<std::wstring> alphaPaths;
47
48
49 // Get the vector of textures_ based on the type
50 std::vector<ID3D11ShaderResourceView*>& Get(TextureType type) const {
51 switch (type) {
52 case TextureType::Diffuse: return const_cast<std::vector<ID3D11ShaderResourceView*>&>(diffuse);
53 case TextureType::Normal: return const_cast<std::vector<ID3D11ShaderResourceView*>&>(normal);
54 case TextureType::Specular: return const_cast<std::vector<ID3D11ShaderResourceView*>&>(specular);
55 case TextureType::Alpha: return const_cast<std::vector<ID3D11ShaderResourceView*>&>(alpha);
56 default: return const_cast<std::vector<ID3D11ShaderResourceView*>&>(diffuse);
57 }
58 }
59
60 // Get the vector of textures_ paths based on the type
61 std::vector<std::wstring> GetPaths(TextureType type) const {
62 switch (type)
63 {
64 case TextureType::Diffuse: return std::vector<std::wstring>(diffusePaths);
65 case TextureType::Normal: return std::vector<std::wstring>(normalPaths);
66 case TextureType::Specular: return std::vector<std::wstring>(specularPaths);
67 case TextureType::Alpha: return std::vector<std::wstring>(alphaPaths);
68 default: return std::vector<std::wstring>(diffusePaths);
69 }
70 }
71
72
73 // Get The texture based on the type and index
74 ID3D11ShaderResourceView* GetTexture(TextureType type, int index) const {
75 auto& vec = Get(type);
76 if (index >= 0 && index < vec.size())
77 return vec[index];
78 return nullptr;
79 }
80
81 // Get The texture path based on the type and index
82 std::wstring GetTexturePath(TextureType type, int index) const
83 {
84 std::vector<std::wstring> path = GetPaths(type);
85 if (index >= 0 && index < path.size())
86 return path[index];
87 return L"";
88 }
89
90 // Release all textures_ and textures_ paths
91
92 void ReleaseAll() {
93 ReleaseVector(diffuse);
94 ReleaseVector(normal);
95 ReleaseVector(specular);
96 ReleaseVector(alpha);
97
98 ReleaseVector(diffusePaths);
99 ReleaseVector(normalPaths);
100 ReleaseVector(specularPaths);
101 ReleaseVector(alphaPaths);
102 }
103
104 // Assign a texture and its path to the appropriate vector based on the index
105 void AssignTexture(TextureContainer& textContainer, ID3D11ShaderResourceView* texture , const std::wstring paths, int index)
106 {
107 switch (index)
108 {
109 case 0:
110 textContainer.diffuse.push_back(texture);
111 textContainer.diffusePaths.push_back(paths);
112 break;
113 case 1:
114 textContainer.normal.push_back(texture);
115 textContainer.normalPaths.push_back(paths);
116 break;
117 case 2:
118 textContainer.specular.push_back(texture);
119 textContainer.specularPaths.push_back(paths);
120 break;
121 case 3:
122 textContainer.alpha.push_back(texture);
123 textContainer.alphaPaths.push_back(paths);
124 break;
125 default:
126 textContainer.diffuse.push_back(texture);
127 textContainer.diffusePaths.push_back(paths);
128 break;
129 }
130 }
131
132private:
133 void ReleaseVector(std::vector<ID3D11ShaderResourceView*>& vec) {
134 for (auto& tex : vec) {
135 if (tex) {
136 tex->Release();
137 tex = nullptr;
138 }
139 }
140 vec.clear();
141 }
142
143 void ReleaseVector(std::vector<std::wstring>& vec) {
144 vec.clear();
145 }
146};
148// Class name: model_class
151{
152protected:
153
155 {
156 XMFLOAT3 position;
157 XMFLOAT2 texture;
158 XMFLOAT3 normal;
159 XMFLOAT3 tangent;
160 XMFLOAT3 binormal;
161 };
162
164 {
165 float x, y, z;
166 float tu, tv;
167 float nx, ny, nz;
168 float tx, ty, tz;
169 float bx, by, bz;
170 };
171
172 struct Vertex {
173 float x, y, z;
174 };
175
176 struct Texture {
177 float u, v;
178 };
179
180 struct Normal {
181 float nx, ny, nz;
182 };
183
185 {
186 float x, y, z;
187 float tu, tv;
188 float nx, ny, nz;
189 };
190
192 {
193 float x, y, z;
194 };
195
196 struct Face {
197 int v1, v2, v3;
198 int t1, t2, t3;
199 int n1, n2, n3;
200 };
201
202public:
203 model_class();
204 model_class(const model_class&) = delete;
205 model_class& operator=(const model_class&) = delete;
206 ~model_class();
207
208 //bool initialize(ID3D11Device*, ID3D11DeviceContext*, char*, std::vector<ID3D11ShaderResourceView*>);
209 // Nouvelle surcharge avec TextureContainer
210 bool Initialize(ID3D11Device*, ID3D11DeviceContext*, char*, const TextureContainer&);
211 // Nouvelle méthode - initialisation sans textures_
212 bool Initialize(ID3D11Device*, ID3D11DeviceContext*, char*);
213 void Shutdown();
214 void Render(ID3D11DeviceContext*);
215
216 int GetIndexCount();
217 int GetVertexCount() const { return m_vertexCount; }
218
219 // TEXTURE //
220
221 //ID3D11ShaderResourceView* get_texture(int index) const;
222 ID3D11ShaderResourceView* GetTexture(TextureType type, int index) const;
223
224 //bool ChangeTexture(ID3D11Device* device, ID3D11DeviceContext* deviceContext, std::wstring filename, int index);
225 bool ChangeTexture(ID3D11Device* device, ID3D11DeviceContext* deviceContext,std::wstring filename, TextureType type, int index);
226
227 // Méthodes pour ajouter une nouvelle texture
228 bool AddTexture(ID3D11Device* device, ID3D11DeviceContext* deviceContext,std::wstring filename, TextureType type);
229 bool AddTexture(ID3D11ShaderResourceView* texture, TextureType type);
230
231 void SetTextureContainer (TextureContainer& texturesContainer) { m_Textures = texturesContainer; }
232 TextureContainer GetTextureContainer() const { return m_Textures; }
233
234 bool PreloadTextures(ID3D11Device* device, ID3D11DeviceContext* deviceContext, TextureContainer& textureContainer);
235
236protected:
237 int m_vertexCount, m_indexCount;
238 ID3D11Buffer* m_vertexBuffer, * m_indexBuffer;
239
240private:
241 bool InitializeBuffers(ID3D11Device*);
242 void ShutdownBuffers();
243 void RenderBuffers(ID3D11DeviceContext*);
244 void ReleaseTextures();
245
246 bool LoadModel(char*);
247 bool LoadObjModel(char*);
248 bool LoadTxtModel(char*);
249 void ReleaseModel();
250
251 void CalculateModelVectors();
252 void CalculateTangentBinormal(TempVertexType, TempVertexType, TempVertexType, VectorType&, VectorType&);
253
254 TextureContainer m_Textures;
255 ModelType* m_model;
256};
257
258#endif