-/*
-=============================================================
-
- WORLD MODEL
-
-=============================================================
-*/
-
-static void RSurf_Callback(void *data, void *junk)
-{
- ((msurface_t *)data)->visframe = r_framecount;
-}
-
-static void R_SolidWorldNode (void)
-{
- if (r_viewleaf->contents != CONTENTS_SOLID)
- {
- int portalstack;
- mportal_t *p, *pstack[8192];
- msurface_t *surf, **mark, **endmark;
- mleaf_t *leaf;
- tinyplane_t plane;
- // LordHavoc: portal-passage worldnode; follows portals leading
- // outward from viewleaf, if a portal leads offscreen it is not
- // followed, in indoor maps this can often cull a great deal of
- // geometry away when pvs data is not present (useful with pvs as well)
-
- leaf = r_viewleaf;
- leaf->worldnodeframe = r_framecount;
- portalstack = 0;
- loc0:
- c_leafs++;
-
- leaf->visframe = r_framecount;
-
- if (leaf->nummarksurfaces)
- {
- mark = leaf->firstmarksurface;
- endmark = mark + leaf->nummarksurfaces;
- if (r_ser.integer)
- {
- do
- {
- surf = *mark++;
- // make sure surfaces are only processed once
- if (surf->worldnodeframe == r_framecount)
- continue;
- surf->worldnodeframe = r_framecount;
- if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
- {
- if (surf->flags & SURF_PLANEBACK)
- {
- VectorNegate(surf->plane->normal, plane.normal);
- plane.dist = -surf->plane->dist;
- R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
- }
- }
- else
- {
- if (!(surf->flags & SURF_PLANEBACK))
- R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, (tinyplane_t *)surf->plane);
- }
- }
- while (mark < endmark);
- }
- else
- {
- do
- {
- surf = *mark++;
- // make sure surfaces are only processed once
- if (surf->worldnodeframe == r_framecount)
- continue;
- surf->worldnodeframe = r_framecount;
- if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
- {
- if (surf->flags & SURF_PLANEBACK)
- surf->visframe = r_framecount;
- }
- else
- {
- if (!(surf->flags & SURF_PLANEBACK))
- surf->visframe = r_framecount;
- }
- }
- while (mark < endmark);
- }
- }
-
- // follow portals into other leafs
- p = leaf->portals;
- for (;p;p = p->next)
- {
- if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
- {
- leaf = p->past;
- if (leaf->worldnodeframe != r_framecount)
- {
- leaf->worldnodeframe = r_framecount;
- if (leaf->contents != CONTENTS_SOLID)
- {
- if (R_NotCulledBox(leaf->mins, leaf->maxs))
- {
- p->visframe = r_framecount;
- pstack[portalstack++] = p;
- goto loc0;
-
- loc1:
- p = pstack[--portalstack];
- }
- }
- }
- }
- }
-
- if (portalstack)
- goto loc1;
- }
- else
- {
- mnode_t *nodestack[8192], *node = cl.worldmodel->nodes;
- int nodestackpos = 0;
- // LordHavoc: recursive descending worldnode; if portals are not
- // available, this is a good last resort, can cull large amounts of
- // geometry, but is more time consuming than portal-passage and renders
- // things behind walls
-
-loc2:
- if (R_NotCulledBox(node->mins, node->maxs))
- {
- if (node->numsurfaces)
- {
- if (r_ser.integer)
- {
- msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
- tinyplane_t plane;
- if (PlaneDiff (r_origin, node->plane) < 0)
- {
- for (;surf < surfend;surf++)
- {
- if (surf->flags & SURF_PLANEBACK)
- {
- VectorNegate(surf->plane->normal, plane.normal);
- plane.dist = -surf->plane->dist;
- R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), surf->flags & SURF_CLIPSOLID, RSurf_Callback, surf, NULL, &plane);
- }
- }
- }
- else
- {
- for (;surf < surfend;surf++)
- {
- if (!(surf->flags & SURF_PLANEBACK))
- R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), surf->flags & SURF_CLIPSOLID, RSurf_Callback, surf, NULL, (tinyplane_t *)surf->plane);
- }
- }
- }
- else
- {
- msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
- if (PlaneDiff (r_origin, node->plane) < 0)
- {
- for (;surf < surfend;surf++)
- {
- if (surf->flags & SURF_PLANEBACK)
- surf->visframe = r_framecount;
- }
- }
- else
- {
- for (;surf < surfend;surf++)
- {
- if (!(surf->flags & SURF_PLANEBACK))
- surf->visframe = r_framecount;
- }
- }
- }
- }
-
- // recurse down the children
- if (node->children[0]->contents >= 0)
- {
- if (node->children[1]->contents >= 0)
- {
- if (nodestackpos < 8192)
- nodestack[nodestackpos++] = node->children[1];
- node = node->children[0];
- goto loc2;
- }
- else
- ((mleaf_t *)node->children[1])->visframe = r_framecount;
- node = node->children[0];
- goto loc2;
- }
- else
- {
- ((mleaf_t *)node->children[0])->visframe = r_framecount;
- if (node->children[1]->contents >= 0)
- {
- node = node->children[1];
- goto loc2;
- }
- else if (nodestackpos > 0)
- {
- ((mleaf_t *)node->children[1])->visframe = r_framecount;
- node = nodestack[--nodestackpos];
- goto loc2;
- }
- }
- }
- else if (nodestackpos > 0)
- {
- node = nodestack[--nodestackpos];
- goto loc2;
- }
- }
-}
-
-static int r_portalframecount = 0;
-
-static void R_PVSWorldNode()
-{
- int portalstack, i;
- mportal_t *p, *pstack[8192];
- msurface_t *surf, **mark, **endmark;
- mleaf_t *leaf;
- tinyplane_t plane;
- byte *worldvis;
-
- worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
-
- leaf = r_viewleaf;
- leaf->worldnodeframe = r_framecount;
- portalstack = 0;
-loc0:
- c_leafs++;
-
- leaf->visframe = r_framecount;
-
- if (leaf->nummarksurfaces)
- {
- mark = leaf->firstmarksurface;
- endmark = mark + leaf->nummarksurfaces;
- if (r_ser.integer)
- {
- do
- {
- surf = *mark++;
- // make sure surfaces are only processed once
- if (surf->worldnodeframe == r_framecount)
- continue;
- surf->worldnodeframe = r_framecount;
- if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
- {
- if (surf->flags & SURF_PLANEBACK)
- {
- VectorNegate(surf->plane->normal, plane.normal);
- plane.dist = -surf->plane->dist;
- R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
- }
- }
- else
- {
- if (!(surf->flags & SURF_PLANEBACK))
- R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, (tinyplane_t *)surf->plane);
- }
- }
- while (mark < endmark);
- }
- else
- {
- do
- {
- surf = *mark++;
- // make sure surfaces are only processed once
- if (surf->worldnodeframe == r_framecount)
- continue;
- surf->worldnodeframe = r_framecount;
- if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
- {
- if (surf->flags & SURF_PLANEBACK)
- surf->visframe = r_framecount;
- }
- else
- {
- if (!(surf->flags & SURF_PLANEBACK))
- surf->visframe = r_framecount;
- }
- }
- while (mark < endmark);
- }
- }
-
- // follow portals into other leafs
- for (p = leaf->portals;p;p = p->next)
- {
- if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
- {
- leaf = p->past;
- if (leaf->worldnodeframe != r_framecount)
- {
- leaf->worldnodeframe = r_framecount;
- if (leaf->contents != CONTENTS_SOLID)
- {
- i = (leaf - cl.worldmodel->leafs) - 1;
- if (worldvis[i>>3] & (1<<(i&7)))
- {
- if (R_NotCulledBox(leaf->mins, leaf->maxs))
- {
- pstack[portalstack++] = p;
- goto loc0;
-
-loc1:
- p = pstack[--portalstack];
- }
- }
- }
- }
- }
- }
-
- if (portalstack)
- goto loc1;
-}
-
-Cshader_t Cshader_wall_vertex = {{NULL, RSurfShader_Wall_Vertex, RSurfShader_Wall_Fog}, NULL};
-Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap, RSurfShader_Wall_Fog}, NULL};
-Cshader_t Cshader_wall_fullbright = {{NULL, RSurfShader_Wall_Fullbright, RSurfShader_Wall_Fog}, NULL};
-Cshader_t Cshader_water = {{NULL, RSurfShader_Water, NULL}, NULL};
-Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL, NULL}, NULL};
-
-int Cshader_count = 5;
-Cshader_t *Cshaders[5] =
-{
- &Cshader_wall_vertex,
- &Cshader_wall_lightmap,
- &Cshader_wall_fullbright,
- &Cshader_water,
- &Cshader_sky
-};
-
-void R_PrepareSurfaces(void)
-{
- int i;
- texture_t *t;
- model_t *model;
- msurface_t *surf;
-
- for (i = 0;i < Cshader_count;i++)
- Cshaders[i]->chain = NULL;
-
- model = currentrenderentity->model;
-
- for (i = 0;i < model->nummodelsurfaces;i++)
- {
- surf = model->modelsortedsurfaces[i];
- if (surf->visframe == r_framecount)
- {
- if (surf->insertframe != r_framecount)
- {
- surf->insertframe = r_framecount;
- c_faces++;
- // manually inlined R_TextureAnimation
- //t = R_TextureAnimation(surf->texinfo->texture);
- t = surf->texinfo->texture;
- if (t->alternate_anims != NULL && currentrenderentity->frame)
- t = t->alternate_anims;
- if (t->anim_total >= 2)
- t = t->anim_frames[(int)(cl.time * 5.0f) % t->anim_total];
- surf->currenttexture = t;
- }
-
- surf->chain = surf->shader->chain;
- surf->shader->chain = surf;
- }
- }
-}
-
-void R_DrawSurfaces (int type)
-{
- int i, stage;
- msurface_t *surf;
- Cshader_t *shader;
-
- for (i = 0;i < Cshader_count;i++)
- {
- shader = Cshaders[i];
- if (shader->chain && shader->shaderfunc[type])
- for (stage = 0;stage < 1000;stage++)
- for (surf = shader->chain;surf;surf = surf->chain)
- if (shader->shaderfunc[type](stage, surf))
- goto done;
-done:;
- }
-}
-
-void R_DrawSurfacesAll (void)
-{
- R_DrawSurfaces(SHADERSTAGE_SKY);
- R_DrawSurfaces(SHADERSTAGE_NORMAL);
- R_DrawSurfaces(SHADERSTAGE_FOG);
-}
-
-static float portalpointbuffer[256][3];
-
-void R_DrawPortals(void)
-{
- int drawportals, i;
-// mleaf_t *leaf, *endleaf;
- mportal_t *portal, *endportal;
- mvertex_t *point/*, *endpoint*/;
- rmeshinfo_t m;
- drawportals = r_drawportals.integer;
- if (drawportals < 1)
- return;
- /*
- leaf = cl.worldmodel->leafs;
- endleaf = leaf + cl.worldmodel->numleafs;
- for (;leaf < endleaf;leaf++)
- {
- if (leaf->visframe == r_framecount && leaf->portals)
- {
- i = leaf - cl.worldmodel->leafs;
- r = (i & 0x0007) << 5;
- g = (i & 0x0038) << 2;
- b = (i & 0x01C0) >> 1;
- portal = leaf->portals;
- while (portal)
- {
- transpolybegin(0, 0, 0, TPOLYTYPE_ALPHA);
- point = portal->points + portal->numpoints - 1;
- endpoint = portal->points;
- for (;point >= endpoint;point--)
- transpolyvertub(point->position[0], point->position[1], point->position[2], 0, 0, r, g, b, 32);
- transpolyend();
- portal = portal->next;
- }
- }
- }
- */
- memset(&m, 0, sizeof(m));
- m.transparent = true;
- m.blendfunc1 = GL_SRC_ALPHA;
- m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
- m.vertex = &portalpointbuffer[0][0];
- m.vertexstep = sizeof(float[3]);
- m.ca = 0.125;
- for (portal = cl.worldmodel->portals, endportal = portal + cl.worldmodel->numportals;portal < endportal;portal++)
- {
- if (portal->visframe == r_portalframecount)
- {
- if (portal->numpoints <= 256)
- {
- i = portal - cl.worldmodel->portals;
- m.cr = ((i & 0x0007) >> 0) * (1.0f / 7.0f);
- m.cg = ((i & 0x0038) >> 3) * (1.0f / 7.0f);
- m.cb = ((i & 0x01C0) >> 6) * (1.0f / 7.0f);
- point = portal->points;
- if (PlaneDiff(r_origin, (&portal->plane)) > 0)
- {
- for (i = portal->numpoints - 1;i >= 0;i--)
- VectorCopy(point[i].position, portalpointbuffer[i]);
- }
- else
- {
- for (i = 0;i < portal->numpoints;i++)
- VectorCopy(point[i].position, portalpointbuffer[i]);
- }
- R_Mesh_DrawPolygon(&m, portal->numpoints);
- }
- }
- }
-}
-
-void R_SetupForBModelRendering(void)
-{
- int i;
- msurface_t *s;
- model_t *model;
- vec3_t modelorg;
-
- // because bmodels can be reused, we have to decide which things to render
- // from scratch every time
-
- model = currentrenderentity->model;
-
- softwaretransformforentity (currentrenderentity);
- softwareuntransform(r_origin, modelorg);
-
- for (i = 0;i < model->nummodelsurfaces;i++)
- {
- s = model->modelsortedsurfaces[i];
- if (((s->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, s->plane) >= 0))
- s->visframe = r_framecount;
- else
- s->visframe = -1;
- s->worldnodeframe = -1;
- s->lightframe = -1;
- s->dlightframe = -1;
- s->insertframe = -1;
- }
-}
-
-void R_SetupForWorldRendering(void)