+ // if leaf is in current pvs and on the screen, mark its surfaces
+ if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex) && !R_CullBox(leaf->mins, leaf->maxs))
+ {
+ renderstats.world_leafs++;
+ r_worldleafvisible[j] = true;
+ if (leaf->numleafsurfaces)
+ for (i = 0, mark = leaf->firstleafsurface;i < leaf->numleafsurfaces;i++, mark++)
+ r_worldsurfacevisible[*mark] = true;
+ }
+ }
+ }
+ // otherwise use a recursive portal flow, culling each portal to
+ // frustum and checking if the leaf the portal leads to is in the pvs
+ else
+ {
+ int leafstackpos;
+ mportal_t *p;
+ mleaf_t *leafstack[8192];
+ // simple-frustum portal method:
+ // follows portals leading outward from viewleaf, does not venture
+ // offscreen or into leafs that are not visible, faster than
+ // Quake's RecursiveWorldNode and vastly better in unvised maps,
+ // often culls some surfaces that pvs alone would miss
+ // (such as a room in pvs that is hidden behind a wall, but the
+ // passage leading to the room is off-screen)
+ leafstack[0] = viewleaf;
+ leafstackpos = 1;
+ while (leafstackpos)
+ {
+ renderstats.world_leafs++;
+ leaf = leafstack[--leafstackpos];
+ r_worldleafvisible[leaf - model->brush.data_leafs] = true;
+ // mark any surfaces bounding this leaf
+ if (leaf->numleafsurfaces)
+ for (i = 0, mark = leaf->firstleafsurface;i < leaf->numleafsurfaces;i++, mark++)
+ r_worldsurfacevisible[*mark] = true;
+ // follow portals into other leafs
+ // the checks are:
+ // if viewer is behind portal (portal faces outward into the scene)
+ // and the portal polygon's bounding box is on the screen
+ // and the leaf has not been visited yet
+ // and the leaf is visible in the pvs
+ // (the first two checks won't cause as many cache misses as the leaf checks)
+ for (p = leaf->portals;p;p = p->next)
+ {
+ renderstats.world_portals++;
+ if (DotProduct(r_vieworigin, p->plane.normal) < (p->plane.dist + 1) && !R_CullBox(p->mins, p->maxs) && !r_worldleafvisible[p->past - model->brush.data_leafs] && CHECKPVSBIT(r_pvsbits, p->past->clusterindex))
+ leafstack[leafstackpos++] = p->past;
+ }