doom3-bfg/neo/d3xp/SmokeParticles.cpp

501 lines
12 KiB
C++
Raw Normal View History

2012-11-26 18:58:24 +00:00
/*
===========================================================================
Doom 3 BFG Edition GPL Source Code
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
2012-11-26 18:58:24 +00:00
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
2012-11-26 18:58:24 +00:00
Doom 3 BFG Edition Source Code 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.
Doom 3 BFG Edition Source Code 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 Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#include "precompiled.h"
2012-11-26 18:58:24 +00:00
#pragma hdrstop
#include "Game_local.h"
static const char* smokeParticle_SnapshotName = "_SmokeParticle_Snapshot_";
2012-11-26 18:58:24 +00:00
/*
================
idSmokeParticles::idSmokeParticles
================
*/
idSmokeParticles::idSmokeParticles()
{
2012-11-26 18:58:24 +00:00
initialized = false;
memset( &renderEntity, 0, sizeof( renderEntity ) );
renderEntityHandle = -1;
memset( smokes, 0, sizeof( smokes ) );
freeSmokes = NULL;
numActiveSmokes = 0;
currentParticleTime = -1;
}
/*
================
idSmokeParticles::Init
================
*/
void idSmokeParticles::Init()
{
if( initialized )
{
2012-11-26 18:58:24 +00:00
Shutdown();
}
2012-11-26 18:58:24 +00:00
// set up the free list
for( int i = 0; i < MAX_SMOKE_PARTICLES - 1; i++ )
{
smokes[i].next = &smokes[i + 1];
2012-11-26 18:58:24 +00:00
}
smokes[MAX_SMOKE_PARTICLES - 1].next = NULL;
2012-11-26 18:58:24 +00:00
freeSmokes = &smokes[0];
numActiveSmokes = 0;
2012-11-26 18:58:24 +00:00
activeStages.Clear();
2012-11-26 18:58:24 +00:00
memset( &renderEntity, 0, sizeof( renderEntity ) );
2012-11-26 18:58:24 +00:00
renderEntity.bounds.Clear();
renderEntity.axis = mat3_identity;
renderEntity.shaderParms[ SHADERPARM_RED ] = 1;
renderEntity.shaderParms[ SHADERPARM_GREEN ] = 1;
renderEntity.shaderParms[ SHADERPARM_BLUE ] = 1;
renderEntity.shaderParms[3] = 1;
2012-11-26 18:58:24 +00:00
renderEntity.hModel = renderModelManager->AllocModel();
renderEntity.hModel->InitEmpty( smokeParticle_SnapshotName );
2012-11-26 18:58:24 +00:00
// we certainly don't want particle shadows
renderEntity.noShadow = 1;
2012-11-26 18:58:24 +00:00
// huge bounds, so it will be present in every world area
renderEntity.bounds.AddPoint( idVec3( -100000, -100000, -100000 ) );
renderEntity.bounds.AddPoint( idVec3( 100000, 100000, 100000 ) );
2012-11-26 18:58:24 +00:00
renderEntity.callback = idSmokeParticles::ModelCallback;
// add to renderer list
renderEntityHandle = gameRenderWorld->AddEntityDef( &renderEntity );
2012-11-26 18:58:24 +00:00
currentParticleTime = -1;
2012-11-26 18:58:24 +00:00
initialized = true;
}
/*
================
idSmokeParticles::Shutdown
================
*/
void idSmokeParticles::Shutdown()
{
2012-11-26 18:58:24 +00:00
// make sure the render entity is freed before the model is freed
if( renderEntityHandle != -1 )
{
2012-11-26 18:58:24 +00:00
gameRenderWorld->FreeEntityDef( renderEntityHandle );
renderEntityHandle = -1;
}
if( renderEntity.hModel != NULL )
{
2012-11-26 18:58:24 +00:00
renderModelManager->FreeModel( renderEntity.hModel );
renderEntity.hModel = NULL;
}
initialized = false;
}
/*
================
idSmokeParticles::FreeSmokes
================
*/
void idSmokeParticles::FreeSmokes()
{
for( int activeStageNum = 0; activeStageNum < activeStages.Num(); activeStageNum++ )
{
singleSmoke_t* smoke, *next, *last;
activeSmokeStage_t* active = &activeStages[activeStageNum];
const idParticleStage* stage = active->stage;
for( last = NULL, smoke = active->smokes; smoke; smoke = next )
{
2012-11-26 18:58:24 +00:00
next = smoke->next;
2012-11-26 18:58:24 +00:00
float frac;
if( smoke->timeGroup )
{
frac = ( float )( gameLocal.fast.time - smoke->privateStartTime ) / ( stage->particleLife * 1000 );
2012-11-26 18:58:24 +00:00
}
else
{
frac = ( float )( gameLocal.slow.time - smoke->privateStartTime ) / ( stage->particleLife * 1000 );
2012-11-26 18:58:24 +00:00
}
if( frac >= 1.0f )
{
2012-11-26 18:58:24 +00:00
// remove the particle from the stage list
if( last != NULL )
{
2012-11-26 18:58:24 +00:00
last->next = smoke->next;
}
else
{
2012-11-26 18:58:24 +00:00
active->smokes = smoke->next;
}
// put the particle on the free list
smoke->next = freeSmokes;
freeSmokes = smoke;
numActiveSmokes--;
continue;
}
2012-11-26 18:58:24 +00:00
last = smoke;
}
if( !active->smokes )
{
2012-11-26 18:58:24 +00:00
// remove this from the activeStages list
activeStages.RemoveIndex( activeStageNum );
activeStageNum--;
}
}
}
/*
================
idSmokeParticles::EmitSmoke
Called by game code to drop another particle into the list
================
*/
bool idSmokeParticles::EmitSmoke( const idDeclParticle* smoke, const int systemStartTime, const float diversity, const idVec3& origin, const idMat3& axis, int timeGroup /*_D3XP*/ )
{
2012-11-26 18:58:24 +00:00
bool continues = false;
SetTimeState ts( timeGroup );
if( !smoke )
{
2012-11-26 18:58:24 +00:00
return false;
}
if( !gameLocal.isNewFrame )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
// dedicated doesn't smoke. No UpdateRenderEntity, so they would not be freed
if( gameLocal.GetLocalClientNum() < 0 )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
assert( gameLocal.time == 0 || systemStartTime <= gameLocal.time );
if( systemStartTime > gameLocal.time )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
idRandom steppingRandom( 0xffff * diversity );
2012-11-26 18:58:24 +00:00
// for each stage in the smoke that is still emitting particles, emit a new singleSmoke_t
for( int stageNum = 0; stageNum < smoke->stages.Num(); stageNum++ )
{
const idParticleStage* stage = smoke->stages[stageNum];
if( !stage->cycleMsec )
{
2012-11-26 18:58:24 +00:00
continue;
}
if( !stage->material )
{
2012-11-26 18:58:24 +00:00
continue;
}
if( stage->particleLife <= 0 )
{
2012-11-26 18:58:24 +00:00
continue;
}
2012-11-26 18:58:24 +00:00
// see how many particles we should emit this tic
// FIXME: smoke.privateStartTime += stage->timeOffset;
int finalParticleTime = stage->cycleMsec * stage->spawnBunching;
int deltaMsec = gameLocal.time - systemStartTime;
2012-11-26 18:58:24 +00:00
int nowCount = 0, prevCount = 0;
if( finalParticleTime == 0 )
{
2012-11-26 18:58:24 +00:00
// if spawnBunching is 0, they will all come out at once
if( gameLocal.time == systemStartTime )
{
2012-11-26 18:58:24 +00:00
prevCount = -1;
nowCount = stage->totalParticles - 1;
}
else
{
2012-11-26 18:58:24 +00:00
prevCount = stage->totalParticles;
}
}
else
{
nowCount = floor( ( ( float )deltaMsec / finalParticleTime ) * stage->totalParticles );
if( nowCount >= stage->totalParticles )
{
nowCount = stage->totalParticles - 1;
2012-11-26 18:58:24 +00:00
}
prevCount = floor( ( ( float )( deltaMsec - ( gameLocal.time - gameLocal.previousTime ) ) / finalParticleTime ) * stage->totalParticles );
if( prevCount < -1 )
{
2012-11-26 18:58:24 +00:00
prevCount = -1;
}
}
if( prevCount >= stage->totalParticles )
{
2012-11-26 18:58:24 +00:00
// no more particles from this stage
continue;
}
if( nowCount < stage->totalParticles - 1 )
{
2012-11-26 18:58:24 +00:00
// the system will need to emit particles next frame as well
continues = true;
}
2012-11-26 18:58:24 +00:00
// find an activeSmokeStage that matches this
activeSmokeStage_t* active = NULL;
2012-11-26 18:58:24 +00:00
int i;
for( i = 0 ; i < activeStages.Num() ; i++ )
{
2012-11-26 18:58:24 +00:00
active = &activeStages[i];
if( active->stage == stage )
{
2012-11-26 18:58:24 +00:00
break;
}
}
if( i == activeStages.Num() )
{
2012-11-26 18:58:24 +00:00
// add a new one
activeSmokeStage_t newActive;
2012-11-26 18:58:24 +00:00
newActive.smokes = NULL;
newActive.stage = stage;
i = activeStages.Append( newActive );
active = &activeStages[i];
}
2012-11-26 18:58:24 +00:00
// add all the required particles
for( prevCount++ ; prevCount <= nowCount && active != NULL ; prevCount++ )
{
if( !freeSmokes )
{
2012-11-26 18:58:24 +00:00
gameLocal.Printf( "idSmokeParticles::EmitSmoke: no free smokes with %d active stages\n", activeStages.Num() );
return true;
}
singleSmoke_t* newSmoke = freeSmokes;
2012-11-26 18:58:24 +00:00
freeSmokes = freeSmokes->next;
numActiveSmokes++;
2012-11-26 18:58:24 +00:00
newSmoke->timeGroup = timeGroup;
newSmoke->index = prevCount;
newSmoke->axis = axis;
newSmoke->origin = origin;
newSmoke->random = steppingRandom;
newSmoke->privateStartTime = systemStartTime + prevCount * finalParticleTime / stage->totalParticles;
newSmoke->next = active->smokes;
active->smokes = newSmoke;
2012-11-26 18:58:24 +00:00
steppingRandom.RandomInt(); // advance the random
}
}
2012-11-26 18:58:24 +00:00
return continues;
}
/*
================
idSmokeParticles::UpdateRenderEntity
================
*/
bool idSmokeParticles::UpdateRenderEntity( renderEntity_s* renderEntity, const renderView_t* renderView )
{
2012-11-26 18:58:24 +00:00
// this may be triggered by a model trace or other non-view related source,
// to which we should look like an empty model
if( !renderView )
{
2012-11-26 18:58:24 +00:00
// FIXME: re-use model surfaces
renderEntity->hModel->InitEmpty( smokeParticle_SnapshotName );
return false;
}
2012-11-26 18:58:24 +00:00
// don't regenerate it if it is current
if( renderView->time[renderEntity->timeGroup] == currentParticleTime && !renderView->forceUpdate )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
// FIXME: re-use model surfaces
renderEntity->hModel->InitEmpty( smokeParticle_SnapshotName );
2012-11-26 18:58:24 +00:00
currentParticleTime = renderView->time[renderEntity->timeGroup];
2012-11-26 18:58:24 +00:00
particleGen_t g;
2012-11-26 18:58:24 +00:00
g.renderEnt = renderEntity;
g.renderView = renderView;
for( int activeStageNum = 0; activeStageNum < activeStages.Num(); activeStageNum++ )
{
singleSmoke_t* smoke, *next, *last;
activeSmokeStage_t* active = &activeStages[activeStageNum];
const idParticleStage* stage = active->stage;
if( !stage->material )
{
2012-11-26 18:58:24 +00:00
continue;
}
2012-11-26 18:58:24 +00:00
// allocate a srfTriangles that can hold all the particles
int count = 0;
for( smoke = active->smokes; smoke; smoke = smoke->next )
{
2012-11-26 18:58:24 +00:00
count++;
}
int quads = count * stage->NumQuadsPerParticle();
srfTriangles_t* tri = renderEntity->hModel->AllocSurfaceTriangles( quads * 4, quads * 6 );
2012-11-26 18:58:24 +00:00
tri->numIndexes = quads * 6;
tri->numVerts = quads * 4;
2012-11-26 18:58:24 +00:00
// just always draw the particles
tri->bounds[0][0] =
tri->bounds[0][1] =
tri->bounds[0][2] = -99999;
2012-11-26 18:58:24 +00:00
tri->bounds[1][0] =
tri->bounds[1][1] =
tri->bounds[1][2] = 99999;
2012-11-26 18:58:24 +00:00
tri->numVerts = 0;
for( last = NULL, smoke = active->smokes; smoke; smoke = next )
{
2012-11-26 18:58:24 +00:00
next = smoke->next;
if( smoke->timeGroup )
{
g.frac = ( float )( gameLocal.fast.time - smoke->privateStartTime ) / ( stage->particleLife * 1000 );
2012-11-26 18:58:24 +00:00
}
else
{
g.frac = ( float )( gameLocal.time - smoke->privateStartTime ) / ( stage->particleLife * 1000 );
2012-11-26 18:58:24 +00:00
}
if( g.frac >= 1.0f )
{
2012-11-26 18:58:24 +00:00
// remove the particle from the stage list
if( last != NULL )
{
2012-11-26 18:58:24 +00:00
last->next = smoke->next;
}
else
{
2012-11-26 18:58:24 +00:00
active->smokes = smoke->next;
}
// put the particle on the free list
smoke->next = freeSmokes;
freeSmokes = smoke;
numActiveSmokes--;
continue;
}
2012-11-26 18:58:24 +00:00
g.index = smoke->index;
g.random = smoke->random;
2012-11-26 18:58:24 +00:00
g.origin = smoke->origin;
g.axis = smoke->axis;
2012-11-26 18:58:24 +00:00
g.originalRandom = g.random;
g.age = g.frac * stage->particleLife;
2012-11-26 18:58:24 +00:00
tri->numVerts += stage->CreateParticle( &g, tri->verts + tri->numVerts );
2012-11-26 18:58:24 +00:00
last = smoke;
}
if( tri->numVerts > quads * 4 )
{
2012-11-26 18:58:24 +00:00
gameLocal.Error( "idSmokeParticles::UpdateRenderEntity: miscounted verts" );
}
if( tri->numVerts == 0 )
{
2012-11-26 18:58:24 +00:00
// they were all removed
renderEntity->hModel->FreeSurfaceTriangles( tri );
if( !active->smokes )
{
2012-11-26 18:58:24 +00:00
// remove this from the activeStages list
activeStages.RemoveIndex( activeStageNum );
activeStageNum--;
}
}
else
{
2012-11-26 18:58:24 +00:00
// build the index list
int indexes = 0;
for( int i = 0 ; i < tri->numVerts ; i += 4 )
{
tri->indexes[indexes + 0] = i;
tri->indexes[indexes + 1] = i + 2;
tri->indexes[indexes + 2] = i + 3;
tri->indexes[indexes + 3] = i;
tri->indexes[indexes + 4] = i + 3;
tri->indexes[indexes + 5] = i + 1;
2012-11-26 18:58:24 +00:00
indexes += 6;
}
tri->numIndexes = indexes;
2012-11-26 18:58:24 +00:00
modelSurface_t surf;
surf.geometry = tri;
surf.shader = stage->material;
surf.id = 0;
2012-11-26 18:58:24 +00:00
renderEntity->hModel->AddSurface( surf );
}
}
return true;
}
/*
================
idSmokeParticles::ModelCallback
================
*/
bool idSmokeParticles::ModelCallback( renderEntity_s* renderEntity, const renderView_t* renderView )
{
2012-11-26 18:58:24 +00:00
// update the particles
if( gameLocal.smokeParticles )
{
2012-11-26 18:58:24 +00:00
return gameLocal.smokeParticles->UpdateRenderEntity( renderEntity, renderView );
}
2012-11-26 18:58:24 +00:00
return true;
}