fix and improve polygon clipping

Sets the vertex count to zero when the polygon is invalid
Stops clipping when the vertex count drops below 3
This commit is contained in:
Bigfoot71 2025-05-18 03:49:11 +02:00
parent 8624edf63d
commit 0b08e75a2e

11
src/external/rlsw.h vendored
View File

@ -2302,7 +2302,10 @@ static inline bool sw_polygon_clip(sw_vertex_t polygon[SW_MAX_CLIPPED_POLYGON_VE
#define CLIP_AGAINST_PLANE(FUNC_CLIP) \ #define CLIP_AGAINST_PLANE(FUNC_CLIP) \
{ \ { \
n = FUNC_CLIP(tmp, polygon, n); \ n = FUNC_CLIP(tmp, polygon, n); \
if (n == 0) return false; \ if (n < 3) { \
*vertexCounter = 0; \
return false; \
} \
for (int i = 0; i < n; i++) { \ for (int i = 0; i < n; i++) { \
polygon[i] = tmp[i]; \ polygon[i] = tmp[i]; \
} \ } \
@ -2325,7 +2328,7 @@ static inline bool sw_polygon_clip(sw_vertex_t polygon[SW_MAX_CLIPPED_POLYGON_VE
*vertexCounter = n; *vertexCounter = n;
return n > 0; return (n >= 3);
} }
@ -2366,7 +2369,7 @@ static inline void sw_triangle_clip_and_project(void)
sw_vertex_t* polygon = RLSW.vertexBuffer; sw_vertex_t* polygon = RLSW.vertexBuffer;
int* vertexCounter = &RLSW.vertexCounter; int* vertexCounter = &RLSW.vertexCounter;
if (sw_polygon_clip(polygon, vertexCounter) && *vertexCounter >= 3) { if (sw_polygon_clip(polygon, vertexCounter)) {
// Transformation to screen space and normalization // Transformation to screen space and normalization
for (int i = 0; i < *vertexCounter; i++) { for (int i = 0; i < *vertexCounter; i++) {
@ -2751,7 +2754,7 @@ static inline void sw_quad_clip_and_project(void)
sw_vertex_t* polygon = RLSW.vertexBuffer; sw_vertex_t* polygon = RLSW.vertexBuffer;
int* vertexCounter = &RLSW.vertexCounter; int* vertexCounter = &RLSW.vertexCounter;
if (sw_polygon_clip(polygon, vertexCounter) && *vertexCounter >= 3) { if (sw_polygon_clip(polygon, vertexCounter)) {
// Transformation to screen space and normalization // Transformation to screen space and normalization
for (int i = 0; i < *vertexCounter; i++) { for (int i = 0; i < *vertexCounter; i++) {