Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

feat(w3ddevice): light smoke with the fires burning under it

Olcay Seygan9086d3ce18dc
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DParticleSys.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DParticleSys.cpp
index 606a75a0a..a76b5e5d0 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DParticleSys.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DParticleSys.cpp
@@ -37,6 +37,8 @@
#include "W3DDevice/GameClient/HeightMap.h"
#include "W3DDevice/GameClient/W3DSmudge.h"
#include "W3DDevice/GameClient/W3DSnow.h"
+#include "W3DDevice/GameClient/W3DScene.h"
+#include "W3DDevice/GameClient/W3DDynamicLight.h"
#include "WW3D2/camera.h"
#include "WW3D2/dx8wrapper.h"
#include "Common/JobSystem.h"
@@ -77,6 +79,232 @@ static Bool particlePresetShader( ParticleSystemInfo::ParticleShaderType type, S
return FALSE;
}
+//-------------------------------------------------------------------------------------------------
+// Fire lights the smoke around it. Each frame the additive systems (flames, explosions, muzzle
+// flashes) are reduced to one point light apiece at their brightness-weighted centre, the scene's
+// enabled light pulses join them, and every alpha-blended particle near one of those gets the
+// light's colour added to its own, fading with distance. A plume is lit at its base and goes grey
+// as it rises out of reach, and it flickers with the flames because the flames' own particles are
+// what measure the light. All of it is client-side colour on the CPU, so both devices draw it.
+//-------------------------------------------------------------------------------------------------
+
+/// placeholder for the option; becomes TheGlobalData->m_smokeFireLighting once that field exists
+static const Bool SMOKE_FIRE_LIGHTING = TRUE;
+
+static const Int SMOKE_LIGHTS_MAX = 32; ///< lights gathered a frame, the strongest kept
+static const Int SMOKE_LIGHTS_PER_SYSTEM = 4; ///< lights one smoke system evaluates per particle
+static const Real FIRE_LIGHT_FULL_WEIGHT = 120.0f; ///< sum of brightness x size at which a fire is full strength
+static const Real FIRE_LIGHT_MIN_WEIGHT = 4.0f; ///< below this an additive system lights nothing
+static const Real FIRE_LIGHT_RADIUS = 35.0f; ///< reach of a fire's light beyond its own spread
+static const Real FIRE_LIGHT_SPREAD_SCALE = 2.0f; ///< times the flames' RMS spread, added to the reach
+static const Real FIRE_LIGHT_MAX_RADIUS = 140.0f;
+static const Real FIRE_LIGHT_GAIN = 0.6f; ///< glow a full-strength fire adds at its centre
+static const Real PULSE_LIGHT_GAIN = 0.4f; ///< same for an FX light pulse, whose colour is already full
+static const Real SMOKE_LIGHT_MAX_GLOW = 0.7f; ///< most any one channel is raised by
+
+/** A light as the smoke sees it: its colour is already scaled by its strength and gain. */
+struct SmokeLight
+{
+ Real x, y, z;
+ Real radiusSq, invRadiusSq;
+ Real r, g, b;
+ Real strength; ///< orders the list, strongest first
+};
+
+/** Keeps the list at SMOKE_LIGHTS_MAX by dropping the weakest. */
+static void addSmokeLight( SmokeLight *lights, Int &count, const SmokeLight &light )
+{
+ Int slot = count;
+ if (count == SMOKE_LIGHTS_MAX)
+ {
+ slot = 0;
+ for (Int i = 1; i < count; ++i)
+ if (lights[ i ].strength < lights[ slot ].strength)
+ slot = i;
+ if (lights[ slot ].strength >= light.strength)
+ return;
+ }
+ else
+ ++count;
+ lights[ slot ] = light;
+}
+
+/** One additive system as a point light, if it is bright enough and near enough the view box. */
+static void gatherFireLight( ParticleSystem *sys, SmokeLight *lights, Int &count,
+ const AABoxClass &view )
+{
+ Real w = 0.0f, x = 0.0f, y = 0.0f, z = 0.0f, sq = 0.0f, r = 0.0f, g = 0.0f, b = 0.0f;
+ for (Particle *p = sys->getFirstParticle(); p; p = p->m_systemNext)
+ {
+ const RGBColor *c = p->getColor();
+ const Real pw = (c->red + c->green + c->blue) * (1.0f / 3.0f) * p->getSize();
+ if (pw <= 0.0f)
+ continue;
+ const Coord3D *pos = p->getPosition();
+ w += pw;
+ x += pw * pos->x;
+ y += pw * pos->y;
+ z += pw * pos->z;
+ sq += pw * (pos->x * pos->x + pos->y * pos->y + pos->z * pos->z);
+ r += pw * c->red;
+ g += pw * c->green;
+ b += pw * c->blue;
+ }
+ if (w < FIRE_LIGHT_MIN_WEIGHT)
+ return;
+
+ SmokeLight light;
+ const Real inv = 1.0f / w;
+ light.x = x * inv;
+ light.y = y * inv;
+ light.z = z * inv;
+ const Real variance = sq * inv - (light.x * light.x + light.y * light.y + light.z * light.z);
+ Real radius = FIRE_LIGHT_RADIUS + FIRE_LIGHT_SPREAD_SCALE * (variance > 0.0f ? sqrtf( variance ) : 0.0f);
+ if (radius > FIRE_LIGHT_MAX_RADIUS)
+ radius = FIRE_LIGHT_MAX_RADIUS;
+
+ if (WWMath::Fabs( light.x - view.Center.X ) > view.Extent.X + radius
+ || WWMath::Fabs( light.y - view.Center.Y ) > view.Extent.Y + radius
+ || WWMath::Fabs( light.z - view.Center.Z ) > view.Extent.Z + radius)
+ return;
+
+ // the hue from the flames' average colour, the strength from how much of them there is
+ light.strength = w < FIRE_LIGHT_FULL_WEIGHT ? w / FIRE_LIGHT_FULL_WEIGHT : 1.0f;
+ Real peak = r > g ? r : g;
+ if (b > peak)
+ peak = b;
+ const Real scale = light.strength * FIRE_LIGHT_GAIN / peak;
+ light.r = r * scale;
+ light.g = g * scale;
+ light.b = b * scale;
+ light.radiusSq = radius * radius;
+ light.invRadiusSq = 1.0f / light.radiusSq;
+ addSmokeLight( lights, count, light );
+}
+
+/** The scene's enabled light pulses, which FX lists put on explosions. */
+static void gatherPulseLights( SmokeLight *lights, Int &count, const AABoxClass &view )
+{
+ if (W3DDisplay::m_3DScene == NULL)
+ return;
+
+ RefRenderObjListIterator it( W3DDisplay::m_3DScene->getDynamicLights() );
+ for (it.First(); !it.Is_Done(); it.Next())
+ {
+ W3DDynamicLight *pulse = (W3DDynamicLight *)it.Peek_Obj();
+ if (!pulse->isEnabled() || pulse->Get_Type() != LightClass::POINT)
+ continue;
+
+ Real nearRange, farRange;
+ pulse->Get_Far_Attenuation_Range( nearRange, farRange );
+ if (farRange < 1.0f)
+ continue;
+
+ const Vector3 pos = pulse->Get_Position();
+ if (WWMath::Fabs( pos.X - view.Center.X ) > view.Extent.X + farRange
+ || WWMath::Fabs( pos.Y - view.Center.Y ) > view.Extent.Y + farRange
+ || WWMath::Fabs( pos.Z - view.Center.Z ) > view.Extent.Z + farRange)
+ continue;
+
+ Vector3 diffuse;
+ pulse->Get_Diffuse( &diffuse );
+ SmokeLight light;
+ light.x = pos.X;
+ light.y = pos.Y;
+ light.z = pos.Z;
+ light.radiusSq = farRange * farRange;
+ light.invRadiusSq = 1.0f / light.radiusSq;
+ light.r = diffuse.X * PULSE_LIGHT_GAIN;
+ light.g = diffuse.Y * PULSE_LIGHT_GAIN;
+ light.b = diffuse.Z * PULSE_LIGHT_GAIN;
+ light.strength = WWMath::Max( diffuse.X, WWMath::Max( diffuse.Y, diffuse.Z ) );
+ if (light.strength > 0.0f)
+ addSmokeLight( lights, count, light );
+ }
+}
+
+/** Fills lights[] with this frame's lights, strongest first, and returns how many. */
+static Int gatherSmokeLights( SmokeLight *lights, const AABoxClass &view )
+{
+ Int count = 0;
+ ParticleSystemManager::ParticleSystemList &systems = TheParticleSystemManager->getAllParticleSystems();
+ for (ParticleSystemManager::ParticleSystemListIt it = systems.begin(); it != systems.end(); ++it)
+ {
+ ParticleSystem *sys = *it;
+ if (sys && !sys->isUsingDrawables() && sys->getShaderType() == ParticleSystemInfo::ADDITIVE
+ && sys->getParticleCount() > 0)
+ gatherFireLight( sys, lights, count, view );
+ }
+ gatherPulseLights( lights, count, view );
+
+ for (Int i = 1; i < count; ++i)
+ {
+ const SmokeLight light = lights[ i ];
+ Int j = i;
+ for (; j > 0 && lights[ j - 1 ].strength < light.strength; --j)
+ lights[ j ] = lights[ j - 1 ];
+ lights[ j ] = light;
+ }
+ return count;
+}
+
+/** The strongest lights that reach an alpha system's particles, at most SMOKE_LIGHTS_PER_SYSTEM.
+ * Any other system gets none. */
+static Int pickSmokeLights( ParticleSystem *sys, const SmokeLight *lights, Int count, SmokeLight *picked )
+{
+ if (count == 0 || sys->getShaderType() != ParticleSystemInfo::ALPHA)
+ return 0;
+
+ Particle *p = sys->getFirstParticle();
+ if (p == NULL)
+ return 0;
+ Real minX = p->getPosition()->x, maxX = minX;
+ Real minY = p->getPosition()->y, maxY = minY;
+ Real minZ = p->getPosition()->z, maxZ = minZ;
+ for (p = p->m_systemNext; p; p = p->m_systemNext)
+ {
+ const Coord3D *pos = p->getPosition();
+ minX = WWMath::Min( minX, pos->x ); maxX = WWMath::Max( maxX, pos->x );
+ minY = WWMath::Min( minY, pos->y ); maxY = WWMath::Max( maxY, pos->y );
+ minZ = WWMath::Min( minZ, pos->z ); maxZ = WWMath::Max( maxZ, pos->z );
+ }
+
+ Int n = 0;
+ for (Int i = 0; i < count && n < SMOKE_LIGHTS_PER_SYSTEM; ++i)
+ {
+ const SmokeLight &l = lights[ i ];
+ const Real dx = l.x < minX ? minX - l.x : (l.x > maxX ? l.x - maxX : 0.0f);
+ const Real dy = l.y < minY ? minY - l.y : (l.y > maxY ? l.y - maxY : 0.0f);
+ const Real dz = l.z < minZ ? minZ - l.z : (l.z > maxZ ? l.z - maxZ : 0.0f);
+ if (dx * dx + dy * dy + dz * dz < l.radiusSq)
+ picked[ n++ ] = l;
+ }
+ return n;
+}
+
+/** A smoke particle's colour with the picked lights added: (1 - d^2/R^2)^2 of each light's colour,
+ * each channel raised by at most SMOKE_LIGHT_MAX_GLOW and the result clamped to 1. */
+static inline void lightSmoke( const Coord3D *pos, const SmokeLight *lights, Int count,
+ Real &red, Real &green, Real &blue )
+{
+ Real r = 0.0f, g = 0.0f, b = 0.0f;
+ for (Int i = 0; i < count; ++i)
+ {
+ const SmokeLight &l = lights[ i ];
+ const Real dx = pos->x - l.x, dy = pos->y - l.y, dz = pos->z - l.z;
+ const Real t = 1.0f - (dx * dx + dy * dy + dz * dz) * l.invRadiusSq;
+ if (t <= 0.0f)
+ continue;
+ const Real f = t * t;
+ r += f * l.r;
+ g += f * l.g;
+ b += f * l.b;
+ }
+ red = WWMath::Min( 1.0f, red + WWMath::Min( r, SMOKE_LIGHT_MAX_GLOW ) );
+ green = WWMath::Min( 1.0f, green + WWMath::Min( g, SMOKE_LIGHT_MAX_GLOW ) );
+ blue = WWMath::Min( 1.0f, blue + WWMath::Min( b, SMOKE_LIGHT_MAX_GLOW ) );
+}
+
/** What every job of one frame's billboard fill reads. */
struct BillboardFillJob
{
@@ -84,6 +312,8 @@ struct BillboardFillJob
Matrix4x4 view;
Real centerX, centerY, centerZ;
Real extentX, extentY, extentZ;
+ SmokeLight lights[ SMOKE_LIGHTS_MAX ]; ///< this frame's, strongest first
+ Int lightCount;
};
/// systems a pool thread claims at once; a system is a few hundred particles, so one claim a system
@@ -100,6 +330,8 @@ static void fillBillboards( Int index, void *context )
W3DParticleSystemManager::BillboardFill &fill = job->fills[ index ];
VertexFormatXYZNDUV2 *quad = fill.range.Vertices;
Int drawn = 0;
+ SmokeLight lights[ SMOKE_LIGHTS_PER_SYSTEM ];
+ const Int lightCount = pickSmokeLights( fill.system, job->lights, job->lightCount, lights );
for (Particle *p = fill.system->getFirstParticle(); p; p = p->m_systemNext)
{
@@ -117,8 +349,10 @@ static void fillBillboards( Int index, void *context )
continue;
const RGBColor *color = p->getColor();
- const unsigned packed = DX8Wrapper::Convert_Color_Clamp(
- Vector4( color->red, color->green, color->blue, p->getAlpha() ) );
+ Real red = color->red, green = color->green, blue = color->blue;
+ if (lightCount > 0)
+ lightSmoke( pos, lights, lightCount, red, green, blue );
+ const unsigned packed = DX8Wrapper::Convert_Color_Clamp( Vector4( red, green, blue, p->getAlpha() ) );
// The orientation table's index wraps, as it did on Windows (Platform/MsvcFloatCasts.h).
const uint8 orientation = floatToByteAsMsvc( p->getAngle() * 255.0f / (2.0f * PI) );
PointGroupClass::Write_Billboard( quad, job->view, Vector3( pos->x, pos->y, pos->z ), psize,
@@ -287,6 +521,7 @@ void W3DParticleSystemManager::doParticles(RenderInfoClass &rinfo)
fillJob.extentX = beX;
fillJob.extentY = beY;
fillJob.extentZ = beZ;
+ fillJob.lightCount = SMOKE_FIRE_LIGHTING ? gatherSmokeLights( fillJob.lights, bbox ) : 0;
JobSystem::parallel_for( (Int)m_billboardFills.size(), BILLBOARD_FILLS_PER_CLAIM, fillBillboards, &fillJob );
size_t nextFill = 0;
for( ParticleSystemManager::ParticleSystemListIt it = particleSysList.begin(); it != particleSysList.end(); ++it)
@@ -363,6 +598,8 @@ void W3DParticleSystemManager::doParticles(RenderInfoClass &rinfo)
// the same answer for every particle of the system, so asked once rather than once a particle
const UnsignedInt fieldParticleIncrement =
( sys->getPriority() == AREA_EFFECT && sys->m_isGroundAligned != FALSE ) ? 1 : 0;
+ SmokeLight lights[ SMOKE_LIGHTS_PER_SYSTEM ];
+ const Int lightCount = pickSmokeLights( sys, fillJob.lights, fillJob.lightCount, lights );
@@ -399,6 +636,8 @@ void W3DParticleSystemManager::doParticles(RenderInfoClass &rinfo)
RGBAArray[count].Y = color->green;
RGBAArray[count].Z = color->blue;
RGBAArray[count].W = p->getAlpha();
+ if (lightCount > 0)
+ lightSmoke( pos, lights, lightCount, RGBAArray[count].X, RGBAArray[count].Y, RGBAArray[count].Z );
angleArray[count] = floatToByteAsMsvc( p->getAngle() * 255.0f / (2.0f * PI) );