Muzzle Flash Lighting: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
(Complete revamp of this article.)
Line 23: Line 23:
el->color.exponent = 5;
el->color.exponent = 5;
</pre>
</pre>
As you may have noticed, Valve went a little crazy here and the result is a seemingly ugly muzzleflash. This tutorial is all about making things pretty, so we're going to change that. Replace the entries above with this:
As you may have noticed, Valve went a little crazy here and the result was a seemingly ugly muzzleflash. This tutorial is all about making things pretty, so we're going to change that. Replace the entries above with this:
<pre>
<pre>
dlight_t *dl = effects->CL_AllocDlight ( index );
dlight_t *dl = effects->CL_AllocDlight ( index );

Revision as of 10:22, 22 February 2009

This tutorial will show you how to create a muzzleflash that dynamically appears and lights up the world around you, similar to the ones found in Counter-Strike: Source. These types of light are called "dlights".

This code was designed for use with any Orange Box based mod, singleplayer or multiplayer. It also works with NPCs, assuming that you've gotten them to work properly.

Warning.pngWarning: Some dynamic props will not be lit when using dlights.
Warning.pngWarning: Too many may result in severe performance losses on low-end systems. When coding, dlights should only be used sparingly.

Implementing Dlight-based Muzzleflashes

The coding begins! Only a few minor changes are needed here to make your muzzleflashes look better than ever.

First off, open up c_baseanimating.cpp and search for the function void C_BaseAnimating::ProcessMuzzleFlashEvent(). Then, look at this little section of code:

// Make an elight
dlight_t *el = effects->CL_AllocElight( LIGHT_INDEX_MUZZLEFLASH + index );
el->origin = vAttachment;
el->radius = random->RandomInt( 32, 64 ); 
el->decay = el->radius / 0.05f;
el->die = gpGlobals->curtime + 0.05f;
el->color.r = 255;
el->color.g = 192;
el->color.b = 64;
el->color.exponent = 5;

As you may have noticed, Valve went a little crazy here and the result was a seemingly ugly muzzleflash. This tutorial is all about making things pretty, so we're going to change that. Replace the entries above with this:

			
dlight_t *dl = effects->CL_AllocDlight ( index );
dl->origin = vAttachment + Vector( 0, 0, 4 );
dl->color.r = 255;
dl->die = gpGlobals->curtime + 0.1f;
dl->radius = random->RandomFloat( 245.0f, 256.0f );
dl->color.g = dl->color.b = random->RandomInt( 95, 128 );

dlight_t *el= effects->CL_AllocElight( index );
el->origin = vAttachment;
el->color.r = 255;
el->color.g = dl->color.b = random->RandomInt( 95, 128 );
el->radius = random->RandomFloat( 260.0f, 290.0f );
el->die = gpGlobals->curtime + 0.1f;

This two-light system works far better than most any other single dlight system and fixes Valves mistakes.