RenderWorld_portals.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code 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. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. 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.
  17. 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.
  18. ===========================================================================
  19. */
  20. #pragma hdrstop
  21. #include "../idlib/precompiled.h"
  22. #include "tr_local.h"
  23. // if we hit this many planes, we will just stop cropping the
  24. // view down, which is still correct, just conservative
  25. const int MAX_PORTAL_PLANES = 20;
  26. struct portalStack_t {
  27. const portal_t * p;
  28. const portalStack_t * next;
  29. // positive side is outside the visible frustum
  30. int numPortalPlanes;
  31. idPlane portalPlanes[MAX_PORTAL_PLANES+1];
  32. idScreenRect rect;
  33. };
  34. /*
  35. =======================================================================
  36. Create viewLights and viewEntitys for the lights and entities that are
  37. visible in the portal areas that can be seen from the current viewpoint.
  38. =======================================================================
  39. */
  40. /*
  41. =============
  42. R_SetLightDefViewLight
  43. If the lightDef is not already on the viewLight list, create
  44. a viewLight and add it to the list with an empty scissor rect.
  45. =============
  46. */
  47. viewLight_t *R_SetLightDefViewLight( idRenderLightLocal *light ) {
  48. if ( light->viewCount == tr.viewCount ) {
  49. // already set up for this frame
  50. return light->viewLight;
  51. }
  52. light->viewCount = tr.viewCount;
  53. // add to the view light chain
  54. viewLight_t * vLight = (viewLight_t *)R_ClearedFrameAlloc( sizeof( *vLight ), FRAME_ALLOC_VIEW_LIGHT );
  55. vLight->lightDef = light;
  56. // the scissorRect will be expanded as the light bounds is accepted into visible portal chains
  57. // and the scissor will be reduced in R_AddSingleLight based on the screen space projection
  58. vLight->scissorRect.Clear();
  59. // link the view light
  60. vLight->next = tr.viewDef->viewLights;
  61. tr.viewDef->viewLights = vLight;
  62. light->viewLight = vLight;
  63. return vLight;
  64. }
  65. /*
  66. =============
  67. R_SetEntityDefViewEntity
  68. If the entityDef is not already on the viewEntity list, create
  69. a viewEntity and add it to the list with an empty scissor rect.
  70. =============
  71. */
  72. viewEntity_t *R_SetEntityDefViewEntity( idRenderEntityLocal *def ) {
  73. if ( def->viewCount == tr.viewCount ) {
  74. // already set up for this frame
  75. return def->viewEntity;
  76. }
  77. def->viewCount = tr.viewCount;
  78. viewEntity_t * vModel = (viewEntity_t *)R_ClearedFrameAlloc( sizeof( *vModel ), FRAME_ALLOC_VIEW_ENTITY );
  79. vModel->entityDef = def;
  80. // the scissorRect will be expanded as the model bounds is accepted into visible portal chains
  81. // It will remain clear if the model is only needed for shadows.
  82. vModel->scissorRect.Clear();
  83. vModel->next = tr.viewDef->viewEntitys;
  84. tr.viewDef->viewEntitys = vModel;
  85. def->viewEntity = vModel;
  86. return vModel;
  87. }
  88. /*
  89. ================
  90. CullEntityByPortals
  91. Return true if the entity reference bounds do not intersect the current portal chain.
  92. ================
  93. */
  94. bool idRenderWorldLocal::CullEntityByPortals( const idRenderEntityLocal *entity, const portalStack_t *ps ) {
  95. if ( r_useEntityPortalCulling.GetInteger() == 1 ) {
  96. ALIGNTYPE16 frustumCorners_t corners;
  97. idRenderMatrix::GetFrustumCorners( corners, entity->inverseBaseModelProject, bounds_unitCube );
  98. for ( int i = 0; i < ps->numPortalPlanes; i++ ) {
  99. if ( idRenderMatrix::CullFrustumCornersToPlane( corners, ps->portalPlanes[i] ) == FRUSTUM_CULL_FRONT ) {
  100. return true;
  101. }
  102. }
  103. } else if ( r_useEntityPortalCulling.GetInteger() >= 2 ) {
  104. idRenderMatrix baseModelProject;
  105. idRenderMatrix::Inverse( entity->inverseBaseModelProject, baseModelProject );
  106. idPlane frustumPlanes[6];
  107. idRenderMatrix::GetFrustumPlanes( frustumPlanes, baseModelProject, false, true );
  108. // exact clip of light faces against all planes
  109. for ( int i = 0; i < 6; i++ ) {
  110. // the entity frustum planes face inward, so the planes that have the
  111. // view origin on the positive side will be the "back" faces of the entity,
  112. // which must have some fragment inside the portal stack planes to be visible
  113. if ( frustumPlanes[i].Distance( tr.viewDef->renderView.vieworg ) <= 0.0f ) {
  114. continue;
  115. }
  116. // calculate a winding for this frustum side
  117. idFixedWinding w;
  118. w.BaseForPlane( frustumPlanes[i] );
  119. for ( int j = 0; j < 6; j++ ) {
  120. if ( j == i ) {
  121. continue;
  122. }
  123. if ( !w.ClipInPlace( frustumPlanes[j], ON_EPSILON ) ) {
  124. break;
  125. }
  126. }
  127. if ( w.GetNumPoints() <= 2 ) {
  128. continue;
  129. }
  130. assert( ps->numPortalPlanes <= MAX_PORTAL_PLANES );
  131. assert( w.GetNumPoints() + ps->numPortalPlanes < MAX_POINTS_ON_WINDING );
  132. // now clip the winding against each of the portalStack planes
  133. // skip the last plane which is the last portal itself
  134. for ( int j = 0; j < ps->numPortalPlanes - 1; j++ ) {
  135. if ( !w.ClipInPlace( -ps->portalPlanes[j], ON_EPSILON ) ) {
  136. break;
  137. }
  138. }
  139. if ( w.GetNumPoints() > 2 ) {
  140. // part of the winding is visible through the portalStack,
  141. // so the entity is not culled
  142. return false;
  143. }
  144. }
  145. // nothing was visible
  146. return true;
  147. }
  148. return false;
  149. }
  150. /*
  151. ===================
  152. AddAreaViewEntities
  153. Any models that are visible through the current portalStack will have their scissor rect updated.
  154. ===================
  155. */
  156. void idRenderWorldLocal::AddAreaViewEntities( int areaNum, const portalStack_t *ps ) {
  157. portalArea_t * area = &portalAreas[ areaNum ];
  158. for ( areaReference_t * ref = area->entityRefs.areaNext; ref != &area->entityRefs; ref = ref->areaNext ) {
  159. idRenderEntityLocal * entity = ref->entity;
  160. // debug tool to allow viewing of only one entity at a time
  161. if ( r_singleEntity.GetInteger() >= 0 && r_singleEntity.GetInteger() != entity->index ) {
  162. continue;
  163. }
  164. // remove decals that are completely faded away
  165. R_FreeEntityDefFadedDecals( entity, tr.viewDef->renderView.time[0] );
  166. // check for completely suppressing the model
  167. if ( !r_skipSuppress.GetBool() ) {
  168. if ( entity->parms.suppressSurfaceInViewID
  169. && entity->parms.suppressSurfaceInViewID == tr.viewDef->renderView.viewID ) {
  170. continue;
  171. }
  172. if ( entity->parms.allowSurfaceInViewID
  173. && entity->parms.allowSurfaceInViewID != tr.viewDef->renderView.viewID ) {
  174. continue;
  175. }
  176. }
  177. // cull reference bounds
  178. if ( CullEntityByPortals( entity, ps ) ) {
  179. // we are culled out through this portal chain, but it might
  180. // still be visible through others
  181. continue;
  182. }
  183. viewEntity_t * vEnt = R_SetEntityDefViewEntity( entity );
  184. // possibly expand the scissor rect
  185. vEnt->scissorRect.Union( ps->rect );
  186. }
  187. }
  188. /*
  189. ================
  190. CullLightByPortals
  191. Return true if the light frustum does not intersect the current portal chain.
  192. ================
  193. */
  194. bool idRenderWorldLocal::CullLightByPortals( const idRenderLightLocal *light, const portalStack_t *ps ) {
  195. if ( r_useLightPortalCulling.GetInteger() == 1 ) {
  196. ALIGNTYPE16 frustumCorners_t corners;
  197. idRenderMatrix::GetFrustumCorners( corners, light->inverseBaseLightProject, bounds_zeroOneCube );
  198. for ( int i = 0; i < ps->numPortalPlanes; i++ ) {
  199. if ( idRenderMatrix::CullFrustumCornersToPlane( corners, ps->portalPlanes[i] ) == FRUSTUM_CULL_FRONT ) {
  200. return true;
  201. }
  202. }
  203. } else if ( r_useLightPortalCulling.GetInteger() >= 2 ) {
  204. idPlane frustumPlanes[6];
  205. idRenderMatrix::GetFrustumPlanes( frustumPlanes, light->baseLightProject, true, true );
  206. // exact clip of light faces against all planes
  207. for ( int i = 0; i < 6; i++ ) {
  208. // the light frustum planes face inward, so the planes that have the
  209. // view origin on the positive side will be the "back" faces of the light,
  210. // which must have some fragment inside the the portal stack planes to be visible
  211. if ( frustumPlanes[i].Distance( tr.viewDef->renderView.vieworg ) <= 0.0f ) {
  212. continue;
  213. }
  214. // calculate a winding for this frustum side
  215. idFixedWinding w;
  216. w.BaseForPlane( frustumPlanes[i] );
  217. for ( int j = 0; j < 6; j++ ) {
  218. if ( j == i ) {
  219. continue;
  220. }
  221. if ( !w.ClipInPlace( frustumPlanes[j], ON_EPSILON ) ) {
  222. break;
  223. }
  224. }
  225. if ( w.GetNumPoints() <= 2 ) {
  226. continue;
  227. }
  228. assert( ps->numPortalPlanes <= MAX_PORTAL_PLANES );
  229. assert( w.GetNumPoints() + ps->numPortalPlanes < MAX_POINTS_ON_WINDING );
  230. // now clip the winding against each of the portalStack planes
  231. // skip the last plane which is the last portal itself
  232. for ( int j = 0; j < ps->numPortalPlanes - 1; j++ ) {
  233. if ( !w.ClipInPlace( -ps->portalPlanes[j], ON_EPSILON ) ) {
  234. break;
  235. }
  236. }
  237. if ( w.GetNumPoints() > 2 ) {
  238. // part of the winding is visible through the portalStack,
  239. // so the light is not culled
  240. return false;
  241. }
  242. }
  243. // nothing was visible
  244. return true;
  245. }
  246. return false;
  247. }
  248. /*
  249. ===================
  250. AddAreaViewLights
  251. This is the only point where lights get added to the viewLights list.
  252. Any lights that are visible through the current portalStack will have their scissor rect updated.
  253. ===================
  254. */
  255. void idRenderWorldLocal::AddAreaViewLights( int areaNum, const portalStack_t *ps ) {
  256. portalArea_t * area = &portalAreas[ areaNum ];
  257. for ( areaReference_t * lref = area->lightRefs.areaNext; lref != &area->lightRefs; lref = lref->areaNext ) {
  258. idRenderLightLocal * light = lref->light;
  259. // debug tool to allow viewing of only one light at a time
  260. if ( r_singleLight.GetInteger() >= 0 && r_singleLight.GetInteger() != light->index ) {
  261. continue;
  262. }
  263. // check for being closed off behind a door
  264. // a light that doesn't cast shadows will still light even if it is behind a door
  265. if ( r_useLightAreaCulling.GetBool() && !light->LightCastsShadows()
  266. && light->areaNum != -1 && !tr.viewDef->connectedAreas[ light->areaNum ] ) {
  267. continue;
  268. }
  269. // cull frustum
  270. if ( CullLightByPortals( light, ps ) ) {
  271. // we are culled out through this portal chain, but it might
  272. // still be visible through others
  273. continue;
  274. }
  275. viewLight_t * vLight = R_SetLightDefViewLight( light );
  276. // expand the scissor rect
  277. vLight->scissorRect.Union( ps->rect );
  278. }
  279. }
  280. /*
  281. ===================
  282. AddAreaToView
  283. This may be entered multiple times with different planes
  284. if more than one portal sees into the area
  285. ===================
  286. */
  287. void idRenderWorldLocal::AddAreaToView( int areaNum, const portalStack_t *ps ) {
  288. // mark the viewCount, so r_showPortals can display the considered portals
  289. portalAreas[ areaNum ].viewCount = tr.viewCount;
  290. // add the models and lights, using more precise culling to the planes
  291. AddAreaViewEntities( areaNum, ps );
  292. AddAreaViewLights( areaNum, ps );
  293. }
  294. /*
  295. ===================
  296. idRenderWorldLocal::ScreenRectForWinding
  297. ===================
  298. */
  299. idScreenRect idRenderWorldLocal::ScreenRectFromWinding( const idWinding * w, const viewEntity_t * space ) {
  300. const float viewWidth = (float) tr.viewDef->viewport.x2 - (float) tr.viewDef->viewport.x1;
  301. const float viewHeight = (float) tr.viewDef->viewport.y2 - (float) tr.viewDef->viewport.y1;
  302. idScreenRect r;
  303. r.Clear();
  304. for ( int i = 0; i < w->GetNumPoints(); i++ ) {
  305. idVec3 v;
  306. idVec3 ndc;
  307. R_LocalPointToGlobal( space->modelMatrix, (*w)[i].ToVec3(), v );
  308. R_GlobalToNormalizedDeviceCoordinates( v, ndc );
  309. float windowX = ( ndc[0] * 0.5f + 0.5f ) * viewWidth;
  310. float windowY = ( ndc[1] * 0.5f + 0.5f ) * viewHeight;
  311. r.AddPoint( windowX, windowY );
  312. }
  313. r.Expand();
  314. return r;
  315. }
  316. /*
  317. ===================
  318. idRenderWorldLocal::PortalIsFoggedOut
  319. ===================
  320. */
  321. bool idRenderWorldLocal::PortalIsFoggedOut( const portal_t *p ) {
  322. idRenderLightLocal * ldef = p->doublePortal->fogLight;
  323. if ( ldef == NULL ) {
  324. return false;
  325. }
  326. // find the current density of the fog
  327. const idMaterial * lightShader = ldef->lightShader;
  328. const int size = lightShader->GetNumRegisters() * sizeof( float );
  329. float * regs = (float *)_alloca( size );
  330. lightShader->EvaluateRegisters( regs, ldef->parms.shaderParms,
  331. tr.viewDef->renderView.shaderParms, tr.viewDef->renderView.time[0] * 0.001f, ldef->parms.referenceSound );
  332. const shaderStage_t *stage = lightShader->GetStage(0);
  333. const float alpha = regs[ stage->color.registers[3] ];
  334. // if they left the default value on, set a fog distance of 500
  335. float a;
  336. if ( alpha <= 1.0f ) {
  337. a = -0.5f / DEFAULT_FOG_DISTANCE;
  338. } else {
  339. // otherwise, distance = alpha color
  340. a = -0.5f / alpha;
  341. }
  342. idPlane forward;
  343. forward[0] = a * tr.viewDef->worldSpace.modelViewMatrix[0*4+2];
  344. forward[1] = a * tr.viewDef->worldSpace.modelViewMatrix[1*4+2];
  345. forward[2] = a * tr.viewDef->worldSpace.modelViewMatrix[2*4+2];
  346. forward[3] = a * tr.viewDef->worldSpace.modelViewMatrix[3*4+2];
  347. const idWinding * w = p->w;
  348. for ( int i = 0; i < w->GetNumPoints(); i++ ) {
  349. const float d = forward.Distance( (*w)[i].ToVec3() );
  350. if ( d < 0.5f ) {
  351. return false; // a point not clipped off
  352. }
  353. }
  354. return true;
  355. }
  356. /*
  357. ===================
  358. idRenderWorldLocal::FloodViewThroughArea_r
  359. ===================
  360. */
  361. void idRenderWorldLocal::FloodViewThroughArea_r( const idVec3 & origin, int areaNum, const portalStack_t *ps ) {
  362. portalArea_t * area = &portalAreas[ areaNum ];
  363. // cull models and lights to the current collection of planes
  364. AddAreaToView( areaNum, ps );
  365. if ( areaScreenRect[areaNum].IsEmpty() ) {
  366. areaScreenRect[areaNum] = ps->rect;
  367. } else {
  368. areaScreenRect[areaNum].Union( ps->rect );
  369. }
  370. // go through all the portals
  371. for ( const portal_t * p = area->portals; p != NULL; p = p->next ) {
  372. // an enclosing door may have sealed the portal off
  373. if ( p->doublePortal->blockingBits & PS_BLOCK_VIEW ) {
  374. continue;
  375. }
  376. // make sure this portal is facing away from the view
  377. const float d = p->plane.Distance( origin );
  378. if ( d < -0.1f ) {
  379. continue;
  380. }
  381. // make sure the portal isn't in our stack trace,
  382. // which would cause an infinite loop
  383. const portalStack_t * check = ps;
  384. for ( ; check != NULL; check = check->next ) {
  385. if ( check->p == p ) {
  386. break; // don't recursively enter a stack
  387. }
  388. }
  389. if ( check ) {
  390. continue; // already in stack
  391. }
  392. // if we are very close to the portal surface, don't bother clipping
  393. // it, which tends to give epsilon problems that make the area vanish
  394. if ( d < 1.0f ) {
  395. // go through this portal
  396. portalStack_t newStack;
  397. newStack = *ps;
  398. newStack.p = p;
  399. newStack.next = ps;
  400. FloodViewThroughArea_r( origin, p->intoArea, &newStack );
  401. continue;
  402. }
  403. // clip the portal winding to all of the planes
  404. idFixedWinding w; // we won't overflow because MAX_PORTAL_PLANES = 20
  405. w = *p->w;
  406. for ( int j = 0; j < ps->numPortalPlanes; j++ ) {
  407. if ( !w.ClipInPlace( -ps->portalPlanes[j], 0 ) ) {
  408. break;
  409. }
  410. }
  411. if ( !w.GetNumPoints() ) {
  412. continue; // portal not visible
  413. }
  414. // see if it is fogged out
  415. if ( PortalIsFoggedOut( p ) ) {
  416. continue;
  417. }
  418. // go through this portal
  419. portalStack_t newStack;
  420. newStack.p = p;
  421. newStack.next = ps;
  422. // find the screen pixel bounding box of the remaining portal
  423. // so we can scissor things outside it
  424. newStack.rect = ScreenRectFromWinding( &w, &tr.identitySpace );
  425. // slop might have spread it a pixel outside, so trim it back
  426. newStack.rect.Intersect( ps->rect );
  427. // generate a set of clipping planes that will further restrict
  428. // the visible view beyond just the scissor rect
  429. int addPlanes = w.GetNumPoints();
  430. if ( addPlanes > MAX_PORTAL_PLANES ) {
  431. addPlanes = MAX_PORTAL_PLANES;
  432. }
  433. newStack.numPortalPlanes = 0;
  434. for ( int i = 0; i < addPlanes; i++ ) {
  435. int j = i + 1;
  436. if ( j == w.GetNumPoints() ) {
  437. j = 0;
  438. }
  439. const idVec3 & v1 = origin - w[i].ToVec3();
  440. const idVec3 & v2 = origin - w[j].ToVec3();
  441. newStack.portalPlanes[newStack.numPortalPlanes].Normal().Cross( v2, v1 );
  442. // if it is degenerate, skip the plane
  443. if ( newStack.portalPlanes[newStack.numPortalPlanes].Normalize() < 0.01f ) {
  444. continue;
  445. }
  446. newStack.portalPlanes[newStack.numPortalPlanes].FitThroughPoint( origin );
  447. newStack.numPortalPlanes++;
  448. }
  449. // the last stack plane is the portal plane
  450. newStack.portalPlanes[newStack.numPortalPlanes] = p->plane;
  451. newStack.numPortalPlanes++;
  452. FloodViewThroughArea_r( origin, p->intoArea, &newStack );
  453. }
  454. }
  455. /*
  456. =======================
  457. idRenderWorldLocal::FlowViewThroughPortals
  458. Finds viewLights and viewEntities by flowing from an origin through the visible
  459. portals that the origin point can see into. The planes array defines a volume with
  460. the planes pointing outside the volume. Zero planes assumes an unbounded volume.
  461. =======================
  462. */
  463. void idRenderWorldLocal::FlowViewThroughPortals( const idVec3 & origin, int numPlanes, const idPlane *planes ) {
  464. portalStack_t ps;
  465. ps.next = NULL;
  466. ps.p = NULL;
  467. assert( numPlanes <= MAX_PORTAL_PLANES );
  468. for ( int i = 0; i < numPlanes; i++ ) {
  469. ps.portalPlanes[i] = planes[i];
  470. }
  471. ps.numPortalPlanes = numPlanes;
  472. ps.rect = tr.viewDef->scissor;
  473. // if outside the world, mark everything
  474. if ( tr.viewDef->areaNum < 0 ){
  475. for ( int i = 0; i < numPortalAreas; i++ ) {
  476. areaScreenRect[i] = tr.viewDef->scissor;
  477. AddAreaToView( i, &ps );
  478. }
  479. } else {
  480. // flood out through portals, setting area viewCount
  481. FloodViewThroughArea_r( origin, tr.viewDef->areaNum, &ps );
  482. }
  483. }
  484. /*
  485. ===================
  486. idRenderWorldLocal::BuildConnectedAreas_r
  487. ===================
  488. */
  489. void idRenderWorldLocal::BuildConnectedAreas_r( int areaNum ) {
  490. if ( tr.viewDef->connectedAreas[areaNum] ) {
  491. return;
  492. }
  493. tr.viewDef->connectedAreas[areaNum] = true;
  494. // flood through all non-blocked portals
  495. portalArea_t * area = &portalAreas[ areaNum ];
  496. for ( const portal_t * portal = area->portals; portal; portal = portal->next ) {
  497. if ( ( portal->doublePortal->blockingBits & PS_BLOCK_VIEW ) == 0 ) {
  498. BuildConnectedAreas_r( portal->intoArea );
  499. }
  500. }
  501. }
  502. /*
  503. ===================
  504. idRenderWorldLocal::BuildConnectedAreas
  505. This is only valid for a given view, not all views in a frame
  506. ===================
  507. */
  508. void idRenderWorldLocal::BuildConnectedAreas() {
  509. tr.viewDef->connectedAreas = (bool *)R_FrameAlloc( numPortalAreas * sizeof( tr.viewDef->connectedAreas[0] ) );
  510. // if we are outside the world, we can see all areas
  511. if ( tr.viewDef->areaNum == -1 ) {
  512. for ( int i = 0; i < numPortalAreas; i++ ) {
  513. tr.viewDef->connectedAreas[i] = true;
  514. }
  515. return;
  516. }
  517. // start with none visible, and flood fill from the current area
  518. memset( tr.viewDef->connectedAreas, 0, numPortalAreas * sizeof( tr.viewDef->connectedAreas[0] ) );
  519. BuildConnectedAreas_r( tr.viewDef->areaNum );
  520. }
  521. /*
  522. =============
  523. idRenderWorldLocal::FindViewLightsAndEntites
  524. All the modelrefs and lightrefs that are in visible areas
  525. will have viewEntitys and viewLights created for them.
  526. The scissorRects on the viewEntitys and viewLights may be empty if
  527. they were considered, but not actually visible.
  528. Entities and lights can have cached viewEntities / viewLights that
  529. will be used if the viewCount variable matches.
  530. =============
  531. */
  532. void idRenderWorldLocal::FindViewLightsAndEntities() {
  533. SCOPED_PROFILE_EVENT( "FindViewLightsAndEntities" );
  534. // bumping this counter invalidates cached viewLights / viewEntities,
  535. // when a light or entity is next considered, it will create a new
  536. // viewLight / viewEntity
  537. tr.viewCount++;
  538. // clear the visible lightDef and entityDef lists
  539. tr.viewDef->viewLights = NULL;
  540. tr.viewDef->viewEntitys = NULL;
  541. // all areas are initially not visible, but each portal
  542. // chain that leads to them will expand the visible rectangle
  543. for ( int i = 0; i < numPortalAreas; i++ ) {
  544. areaScreenRect[i].Clear();
  545. }
  546. // find the area to start the portal flooding in
  547. if ( !r_usePortals.GetBool() ) {
  548. // debug tool to force no portal culling
  549. tr.viewDef->areaNum = -1;
  550. } else {
  551. tr.viewDef->areaNum = PointInArea( tr.viewDef->initialViewAreaOrigin );
  552. }
  553. // determine all possible connected areas for
  554. // light-behind-door culling
  555. BuildConnectedAreas();
  556. // flow through all the portals and add models / lights
  557. if ( r_singleArea.GetBool() ) {
  558. // if debugging, only mark this area
  559. // if we are outside the world, don't draw anything
  560. if ( tr.viewDef->areaNum >= 0 ) {
  561. static int lastPrintedAreaNum;
  562. if ( tr.viewDef->areaNum != lastPrintedAreaNum ) {
  563. lastPrintedAreaNum = tr.viewDef->areaNum;
  564. common->Printf( "entering portal area %i\n", tr.viewDef->areaNum );
  565. }
  566. portalStack_t ps;
  567. for ( int i = 0; i < 5; i++ ) {
  568. ps.portalPlanes[i] = tr.viewDef->frustum[i];
  569. }
  570. ps.numPortalPlanes = 5;
  571. ps.rect = tr.viewDef->scissor;
  572. AddAreaToView( tr.viewDef->areaNum, &ps );
  573. }
  574. } else {
  575. // note that the center of projection for flowing through portals may
  576. // be a different point than initialViewAreaOrigin for subviews that
  577. // may have the viewOrigin in a solid/invalid area
  578. FlowViewThroughPortals( tr.viewDef->renderView.vieworg, 5, tr.viewDef->frustum );
  579. }
  580. }
  581. /*
  582. =======================================================================
  583. Light linking into the BSP tree by flooding through portals
  584. =======================================================================
  585. */
  586. /*
  587. ===================
  588. idRenderWorldLocal::FloodLightThroughArea_r
  589. ===================
  590. */
  591. void idRenderWorldLocal::FloodLightThroughArea_r( idRenderLightLocal *light, int areaNum,
  592. const portalStack_t *ps ) {
  593. assert( ps != NULL ); // compiler warning
  594. portal_t* p = NULL;
  595. float d;
  596. portalArea_t * area = NULL;
  597. const portalStack_t *check = NULL, *firstPortalStack = NULL;
  598. portalStack_t newStack;
  599. int i, j;
  600. idVec3 v1, v2;
  601. int addPlanes;
  602. idFixedWinding w; // we won't overflow because MAX_PORTAL_PLANES = 20
  603. area = &portalAreas[ areaNum ];
  604. // add an areaRef
  605. AddLightRefToArea( light, area );
  606. // go through all the portals
  607. for ( p = area->portals; p; p = p->next ) {
  608. // make sure this portal is facing away from the view
  609. d = p->plane.Distance( light->globalLightOrigin );
  610. if ( d < -0.1f ) {
  611. continue;
  612. }
  613. // make sure the portal isn't in our stack trace,
  614. // which would cause an infinite loop
  615. for ( check = ps; check; check = check->next ) {
  616. firstPortalStack = check;
  617. if ( check->p == p ) {
  618. break; // don't recursively enter a stack
  619. }
  620. }
  621. if ( check ) {
  622. continue; // already in stack
  623. }
  624. // if we are very close to the portal surface, don't bother clipping
  625. // it, which tends to give epsilon problems that make the area vanish
  626. if ( d < 1.0f ) {
  627. // go through this portal
  628. newStack = *ps;
  629. newStack.p = p;
  630. newStack.next = ps;
  631. FloodLightThroughArea_r( light, p->intoArea, &newStack );
  632. continue;
  633. }
  634. // clip the portal winding to all of the planes
  635. w = *p->w;
  636. for ( j = 0; j < ps->numPortalPlanes; j++ ) {
  637. if ( !w.ClipInPlace( -ps->portalPlanes[j], 0 ) ) {
  638. break;
  639. }
  640. }
  641. if ( !w.GetNumPoints() ) {
  642. continue; // portal not visible
  643. }
  644. // also always clip to the original light planes, because they aren't
  645. // necessarily extending to infinitiy like a view frustum
  646. for ( j = 0; j < firstPortalStack->numPortalPlanes; j++ ) {
  647. if ( !w.ClipInPlace( -firstPortalStack->portalPlanes[j], 0 ) ) {
  648. break;
  649. }
  650. }
  651. if ( !w.GetNumPoints() ) {
  652. continue; // portal not visible
  653. }
  654. // go through this portal
  655. newStack.p = p;
  656. newStack.next = ps;
  657. // generate a set of clipping planes that will further restrict
  658. // the visible view beyond just the scissor rect
  659. addPlanes = w.GetNumPoints();
  660. if ( addPlanes > MAX_PORTAL_PLANES ) {
  661. addPlanes = MAX_PORTAL_PLANES;
  662. }
  663. newStack.numPortalPlanes = 0;
  664. for ( i = 0; i < addPlanes; i++ ) {
  665. j = i+1;
  666. if ( j == w.GetNumPoints() ) {
  667. j = 0;
  668. }
  669. v1 = light->globalLightOrigin - w[i].ToVec3();
  670. v2 = light->globalLightOrigin - w[j].ToVec3();
  671. newStack.portalPlanes[newStack.numPortalPlanes].Normal().Cross( v2, v1 );
  672. // if it is degenerate, skip the plane
  673. if ( newStack.portalPlanes[newStack.numPortalPlanes].Normalize() < 0.01f ) {
  674. continue;
  675. }
  676. newStack.portalPlanes[newStack.numPortalPlanes].FitThroughPoint( light->globalLightOrigin );
  677. newStack.numPortalPlanes++;
  678. }
  679. FloodLightThroughArea_r( light, p->intoArea, &newStack );
  680. }
  681. }
  682. /*
  683. =======================
  684. idRenderWorldLocal::FlowLightThroughPortals
  685. Adds an arearef in each area that the light center flows into.
  686. This can only be used for shadow casting lights that have a generated
  687. prelight, because shadows are cast from back side which may not be in visible areas.
  688. =======================
  689. */
  690. void idRenderWorldLocal::FlowLightThroughPortals( idRenderLightLocal *light ) {
  691. // if the light origin areaNum is not in a valid area,
  692. // the light won't have any area refs
  693. if ( light->areaNum == -1 ) {
  694. return;
  695. }
  696. idPlane frustumPlanes[6];
  697. idRenderMatrix::GetFrustumPlanes( frustumPlanes, light->baseLightProject, true, true );
  698. portalStack_t ps;
  699. memset( &ps, 0, sizeof( ps ) );
  700. ps.numPortalPlanes = 6;
  701. for ( int i = 0; i < 6; i++ ) {
  702. ps.portalPlanes[i] = -frustumPlanes[i];
  703. }
  704. FloodLightThroughArea_r( light, light->areaNum, &ps );
  705. }
  706. /*
  707. =======================================================================
  708. Portal State Management
  709. =======================================================================
  710. */
  711. /*
  712. ==============
  713. NumPortals
  714. ==============
  715. */
  716. int idRenderWorldLocal::NumPortals() const {
  717. return numInterAreaPortals;
  718. }
  719. /*
  720. ==============
  721. FindPortal
  722. Game code uses this to identify which portals are inside doors.
  723. Returns 0 if no portal contacts the bounds
  724. ==============
  725. */
  726. qhandle_t idRenderWorldLocal::FindPortal( const idBounds &b ) const {
  727. int i, j;
  728. idBounds wb;
  729. doublePortal_t *portal;
  730. idWinding *w;
  731. for ( i = 0; i < numInterAreaPortals; i++ ) {
  732. portal = &doublePortals[i];
  733. w = portal->portals[0]->w;
  734. wb.Clear();
  735. for ( j = 0; j < w->GetNumPoints(); j++ ) {
  736. wb.AddPoint( (*w)[j].ToVec3() );
  737. }
  738. if ( wb.IntersectsBounds( b ) ) {
  739. return i + 1;
  740. }
  741. }
  742. return 0;
  743. }
  744. /*
  745. =============
  746. FloodConnectedAreas
  747. =============
  748. */
  749. void idRenderWorldLocal::FloodConnectedAreas( portalArea_t *area, int portalAttributeIndex ) {
  750. if ( area->connectedAreaNum[portalAttributeIndex] == connectedAreaNum ) {
  751. return;
  752. }
  753. area->connectedAreaNum[portalAttributeIndex] = connectedAreaNum;
  754. for ( portal_t *p = area->portals; p != NULL; p = p->next ) {
  755. if ( !(p->doublePortal->blockingBits & (1<<portalAttributeIndex) ) ) {
  756. FloodConnectedAreas( &portalAreas[p->intoArea], portalAttributeIndex );
  757. }
  758. }
  759. }
  760. /*
  761. ==============
  762. AreasAreConnected
  763. ==============
  764. */
  765. bool idRenderWorldLocal::AreasAreConnected( int areaNum1, int areaNum2, portalConnection_t connection ) const {
  766. if ( areaNum1 == -1 || areaNum2 == -1 ) {
  767. return false;
  768. }
  769. if ( areaNum1 > numPortalAreas || areaNum2 > numPortalAreas || areaNum1 < 0 || areaNum2 < 0 ) {
  770. common->Error( "idRenderWorldLocal::AreAreasConnected: bad parms: %i, %i", areaNum1, areaNum2 );
  771. }
  772. int attribute = 0;
  773. int intConnection = (int)connection;
  774. while ( intConnection > 1 ) {
  775. attribute++;
  776. intConnection >>= 1;
  777. }
  778. if ( attribute >= NUM_PORTAL_ATTRIBUTES || ( 1 << attribute ) != (int)connection ) {
  779. common->Error( "idRenderWorldLocal::AreasAreConnected: bad connection number: %i\n", (int)connection );
  780. }
  781. return portalAreas[areaNum1].connectedAreaNum[attribute] == portalAreas[areaNum2].connectedAreaNum[attribute];
  782. }
  783. /*
  784. ==============
  785. SetPortalState
  786. doors explicitly close off portals when shut
  787. ==============
  788. */
  789. void idRenderWorldLocal::SetPortalState( qhandle_t portal, int blockTypes ) {
  790. if ( portal == 0 ) {
  791. return;
  792. }
  793. if ( portal < 1 || portal > numInterAreaPortals ) {
  794. common->Error( "SetPortalState: bad portal number %i", portal );
  795. }
  796. int old = doublePortals[portal-1].blockingBits;
  797. if ( old == blockTypes ) {
  798. return;
  799. }
  800. doublePortals[portal-1].blockingBits = blockTypes;
  801. // leave the connectedAreaGroup the same on one side,
  802. // then flood fill from the other side with a new number for each changed attribute
  803. for ( int i = 0; i < NUM_PORTAL_ATTRIBUTES; i++ ) {
  804. if ( ( old ^ blockTypes ) & ( 1 << i ) ) {
  805. connectedAreaNum++;
  806. FloodConnectedAreas( &portalAreas[doublePortals[portal-1].portals[1]->intoArea], i );
  807. }
  808. }
  809. if ( common->WriteDemo() ) {
  810. common->WriteDemo()->WriteInt( DS_RENDER );
  811. common->WriteDemo()->WriteInt( DC_SET_PORTAL_STATE );
  812. common->WriteDemo()->WriteInt( portal );
  813. common->WriteDemo()->WriteInt( blockTypes );
  814. }
  815. }
  816. /*
  817. ==============
  818. GetPortalState
  819. ==============
  820. */
  821. int idRenderWorldLocal::GetPortalState( qhandle_t portal ) {
  822. if ( portal == 0 ) {
  823. return 0;
  824. }
  825. if ( portal < 1 || portal > numInterAreaPortals ) {
  826. common->Error( "GetPortalState: bad portal number %i", portal );
  827. }
  828. return doublePortals[portal-1].blockingBits;
  829. }
  830. /*
  831. =====================
  832. idRenderWorldLocal::ShowPortals
  833. Debugging tool, won't work correctly with SMP or when mirrors are present
  834. =====================
  835. */
  836. void idRenderWorldLocal::ShowPortals() {
  837. int i, j;
  838. portalArea_t *area;
  839. portal_t *p;
  840. idWinding *w;
  841. // flood out through portals, setting area viewCount
  842. for ( i = 0; i < numPortalAreas; i++ ) {
  843. area = &portalAreas[i];
  844. if ( area->viewCount != tr.viewCount ) {
  845. continue;
  846. }
  847. for ( p = area->portals; p; p = p->next ) {
  848. w = p->w;
  849. if ( !w ) {
  850. continue;
  851. }
  852. if ( portalAreas[ p->intoArea ].viewCount != tr.viewCount ) {
  853. // red = can't see
  854. GL_Color( 1, 0, 0 );
  855. } else {
  856. // green = see through
  857. GL_Color( 0, 1, 0 );
  858. }
  859. qglBegin( GL_LINE_LOOP );
  860. for ( j = 0; j < w->GetNumPoints(); j++ ) {
  861. qglVertex3fv( (*w)[j].ToFloatPtr() );
  862. }
  863. qglEnd();
  864. }
  865. }
  866. }