- if (surf->flags & SURF_PLANEBACK)
- VectorNegate(surf->plane->normal, normal);
- else
- VectorCopy(surf->plane->normal, normal);
- for (i = 0, j = surf->poly_numverts - 1;i < surf->poly_numverts;j = i, i++)
- {
- VectorSubtract(&surf->poly_verts[j * 3], &surf->poly_verts[i * 3], v1);
- VectorNormalizeFast(v1);
- CrossProduct(v1, normal, clipplanenormal);
- clipplanedist = DotProduct(&surf->poly_verts[i * 3], clipplanenormal);
- clipdist = DotProduct(out, clipplanenormal) - clipplanedist;
- if (clipdist > 0)
+ for (i = 0, e = (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle);i < surface->num_triangles;i++, e += 3)
+ {
+ // clip original point to each triangle of the surface and find the
+ // triangle that is closest
+ v[0] = surface->groupmesh->data_vertex3f + e[0] * 3;
+ v[1] = surface->groupmesh->data_vertex3f + e[1] * 3;
+ v[2] = surface->groupmesh->data_vertex3f + e[2] * 3;
+ TriangleNormal(v[0], v[1], v[2], facenormal);
+ VectorNormalize(facenormal);
+ offsetdist = DotProduct(v[0], facenormal) - DotProduct(p, facenormal);
+ VectorMA(p, offsetdist, facenormal, temp);
+ for (j = 0, k = 2;j < 3;k = j, j++)