Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
matinfo.cpp
Go to the documentation of this file.
1/*
2** Command & Conquer Generals Zero Hour(tm)
3** Copyright 2025 Electronic Arts Inc.
4**
5** This program is free software: you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation, either version 3 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19/***********************************************************************************************
20 *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
21 ***********************************************************************************************
22 * *
23 * Project Name : MatInfo.h *
24 * *
25 * $Archive:: /Commando/Code/ww3d2/matinfo.cpp $*
26 * *
27 * Author:: Greg Hjelstrom *
28 * *
29 * $Modtime:: 6/15/01 5:50p $*
30 * *
31 * $Revision:: 10 $*
32 * *
33 *---------------------------------------------------------------------------------------------*
34 * Functions: *
35 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
36
37
38#include "matinfo.h"
39#include "wwdebug.h"
40#include "meshmdl.h"
41#include "texture.h"
42
46
48{
49 for (int mi=0; mi<src.VertexMaterials.Count(); mi++) {
51 vmat = src.VertexMaterials[mi]->Clone();
52 VertexMaterials.Add(vmat);
53 }
54
55 for (int ti=0; ti<src.Textures.Count(); ti++) {
56 TextureClass * tex = src.Textures[ti];
57 tex->Add_Ref();
58 Textures.Add(tex);
59 }
60}
61
62
67
68
73
75{
76 WWASSERT(tex != NULL);
77 tex->Add_Ref();
78 int index = Textures.Count();
79 Textures.Add(tex);
80 return index;
81}
82
84{
85 for (int i=0; i<Textures.Count(); i++) {
86 if (stricmp(name,Textures[i]->Get_Texture_Name()) == 0) {
87 return i;
88 }
89 }
90 return -1;
91}
92
94{
95 WWASSERT(index >= 0);
96 WWASSERT(index < Textures.Count());
97 Textures[index]->Add_Ref();
98 return Textures[index];
99}
100
101/*
102
103void MaterialInfoClass::Set_Texture_Reduction_Factor(float trf)
104{
105 for (int i = 0; i < Textures.Count(); i++) {
106 Textures[i]->Set_Reduction_Factor(trf);
107 }
108}
109
110
111void MaterialInfoClass::Process_Texture_Reduction(void)
112{
113 for (int i = 0; i < Textures.Count(); i++) {
114 Textures[i]->Process_Reduction();
115 }
116}
117*/
118void MaterialInfoClass::Free(void)
119{
120 int i;
121
122 for (i=0; i<VertexMaterials.Count(); i++) {
123 REF_PTR_RELEASE(VertexMaterials[i]);
124 }
125 VertexMaterials.Delete_All();
126
127 for (i=0; i<Textures.Count(); i++) {
128 REF_PTR_RELEASE(Textures[i]);
129 }
130 Textures.Delete_All();
131}
132
133
135 TextureCount(0),
136 TextureRemaps(NULL),
137 VertexMaterialCount(0),
138 VertexMaterialRemaps(NULL),
139 LastSrcVmat(NULL),
140 LastDestVmat(NULL),
141 LastSrcTex(NULL),
142 LastDestTex(NULL)
143{
144 WWASSERT(src);
145 WWASSERT(dest);
146 WWASSERT(src->Texture_Count() == dest->Texture_Count());
148
149 SrcMatInfo = src;
150 SrcMatInfo->Add_Ref();
151 DestMatInfo = dest;
152 DestMatInfo->Add_Ref();
153
154 if (src->Vertex_Material_Count() > 0) {
155 VertexMaterialCount = src->Vertex_Material_Count();
156 VertexMaterialRemaps = W3DNEWARRAY VmatRemapStruct[VertexMaterialCount];
157 for (int i=0; i<src->Vertex_Material_Count(); i++) {
158 VertexMaterialRemaps[i].Src = src->Peek_Vertex_Material(i);
159 VertexMaterialRemaps[i].Dest = dest->Peek_Vertex_Material(i);
160 }
161 }
162
163 if (src->Texture_Count() > 0) {
164 TextureCount = src->Texture_Count();
165 TextureRemaps = W3DNEWARRAY TextureRemapStruct[TextureCount];
166 for (int i=0; i<src->Texture_Count(); i++) {
167 TextureRemaps[i].Src = src->Peek_Texture(i);
168 TextureRemaps[i].Dest = dest->Peek_Texture(i);
169 }
170 }
171}
172
174{
175 SrcMatInfo->Release_Ref();
176 DestMatInfo->Release_Ref();
177
178 if (TextureRemaps) {
179 delete[] TextureRemaps;
180 }
181 if (VertexMaterialRemaps) {
182 delete[] VertexMaterialRemaps;
183 }
184}
185
187{
188 if (src == NULL) return src;
189 if (src == LastSrcTex) return LastDestTex;
190 for (int i=0; i<TextureCount; i++) {
191 if (TextureRemaps[i].Src == src) {
192 LastSrcTex = src;
193 LastDestTex = TextureRemaps[i].Dest;
194 return TextureRemaps[i].Dest;
195 }
196 }
197 WWASSERT(0); // uh-oh didn't find the texture, what happend???
198 return NULL;
199}
200
202{
203 if (src == NULL) return src;
204 if (src == LastSrcVmat) return LastDestVmat;
205 for (int i=0; i<VertexMaterialCount; i++) {
206 if (VertexMaterialRemaps[i].Src == src) {
207 LastSrcVmat = src;
208 LastDestVmat = VertexMaterialRemaps[i].Dest;
209 return VertexMaterialRemaps[i].Dest;
210 }
211 }
212 WWASSERT(0); // uh-oh didn't find the material, what happend???
213 return NULL;
214}
215
216void MaterialRemapperClass::Remap_Mesh(const MeshMatDescClass * srcmeshmatdesc, MeshMatDescClass * destmeshmatdesc)
217{
218 /*
219 ** Remap the vertex materials if there is at least one of them
220 */
221 if (SrcMatInfo->Vertex_Material_Count() >= 1) {
222
223 for (int pass = 0;pass < srcmeshmatdesc->Get_Pass_Count(); pass++) {
224
225 if (srcmeshmatdesc->Has_Material_Array(pass)) {
226
227 for (int vert_index = 0; vert_index < srcmeshmatdesc->Get_Vertex_Count(); vert_index++) {
228 VertexMaterialClass * src = srcmeshmatdesc->Peek_Material(vert_index, pass);
229 destmeshmatdesc->Set_Material(vert_index, Remap_Vertex_Material(src),pass);
230 }
231
232 } else {
233
234 VertexMaterialClass * src = srcmeshmatdesc->Peek_Single_Material(pass);
235 destmeshmatdesc->Set_Single_Material(Remap_Vertex_Material(src), pass);
236
237 }
238 }
239 }
240
241 /*
242 ** Remap the textures if there is at least one of them
243 */
244 if (SrcMatInfo->Texture_Count() >= 1) {
245
246 for (int pass = 0;pass < srcmeshmatdesc->Get_Pass_Count(); pass++) {
247
248 for (int stage = 0; stage < MeshMatDescClass::MAX_TEX_STAGES; stage++) {
249
250 if (srcmeshmatdesc->Has_Texture_Array(pass, stage)) {
251
252 for (int poly_index = 0; poly_index < srcmeshmatdesc->Get_Polygon_Count(); poly_index++) {
253 TextureClass * src = srcmeshmatdesc->Peek_Texture(poly_index, pass, stage);
254 destmeshmatdesc->Set_Texture(poly_index, Remap_Texture(src), pass, stage);
255 }
256
257 } else {
258
259 TextureClass * src = srcmeshmatdesc->Peek_Single_Texture(pass, stage);
260 destmeshmatdesc->Set_Single_Texture(Remap_Texture(src), pass, stage);
261
262 }
263 }
264 }
265 }
266}
267
274
279
281{
282 for (int pass = 0;pass < mesh->Get_Pass_Count(); pass++) {
283
284 // Vertex materials (either single or per vertex)
285 if (mesh->Has_Material_Array(pass)) {
286
287 for (int vert_index = 0;vert_index < mesh->Get_Vertex_Count(); vert_index++) {
288 VertexMaterialClass * mat = mesh->Peek_Material(vert_index,pass);
290 }
291
292 } else {
293 VertexMaterialClass * mat = mesh->Get_Single_Material(pass);
295 REF_PTR_RELEASE(mat);
296 }
297
298
299 // Shaders (single or per poly...)
300 if (mesh->Has_Shader_Array(pass)) {
301 for (int poly_index=0; poly_index < mesh->Get_Polygon_Count(); poly_index++) {
302 Add_Shader(mesh->Get_Shader(poly_index,pass));
303 }
304 } else {
305 ShaderClass sh = mesh->Get_Single_Shader(pass);
306 Add_Shader(sh);
307 }
308
309
310 // Textures per pass, per stage (either array or single...)
311 for (int stage = 0; stage < MeshMatDescClass::MAX_TEX_STAGES; stage++) {
312
313 if (mesh->Has_Texture_Array(pass,stage)) {
314
315 for (int poly_index = 0;poly_index < mesh->Get_Polygon_Count(); poly_index++) {
316 TextureClass * tex = mesh->Peek_Texture(poly_index,pass,stage);
317 Add_Texture(tex);
318 }
319
320 } else {
321
322 TextureClass * tex = mesh->Peek_Single_Texture(pass,stage);
323 Add_Texture(tex);
324
325 }
326 }
327 }
328}
329
331{
332 for (int ti=0; ti<Textures.Count(); ti++) {
334 }
335 for (int vi=0; vi<VertexMaterials.Count(); vi++) {
337 }
338 Textures.Clear();
339 VertexMaterials.Clear();
340 Shaders.Clear();
341}
342
344{
345 if (tex == NULL) return;
346 if (tex == LastTexture) return;
347 if (Find_Texture(tex) != -1) return;
348 Textures.Add(tex);
349 tex->Add_Ref();
350 LastTexture = tex;
351}
352
354{
355 if (shader == LastShader) return;
356 if (Find_Shader(shader) != -1) return;
357 Shaders.Add(shader);
358 LastShader = shader;
359}
360
362{
363 if (vmat == NULL) return;
364 if (vmat == LastMaterial) return;
365 if (Find_Vertex_Material(vmat) != -1) return;
366 VertexMaterials.Add(vmat);
367 vmat->Add_Ref();
368 LastMaterial = vmat;
369}
370
372{
373 return Shaders.Count();
374}
375
380
382{
383 return Textures.Count();
384}
385
390
395
400
402{
403 for (int si=0; si<Shaders.Count(); si++) {
404 if (Shaders[si] == shader) {
405 return si;
406 }
407 }
408 return -1;
409}
410
412{
413 for (int ti=0; ti<Textures.Count(); ti++) {
414 if (Textures[ti] == tex) {
415 return ti;
416 }
417 }
418 return -1;
419}
420
422{
423 for (int vi=0; vi<VertexMaterials.Count(); vi++) {
424 if (VertexMaterials[vi] == mat) {
425 return vi;
426 }
427 }
428 return -1;
429}
#define NULL
Definition BaseType.h:92
#define WWASSERT
#define W3DNEWARRAY
Definition always.h:110
#define W3DNEW
Definition always.h:109
int Count(void) const
Definition Vector.H:507
bool Add(T const &object)
Definition Vector.H:671
int Find_Vertex_Material(VertexMaterialClass *mat)
Definition matinfo.cpp:421
VertexMaterialClass * LastMaterial
Definition matinfo.h:220
DynamicVectorClass< TextureClass * > Textures
Definition matinfo.h:217
void Add_Shader(ShaderClass shader)
Definition matinfo.cpp:353
int Get_Texture_Count(void)
Definition matinfo.cpp:381
void Collect_Materials(MeshModelClass *mesh)
Definition matinfo.cpp:280
int Get_Vertex_Material_Count(void)
Definition matinfo.cpp:376
TextureClass * Peek_Texture(int i)
Definition matinfo.cpp:391
int Get_Shader_Count(void)
Definition matinfo.cpp:371
TextureClass * LastTexture
Definition matinfo.h:221
VertexMaterialClass * Peek_Vertex_Material(int i)
Definition matinfo.cpp:396
int Find_Shader(const ShaderClass &shader)
Definition matinfo.cpp:401
void Add_Texture(TextureClass *tex)
Definition matinfo.cpp:343
DynamicVectorClass< VertexMaterialClass * > VertexMaterials
Definition matinfo.h:216
void Add_Vertex_Material(VertexMaterialClass *vmat)
Definition matinfo.cpp:361
DynamicVectorClass< ShaderClass > Shaders
Definition matinfo.h:215
ShaderClass LastShader
Definition matinfo.h:219
ShaderClass Peek_Shader(int i)
Definition matinfo.cpp:386
int Find_Texture(TextureClass *tex)
Definition matinfo.cpp:411
VertexMaterialClass * Peek_Vertex_Material(int index)
Definition matinfo.h:266
int Get_Texture_Index(const char *name)
Definition matinfo.cpp:83
int Vertex_Material_Count(void) const
Definition matinfo.h:83
TextureClass * Peek_Texture(int index)
Definition matinfo.h:323
int Texture_Count(void) const
Definition matinfo.h:84
TextureClass * Get_Texture(int index)
Definition matinfo.cpp:93
int Add_Texture(TextureClass *tex)
Definition matinfo.cpp:74
MaterialInfoClass * Clone(void) const
Definition matinfo.cpp:69
MaterialRemapperClass(MaterialInfoClass *src, MaterialInfoClass *dest)
Definition matinfo.cpp:134
VertexMaterialClass * Remap_Vertex_Material(VertexMaterialClass *src)
Definition matinfo.cpp:201
void Remap_Mesh(const MeshMatDescClass *srcmeshmatdesc, MeshMatDescClass *destmeshmatdesc)
Definition matinfo.cpp:216
TextureClass * Remap_Texture(TextureClass *src)
Definition matinfo.cpp:186
int Get_Vertex_Count(void) const
int Get_Polygon_Count(void) const
int Get_Pass_Count(void) const
Definition meshmatdesc.h:91
VertexMaterialClass * Peek_Material(int vidx, int pass=0) const
void Set_Texture(int pidx, TextureClass *tex, int pass=0, int stage=0)
void Set_Single_Texture(TextureClass *tex, int pass=0, int stage=0)
int Get_Vertex_Count(void) const
Definition meshmatdesc.h:93
VertexMaterialClass * Peek_Single_Material(int pass=0) const
int Get_Polygon_Count(void) const
Definition meshmatdesc.h:95
bool Has_Texture_Array(int pass, int stage) const
void Set_Material(int vidx, VertexMaterialClass *vmat, int pass=0)
void Set_Single_Material(VertexMaterialClass *vmat, int pass=0)
TextureClass * Peek_Single_Texture(int pass=0, int stage=0) const
TextureClass * Peek_Texture(int pidx, int pass=0, int stage=0) const
bool Has_Material_Array(int pass) const
TextureClass * Peek_Texture(int pidx, int pass=0, int stage=0) const
Definition meshmdl.h:217
bool Has_Shader_Array(int pass) const
Definition meshmdl.h:207
VertexMaterialClass * Peek_Material(int vidx, int pass=0) const
Definition meshmdl.h:216
bool Has_Material_Array(int pass) const
Definition meshmdl.h:206
int Get_Pass_Count(void) const
Definition meshmdl.h:174
VertexMaterialClass * Get_Single_Material(int pass=0) const
Definition meshmdl.h:192
bool Has_Texture_Array(int pass, int stage) const
Definition meshmdl.h:208
ShaderClass Get_Shader(int pidx, int pass=0) const
Definition meshmdl.h:213
ShaderClass Get_Single_Shader(int pass=0) const
Definition meshmdl.h:194
TextureClass * Peek_Single_Texture(int pass=0, int stage=0) const
Definition meshmdl.h:199
void Add_Ref(void) const
Definition refcount.cpp:171
#define REF_PTR_RELEASE(x)
Definition refcount.h:80