Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
decalsys.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 : WW3D *
24 * *
25 * $Archive:: /Commando/Code/ww3d2/decalsys.cpp $*
26 * *
27 * Original Author:: Greg Hjelstrom *
28 * *
29 * $Author:: Kenny Mitchell *
30 * *
31 * $Modtime:: 06/26/02 4:04p $*
32 * *
33 * $Revision:: 8 $*
34 * *
35 * 06/26/02 KM Matrix name change to avoid MAX conflicts *
36 *---------------------------------------------------------------------------------------------*
37 * Functions: *
38 * DecalSystemClass::DecalSystemClass -- Constructor *
39 * DecalSystemClass::~DecalSystemClass -- Destructor *
40 * DecalSystemClass::Lock_Decal_Generator -- returns a DecalGenerator *
41 * DecalSystemClass::Unlock_Decal_Generator -- Destroys the decal generator *
42 * DecalSystemClass::Generate_Unique_Global_Decal_Id -- Generates a unique id for a decal *
43 * DecalGeneratorClass::DecalGeneratorClass -- Constructor *
44 * DecalGeneratorClass::~DecalGeneratorClass -- Destructor *
45 * DecalGeneratorClass::Add_Mesh -- Meshes that generate a decal should add themselves *
46 * DecalGeneratorClass::Get_Mesh_List -- returns the list of meshes *
47 * DecalGeneratorClass::Set_Mesh_Transform -- sets the current mesh coordinate system *
48 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
49
50#include "decalsys.h"
51#include "rendobj.h"
52#include "mesh.h"
53#include "decalmsh.h"
54#include "matrixmapper.h"
55#include "texture.h"
56
57
59
60
61/*
62** DecalSystemClass Implementation
63*/
64
65/***********************************************************************************************
66 * DecalSystemClass::DecalSystemClass -- Constructor *
67 * *
68 * INPUT: *
69 * *
70 * OUTPUT: *
71 * *
72 * WARNINGS: *
73 * *
74 * HISTORY: *
75 *=============================================================================================*/
79
80
81/***********************************************************************************************
82 * DecalSystemClass::~DecalSystemClass -- Destructor *
83 * *
84 * INPUT: *
85 * *
86 * OUTPUT: *
87 * *
88 * WARNINGS: *
89 * *
90 * HISTORY: *
91 *=============================================================================================*/
95
96
97/***********************************************************************************************
98 * DecalSystemClass::Lock_Decal_Generator -- returns a DecalGenerator *
99 * *
100 * INPUT: *
101 * *
102 * OUTPUT: *
103 * *
104 * WARNINGS: *
105 * *
106 * HISTORY: *
107 * 1/26/00 gth : Created. *
108 *=============================================================================================*/
114
115
116/***********************************************************************************************
117 * DecalSystemClass::Unlock_Decal_Generator -- Destroys the decal generator *
118 * *
119 * Derived classes may take a record of the results of the generator here. Then they can *
120 * put a cap on the total number of decals in the system or whatever... *
121 * *
122 * INPUT: *
123 * *
124 * OUTPUT: *
125 * *
126 * WARNINGS: *
127 * *
128 * HISTORY: *
129 * 1/26/00 gth : Created. *
130 *=============================================================================================*/
132{
133 delete generator;
134}
135
136
137/***********************************************************************************************
138 * DecalSystemClass::Generate_Unique_Global_Decal_Id -- Generates a unique id for a decal *
139 * *
140 * INPUT: *
141 * *
142 * OUTPUT: *
143 * *
144 * WARNINGS: *
145 * Hopefully you won't use more than 4 billion decals at one time... *
146 * *
147 * HISTORY: *
148 * 1/26/00 gth : Created. *
149 *=============================================================================================*/
154
155
156/*
157** DecalGeneratorClass Implementation
158*/
159
160/***********************************************************************************************
161 * DecalGeneratorClass::DecalGeneratorClass -- Constructor *
162 * *
163 * INPUT: *
164 * *
165 * OUTPUT: *
166 * *
167 * WARNINGS: *
168 * *
169 * HISTORY: *
170 * 1/26/00 gth : Created. *
171 *=============================================================================================*/
184
185
186/***********************************************************************************************
187 * DecalGeneratorClass::~DecalGeneratorClass -- Destructor *
188 * *
189 * INPUT: *
190 * *
191 * OUTPUT: *
192 * *
193 * WARNINGS: *
194 * *
195 * HISTORY: *
196 * 1/26/00 gth : Created. *
197 *=============================================================================================*/
202
203
204/***********************************************************************************************
205 * DecalGeneratorClass::Add_Mesh -- Meshes that generate a decal should add themselves *
206 * *
207 * This adds a mesh to the internal list. All meshes that actually add a decal should add *
208 * themselves to this list so that the DecalSystem can keep track of them. *
209 * *
210 * INPUT: *
211 * *
212 * OUTPUT: *
213 * *
214 * WARNINGS: *
215 * *
216 * HISTORY: *
217 * 1/26/00 gth : Created. *
218 *=============================================================================================*/
224
225
226/***********************************************************************************************
227 * DecalGeneratorClass::Get_Mesh_List -- returns the list of meshes *
228 * *
229 * This is the list of meshes which generated decals for this "logical decal" *
230 * *
231 * INPUT: *
232 * *
233 * OUTPUT: *
234 * *
235 * WARNINGS: *
236 * *
237 * HISTORY: *
238 * 1/26/00 gth : Created. *
239 *=============================================================================================*/
244
245
246/***********************************************************************************************
247 * DecalGeneratorClass::Set_Mesh_Transform -- sets the current mesh coordinate system *
248 * *
249 * The coordinate system of the mesh is needed in order to compute texture coordinates for *
250 * the decal polygons. *
251 * *
252 * INPUT: *
253 * *
254 * OUTPUT: *
255 * *
256 * WARNINGS: *
257 * *
258 * HISTORY: *
259 * 1/26/00 gth : Created. *
260 *=============================================================================================*/
262{
263 /*
264 ** Mobj-texture = Projection * Mwrld-texture * Mobj-wrld
265 */
266 Matrix3D world_to_texture;
267 Matrix3D tmp;
268 Matrix4x4 mesh_to_texture;
269
270 Transform.Get_Orthogonal_Inverse(world_to_texture);
271 Matrix3D::Multiply(world_to_texture,mesh_transform,&tmp);
272 Matrix4x4::Multiply(Projection,tmp,&mesh_to_texture);
273
274 /*
275 ** Plug the matrix and texture size into the mapper
276 */
278 float texsize = 64.0f;
279 TextureClass * tex = Material->Peek_Texture();
280 WWASSERT(tex != NULL);
281 if (tex) {
282// SurfaceClass::SurfaceDescription surface_desc;
283// tex->Get_Level_Description(surface_desc);
284// texsize = surface_desc.Width;
285 texsize = tex->Get_Width();
286 }
287
288 Mapper->Set_Texture_Transform(mesh_to_texture,texsize);
289 }
290}
291
292
293/*
294** MultiFixedPoolDecalSystemClass implementation
295*/
296
298 Pools(0),
299 PoolCount(num_pools),
302{
303 if (PoolCount)
304 {
305 WWASSERT(pool_sizes);
307 }
308 for (uint32 i = 0; i < PoolCount; i++) {
309 assert(pool_sizes[i]);
310 Pools[i].Initialize(pool_sizes[i]);
311 }
312}
313
315 Pools(0),
316 PoolCount(that.PoolCount),
319{
320 uint32 i;
321
322 // Allocate arrays (we dont' copy array contents because those are mesh-specific and will be
323 // filled when the state is set anyway)
325 for (i = 0; i < PoolCount; i++) {
326 Pools[i].Initialize(that.Pools[i].Size);
327 }
328}
329
331{
332 if (Pools) {
333 delete [] Pools;
334 Pools = 0;
335 }
336}
337
338// This clears the slot in addition to locking the generator, thus preventing any decal id
339// collisions (since any decal previously in that slot will have the same id as the new one).
345
346// This will register the decal in the system in the appropriate pool and slot (determined by
347// the generator's pool and slot ids), removing any decal which may have been there before.
353
354// This notifies the system that a mesh which has decals on it was destroyed - therefore we
355// need to remove the mesh from our list to avoid dangling pointers.
357{
358 // We must remove this mesh from all lists where it is present. The method is: for each
359 // decal id present in the decal mesh, find the logical decal and remove the decal mesh from
360 // its list.
361 assert(mesh);
362 MeshClass *parent = mesh->Peek_Parent();
363 find_logical_decal(decal_id).MeshList.Remove(parent);
364}
365
366// Not part of the DecalSystemClass interface - this function removes any decal currently in
367// the given slot in the given pool.
369{
370 find_logical_decal(pool_id, slot_id).Clear(encode_decal_id(pool_id, slot_id));
371}
372
373// This one removes all decals in a given pool.
375{
376 LogicalDecalPoolClass & pool = Pools[pool_id];
377 uint32 pool_size = pool.Size;
378 for (uint32 slot_id = 0; slot_id < pool_size; slot_id++) {
379 pool.Array[slot_id].Clear(encode_decal_id(pool_id, slot_id));
380 }
381}
382
383// And this one removes all decals in the system.
385{
386 for (uint32 pool_id = 0; pool_id < PoolCount; pool_id++) {
387 LogicalDecalPoolClass & pool = Pools[pool_id];
388 uint32 pool_size = pool.Size;
389 for (uint32 slot_id = 0; slot_id < pool_size; slot_id++) {
390 pool.Array[slot_id].Clear(encode_decal_id(pool_id, slot_id));
391 }
392 }
393}
394
395// Get a reference to the logical decal at the given pool and slot id (performs range checking)
397{
398 assert(pool_id < PoolCount);
399 pool_id = MIN(pool_id, PoolCount);
400 LogicalDecalPoolClass & pool = Pools[pool_id];
401 assert(slot_id < pool.Size);
402 slot_id = MIN(slot_id, pool.Size);
403 return pool.Array[slot_id];
404}
405
406// Get a reference to the logical decal with the given decal id
408{
409 uint32 pool_id, slot_id;
410 decode_decal_id(decal_id, pool_id, slot_id);
411 return find_logical_decal(pool_id, slot_id);
412}
413
414
415/*
416** MultiFixedPoolDecalSystemClass::LogicalDecalClass implementation
417*/
418
422
424{
425 // We don't need to do anything here since the mesh list will get removed automatically and
426 // the decals themselves don't neccessarily need to be removed because the logical decal is.
427 // (we don't hold references to the meshes in the mesh list)
428}
429
430// Sets the logical decal to the one specified by the given generator (clearing any existing
431// decal information)
433{
434 Clear(generator->Get_Decal_ID());
435
436 /*
437 ** Copy the generators' mesh list into our mesh list
438 */
439 NonRefRenderObjListIterator gen_mesh_it(&(generator->Get_Mesh_List()));
440 for (gen_mesh_it.First(); !gen_mesh_it.Is_Done(); gen_mesh_it.Next()) {
441 MeshList.Add(gen_mesh_it.Get_Obj());
442 }
443}
444
445// Just clears any existing logical decal information, leaving the decal empty.
447{
448 // Remove the decal with this ID from all meshes where it appears
450 for (it.First(); !it.Is_Done(); it.Next()) {
451 it.Get_Obj()->Delete_Decal(decal_id);
452 }
453
454 // Delete list
455 while (!MeshList.Is_Empty()) {
456 MeshList.Remove_Head();
457 }
458}
459
460
461/*
462** MultiFixedPoolDecalSystemClass::LogicalDecalPoolClass implementation
463*/
464
470
478
480{
481 if (Array) {
482 delete [] Array;
483 Array = 0;
484 }
485
486 Size = size;
487 assert(Size);
489}
#define NULL
Definition BaseType.h:92
#define WWASSERT
#define W3DNEWARRAY
Definition always.h:110
#define MIN(a, b)
Definition always.h:189
#define W3DNEW
Definition always.h:109
unsigned long uint32
Definition bittype.h:46
@ false
Definition bool.h:59
bool ApplyToTranslucentMeshes
Definition decalsys.h:193
friend class DecalSystemClass
Definition decalsys.h:205
void Add_Mesh(RenderObjClass *mesh)
Definition decalsys.cpp:219
DecalSystemClass * System
Definition decalsys.h:186
void Set_Mesh_Transform(const Matrix3D &tm)
Definition decalsys.cpp:261
NonRefRenderObjListClass & Get_Mesh_List(void)
Definition decalsys.cpp:240
MaterialPassClass * Material
Definition decalsys.h:198
NonRefRenderObjListClass MeshList
Definition decalsys.h:203
uint32 Get_Decal_ID(void)
Definition decalsys.h:147
DecalGeneratorClass(uint32 id, DecalSystemClass *system)
Definition decalsys.cpp:172
MeshClass * Peek_Parent(void)
Definition decalmsh.h:252
static uint32 DecalIDGenerator
Definition decalsys.h:122
DecalSystemClass(void)
Definition decalsys.cpp:76
static uint32 Generate_Unique_Global_Decal_Id(void)
Definition decalsys.cpp:150
virtual DecalGeneratorClass * Lock_Decal_Generator(void)
Definition decalsys.cpp:109
virtual uint32 Generate_Decal_Id(void)
Definition decalsys.h:113
virtual void Unlock_Decal_Generator(DecalGeneratorClass *generator)
Definition decalsys.cpp:131
virtual ~DecalSystemClass(void)
Definition decalsys.cpp:92
void First(GenericMultiListClass *list)
Definition multilist.h:182
static void Multiply(const Matrix3D &A, const Matrix3D &B, Matrix3D *set_result)
Definition matrix3d.cpp:640
WWINLINE friend Matrix4x4 Multiply(const Matrix4x4 &a, const Matrix4x4 &b)
Definition matrix4.h:743
void Set(DecalGeneratorClass *generator)
Definition decalsys.cpp:432
virtual void Unlock_Decal_Generator(DecalGeneratorClass *generator)
Definition decalsys.cpp:348
void Clear_Decal_Slot(uint32 pool_id, uint32 slot_id)
Definition decalsys.cpp:368
static uint32 encode_decal_id(uint32 pool_id, uint32 slot_id)
Definition decalsys.h:269
virtual void Decal_Mesh_Destroyed(uint32 id, DecalMeshClass *mesh)
Definition decalsys.cpp:356
static void decode_decal_id(uint32 decal_id, uint32 &pool_id, uint32 &slot_id)
Definition decalsys.h:270
MultiFixedPoolDecalSystemClass(uint32 num_pools, const uint32 *pool_sizes)
Definition decalsys.cpp:297
LogicalDecalClass & find_logical_decal(uint32 pool_id, uint32 slot_id)
Definition decalsys.cpp:396
LogicalDecalPoolClass * Pools
Definition decalsys.h:299
void Clear_Pool(uint32 pool_id)
Definition decalsys.cpp:374
virtual ~MultiFixedPoolDecalSystemClass(void)
Definition decalsys.cpp:330
virtual DecalGeneratorClass * Lock_Decal_Generator(void)
Definition decalsys.cpp:340
bool Remove(ObjectType *obj)
Definition multilist.h:249
ObjectType * Get_Obj(void)
Definition multilist.h:301
MatrixMapperClass * Mapper
Definition projector.h:86
Matrix3D Transform
Definition projector.h:80
Matrix4x4 Projection
Definition projector.h:81
virtual void Delete_Decal(uint32 decal_id)
Definition rendobj.h:439
virtual int Class_ID(void) const
Definition rendobj.h:249
int Get_Width() const
Definition texture.h:124
static bool Is_Texturing_Enabled()
Definition ww3d.h:257
#define REF_PTR_RELEASE(x)
Definition refcount.h:80
#define NEW_REF(C, P)
Definition refcount.h:62
MultiListIterator< RenderObjClass > NonRefRenderObjListIterator
Definition robjlist.h:63
MultiListClass< RenderObjClass > NonRefRenderObjListClass
Definition robjlist.h:62