Tries to split a polygon across a plane
| 319 | |
| 320 | // Tries to split a polygon across a plane |
| 321 | int SplitPolygon(bspplane *plane, bsppolygon *testpoly, bsppolygon **frontpoly, bsppolygon **backpoly) { |
| 322 | float dists[256], t; |
| 323 | int numvert, numfront, numback, i, codes[256] = {}; |
| 324 | vector *frontvert[256], *backvert[256], *polyvert[256]; |
| 325 | vector *vertptr1, *vertptr2; |
| 326 | vector delta, *newvert[256]; |
| 327 | int num_new_verts = 0; |
| 328 | |
| 329 | /* Set up the function. Set all counters, variables, lists etc to |
| 330 | * zero or NULL, which provides a fall back result, should nothing |
| 331 | * be changed, and simply means that particular result is to be |
| 332 | * ignored |
| 333 | */ |
| 334 | |
| 335 | numvert = testpoly->nv; |
| 336 | numfront = numback = 0; |
| 337 | |
| 338 | /* Now, we shall classify each vertex in the polygon, with both |
| 339 | * a plane distance, and a classification code. This is done here, |
| 340 | * as the results are often re-used, which saves time. |
| 341 | */ |
| 342 | |
| 343 | for (i = 0; i < numvert; i++) { |
| 344 | vertptr1 = &testpoly->verts[i]; |
| 345 | polyvert[i] = vertptr1; |
| 346 | codes[i] = ClassifyVector(plane, vertptr1); |
| 347 | dists[i] = plane->a * vertptr1->x + plane->b * vertptr1->y + plane->c * vertptr1->z + plane->d; |
| 348 | } |
| 349 | |
| 350 | /* We must duplicate the first entry in the numvert+1 slot, so that |
| 351 | * we can use wraparound to easily check things |
| 352 | */ |
| 353 | |
| 354 | codes[numvert] = codes[0]; |
| 355 | dists[numvert] = dists[0]; |
| 356 | |
| 357 | /* Now, the actual splitting work. We must work through each vertex |
| 358 | * of the polygon, examining it with regard to the plane |
| 359 | * Where the classification codes differ, we have to split |
| 360 | */ |
| 361 | |
| 362 | for (i = 0; i < numvert; i++) { |
| 363 | vertptr1 = polyvert[i]; |
| 364 | |
| 365 | /* A point is found to be on the plane. This counts for |
| 366 | * both polygons, as often a vertex may lie along a plane, |
| 367 | * but will not be splitting it |
| 368 | */ |
| 369 | |
| 370 | if (codes[i] == BSP_ON_PLANE) { |
| 371 | frontvert[numfront++] = vertptr1; |
| 372 | backvert[numback++] = vertptr1; |
| 373 | } else if (codes[i] == BSP_IN_FRONT) { |
| 374 | /* Simple cases of point being in front or behind |
| 375 | * the plane. Just insert them to the appropriate |
| 376 | * lists. |
| 377 | */ |
| 378 | frontvert[numfront++] = vertptr1; |
no test coverage detected