- c_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)
- 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;
+ // no visibility method: (used when floating around in the void)
+ // simply cull each leaf to the frustum (view pyramid)
+ // similar to quake's RecursiveWorldNode but without cache misses
+ r_viewcache.world_novis = true;
+ for (j = 0, leaf = model->brush.data_leafs;j < model->brush.num_leafs;j++, leaf++)
+ {
+ // if leaf is in current pvs and on the screen, mark its surfaces
+ if (!R_CullBox(leaf->mins, leaf->maxs))
+ {
+ r_refdef.stats.world_leafs++;
+ r_viewcache.world_leafvisible[j] = true;
+ if (leaf->numleafsurfaces)
+ for (i = 0, mark = leaf->firstleafsurface;i < leaf->numleafsurfaces;i++, mark++)
+ r_viewcache.world_surfacevisible[*mark] = true;
+ }
+ }
+ }
+ // if the user prefers to disable portal culling (testing?), simply
+ // use all on-screen leafs that are in the pvs.
+ else if (!r_useportalculling.integer)
+ {
+ // pvs method:
+ // simply check if each leaf is in the Potentially Visible Set,
+ // and cull to frustum (view pyramid)
+ // similar to quake's RecursiveWorldNode but without cache misses
+ for (j = 0, leaf = model->brush.data_leafs;j < model->brush.num_leafs;j++, leaf++)
+ {
+ // if leaf is in current pvs and on the screen, mark its surfaces
+ if (CHECKPVSBIT(r_viewcache.world_pvsbits, leaf->clusterindex) && !R_CullBox(leaf->mins, leaf->maxs))
+ {
+ r_refdef.stats.world_leafs++;
+ r_viewcache.world_leafvisible[j] = true;
+ if (leaf->numleafsurfaces)
+ for (i = 0, mark = leaf->firstleafsurface;i < leaf->numleafsurfaces;i++, mark++)
+ r_viewcache.world_surfacevisible[*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)
+ {
+ leaf = leafstack[--leafstackpos];
+ if (r_viewcache.world_leafvisible[leaf - model->brush.data_leafs])
+ continue;
+ r_refdef.stats.world_leafs++;
+ r_viewcache.world_leafvisible[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_viewcache.world_surfacevisible[*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)
+ {
+ r_refdef.stats.world_portals++;
+ if (DotProduct(r_view.origin, p->plane.normal) < (p->plane.dist + 1)
+ && !r_viewcache.world_leafvisible[p->past - model->brush.data_leafs]
+ && CHECKPVSBIT(r_viewcache.world_pvsbits, p->past->clusterindex)
+ && !R_CullBox(p->mins, p->maxs)
+ && leafstackpos < (int)(sizeof(leafstack) / sizeof(leafstack[0])))
+ leafstack[leafstackpos++] = p->past;
+ }
+ }