2002-09-19 18:51:19 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
2002-09-19 16:58:48 +00:00
|
|
|
|
2002-09-19 18:51:19 +00:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
2002-09-19 16:58:48 +00:00
|
|
|
|
2002-09-19 18:51:19 +00:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2002-09-19 16:58:48 +00:00
|
|
|
|
2002-09-19 18:51:19 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2002-09-19 16:58:48 +00:00
|
|
|
|
2002-09-19 18:51:19 +00:00
|
|
|
See file, 'COPYING', for details.
|
2002-09-19 16:58:48 +00:00
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2005-08-04 15:27:09 +00:00
|
|
|
static __attribute__ ((used)) const char rcsid[] =
|
2003-01-15 15:31:36 +00:00
|
|
|
"$Id$";
|
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
#include "QF/sys.h"
|
|
|
|
|
2010-08-18 08:52:13 +00:00
|
|
|
#include "brush.h"
|
2002-09-19 16:58:48 +00:00
|
|
|
#include "bsp5.h"
|
2010-08-18 08:52:13 +00:00
|
|
|
#include "csg4.h"
|
|
|
|
#include "draw.h"
|
|
|
|
#include "merge.h"
|
2010-08-29 03:43:07 +00:00
|
|
|
#include "surfaces.h"
|
2010-08-18 08:52:13 +00:00
|
|
|
#include "winding.h"
|
2002-09-19 16:58:48 +00:00
|
|
|
|
|
|
|
#define CONTINUOUS_EPSILON 0.001
|
|
|
|
|
|
|
|
|
2010-08-29 11:35:43 +00:00
|
|
|
/** Try to merge two polygons.
|
2002-09-19 16:58:48 +00:00
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
If two polygons share a common edge and the edges that meet at the common
|
2010-08-29 11:35:43 +00:00
|
|
|
points are both inside the other polygons, merge them. The two polygons
|
|
|
|
must be on the same plane, the same side of the plane, have the same
|
|
|
|
texture and have the same contents on each side.
|
|
|
|
|
|
|
|
\param f1 The first face.
|
|
|
|
\param f2 The second face.
|
|
|
|
\return The new face or NULL if the faces could not be merged.
|
2002-09-19 16:58:48 +00:00
|
|
|
|
2010-08-29 11:35:43 +00:00
|
|
|
\note The originals will NOT be freed.
|
2002-09-19 16:58:48 +00:00
|
|
|
*/
|
2010-08-29 11:35:43 +00:00
|
|
|
static face_t *
|
|
|
|
TryMerge (const face_t *f1, const face_t *f2)
|
2002-09-19 16:58:48 +00:00
|
|
|
{
|
2002-09-19 17:14:23 +00:00
|
|
|
face_t *newf;
|
|
|
|
int i, j, k, l;
|
|
|
|
plane_t *plane;
|
|
|
|
qboolean keep1, keep2;
|
2002-09-19 18:51:19 +00:00
|
|
|
vec3_t normal, delta, planenormal;
|
|
|
|
vec_t dot;
|
|
|
|
vec_t *p1, *p2, *p3, *p4, *back;
|
2003-09-08 03:00:53 +00:00
|
|
|
winding_t *f1p, *f2p, *newfp;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2003-09-08 03:00:53 +00:00
|
|
|
if (!(f1p = f1->points) || !(f2p = f2->points))
|
2002-09-19 16:58:48 +00:00
|
|
|
return NULL;
|
|
|
|
if (f1->planeside != f2->planeside)
|
|
|
|
return NULL;
|
|
|
|
if (f1->texturenum != f2->texturenum)
|
|
|
|
return NULL;
|
|
|
|
if (f1->contents[0] != f2->contents[0])
|
|
|
|
return NULL;
|
|
|
|
if (f1->contents[1] != f2->contents[1])
|
|
|
|
return NULL;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2010-08-29 11:35:43 +00:00
|
|
|
p1 = p2 = NULL;
|
2002-09-19 18:51:19 +00:00
|
|
|
j = 0;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2010-08-29 11:35:43 +00:00
|
|
|
// find a common edge
|
2003-09-08 03:00:53 +00:00
|
|
|
for (i = 0; i < f1p->numpoints; i++) {
|
|
|
|
p1 = f1p->points[i];
|
|
|
|
p2 = f1p->points[(i + 1) % f1p->numpoints];
|
|
|
|
for (j = 0; j < f2p->numpoints; j++) {
|
|
|
|
p3 = f2p->points[j];
|
|
|
|
p4 = f2p->points[(j + 1) % f2p->numpoints];
|
2002-09-19 17:14:23 +00:00
|
|
|
for (k = 0; k < 3; k++) {
|
|
|
|
if (fabs (p1[k] - p4[k]) > EQUAL_EPSILON)
|
2002-09-19 16:58:48 +00:00
|
|
|
break;
|
2002-09-19 17:14:23 +00:00
|
|
|
if (fabs (p2[k] - p3[k]) > EQUAL_EPSILON)
|
2002-09-19 16:58:48 +00:00
|
|
|
break;
|
|
|
|
}
|
2002-09-19 17:14:23 +00:00
|
|
|
if (k == 3)
|
2003-09-08 03:00:53 +00:00
|
|
|
goto found_edge;
|
2002-09-19 16:58:48 +00:00
|
|
|
}
|
|
|
|
}
|
2003-09-08 03:00:53 +00:00
|
|
|
return NULL; // no matching edges
|
|
|
|
found_edge:
|
2002-09-23 16:27:17 +00:00
|
|
|
// check slope of connected lines
|
|
|
|
// if the slopes are colinear, the point can be removed
|
2002-09-19 16:58:48 +00:00
|
|
|
plane = &planes[f1->planenum];
|
|
|
|
VectorCopy (plane->normal, planenormal);
|
|
|
|
if (f1->planeside)
|
2003-09-03 22:00:08 +00:00
|
|
|
VectorNegate (planenormal, planenormal);
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2003-09-08 03:00:53 +00:00
|
|
|
back = f1p->points[(i + f1p->numpoints - 1) % f1p->numpoints];
|
2002-09-19 16:58:48 +00:00
|
|
|
VectorSubtract (p1, back, delta);
|
|
|
|
CrossProduct (planenormal, delta, normal);
|
2002-09-25 21:35:49 +00:00
|
|
|
_VectorNormalize (normal);
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2003-09-08 03:00:53 +00:00
|
|
|
back = f2p->points[(j + 2) % f2p->numpoints];
|
2002-09-19 16:58:48 +00:00
|
|
|
VectorSubtract (back, p1, delta);
|
|
|
|
dot = DotProduct (delta, normal);
|
|
|
|
if (dot > CONTINUOUS_EPSILON)
|
2002-09-19 17:14:23 +00:00
|
|
|
return NULL; // not a convex polygon
|
2002-09-19 16:58:48 +00:00
|
|
|
keep1 = dot < -CONTINUOUS_EPSILON;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2003-09-08 03:00:53 +00:00
|
|
|
back = f1p->points[(i + 2) % f1p->numpoints];
|
2002-09-19 16:58:48 +00:00
|
|
|
VectorSubtract (back, p2, delta);
|
|
|
|
CrossProduct (planenormal, delta, normal);
|
2002-09-25 21:35:49 +00:00
|
|
|
_VectorNormalize (normal);
|
2002-09-19 16:58:48 +00:00
|
|
|
|
2003-09-08 03:00:53 +00:00
|
|
|
back = f2p->points[(j + f2p->numpoints - 1) % f2p->numpoints];
|
2002-09-19 16:58:48 +00:00
|
|
|
VectorSubtract (back, p2, delta);
|
|
|
|
dot = DotProduct (delta, normal);
|
|
|
|
if (dot > CONTINUOUS_EPSILON)
|
2002-09-19 17:14:23 +00:00
|
|
|
return NULL; // not a convex polygon
|
2002-09-19 16:58:48 +00:00
|
|
|
keep2 = dot < -CONTINUOUS_EPSILON;
|
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
// build the new polygon
|
2002-09-19 16:58:48 +00:00
|
|
|
|
2003-09-08 03:00:53 +00:00
|
|
|
k = f1p->numpoints + f2p->numpoints - 4 + keep1 + keep2;
|
2002-09-19 16:58:48 +00:00
|
|
|
newf = NewFaceFromFace (f1);
|
2003-09-08 03:00:53 +00:00
|
|
|
newfp = newf->points = NewWinding (k);
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
// copy first polygon
|
2003-09-08 03:00:53 +00:00
|
|
|
for (k = (i + 1) % f1p->numpoints; k != i; k = (k + 1) % f1p->numpoints) {
|
|
|
|
if (k == (i + 1) % f1p->numpoints && !keep2)
|
2002-09-19 16:58:48 +00:00
|
|
|
continue;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2003-09-08 03:00:53 +00:00
|
|
|
VectorCopy (f1p->points[k], newfp->points[newfp->numpoints]);
|
|
|
|
newfp->numpoints++;
|
2002-09-19 16:58:48 +00:00
|
|
|
}
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
// copy second polygon
|
2003-09-08 03:00:53 +00:00
|
|
|
for (l = (j + 1) % f2p->numpoints; l != j; l = (l + 1) % f2p->numpoints) {
|
|
|
|
if (l == (j + 1) % f2p->numpoints && !keep1)
|
2002-09-19 16:58:48 +00:00
|
|
|
continue;
|
2003-09-08 03:00:53 +00:00
|
|
|
VectorCopy (f2p->points[l], newfp->points[newfp->numpoints]);
|
|
|
|
newfp->numpoints++;
|
2002-09-19 16:58:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return newf;
|
|
|
|
}
|
|
|
|
|
2002-09-19 17:14:23 +00:00
|
|
|
qboolean mergedebug;
|
2010-08-29 11:35:43 +00:00
|
|
|
face_t *
|
2002-09-19 18:51:19 +00:00
|
|
|
MergeFaceToList (face_t *face, face_t *list)
|
2002-09-19 16:58:48 +00:00
|
|
|
{
|
2002-09-19 17:14:23 +00:00
|
|
|
face_t *newf, *f;
|
|
|
|
|
|
|
|
for (f = list; f; f = f->next) {
|
2010-08-27 13:12:13 +00:00
|
|
|
// CheckColinear (f);
|
2002-09-19 17:14:23 +00:00
|
|
|
if (mergedebug) {
|
|
|
|
Draw_ClearWindow ();
|
|
|
|
Draw_DrawFace (face);
|
|
|
|
Draw_DrawFace (f);
|
|
|
|
Draw_SetBlack ();
|
|
|
|
}
|
2002-09-19 16:58:48 +00:00
|
|
|
newf = TryMerge (face, f);
|
|
|
|
if (!newf)
|
|
|
|
continue;
|
|
|
|
FreeFace (face);
|
2003-09-08 03:00:53 +00:00
|
|
|
FreeWinding (f->points); // merged out
|
|
|
|
f->points = 0;
|
2002-09-19 16:58:48 +00:00
|
|
|
return MergeFaceToList (newf, list);
|
|
|
|
}
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
// didn't merge, so add at start
|
2002-09-19 16:58:48 +00:00
|
|
|
face->next = list;
|
|
|
|
return face;
|
|
|
|
}
|
|
|
|
|
2010-08-29 11:35:43 +00:00
|
|
|
face_t *
|
2002-09-19 18:51:19 +00:00
|
|
|
FreeMergeListScraps (face_t *merged)
|
2002-09-19 16:58:48 +00:00
|
|
|
{
|
2002-09-19 17:14:23 +00:00
|
|
|
face_t *head, *next;
|
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
head = NULL;
|
2002-09-19 17:14:23 +00:00
|
|
|
for (; merged; merged = next) {
|
2002-09-19 16:58:48 +00:00
|
|
|
next = merged->next;
|
2003-09-08 03:00:53 +00:00
|
|
|
if (!merged->points)
|
2002-09-19 16:58:48 +00:00
|
|
|
FreeFace (merged);
|
2002-09-19 17:14:23 +00:00
|
|
|
else {
|
2002-09-19 16:58:48 +00:00
|
|
|
merged->next = head;
|
|
|
|
head = merged;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return head;
|
|
|
|
}
|
|
|
|
|
2002-09-19 17:14:23 +00:00
|
|
|
void
|
2002-09-19 18:51:19 +00:00
|
|
|
MergePlaneFaces (surface_t *plane)
|
2002-09-19 16:58:48 +00:00
|
|
|
{
|
2002-09-19 18:51:19 +00:00
|
|
|
face_t *merged, *next, *f1;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
merged = NULL;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
|
|
|
for (f1 = plane->faces; f1; f1 = next) {
|
2002-09-19 16:58:48 +00:00
|
|
|
next = f1->next;
|
|
|
|
merged = MergeFaceToList (f1, merged);
|
|
|
|
}
|
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
// chain all of the non-empty faces to the plane
|
2002-09-19 16:58:48 +00:00
|
|
|
plane->faces = FreeMergeListScraps (merged);
|
|
|
|
}
|
|
|
|
|
2002-09-19 17:14:23 +00:00
|
|
|
void
|
|
|
|
MergeAll (surface_t * surfhead)
|
2002-09-19 16:58:48 +00:00
|
|
|
{
|
2002-09-19 17:14:23 +00:00
|
|
|
face_t *f;
|
2002-09-19 18:51:19 +00:00
|
|
|
int mergefaces;
|
|
|
|
surface_t *surf;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
printf ("---- MergeAll ----\n");
|
|
|
|
|
2002-09-19 17:14:23 +00:00
|
|
|
mergefaces = 0;
|
|
|
|
for (surf = surfhead; surf; surf = surf->next) {
|
2002-09-19 16:58:48 +00:00
|
|
|
MergePlaneFaces (surf);
|
2002-09-19 17:14:23 +00:00
|
|
|
Draw_ClearWindow ();
|
|
|
|
for (f = surf->faces; f; f = f->next) {
|
|
|
|
Draw_DrawFace (f);
|
2002-09-19 16:58:48 +00:00
|
|
|
mergefaces++;
|
|
|
|
}
|
|
|
|
}
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
printf ("%i mergefaces\n", mergefaces);
|
|
|
|
}
|