Fix infinite loop in R_TerrainHeightForPoly

This commit is contained in:
smallmodel 2024-09-04 20:46:10 +02:00
parent 1e0195f6e3
commit c0d37c504a
No known key found for this signature in database
GPG key ID: 9F2D623CEDF08512

View file

@ -1229,6 +1229,7 @@ qboolean R_TerrainHeightForPoly(cTerraPatchUnpacked_t *pPatch, polyVert_t *pVert
float x = pVerts->xyz[0];
float y = pVerts->xyz[1];
terraInt iTri;
// Calculate the average of the x and y coordinates of all vertices
if (nVerts > 1) {
@ -1243,24 +1244,10 @@ qboolean R_TerrainHeightForPoly(cTerraPatchUnpacked_t *pPatch, polyVert_t *pVert
}
// Get the first valid triangle in the patch
terraInt iTri = pPatch->drawinfo.iTriHead;
if (!iTri) {
assert(
!pPatch->drawinfo.iTriHead
&& va(
"R_TerrainHeightForPoly: point(%f %f) not in patch(%f %f %f)\n",
x,
y,
pPatch->x0,
pPatch->y0,
pPatch->z0
)
);
return qfalse;
}
iTri = pPatch->drawinfo.iTriHead;
// Find a triangle that contains the point (x, y)
while (qtrue) {
for(iTri = pPatch->drawinfo.iTriHead; iTri; iTri = g_pTris[iTri].iNext) {
if (g_pTris[iTri].byConstChecks & 4) {
// Get all three x-y coordinates of the current triangle's vertices
x0 = g_pVert[g_pTris[iTri].iPt[0]].xyz[0];
@ -1291,30 +1278,11 @@ qboolean R_TerrainHeightForPoly(cTerraPatchUnpacked_t *pPatch, polyVert_t *pVert
fAreaTotal = fArea[0] + fArea[1] + fArea[2];
if (fAreaTotal > 0.0) {
//
// Found it - the point is inside the triangle
break;
}
}
iTri = g_pTris[iTri].iNext;
if (!iTri) {
// There's no triangle that contains point (x, y) - bail out
assert(
!g_pTris[iTri].iNext
&& va(
"R_TerrainHeightForPoly: point(%f %f) not in patch(%f %f %f)\n",
x,
y,
pPatch->x0,
pPatch->y0,
pPatch->z0
)
);
return qfalse;
}
}
// Calculate the barycentric coordinates (fKx, fKy, fKz) of the triangle
//
float z0 = g_pVert[g_pTris[iTri].iPt[0]].xyz[2] / fAreaTotal;
float z1 = g_pVert[g_pTris[iTri].iPt[1]].xyz[2] / fAreaTotal;
float z2 = g_pVert[g_pTris[iTri].iPt[2]].xyz[2] / fAreaTotal;
@ -1343,6 +1311,23 @@ qboolean R_TerrainHeightForPoly(cTerraPatchUnpacked_t *pPatch, polyVert_t *pVert
}
return qtrue;
}
}
}
assert(
!pPatch->drawinfo.iTriHead
&& va(
"R_TerrainHeightForPoly: point(%f %f) not in patch(%f %f %f)\n",
x,
y,
pPatch->x0,
pPatch->y0,
pPatch->z0
)
);
return qfalse;
}
void R_TerrainRestart_f(void)