/* * ================ * R_RenderBmodelFace * ================ */ static void R_RenderBmodelFace(bedge_t pedges, model.msurface_t psurf) { int i; uint mask; model.mplane_t pplane; double distinv; double[] p_normal = new double[3]; model.medge_t tedge = new model.medge_t(); clipplane_t pclip; // skip out if no more surfs if (surface_p >= surf_max) { r_outofsurfaces++; return; } // ditto if not enough edges left, or switch to auxedges if possible if ((edge_p + psurf.numedges + 4) >= edge_max) { r_outofedges += psurf.numedges; return; } c_faceclip++; // this is a dummy to give the caching mechanism someplace to write to r_pedge = tedge; // set up clip planes pclip = null; for (i = 3, mask = 0x08; i >= 0; i--, mask >>= 1) { if ((r_clipflags & mask) != 0) { view_clipplanes[i].next = pclip; pclip = view_clipplanes[i]; } } // push the edges through r_emitted = 0; r_nearzi = 0; r_nearzionly = false; makeleftedge = makerightedge = false; // FIXME: keep clipped bmodel edges in clockwise order so last vertex caching // can be used? r_lastvertvalid = false; for ( ; pedges != null; pedges = pedges.pnext) { r_leftclipped = r_rightclipped = false; R_ClipEdge(pedges.v[0], pedges.v[1], pclip); if (r_leftclipped) { makeleftedge = true; } if (r_rightclipped) { makerightedge = true; } } // if there was a clip off the left edge, add that edge too // FIXME: faster to do in screen space? // FIXME: share clipped edges? if (makeleftedge) { r_pedge = tedge; R_ClipEdge(r_leftexit, r_leftenter, pclip.next); } // if there was a clip off the right edge, get the right r_nearzi if (makerightedge) { r_pedge = tedge; r_nearzionly = true; R_ClipEdge(r_rightexit, r_rightenter, view_clipplanes[1].next); } // if no edges made it out, return without posting the surface if (r_emitted == 0) { return; } r_polycount++; surfaces[surface_p].data = psurf; surfaces[surface_p].nearzi = r_nearzi; surfaces[surface_p].flags = psurf.flags; surfaces[surface_p].insubmodel = true; surfaces[surface_p].spanstate = 0; surfaces[surface_p].entity = currententity; surfaces[surface_p].key = r_currentbkey; surfaces[surface_p].spans = null; pplane = psurf.plane; // FIXME: cache this? TransformVector(pplane.normal, ref p_normal); // FIXME: cache this? distinv = 1.0 / (pplane.dist - mathlib.DotProduct(modelorg, pplane.normal)); surfaces[surface_p].d_zistepu = p_normal[0] * xscaleinv * distinv; surfaces[surface_p].d_zistepv = -p_normal[1] * yscaleinv * distinv; surfaces[surface_p].d_ziorigin = p_normal[2] * distinv - xcenter * surfaces[surface_p].d_zistepu - ycenter * surfaces[surface_p].d_zistepv; //JDC VectorCopy (r_worldmodelorg, surface_p.modelorg); surface_p++; }
/* ================ R_RecursiveClipBPoly ================ */ static void R_RecursiveClipBPoly(bedge_t pedges, model.mnode_t pnode, model.msurface_t psurf) { bedge_t[] psideedges = new bedge_t[2]; bedge_t pnextedge, ptedge; int i, side, lastside; double dist, frac, lastdist; model.mplane_t splitplane, tplane = new model.mplane_t(); model.mvertex_t pvert, plastvert, ptvert; model.node_or_leaf_t pn; psideedges[0] = psideedges[1] = null; makeclippededge = false; // transform the BSP plane into model space // FIXME: cache these? splitplane = pnode.plane; tplane.dist = splitplane.dist - mathlib.DotProduct(r_entorigin, splitplane.normal); tplane.normal[0] = mathlib.DotProduct(entity_rotation[0], splitplane.normal); tplane.normal[1] = mathlib.DotProduct(entity_rotation[1], splitplane.normal); tplane.normal[2] = mathlib.DotProduct(entity_rotation[2], splitplane.normal); // clip edges to BSP plane for ( ; pedges != null ; pedges = pnextedge) { pnextedge = pedges.pnext; // set the status for the last point as the previous point // FIXME: cache this stuff somehow? plastvert = pedges.v[0]; lastdist = mathlib.DotProduct (plastvert.position, tplane.normal) - tplane.dist; if (lastdist > 0) lastside = 0; else lastside = 1; pvert = pedges.v[1]; dist = mathlib.DotProduct(pvert.position, tplane.normal) - tplane.dist; if (dist > 0) side = 0; else side = 1; if (side != lastside) { // clipped if (numbverts >= MAX_BMODEL_VERTS) return; // generate the clipped vertex frac = lastdist / (lastdist - dist); ptvert = pbverts[numbverts++]; ptvert.position[0] = plastvert.position[0] + frac * (pvert.position[0] - plastvert.position[0]); ptvert.position[1] = plastvert.position[1] + frac * (pvert.position[1] - plastvert.position[1]); ptvert.position[2] = plastvert.position[2] + frac * (pvert.position[2] - plastvert.position[2]); // split into two edges, one on each side, and remember entering // and exiting points // FIXME: share the clip edge by having a winding direction flag? if (numbedges >= (MAX_BMODEL_EDGES - 1)) { console.Con_Printf ("Out of edges for bmodel\n"); return; } ptedge = pbedges[numbedges]; ptedge.pnext = psideedges[lastside]; psideedges[lastside] = ptedge; ptedge.v[0] = plastvert; ptedge.v[1] = ptvert; ptedge = pbedges[numbedges + 1]; ptedge.pnext = psideedges[side]; psideedges[side] = ptedge; ptedge.v[0] = ptvert; ptedge.v[1] = pvert; numbedges += 2; if (side == 0) { // entering for front, exiting for back pfrontenter = ptvert; makeclippededge = true; } else { pfrontexit = ptvert; makeclippededge = true; } } else { // add the edge to the appropriate side pedges.pnext = psideedges[side]; psideedges[side] = pedges; } } // if anything was clipped, reconstitute and add the edges along the clip // plane to both sides (but in opposite directions) if (makeclippededge) { if (numbedges >= (MAX_BMODEL_EDGES - 2)) { console.Con_Printf ("Out of edges for bmodel\n"); return; } ptedge = pbedges[numbedges]; ptedge.pnext = psideedges[0]; psideedges[0] = ptedge; ptedge.v[0] = pfrontexit; ptedge.v[1] = pfrontenter; ptedge = pbedges[numbedges + 1]; ptedge.pnext = psideedges[1]; psideedges[1] = ptedge; ptedge.v[0] = pfrontenter; ptedge.v[1] = pfrontexit; numbedges += 2; } // draw or recurse further for (i=0 ; i<2 ; i++) { if (psideedges[i] != null) { // draw if we've reached a non-solid leaf, done if all that's left is a // solid leaf, and continue down the tree if it's not a leaf pn = pnode.children[i]; // we're done with this branch if the node or leaf isn't in the PVS if (pn.visframe == r_visframecount) { if (pn.contents < 0) { if (pn.contents != bspfile.CONTENTS_SOLID) { r_currentbkey = ((model.mleaf_t)pn).key; R_RenderBmodelFace (psideedges[i], psurf); } } else { R_RecursiveClipBPoly (psideedges[i], (model.mnode_t)pnode.children[i], psurf); } } } } }
/* ================ R_DrawSolidClippedSubmodelPolygons ================ */ static void R_DrawSolidClippedSubmodelPolygons(model.model_t pmodel) { int i, j, lindex; double dot; model.msurface_t psurf; int numsurfaces; model.mplane_t pplane; model.mvertex_t[] bverts = new model.mvertex_t[MAX_BMODEL_VERTS]; bedge_t[] bedges = new bedge_t[MAX_BMODEL_EDGES]; int pbedge; model.medge_t pedge; model.medge_t[] pedges; int kk; for (kk = 0; kk < MAX_BMODEL_VERTS; kk++) bverts[kk] = new model.mvertex_t(); for (kk = 0; kk < MAX_BMODEL_EDGES; kk++) bedges[kk] = new bedge_t(); // FIXME: use bounding-box-based frustum clipping info? numsurfaces = pmodel.nummodelsurfaces; pedges = pmodel.edges; for (i=0 ; i<numsurfaces ; i++) { psurf = pmodel.surfaces[pmodel.firstmodelsurface + i]; // find which side of the node we are on pplane = psurf.plane; dot = mathlib.DotProduct (modelorg, pplane.normal) - pplane.dist; // draw the polygon if (((psurf.flags & model.SURF_PLANEBACK) != 0 && (dot < -BACKFACE_EPSILON)) || ((psurf.flags & model.SURF_PLANEBACK) == 0 && (dot > BACKFACE_EPSILON))) { // FIXME: use bounding-box-based frustum clipping info? // copy the edges to bedges, flipping if necessary so always // clockwise winding // FIXME: if edges and vertices get caches, these assignments must move // outside the loop, and overflow checking must be done here pbverts = bverts; pbedges = bedges; numbverts = numbedges = 0; if (psurf.numedges > 0) { pbedge = numbedges; numbedges += psurf.numedges; for (j=0 ; j<psurf.numedges ; j++) { lindex = pmodel.surfedges[psurf.firstedge+j]; if (lindex > 0) { pedge = pedges[lindex]; bedges[pbedge + j].v[0] = r_pcurrentvertbase[pedge.v[0]]; bedges[pbedge + j].v[1] = r_pcurrentvertbase[pedge.v[1]]; } else { lindex = -lindex; pedge = pedges[lindex]; bedges[pbedge + j].v[0] = r_pcurrentvertbase[pedge.v[1]]; bedges[pbedge + j].v[1] = r_pcurrentvertbase[pedge.v[0]]; } bedges[pbedge + j].pnext = bedges[pbedge + j + 1]; } bedges[pbedge + j - 1].pnext = null; // mark end of edges R_RecursiveClipBPoly (bedges[pbedge], (model.mnode_t)currententity.topnode, psurf); } else { sys_linux.Sys_Error ("no edges in bmodel"); } } } }
/* * ================ * R_DrawSolidClippedSubmodelPolygons * ================ */ static void R_DrawSolidClippedSubmodelPolygons(model.model_t pmodel) { int i, j, lindex; double dot; model.msurface_t psurf; int numsurfaces; model.mplane_t pplane; model.mvertex_t[] bverts = new model.mvertex_t[MAX_BMODEL_VERTS]; bedge_t[] bedges = new bedge_t[MAX_BMODEL_EDGES]; int pbedge; model.medge_t pedge; model.medge_t[] pedges; int kk; for (kk = 0; kk < MAX_BMODEL_VERTS; kk++) { bverts[kk] = new model.mvertex_t(); } for (kk = 0; kk < MAX_BMODEL_EDGES; kk++) { bedges[kk] = new bedge_t(); } // FIXME: use bounding-box-based frustum clipping info? numsurfaces = pmodel.nummodelsurfaces; pedges = pmodel.edges; for (i = 0; i < numsurfaces; i++) { psurf = pmodel.surfaces[pmodel.firstmodelsurface + i]; // find which side of the node we are on pplane = psurf.plane; dot = mathlib.DotProduct(modelorg, pplane.normal) - pplane.dist; // draw the polygon if (((psurf.flags & model.SURF_PLANEBACK) != 0 && (dot < -BACKFACE_EPSILON)) || ((psurf.flags & model.SURF_PLANEBACK) == 0 && (dot > BACKFACE_EPSILON))) { // FIXME: use bounding-box-based frustum clipping info? // copy the edges to bedges, flipping if necessary so always // clockwise winding // FIXME: if edges and vertices get caches, these assignments must move // outside the loop, and overflow checking must be done here pbverts = bverts; pbedges = bedges; numbverts = numbedges = 0; if (psurf.numedges > 0) { pbedge = numbedges; numbedges += psurf.numedges; for (j = 0; j < psurf.numedges; j++) { lindex = pmodel.surfedges[psurf.firstedge + j]; if (lindex > 0) { pedge = pedges[lindex]; bedges[pbedge + j].v[0] = r_pcurrentvertbase[pedge.v[0]]; bedges[pbedge + j].v[1] = r_pcurrentvertbase[pedge.v[1]]; } else { lindex = -lindex; pedge = pedges[lindex]; bedges[pbedge + j].v[0] = r_pcurrentvertbase[pedge.v[1]]; bedges[pbedge + j].v[1] = r_pcurrentvertbase[pedge.v[0]]; } bedges[pbedge + j].pnext = bedges[pbedge + j + 1]; } bedges[pbedge + j - 1].pnext = null; // mark end of edges R_RecursiveClipBPoly(bedges[pbedge], (model.mnode_t)currententity.topnode, psurf); } else { sys_linux.Sys_Error("no edges in bmodel"); } } } }
/* * ================ * R_RecursiveClipBPoly * ================ */ static void R_RecursiveClipBPoly(bedge_t pedges, model.mnode_t pnode, model.msurface_t psurf) { bedge_t[] psideedges = new bedge_t[2]; bedge_t pnextedge, ptedge; int i, side, lastside; double dist, frac, lastdist; model.mplane_t splitplane, tplane = new model.mplane_t(); model.mvertex_t pvert, plastvert, ptvert; model.node_or_leaf_t pn; psideedges[0] = psideedges[1] = null; makeclippededge = false; // transform the BSP plane into model space // FIXME: cache these? splitplane = pnode.plane; tplane.dist = splitplane.dist - mathlib.DotProduct(r_entorigin, splitplane.normal); tplane.normal[0] = mathlib.DotProduct(entity_rotation[0], splitplane.normal); tplane.normal[1] = mathlib.DotProduct(entity_rotation[1], splitplane.normal); tplane.normal[2] = mathlib.DotProduct(entity_rotation[2], splitplane.normal); // clip edges to BSP plane for ( ; pedges != null; pedges = pnextedge) { pnextedge = pedges.pnext; // set the status for the last point as the previous point // FIXME: cache this stuff somehow? plastvert = pedges.v[0]; lastdist = mathlib.DotProduct(plastvert.position, tplane.normal) - tplane.dist; if (lastdist > 0) { lastside = 0; } else { lastside = 1; } pvert = pedges.v[1]; dist = mathlib.DotProduct(pvert.position, tplane.normal) - tplane.dist; if (dist > 0) { side = 0; } else { side = 1; } if (side != lastside) { // clipped if (numbverts >= MAX_BMODEL_VERTS) { return; } // generate the clipped vertex frac = lastdist / (lastdist - dist); ptvert = pbverts[numbverts++]; ptvert.position[0] = plastvert.position[0] + frac * (pvert.position[0] - plastvert.position[0]); ptvert.position[1] = plastvert.position[1] + frac * (pvert.position[1] - plastvert.position[1]); ptvert.position[2] = plastvert.position[2] + frac * (pvert.position[2] - plastvert.position[2]); // split into two edges, one on each side, and remember entering // and exiting points // FIXME: share the clip edge by having a winding direction flag? if (numbedges >= (MAX_BMODEL_EDGES - 1)) { console.Con_Printf("Out of edges for bmodel\n"); return; } ptedge = pbedges[numbedges]; ptedge.pnext = psideedges[lastside]; psideedges[lastside] = ptedge; ptedge.v[0] = plastvert; ptedge.v[1] = ptvert; ptedge = pbedges[numbedges + 1]; ptedge.pnext = psideedges[side]; psideedges[side] = ptedge; ptedge.v[0] = ptvert; ptedge.v[1] = pvert; numbedges += 2; if (side == 0) { // entering for front, exiting for back pfrontenter = ptvert; makeclippededge = true; } else { pfrontexit = ptvert; makeclippededge = true; } } else { // add the edge to the appropriate side pedges.pnext = psideedges[side]; psideedges[side] = pedges; } } // if anything was clipped, reconstitute and add the edges along the clip // plane to both sides (but in opposite directions) if (makeclippededge) { if (numbedges >= (MAX_BMODEL_EDGES - 2)) { console.Con_Printf("Out of edges for bmodel\n"); return; } ptedge = pbedges[numbedges]; ptedge.pnext = psideedges[0]; psideedges[0] = ptedge; ptedge.v[0] = pfrontexit; ptedge.v[1] = pfrontenter; ptedge = pbedges[numbedges + 1]; ptedge.pnext = psideedges[1]; psideedges[1] = ptedge; ptedge.v[0] = pfrontenter; ptedge.v[1] = pfrontexit; numbedges += 2; } // draw or recurse further for (i = 0; i < 2; i++) { if (psideedges[i] != null) { // draw if we've reached a non-solid leaf, done if all that's left is a // solid leaf, and continue down the tree if it's not a leaf pn = pnode.children[i]; // we're done with this branch if the node or leaf isn't in the PVS if (pn.visframe == r_visframecount) { if (pn.contents < 0) { if (pn.contents != bspfile.CONTENTS_SOLID) { r_currentbkey = ((model.mleaf_t)pn).key; R_RenderBmodelFace(psideedges[i], psurf); } } else { R_RecursiveClipBPoly(psideedges[i], (model.mnode_t)pnode.children[i], psurf); } } } } }
/* ================ R_RenderBmodelFace ================ */ static void R_RenderBmodelFace(bedge_t pedges, model.msurface_t psurf) { int i; uint mask; model.mplane_t pplane; double distinv; double[] p_normal = new double[3] {0, 0, 0}; model.medge_t tedge = new model.medge_t(); clipplane_t pclip; // skip out if no more surfs if (surface_p >= surf_max) { r_outofsurfaces++; return; } // ditto if not enough edges left, or switch to auxedges if possible if ((edge_p + psurf.numedges + 4) >= edge_max) { r_outofedges += psurf.numedges; return; } c_faceclip++; // this is a dummy to give the caching mechanism someplace to write to r_pedge = tedge; // set up clip planes pclip = null; for (i=3, mask = 0x08 ; i>=0 ; i--, mask >>= 1) { if ((r_clipflags & mask) != 0) { view_clipplanes[i].next = pclip; pclip = view_clipplanes[i]; } } // push the edges through r_emitted = 0; r_nearzi = 0; r_nearzionly = false; makeleftedge = makerightedge = false; // FIXME: keep clipped bmodel edges in clockwise order so last vertex caching // can be used? r_lastvertvalid = false; for ( ; pedges != null ; pedges = pedges.pnext) { r_leftclipped = r_rightclipped = false; R_ClipEdge (pedges.v[0], pedges.v[1], pclip); if (r_leftclipped) makeleftedge = true; if (r_rightclipped) makerightedge = true; } // if there was a clip off the left edge, add that edge too // FIXME: faster to do in screen space? // FIXME: share clipped edges? if (makeleftedge) { r_pedge = tedge; R_ClipEdge (r_leftexit, r_leftenter, pclip.next); } // if there was a clip off the right edge, get the right r_nearzi if (makerightedge) { r_pedge = tedge; r_nearzionly = true; R_ClipEdge (r_rightexit, r_rightenter, view_clipplanes[1].next); } // if no edges made it out, return without posting the surface if (r_emitted == 0) return; r_polycount++; surfaces[surface_p].data = psurf; surfaces[surface_p].nearzi = r_nearzi; surfaces[surface_p].flags = psurf.flags; surfaces[surface_p].insubmodel = true; surfaces[surface_p].spanstate = 0; surfaces[surface_p].entity = currententity; surfaces[surface_p].key = r_currentbkey; surfaces[surface_p].spans = null; pplane = psurf.plane; // FIXME: cache this? TransformVector (pplane.normal, p_normal); // FIXME: cache this? distinv = 1.0 / (pplane.dist - mathlib.DotProduct (modelorg, pplane.normal)); surfaces[surface_p].d_zistepu = p_normal[0] * xscaleinv * distinv; surfaces[surface_p].d_zistepv = -p_normal[1] * yscaleinv * distinv; surfaces[surface_p].d_ziorigin = p_normal[2] * distinv - xcenter * surfaces[surface_p].d_zistepu - ycenter * surfaces[surface_p].d_zistepv; //JDC VectorCopy (r_worldmodelorg, surface_p.modelorg); surface_p++; }