forked from electronicarts/CnC_Generals_Zero_Hour
-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathW3DParticleSys.cpp
More file actions
376 lines (300 loc) · 11.4 KB
/
W3DParticleSys.cpp
File metadata and controls
376 lines (300 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
** Command & Conquer Generals Zero Hour(tm)
** Copyright 2025 Electronic Arts Inc.
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
////////////////////////////////////////////////////////////////////////////////
// //
// (c) 2001-2003 Electronic Arts Inc. //
// //
////////////////////////////////////////////////////////////////////////////////
// W3DParticleSys.cpp
// W3D Particle System implementation
// Author: Michael S. Booth, November 2001
#include "Common/GlobalData.h"
#include "GameClient/Color.h"
#include "W3DDevice/GameClient/W3DParticleSys.h"
#include "W3DDevice/GameClient/W3DAssetManager.h"
#include "W3DDevice/GameClient/W3DDisplay.h"
#include "W3DDevice/GameClient/HeightMap.h"
#include "W3DDevice/GameClient/W3DSmudge.h"
#include "W3DDevice/GameClient/W3DSnow.h"
#include "WW3D2/camera.h"
//------------------------------------------------------------------------------ Performance Timers
//#include "Common/PerfMetrics.h"
//#include "Common/PerfTimer.h"
//-------------------------------------------------------------------------------------------------
W3DParticleSystemManager::W3DParticleSystemManager()
{
m_pointGroup = nullptr;
m_streakLine = nullptr;
m_posBuffer = nullptr;
m_RGBABuffer = nullptr;
m_sizeBuffer = nullptr;
m_angleBuffer = nullptr;
m_readyToRender = false;
m_onScreenParticleCount = 0;
m_pointGroup = NEW PointGroupClass();
//m_streakLine = nullptr;
m_streakLine = NEW StreakLineClass();
m_posBuffer = NEW_REF( ShareBufferClass<Vector3>, (MAX_POINTS_PER_GROUP, "W3DParticleSystemManager::m_posBuffer") );
m_RGBABuffer = NEW_REF( ShareBufferClass<Vector4>, (MAX_POINTS_PER_GROUP, "W3DParticleSystemManager::m_RGBABuffer") );
m_sizeBuffer = NEW_REF( ShareBufferClass<float>, (MAX_POINTS_PER_GROUP, "W3DParticleSystemManager::m_sizeBuffer") );
m_angleBuffer = NEW_REF( ShareBufferClass<uint8>, (MAX_POINTS_PER_GROUP, "W3DParticleSystemManager::m_angleBuffer") );
}
W3DParticleSystemManager::~W3DParticleSystemManager()
{
delete m_pointGroup;
// W3DDisplay::m_3DScene->Remove_Render_Object( m_streakLine );
if (m_streakLine)
{
REF_PTR_RELEASE(m_streakLine);
}
REF_PTR_RELEASE(m_posBuffer);
REF_PTR_RELEASE(m_RGBABuffer);
REF_PTR_RELEASE(m_sizeBuffer);
REF_PTR_RELEASE(m_angleBuffer);
}
/**
* Hack because DoParticles is called from Flush(), which is called
* multiple times per frame. We only want to render once.
* @todo Clean up the flag/Flush hack.
*/
void W3DParticleSystemManager::queueParticleRender()
{
m_readyToRender = true;
}
/**
* Nasty hack to render particles last. Called directly by WW3D::Flush()
*/
void DoParticles( RenderInfoClass &rinfo )
{
if (TheParticleSystemManager)
TheParticleSystemManager->doParticles(rinfo);
}
void W3DParticleSystemManager::doParticles(RenderInfoClass &rinfo)
{
if (m_readyToRender == false)
return;
// external mechanism must tell us when it's OK to render again...
m_readyToRender = false;
//reset each frame
/// @todo lorenzen sez: this should be debug only:
m_onScreenParticleCount = 0;
const FrustumClass & frustum = rinfo.Camera.Get_Frustum();
AABoxClass bbox;
//Get a bounding box around our visible universe. Bounded by terrain and the sky
//so much tighter fitting volume than what's actually visible. This will cull
//particles falling under the ground.
TheTerrainRenderObject->getMaximumVisibleBox(frustum, &bbox, TRUE);
//@todo lorenzen sez: put these in registers for sure
Real bcX = bbox.Center.X;
Real bcY = bbox.Center.Y;
Real bcZ = bbox.Center.Z;
Real beX = bbox.Extent.X;
Real beY = bbox.Extent.Y;
Real beZ = bbox.Extent.Z;
unsigned int personalities[MAX_POINTS_PER_GROUP];
m_fieldParticleCount = 0;
const Bool drawSmudge = TheSmudgeManager && TheSmudgeManager->getHardwareSupport() && TheGlobalData->m_useHeatEffects;
if (drawSmudge)
{
TheSmudgeManager->resetDraw();
}
ParticleSystemManager::ParticleSystemList &particleSysList = TheParticleSystemManager->getAllParticleSystems();
for( ParticleSystemManager::ParticleSystemListIt it = particleSysList.begin(); it != particleSysList.end(); ++it)
{
ParticleSystem *sys = (*it);
if (!sys) {
continue;
}
// only look at particle/point style systems
if (sys->isUsingDrawables())
continue;
//temporary hack that checks if texture name starts with "SMUD" - if so, we can assume it's a smudge type
if (/*sys->isUsingSmudge()*/ *((DWORD *)sys->getParticleTypeName().str()) == 0x44554D53)
{
if (drawSmudge)
{
for (Particle *p = sys->getFirstParticle(); p; p = p->m_systemNext)
{
const Coord3D *pos = p->getPosition();
Real psize = p->getSize();
//Cull particle to edges of screen and terrain.
if (WWMath::Fabs( pos->x - bcX ) > ( beX + psize ) )
continue;
if (WWMath::Fabs( pos->y - bcY ) > ( beY + psize ) )
continue;
if (WWMath::Fabs( pos->z - bcZ ) > ( beZ + psize ) )
continue;
if (Smudge *smudge = TheSmudgeManager->findSmudge(p))
{
// The particle is in view. Draw the smudge!
smudge->m_draw = true;
}
}
}
continue;
}
/// @todo lorenzen sez: declare these outside the sys loop, and put some in registers
// initialize them here still, of course
// build W3D particle buffer
Int count = 0;
Vector3 *posArray = m_posBuffer->Get_Array();
Real *sizeArray = m_sizeBuffer->Get_Array();
Vector4 *RGBAArray = m_RGBABuffer->Get_Array();
uint8 *angleArray = m_angleBuffer->Get_Array();
const Coord3D *pos;
const RGBColor *color;
Real psize;
//set-up all the per-particle
for (Particle *p = sys->getFirstParticle(); p; p = p->m_systemNext)
{
pos = p->getPosition();
psize = p->getSize();
//Cull particle to edges of screen and terrain.
if (WWMath::Fabs(pos->x - bcX) > (beX + psize))
continue;
if (WWMath::Fabs(pos->y - bcY) > (beY + psize))
continue;
if (WWMath::Fabs(pos->z - bcZ) > (beZ + psize))
continue;
m_fieldParticleCount += ( sys->getPriority() == AREA_EFFECT && sys->m_isGroundAligned != FALSE );
//@todo lorenzen sez: use pointer arithmetic for these arrays
personalities[count] = p->getPersonality();
posArray[count].X = pos->x;
posArray[count].Y = pos->y;
posArray[count].Z = pos->z;
sizeArray[count] = psize;
color = p->getColor();
RGBAArray[count].X = color->red;
RGBAArray[count].Y = color->green;
RGBAArray[count].Z = color->blue;
RGBAArray[count].W = p->getAlpha();
angleArray[count] = (uint8)(p->getAngle() * 255.0f / (2.0f * PI));
if (++count == MAX_POINTS_PER_GROUP)
break;
}
if ( count == 0 )
continue; //this system has no particles to render
TextureClass *texture = W3DDisplay::m_assetManager->Get_Texture( sys->getParticleTypeName().str() );
if ( m_streakLine && sys->isUsingStreak() && (count >= 2) )
{
m_streakLine->Reset_Line();
m_streakLine->Set_Texture( texture );
texture->Release_Ref();//release reference since it's held by streakline
switch( sys->getShaderType() )
{
case ParticleSystemInfo::ADDITIVE:
m_streakLine->Set_Shader( ShaderClass::_PresetAdditiveSpriteShader );
break;
case ParticleSystemInfo::ALPHA:
m_streakLine->Set_Shader( ShaderClass::_PresetAlphaSpriteShader );
break;
case ParticleSystemInfo::ALPHA_TEST:
m_streakLine->Set_Shader( ShaderClass::_PresetATestSpriteShader );
break;
case ParticleSystemInfo::MULTIPLY:
m_streakLine->Set_Shader( ShaderClass::_PresetMultiplicativeSpriteShader );
break;
}
//UPDATE THE STREAK'S ARRAYS
m_streakLine->Set_LocsWidthsColors(
count,
m_posBuffer->Get_Array(),
m_sizeBuffer->Get_Array(),
m_RGBABuffer->Get_Array(),
&personalities[0]
);
//WWASSERT( m_streakLine->Get_Num_Points() == count );
// This is the happy place for this!
RGBAArray[0].X = 0;//eliminates the scissor edge on the trailing edge of the streak
RGBAArray[0].Y = 0;
RGBAArray[0].Z = 0;
RGBAArray[0].W = 0;
//RENDER STREAK!
m_streakLine->Render( rinfo );
}
else
{
WWASSERT( m_pointGroup );
if ( m_pointGroup ) // this catches the particle and volumeparticle cases
{
// render all the systems' particles
m_pointGroup->Set_Texture( texture );
texture->Release_Ref();//release reference since it's held by pointGroup
m_pointGroup->Set_Flag( PointGroupClass::TRANSFORM, true ); // transform to screen space
switch( sys->getShaderType() )
{
case ParticleSystemInfo::ADDITIVE:
m_pointGroup->Set_Shader( ShaderClass::_PresetAdditiveSpriteShader );
break;
case ParticleSystemInfo::ALPHA:
m_pointGroup->Set_Shader( ShaderClass::_PresetAlphaSpriteShader );
break;
case ParticleSystemInfo::ALPHA_TEST:
m_pointGroup->Set_Shader( ShaderClass::_PresetATestSpriteShader );
break;
case ParticleSystemInfo::MULTIPLY:
m_pointGroup->Set_Shader( ShaderClass::_PresetMultiplicativeSpriteShader );
break;
}
/// @todo Use both QUADS and TRIS for particles
m_pointGroup->Set_Point_Mode( PointGroupClass::QUADS );
m_pointGroup->Set_Arrays( m_posBuffer, m_RGBABuffer, nullptr, m_sizeBuffer, m_angleBuffer, nullptr, count );
m_pointGroup->Set_Billboard(sys->shouldBillboard());
/// @todo Support animated texture particles
/// @todo lorenzen sez: unimplemented code wastes cpu cycles
m_pointGroup->Set_Point_Frame( 0 );
//RENDER IT!
if( sys->getVolumeParticleDepth() > 1 )
{
m_pointGroup->RenderVolumeParticle( rinfo, sys->getVolumeParticleDepth() );
}
else
m_pointGroup->Render( rinfo );
}
}
/// @todo lorenzen sez: this should be debug only:
//add particle count to total
m_onScreenParticleCount += count;
/*
// draw the wind vector for this particle system on the screen
UnsignedInt width = TheDisplay->getWidth();
UnsignedInt height = TheDisplay->getHeight();
Coord3D worldStart, worldEnd;
ICoord2D pixelStart, pixelEnd;
sys->getPosition( &worldStart );
worldEnd.x = Cos( sys->getWindAngle() ) * 50.0f + worldStart.x;
worldEnd.y = Sin( sys->getWindAngle() ) * 50.0f + worldStart.y;
worldEnd.z = worldStart.z;
TheTacticalView->worldToScreen( &worldStart, &pixelStart );
TheTacticalView->worldToScreen( &worldEnd, &pixelEnd );
Color colorStart = GameMakeColor( 255, 255, 255, 255 );
Color colorEnd = GameMakeColor( 255, 128, 128, 255 );
TheDisplay->drawLine( pixelStart.x, pixelStart.y, pixelEnd.x, pixelEnd.y, 1.0f, colorStart, colorEnd );
*/
}
/// @todo lorenzen sez: this should be debug only:
TheParticleSystemManager->setOnScreenParticleCount(m_onScreenParticleCount);
//Draw any particles belonging to weather effects
if (TheSnowManager)
((W3DSnowManager *)TheSnowManager)->render(rinfo);
//Now process screen smudges which are particles that distort the background behind them.
if(TheSmudgeManager)
{
((W3DSmudgeManager *)TheSmudgeManager)->render(rinfo);
}
}