diff --git a/src/hardware/hw_bsp.c b/src/hardware/hw_bsp.c index 6eab057f5..9e0362291 100644 --- a/src/hardware/hw_bsp.c +++ b/src/hardware/hw_bsp.c @@ -57,9 +57,11 @@ boolean gl_maphashorizonlines = false; // ========================================================================== //debug counters +#ifdef DEBUG_HWBSP static INT32 nobackpoly = 0; static INT32 skipcut = 0; static INT32 totalsubsecpolys = 0; +#endif // -------------------------------------------------------------------------- // Polygon fast alloc / free @@ -100,8 +102,8 @@ static polyvertex_t *fracdivline(fdivline_t *bsp, polyvertex_t *v1, double frac; double num; double den; - double v1x,v1y,v1dx,v1dy; - double v2x,v2y,v2dx,v2dy; + double v1x, v1y, v1dx, v1dy; + double v2x, v2y, v2dx, v2dy; // a segment of a polygon v1x = v1->x; @@ -116,6 +118,7 @@ static polyvertex_t *fracdivline(fdivline_t *bsp, polyvertex_t *v1, v2dy = bsp->dy; den = v2dy*v1dx - v2dx*v1dy; + if (fabsf((float)den) < 1.0E-36f) // avoid checking exactly for 0.0 return NULL; // parallel @@ -123,6 +126,7 @@ static polyvertex_t *fracdivline(fdivline_t *bsp, polyvertex_t *v1, // (do not accept hit with the extensions) num = (v2x - v1x)*v2dy + (v1y - v2y)*v2dx; frac = num / den; + if (frac < 0.0l || frac > 1.0l) return NULL; @@ -132,7 +136,6 @@ static polyvertex_t *fracdivline(fdivline_t *bsp, polyvertex_t *v1, frac = num / den; bspfrac = (float)frac; - // find the interception point along the partition line pt.x = (float)(v2x + v2dx*frac); pt.y = (float)(v2y + v2dy*frac); @@ -140,37 +143,18 @@ static polyvertex_t *fracdivline(fdivline_t *bsp, polyvertex_t *v1, return &pt; } +#define DIVLINE_VERTEX_DIFF 0.45f + // if two vertice coords have a x and/or y difference // of less or equal than 1 FRACUNIT, they are considered the same // point. Note: hardcoded value, 1.0f could be anything else. static boolean SameVertice (polyvertex_t *p1, polyvertex_t *p2) { -#if 0 - float diff; - diff = p2->x - p1->x; - if (diff < -1.5f || diff > 1.5f) + if (fabsf( p2->x - p1->x ) > DIVLINE_VERTEX_DIFF) return false; - diff = p2->y - p1->y; - if (diff < -1.5f || diff > 1.5f) + if (fabsf( p2->y - p1->y ) > DIVLINE_VERTEX_DIFF) return false; -#elif 0 - if (p1->x != p2->x) - return false; - if (p1->y != p2->y) - return false; -#elif 0 - if (fabsf( p2->x - p1->x ) > 1.0E-36f ) - return false; - if (fabsf( p2->y - p1->y ) > 1.0E-36f ) - return false; -#else -#define DIVLINE_VERTEX_DIFF 0.45f - static const float ep = DIVLINE_VERTEX_DIFF; - if (fabsf( p2->x - p1->x ) > ep ) - return false; - if (fabsf( p2->y - p1->y ) > ep ) - return false; -#endif + // p1 and p2 are considered the same vertex return true; } @@ -186,22 +170,23 @@ static void SplitPoly (fdivline_t *bsp, //splitting parametric line poly_t **frontpoly, //return one poly here poly_t **backpoly) //return the other here { - INT32 i,j; + INT32 i, j; polyvertex_t *pv; - INT32 ps = -1,pe = -1; - INT32 nptfront,nptback; + INT32 ps = -1, pe = -1; + INT32 nptfront, nptback; polyvertex_t vs = {0,0,0}; polyvertex_t ve = {0,0,0}; polyvertex_t lastpv = {0,0,0}; - float fracs = 0.0f,frace = 0.0f; //used to tell which poly is on - // the front side of the bsp partition line - INT32 psonline = 0, peonline = 0; + float fracs = 0.0f, frace = 0.0f; //used to tell which poly is on + // the front side of the bsp partition line + INT32 psonline = 0, peonline = 0; for (i = 0; i < poly->numpts; i++) { j = i + 1; - if (j == poly->numpts) j = 0; + if (j == poly->numpts) + j = 0; // start & end points pv = fracdivline(bsp, &poly->pts[i], &poly->pts[j]); @@ -288,7 +273,7 @@ static void SplitPoly (fdivline_t *bsp, //splitting parametric line return; } - if (pe <= ps) + if (UNLIKELY(pe <= ps)) I_Error("SplitPoly: invalid splitting line (%d %d)", ps, pe); // number of points on each side, _not_ counting those @@ -300,6 +285,7 @@ static void SplitPoly (fdivline_t *bsp, //splitting parametric line *backpoly = HWR_AllocPoly(2 + nptback); else *backpoly = NULL; + if (nptfront > 0) *frontpoly = HWR_AllocPoly(2 + nptfront); else @@ -312,10 +298,12 @@ static void SplitPoly (fdivline_t *bsp, //splitting parametric line *pv++ = vs; *pv++ = ve; i = pe; + do { if (++i == poly->numpts) i = 0; + *pv++ = poly->pts[i]; } while (i != ps && --nptfront); } @@ -327,10 +315,12 @@ static void SplitPoly (fdivline_t *bsp, //splitting parametric line *pv++ = ve; *pv++ = vs; i = ps; + do { if (++i == poly->numpts) i = 0; + *pv++ = poly->pts[i]; } while (i != pe && --nptback); } @@ -357,32 +347,29 @@ static void SplitPoly (fdivline_t *bsp, //splitting parametric line static poly_t *CutOutSubsecPoly(seg_t *lseg, INT32 count, poly_t *poly) { INT32 i, j; - polyvertex_t *pv; - INT32 nump = 0, ps, pe; polyvertex_t vs = {0, 0, 0}, ve = {0, 0, 0}, - p1 = {0, 0, 0}, p2 = {0, 0, 0}; + p1 = {0, 0, 0}, p2 = {0, 0, 0}; float fracs = 0.0f; - fdivline_t cutseg; // x, y, dx, dy as start of node_t struct - poly_t *temppoly; // for each seg of the subsector for (; count--; lseg++) { - //x,y,dx,dy (like a divline) + // x, y, dx, dy (like a divline) const line_t *line = lseg->linedef; // portal check - if (!gl_maphasportals && line->special == PORTALSPECIAL && lseg->side == 0) + if (UNLIKELY(!gl_maphasportals && line->special == PORTALSPECIAL && lseg->side == 0)) { // Find the other side! INT32 line2 = P_FindSpecialLineFromTag(PORTALSPECIAL, line->tag, -1); if (line == &lines[line2]) line2 = P_FindSpecialLineFromTag(PORTALSPECIAL, line->tag, line2); + if (line2 >= 0) // found it! gl_maphasportals = 1; } @@ -403,6 +390,7 @@ static poly_t *CutOutSubsecPoly(seg_t *lseg, INT32 count, poly_t *poly) // see if it cuts the convex poly ps = -1; pe = -1; + for (i = 0; i < poly->numpts; i++) { j = i + 1; @@ -445,6 +433,7 @@ static poly_t *CutOutSubsecPoly(seg_t *lseg, INT32 count, poly_t *poly) ve = vs; vs = *pv; } + //found 2nd point break; } @@ -458,25 +447,32 @@ static poly_t *CutOutSubsecPoly(seg_t *lseg, INT32 count, poly_t *poly) { // generate FRONT poly temppoly = HWR_AllocPoly(nump); + pv = temppoly->pts; *pv++ = vs; *pv++ = ve; + do { if (++ps == poly->numpts) ps = 0; *pv++ = poly->pts[ps]; } while (ps != pe); + HWR_FreePoly(poly); poly = temppoly; } - //hmmm... maybe we should NOT accept this, but this happens +#ifdef DEBUG_HWBSP + // hmmm... maybe we should NOT accept this, but this happens // only when the cut is not needed it seems (when the cut // line is aligned to one of the borders of the poly, and // only some times..) else + { skipcut++; - // I_Error("CutOutPoly: only one point for split line (%d %d) %d", ps, pe, debugpos); + I_Error("CutOutPoly: only one point for split line (%d %d) %d", ps, pe, debugpos); + } +#endif } } return poly; @@ -499,8 +495,10 @@ static inline void HWR_SubsecPoly(INT32 num, poly_t *poly) if (poly) { - poly = CutOutSubsecPoly (lseg,count,poly); + poly = CutOutSubsecPoly(lseg,count,poly); +#ifdef DEBUG_HWBSP totalsubsecpolys++; +#endif //extra data for this subsector extrasubsectors[num].planepoly = poly; } @@ -510,8 +508,8 @@ static inline void HWR_SubsecPoly(INT32 num, poly_t *poly) // search for the segs source of this divline static inline void SearchDivline(node_t *bsp, fdivline_t *divline) { - divline->x = FIXED_TO_FLOAT(bsp->x); - divline->y = FIXED_TO_FLOAT(bsp->y); + divline->x = FIXED_TO_FLOAT(bsp->x); + divline->y = FIXED_TO_FLOAT(bsp->y); divline->dx = FIXED_TO_FLOAT(bsp->dx); divline->dy = FIXED_TO_FLOAT(bsp->dy); } @@ -569,10 +567,12 @@ static void WalkBSPNode(INT32 bspnum, poly_t *poly, UINT16 *leafnode, fixed_t *b if (poly && poly->numpts > 2) { CONS_Debug(DBG_RENDER, "Adding a new subsector\n"); - if (addsubsector == numsubsectors + NEWSUBSECTORS) + + if (UNLIKELY(addsubsector == numsubsectors + NEWSUBSECTORS)) I_Error("WalkBSPNode: not enough addsubsectors\n"); - else if (addsubsector > 0x7fff) + else if (UNLIKELY(addsubsector > 0x7fff)) I_Error("WalkBSPNode: addsubsector > 0x7fff\n"); + *leafnode = (UINT16)((UINT16)addsubsector | NF_SUBSECTOR); extrasubsectors[addsubsector].planepoly = poly; addsubsector++; @@ -594,6 +594,7 @@ static void WalkBSPNode(INT32 bspnum, poly_t *poly, UINT16 *leafnode, fixed_t *b } #endif } + M_ClearBox(bbox); poly = extrasubsectors[bspnum & ~NF_SUBSECTOR].planepoly; @@ -608,12 +609,14 @@ static void WalkBSPNode(INT32 bspnum, poly_t *poly, UINT16 *leafnode, fixed_t *b SplitPoly(&fdivline, poly, &frontpoly, &backpoly); poly = NULL; +#ifdef DEBUG_HWBSP //debug if (!backpoly) nobackpoly++; +#endif // Recursively divide front space. - if (frontpoly) + if (LIKELY(frontpoly)) { WalkBSPNode(bsp->children[0], frontpoly, &bsp->children[0],bsp->bbox[0]); @@ -648,9 +651,11 @@ void HWR_FreeExtraSubsectors(void) #define MAXDIST 1.5f // BP: can't move vertex: DON'T change polygon geometry! (convex) //#define MOVEVERTEX + +// Is vertex va within the seg v1, v2 static boolean PointInSeg(polyvertex_t *a,polyvertex_t *v1,polyvertex_t *v2) { - register float ax,ay,bx,by,cx,cy,d,norm; + register float ax, ay, bx, by, cx, cy, d, norm; register polyvertex_t *p; // check bbox of the seg first @@ -677,6 +682,7 @@ static boolean PointInSeg(polyvertex_t *a,polyvertex_t *v1,polyvertex_t *v2) ax= v2->x-v1->x; ay= v2->y-v1->y; norm = hypotf(ax, ay); + #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wfloat-equal" if (norm != 0) // yes, this can be exactly 0 @@ -685,13 +691,16 @@ static boolean PointInSeg(polyvertex_t *a,polyvertex_t *v1,polyvertex_t *v2) ax /= norm; ay /= norm; } + bx = a->x-v1->x; by = a->y-v1->y; //d = a.b d =ax*bx+ay*by; + // bound of the seg if (d < 0 || d > norm) return false; + //c = d.1a-b cx = ax*d-bx; cy = ay*d-by; @@ -715,7 +724,7 @@ static INT32 numsplitpoly; static void SearchSegInBSP(INT32 bspnum,polyvertex_t *p,poly_t *poly) { poly_t *q; - INT32 j,k; + INT32 j,k; if (bspnum & NF_SUBSECTOR) { @@ -723,47 +732,55 @@ static void SearchSegInBSP(INT32 bspnum,polyvertex_t *p,poly_t *poly) { bspnum &= ~NF_SUBSECTOR; q = extrasubsectors[bspnum].planepoly; + if (poly == q || !q) return; - for (j = 0; j < q->numpts; j++) + + const INT32 numpts = q->numpts; + + for (j = 0; j < numpts; j++) { k = j+1; - if (k == q->numpts) k = 0; + if (k == numpts) + k = 0; + if (!SameVertice(p, &q->pts[j]) && !SameVertice(p, &q->pts[k]) - && PointInSeg(p, &q->pts[j], - &q->pts[k])) + && PointInSeg(p, &q->pts[j], &q->pts[k])) { - poly_t *newpoly = HWR_AllocPoly(q->numpts+1); + poly_t *newpoly = HWR_AllocPoly(numpts+1); INT32 n; for (n = 0; n <= j; n++) newpoly->pts[n] = q->pts[n]; + newpoly->pts[k] = *p; + for (n = k+1; n < newpoly->numpts; n++) newpoly->pts[n] = q->pts[n-1]; + numsplitpoly++; - extrasubsectors[bspnum].planepoly = - newpoly; + extrasubsectors[bspnum].planepoly = newpoly; HWR_FreePoly(q); return; } } } + return; } if ((FIXED_TO_FLOAT(nodes[bspnum].bbox[0][BOXBOTTOM])-MAXDIST <= p->y) && - (FIXED_TO_FLOAT(nodes[bspnum].bbox[0][BOXTOP ])+MAXDIST >= p->y) && - (FIXED_TO_FLOAT(nodes[bspnum].bbox[0][BOXLEFT ])-MAXDIST <= p->x) && - (FIXED_TO_FLOAT(nodes[bspnum].bbox[0][BOXRIGHT ])+MAXDIST >= p->x)) - SearchSegInBSP(nodes[bspnum].children[0],p,poly); + (FIXED_TO_FLOAT(nodes[bspnum].bbox[0][BOXTOP ])+MAXDIST >= p->y) && + (FIXED_TO_FLOAT(nodes[bspnum].bbox[0][BOXLEFT ])-MAXDIST <= p->x) && + (FIXED_TO_FLOAT(nodes[bspnum].bbox[0][BOXRIGHT ])+MAXDIST >= p->x)) + SearchSegInBSP(nodes[bspnum].children[0], p, poly); if ((FIXED_TO_FLOAT(nodes[bspnum].bbox[1][BOXBOTTOM])-MAXDIST <= p->y) && - (FIXED_TO_FLOAT(nodes[bspnum].bbox[1][BOXTOP ])+MAXDIST >= p->y) && - (FIXED_TO_FLOAT(nodes[bspnum].bbox[1][BOXLEFT ])-MAXDIST <= p->x) && - (FIXED_TO_FLOAT(nodes[bspnum].bbox[1][BOXRIGHT ])+MAXDIST >= p->x)) - SearchSegInBSP(nodes[bspnum].children[1],p,poly); + (FIXED_TO_FLOAT(nodes[bspnum].bbox[1][BOXTOP ])+MAXDIST >= p->y) && + (FIXED_TO_FLOAT(nodes[bspnum].bbox[1][BOXLEFT ])-MAXDIST <= p->x) && + (FIXED_TO_FLOAT(nodes[bspnum].bbox[1][BOXRIGHT ])+MAXDIST >= p->x)) + SearchSegInBSP(nodes[bspnum].children[1], p, poly); } // search for T-intersection problem @@ -787,13 +804,18 @@ static INT32 SolveTProblem(void) for (l = 0; l < addsubsector; l++) { p = extrasubsectors[l].planepoly; + if (!p) continue; for (i = 0; i < p->numpts; i++) SearchSegInBSP((INT32)numnodes-1, &p->pts[i], p); } - //CONS_Debug(DBG_RENDER, "numsplitpoly %d\n", numsplitpoly); + +#ifdef DEBUG_HWBSP + CONS_Debug(DBG_RENDER, "numsplitpoly %d\n", numsplitpoly); +#endif + return numsplitpoly; } @@ -833,29 +855,31 @@ static void AdjustSegs(void) if (lseg->polyseg) continue; - if (p) { - for (j = 0; j < p->numpts; j++) + for (j = 0; j < p->numpts; j++) + { + distv1 = p->pts[j].x - FIXED_TO_FLOAT(lseg->v1->x); + tmp = p->pts[j].y - FIXED_TO_FLOAT(lseg->v1->y); + distv1 = distv1*distv1+tmp*tmp; + + if (distv1 <= nearv1) { - distv1 = p->pts[j].x - FIXED_TO_FLOAT(lseg->v1->x); - tmp = p->pts[j].y - FIXED_TO_FLOAT(lseg->v1->y); - distv1 = distv1*distv1+tmp*tmp; - if (distv1 <= nearv1) - { - v1found = j; - nearv1 = distv1; - } - // the same with v2 - distv2 = p->pts[j].x - FIXED_TO_FLOAT(lseg->v2->x); - tmp = p->pts[j].y - FIXED_TO_FLOAT(lseg->v2->y); - distv2 = distv2*distv2+tmp*tmp; - if (distv2 <= nearv2) - { - v2found = j; - nearv2 = distv2; - } + v1found = j; + nearv1 = distv1; + } + + // the same with v2 + distv2 = p->pts[j].x - FIXED_TO_FLOAT(lseg->v2->x); + tmp = p->pts[j].y - FIXED_TO_FLOAT(lseg->v2->y); + distv2 = distv2*distv2+tmp*tmp; + + if (distv2 <= nearv2) + { + v2found = j; + nearv2 = distv2; } } - if (p && nearv1 <= NEARDIST*NEARDIST) + + if (nearv1 <= NEARDIST*NEARDIST) // share vertice with segs lseg->pv1 = &(p->pts[v1found]); else @@ -870,7 +894,8 @@ static void AdjustSegs(void) pv->y = FIXED_TO_FLOAT(lseg->v1->y); lseg->pv1 = pv; } - if (p && nearv2 <= NEARDIST*NEARDIST) + + if (nearv2 <= NEARDIST*NEARDIST) lseg->pv2 = &(p->pts[v2found]); else { @@ -883,11 +908,15 @@ static void AdjustSegs(void) // recompute length { float x,y; - x = ((polyvertex_t *)lseg->pv2)->x - ((polyvertex_t *)lseg->pv1)->x - + FIXED_TO_FLOAT(FRACUNIT/2); - y = ((polyvertex_t *)lseg->pv2)->y - ((polyvertex_t *)lseg->pv1)->y - + FIXED_TO_FLOAT(FRACUNIT/2); - lseg->flength = (float)hypot(x, y); + + const polyvertex_t *pv1 = (polyvertex_t *)lseg->pv1; + const polyvertex_t *pv2 = (polyvertex_t *)lseg->pv2; + + x = pv2->x - pv1->x + 0.5f; + y = pv2->y - pv1->y + 0.5f; + + lseg->flength = hypotf(x, y); + // BP: debug see this kind of segs //if (nearv2 > NEARDIST*NEARDIST || nearv1 > NEARDIST*NEARDIST) // lseg->length = 1; @@ -917,25 +946,25 @@ void HWR_CreatePlanePolygons(INT32 bspnum) gl_maphasportals = gl_maphashorizonlines = 0; // find min/max boundaries of map - //CONS_Debug(DBG_RENDER, "Looking for boundaries of map...\n"); +#ifdef DEBUG_HWBSP + CONS_Debug(DBG_RENDER, "Looking for boundaries of map...\n"); +#endif M_ClearBox(rootbbox); for (i = 0;i < numvertexes; i++) M_AddToBox(rootbbox, vertexes[i].x, vertexes[i].y); - //CONS_Debug(DBG_RENDER, "Generating subsector polygons... %d subsectors\n", numsubsectors); +#ifdef DEBUG_HWBSP + CONS_Debug(DBG_RENDER, "Generating subsector polygons... %d subsectors\n", numsubsectors); +#endif HWR_FreeExtraSubsectors(); + // allocate extra data for each subsector present in map totsubsectors = numsubsectors + NEWSUBSECTORS; extrasubsectors = calloc(totsubsectors, sizeof (*extrasubsectors)); - if (extrasubsectors == NULL) + if (UNLIKELY(extrasubsectors == NULL)) I_Error("couldn't malloc extrasubsectors totsubsectors %s\n", sizeu1(totsubsectors)); - // allocate table for back to front drawing of subsectors - /*gl_drawsubsectors = (INT16 *)malloc(sizeof (*gl_drawsubsectors) * totsubsectors); - if (!gl_drawsubsectors) - I_Error("couldn't malloc gl_drawsubsectors\n");*/ - // number of the first new subsector that might be added addsubsector = numsubsectors; @@ -959,17 +988,21 @@ void HWR_CreatePlanePolygons(INT32 bspnum) WalkBSPNode(bspnum, rootp, NULL,rootbbox); i = SolveTProblem(); - //CONS_Debug(DBG_RENDER, "%d point divides a polygon line\n",i); +#ifdef DEBUG_HWBSP + CONS_Debug(DBG_RENDER, "%d point divides a polygon line\n",i); +#endif AdjustSegs(); +#ifdef DEBUG_HWBSP //debug debug.. - //if (nobackpoly) - // CONS_Debug(DBG_RENDER, "no back polygon %u times\n",nobackpoly); + if (nobackpoly) + CONS_Debug(DBG_RENDER, "no back polygon %u times\n",nobackpoly); //"(should happen only with the deep water trick)" - //if (skipcut) - // CONS_Debug(DBG_RENDER, "%u cuts were skipped because of only one point\n",skipcut); + if (skipcut) + CONS_Debug(DBG_RENDER, "%u cuts were skipped because of only one point\n",skipcut); - //CONS_Debug(DBG_RENDER, "done: %u total subsector convex polygons\n", totalsubsecpolys); + CONS_Debug(DBG_RENDER, "done: %u total subsector convex polygons\n", totalsubsecpolys); +#endif } #endif //HWRENDER