2 GenSurf plugin for GtkRadiant
3 Copyright (C) 2001 David Hyde, Loki software and qeradiant.com
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #else /* not SINGLE */
25 #endif /* not SINGLE */
42 #define Absolute(a) ((a) >= 0.0 ? (a) : -(a))
44 void MakeDecimatedMap(int *NumNodes, int *NumTris, NODE **pNode, TRI **pTri)
46 int compare(TRITABLE *, TRITABLE *);
47 int Bisect(NODE *, int, int, int);
48 void CalcAngles(NODE *, int *, float *);
49 void EdgeOnSide(int *, int *, int *);
50 int tricall(int, NODE *, int *, TRI **, TRI **, const char *);
51 int CheckBorders(int *,int,NODE *,int *,TRI **);
62 if(Decimate <= 0) return;
64 ghCursorCurrent = LoadCursor(NULL,IDC_WAIT);
65 SetCursor(ghCursorCurrent);
71 NumNodes[0] = (NH+1)*(NVP1);
72 *pNode = (NODE *) malloc(NumNodes[0] * sizeof(NODE));
74 memset(Node,0,NumNodes[0]*sizeof(NODE));
76 // Copy [NH][NV] vertex array to our working node array
77 for(i=0,N=0; i<=NH; i++)
79 for(j=0; j<=NV; j++, N++)
81 Node[N].p[0] = (float)xyz[i][j].p[0];
82 Node[N].p[1] = (float)xyz[i][j].p[1];
83 Node[N].p[2] = (float)xyz[i][j].p[2];
84 Node[N].fixed = xyz[i][j].fixed;
87 // Start things off with the corner values
90 Node[NH*NVP1].used = 1;
91 Node[NH*NVP1+NV].used = 1;
93 tricall(NumNodes[0], Node, NumTris, NULL, pTri, "cnzBNPY");
96 // Which coordinates are we triangulating on?
117 // TriTable stores the largest error in a triangle and the node where that
119 TriTable = (TRITABLE *) malloc(NH*NV*2 * sizeof(TRITABLE));
120 NumNodesToSave = min(NumNodes[0], (int)(0.01*(100-Decimate)*(NumNodes[0]-NumNodesUsed)+NumNodesUsed));
122 while(NumNodesUsed < NumNodesToSave)
124 for(i=0; i<NumTris[0]; i++)
127 // For every node that's not currently used, find what triangle it
128 // lies on, and the error at this node
129 for(i=0, biggesterror=0; i<NumNodes[0]; i++)
131 if(Node[i].used) continue;
132 for(j=0, Node[i].tri=-1; (j<NumTris[0]) && (Node[i].tri==-1); j++)
134 if( side(Node[i].p[j1], Node[i].p[j2],
135 Node[Tri[j].v[0]].p[j1],Node[Tri[j].v[0]].p[j2],
136 Node[Tri[j].v[1]].p[j1],Node[Tri[j].v[1]].p[j2]) < 0. ) continue;
137 if( side(Node[i].p[j1], Node[i].p[j2],
138 Node[Tri[j].v[1]].p[j1],Node[Tri[j].v[1]].p[j2],
139 Node[Tri[j].v[2]].p[j1],Node[Tri[j].v[2]].p[j2]) < 0. ) continue;
140 if( side(Node[i].p[j1], Node[i].p[j2],
141 Node[Tri[j].v[2]].p[j1],Node[Tri[j].v[2]].p[j2],
142 Node[Tri[j].v[0]].p[j1],Node[Tri[j].v[0]].p[j2]) < 0. ) continue;
148 ghCursorCurrent = ghCursorDefault;
149 SetCursor(ghCursorCurrent);
151 g_FuncTable.m_pfnMessageBox(g_pRadiantWnd,
152 "Error: Couldn't find the triangle bounding a point.",
153 "Decimation Error", eMB_OK, eMB_ICONWARNING);
156 if(!Tri[Node[i].tri].flag)
158 PlaneFromPoints(Node[Tri[Node[i].tri].v[0]].p,
159 Node[Tri[Node[i].tri].v[1]].p,
160 Node[Tri[Node[i].tri].v[2]].p,
161 &Tri[Node[i].tri].plane);
162 Tri[Node[i].tri].flag = 1;
165 Node[i].p[j0] - (Tri[Node[i].tri].plane.dist -
166 Tri[Node[i].tri].plane.normal[j1]*Node[i].p[j1] -
167 Tri[Node[i].tri].plane.normal[j2]*Node[i].p[j2] )/
168 Tri[Node[i].tri].plane.normal[j0];
169 biggesterror = max(biggesterror,Absolute(Node[i].error));
171 if(biggesterror == 0)
172 NumNodesToSave = NumNodesUsed;
175 // For all current triangles, build a list of worst-case nodes
176 memset(TriTable,0,NH*NV*2*sizeof(TRITABLE));
177 for(i=0; i<NumNodes[0]; i++)
179 if(Node[i].used) continue;
180 if(Absolute(Node[i].error) > TriTable[Node[i].tri].error)
182 TriTable[Node[i].tri].error = (float)(Absolute(Node[i].error));
183 TriTable[Node[i].tri].node = i;
186 qsort( (void *)TriTable, (size_t)(NumTris[0]), sizeof(TRITABLE), (int (*)(const void *, const void *))compare );
187 for(i=0; i<NumTris[0] && NumNodesUsed < NumNodesToSave && TriTable[i].error > 0.5*biggesterror; i++)
189 if(Node[TriTable[i].node].used) continue; // shouldn't happen
191 Node[TriTable[i].node].used++;
194 tricall(NumNodes[0], Node, NumTris, NULL, pTri, "cnzBNPY");
196 // Sliver-check along borders. Since borders are often linear, the errors
197 // along borders will often be zero, so no new points will be added. This
198 // tends to produce long, thin brushes. For all border triangles, check
199 // that minimum angle isn't less than SLIVER_ANGLE. If it is, add another
201 while(CheckBorders(&NumNodesUsed,NumNodes[0],Node,NumTris,pTri) > 0)
208 // One last time (because we're pessimistic), check border triangles
209 // CheckBorders(&NumNodesUsed,NumNodes[0],Node,NumTris,pTri);
212 // Check that all fixed points are exact. If not, add them to the mix.
213 // First check to see if we have any fixed points that aren't already used.
214 for(i=0, N=0; i<NumNodes[0] && !N; i++)
216 if(Node[i].used) continue;
217 if(Node[i].fixed) N++;
221 // Zero out the flag member of all triangles, indicating that
222 // the plane equation has not been found.
223 for(i=0; i<NumTris[0]; i++)
226 for(i=0; i<NumNodes[0]; i++)
228 if(Node[i].used) continue;
229 if(!Node[i].fixed) continue;
231 for(j=0; j<NumTris[0] && Node[i].tri==-1; j++)
233 if( side(Node[i].p[j1], Node[i].p[j2],
234 Node[Tri[j].v[0]].p[j1],Node[Tri[j].v[0]].p[j2],
235 Node[Tri[j].v[1]].p[j1],Node[Tri[j].v[1]].p[j2]) < 0. ) continue;
236 if( side(Node[i].p[j1], Node[i].p[j2],
237 Node[Tri[j].v[1]].p[j1],Node[Tri[j].v[1]].p[j2],
238 Node[Tri[j].v[2]].p[j1],Node[Tri[j].v[2]].p[j2]) < 0. ) continue;
239 if( side(Node[i].p[j1], Node[i].p[j2],
240 Node[Tri[j].v[2]].p[j1],Node[Tri[j].v[2]].p[j2],
241 Node[Tri[j].v[0]].p[j1],Node[Tri[j].v[0]].p[j2]) < 0. ) continue;
247 ghCursorCurrent = ghCursorDefault;
248 SetCursor(ghCursorCurrent);
250 g_FuncTable.m_pfnMessageBox(g_pRadiantWnd,
251 "Error: Couldn't find the triangle bounding a point.",
252 "Decimation Error", eMB_OK, eMB_ICONWARNING);
255 if(!Tri[Node[i].tri].flag)
257 PlaneFromPoints(Node[Tri[Node[i].tri].v[0]].p,
258 Node[Tri[Node[i].tri].v[1]].p,
259 Node[Tri[Node[i].tri].v[2]].p,
260 &Tri[Node[i].tri].plane);
261 Tri[Node[i].tri].flag = 1;
264 Node[i].p[j0] - (Tri[Node[i].tri].plane.dist -
265 Tri[Node[i].tri].plane.normal[j1]*Node[i].p[j1] -
266 Tri[Node[i].tri].plane.normal[j2]*Node[i].p[j2] )/
267 Tri[Node[i].tri].plane.normal[j0];
268 if(Absolute(Node[i].error) > 0.5)
273 tricall(NumNodes[0], Node, NumTris, NULL, pTri, "cnzBNPY");
279 // Swap node orders for surfaces facing down, north or west so that
280 // they are counterclockwise when facing the surface
282 if((Plane == PLANE_XY1) || (Plane == PLANE_XZ0) || (Plane == PLANE_YZ1) )
284 for(i=0; i<NumTris[0]; i++)
287 Tri[i].v[1] = Tri[i].v[2];
292 // Store bounding box coords
293 for(i=0; i<NumTris[0]; i++)
295 Tri[i].min[0] = Node[Tri[i].v[0]].p[0];
296 Tri[i].min[0] = min(Tri[i].min[0],Node[Tri[i].v[1]].p[0]);
297 Tri[i].min[0] = min(Tri[i].min[0],Node[Tri[i].v[2]].p[0]);
298 Tri[i].min[1] = Node[Tri[i].v[0]].p[1];
299 Tri[i].min[1] = min(Tri[i].min[1],Node[Tri[i].v[1]].p[1]);
300 Tri[i].min[1] = min(Tri[i].min[1],Node[Tri[i].v[2]].p[1]);
301 Tri[i].min[2] = Node[Tri[i].v[0]].p[2];
302 Tri[i].min[2] = min(Tri[i].min[2],Node[Tri[i].v[1]].p[2]);
303 Tri[i].min[2] = min(Tri[i].min[2],Node[Tri[i].v[2]].p[2]);
304 Tri[i].max[0] = Node[Tri[i].v[0]].p[0];
305 Tri[i].max[0] = max(Tri[i].max[0],Node[Tri[i].v[1]].p[0]);
306 Tri[i].max[0] = max(Tri[i].max[0],Node[Tri[i].v[2]].p[0]);
307 Tri[i].max[1] = Node[Tri[i].v[0]].p[1];
308 Tri[i].max[1] = max(Tri[i].max[1],Node[Tri[i].v[1]].p[1]);
309 Tri[i].max[1] = max(Tri[i].max[1],Node[Tri[i].v[2]].p[1]);
310 Tri[i].max[2] = Node[Tri[i].v[0]].p[2];
311 Tri[i].max[2] = max(Tri[i].max[2],Node[Tri[i].v[1]].p[2]);
312 Tri[i].max[2] = max(Tri[i].max[2],Node[Tri[i].v[2]].p[2]);
315 ghCursorCurrent = ghCursorDefault;
316 SetCursor(ghCursorCurrent);
319 /* end MakeDecimatedMap */
321 /*****************************************************************************/
323 /* tricall Takes an array of nodes, spits out an array of triangles */
325 /*****************************************************************************/
326 int tricall(int NumNodes, NODE *Node, int *NumTris, TRI **inTri, TRI **Tri, const char *Options)
328 struct triangulateio in, out;
334 /* Define input points. */
336 for(i=0,NumUsedNodes=0; i<NumNodes; i++)
337 if(Node[i].used) NumUsedNodes++;
339 memset(&in, 0,sizeof(in));
340 memset(&out,0,sizeof(out));
342 NodeTable = (int *) malloc(NumUsedNodes * sizeof(int));
344 in.numberofpoints = NumUsedNodes;
345 in.numberofpointattributes = 0;
346 in.pointlist = (REAL *) malloc(in.numberofpoints * 2 * sizeof(REAL));
347 for(i=0,N=0; i<NumNodes; i++)
355 in.pointlist[N*2 ] = Node[i].p[0];
356 in.pointlist[N*2+1] = Node[i].p[2];
360 in.pointlist[N*2 ] = Node[i].p[1];
361 in.pointlist[N*2+1] = Node[i].p[2];
364 in.pointlist[N*2 ] = Node[i].p[0];
365 in.pointlist[N*2+1] = Node[i].p[1];
371 in.pointattributelist = (REAL *) NULL;
372 in.pointmarkerlist = (int *) NULL;
374 if(strstr(Options,"r"))
377 TriTable = (int *) malloc(NumNodes * sizeof(int));
378 for(i=0,N=0; i<NumNodes; i++)
386 in.numberoftriangles = NumTris[0];
387 in.numberofcorners = 3;
388 in.numberoftriangleattributes = 0;
389 in.trianglelist = (int *) malloc(in.numberofcorners * in.numberoftriangles * sizeof(int));
390 in.triangleattributelist = (REAL *) NULL;
391 in.trianglearealist = (REAL *) NULL;
393 for(i=0; i<in.numberoftriangles; i++)
395 in.trianglelist[i*in.numberofcorners ] = TriTable[ptri[i].v[0]];
396 in.trianglelist[i*in.numberofcorners+1] = TriTable[ptri[i].v[1]];
397 in.trianglelist[i*in.numberofcorners+2] = TriTable[ptri[i].v[2]];
403 in.numberoftriangles = 0;
404 in.numberofcorners = 3;
405 in.numberoftriangleattributes = 0;
406 in.trianglelist = (int *) NULL;
407 in.triangleattributelist = (REAL *) NULL;
408 in.trianglearealist = (REAL *) NULL;
411 in.numberofsegments = 0;
412 in.segmentlist = (int *) NULL;
413 in.segmentmarkerlist = (int *) NULL;
415 in.numberofholes = 0;
416 in.holelist = (REAL *) NULL;
418 in.numberofregions = 0;
419 in.regionlist = (REAL *) NULL;
421 in.numberofedges = 0;
422 in.edgelist = (int *) NULL;
423 in.edgemarkerlist = (int *) NULL;
424 in.normlist = (REAL *) NULL;
426 /* Make necessary initializations */
427 out.pointlist = (REAL *) NULL; /* Not needed if -N switch used. */
428 out.pointattributelist = (REAL *) NULL; /* Not needed if -N switch used or
429 number of point attributes is zero: */
430 out.pointmarkerlist = (int *) NULL; /* Not needed if -N or -B switch used. */
431 out.trianglelist = (int *) NULL; /* Not needed if -E switch used. */
432 out.triangleattributelist = (REAL *) NULL; /* Not needed if -E switch used or
433 number of triangle attributes is
435 out.trianglearealist = (REAL *) NULL;
436 out.neighborlist = (int *) NULL; /* Needed only if -n switch used. */
437 out.segmentlist = (int *) NULL; /* Needed only if segments are output
438 (-p or -c) and -P not used: */
439 out.segmentmarkerlist = (int *) NULL; /* Needed only if segments are output
440 (-p or -c) and -P and -B not used: */
441 out.edgelist = (int *) NULL; /* Needed only if -e switch used. */
442 out.edgemarkerlist = (int *) NULL; /* Needed if -e used and -B not used. */
444 triangulate(Options, &in, &out, NULL);
446 NumTris[0] = out.numberoftriangles;
447 *Tri = (TRI *) malloc(NumTris[0] * sizeof(TRI));
450 for(i=0; i<NumTris[0]; i++)
452 ptri[i].v[0] = NodeTable[out.trianglelist[i*out.numberofcorners ]];
453 ptri[i].v[1] = NodeTable[out.trianglelist[i*out.numberofcorners+1]];
454 ptri[i].v[2] = NodeTable[out.trianglelist[i*out.numberofcorners+2]];
455 ptri[i].n[0] = out.neighborlist[i*3 ];
456 ptri[i].n[1] = out.neighborlist[i*3+1];
457 ptri[i].n[2] = out.neighborlist[i*3+2];
460 /* Free all allocated arrays, including those allocated by Triangle. */
461 if(in.pointlist) free(in.pointlist);
462 if(in.pointattributelist) free(in.pointattributelist);
463 if(in.pointmarkerlist) free(in.pointmarkerlist);
464 if(in.trianglelist) free(in.trianglelist);
465 if(in.triangleattributelist) free(in.triangleattributelist);
466 if(in.trianglearealist) free(in.trianglearealist);
467 if(in.neighborlist) free(in.neighborlist);
468 if(in.segmentlist) free(in.segmentlist);
469 if(in.segmentmarkerlist) free(in.segmentmarkerlist);
470 if(in.holelist) free(in.holelist);
471 if(in.regionlist) free(in.regionlist);
472 if(in.edgelist) free(in.edgelist);
473 if(in.edgemarkerlist) free(in.edgemarkerlist);
474 if(in.normlist) free(in.normlist);
475 if(out.pointlist) free(out.pointlist);
476 if(out.pointattributelist) free(out.pointattributelist);
477 if(out.pointmarkerlist) free(out.pointmarkerlist);
478 if(out.trianglelist) free(out.trianglelist);
479 if(out.triangleattributelist) free(out.triangleattributelist);
480 if(out.trianglearealist) free(out.trianglearealist);
481 if(out.neighborlist) free(out.neighborlist);
482 if(out.segmentlist) free(out.segmentlist);
483 if(out.segmentmarkerlist) free(out.segmentmarkerlist);
484 if(out.holelist) free(out.holelist);
485 if(out.regionlist) free(out.regionlist);
486 if(out.edgelist) free(out.edgelist);
487 if(out.edgemarkerlist) free(out.edgemarkerlist);
488 if(out.normlist) free(out.normlist);
494 void EdgeOnSide(int *v, int *edge, int *border)
502 if( (v[0] <= NV) && (v[1] <= NV) )
507 if( (v[1] <= NV) && (v[2] <= NV) )
512 if( (v[2] <= NV) && (v[0] <= NV) )
520 if( (v[0] >= R) && (v[1] >= R) )
525 if( (v[1] >= R) && (v[2] >= R) )
530 if( (v[2] >= R) && (v[0] >= R) )
540 N = Absolute(v[k0] - v[k1]);
543 if( ((v[0] % NVP1) == 0) && ((v[1] % NVP1) == 0) )
546 if( Ndv > (Absolute(v[0] - v[1])*dh)) return;
551 if( ((v[1] % NVP1) == 0) && ((v[2] % NVP1) == 0) )
554 if( Ndv > (Absolute(v[1] - v[2])*dh)) return;
559 if( ((v[2] % NVP1) == 0) && ((v[0] % NVP1) == 0) )
562 if( Ndv > (Absolute(v[2] - v[0])*dh)) return;
568 if( ((v[0] % NVP1) == NV) && ((v[1] % NVP1) == NV) )
571 if( Ndv > (Absolute(v[0] - v[1])*dh)) return;
576 if( ((v[1] % NVP1) == NV) && ((v[2] % NVP1) == NV) )
579 if( Ndv > (Absolute(v[1] - v[2])*dh)) return;
584 if( ((v[2] % NVP1) == NV) && ((v[0] % NVP1) == NV) )
587 if( Ndv > (Absolute(v[2] - v[0])*dh)) return;
595 void CalcAngles(NODE *node, int *v, float *angle)
599 vec x0, x1, x2, y0, y1, y2;
619 x0 = node[v[0]].p[i];
620 x1 = node[v[1]].p[i];
621 x2 = node[v[2]].p[i];
622 y0 = node[v[0]].p[j];
623 y1 = node[v[1]].p[j];
624 y2 = node[v[2]].p[j];
635 l = (vec)(sqrt( vv[k][0]*vv[k][0] + vv[k][1]*vv[k][1] ));
643 dot = -(vv[0][0]*vv[2][0] + vv[0][1]*vv[2][1]);
644 angle[0] = (float)(acos(dot));
645 dot = -(vv[1][0]*vv[0][0] + vv[1][1]*vv[0][1]);
646 angle[1] = (float)(acos(dot));
647 dot = -(vv[2][0]*vv[1][0] + vv[2][1]*vv[1][1]);
648 angle[2] = (float)(acos(dot));
650 //=================================================================
651 int Bisect(NODE *node, int border, int j0, int j1)
664 k = (int)((j0+j1)/(2*NVP1)) * NVP1;
667 k = (int)((j0+j1+2)/(2*NVP1)) * NVP1 - 1;
670 return( ((k != j0) && (k != j1)) ? k : 0 );
672 //=================================================================
673 int compare(TRITABLE *t1, TRITABLE *t2)
675 if(t1->error > t2->error) return -1;
676 if(t1->error < t2->error) return 1;
680 void MakeBrushes(int NumTris, NODE *Node, TRI *Tri,bool surf,
681 int offset,char *texture0, char *texture1, char *texture2)
683 extern double backface;
688 vec3_t PlaneNormal,SurfNormal;
692 // if texture2 is identical to texture0, there's no need to
693 // check surface angle
694 if(!g_strcasecmp(texture0,texture2) || !strlen(texture2))
699 Steep = (float)cos((double)SlantAngle/57.2957795);
702 case PLANE_XY0: PlaneNormal[0]= 0.;PlaneNormal[1]= 0.;PlaneNormal[2]= 1.;break;
703 case PLANE_XY1: PlaneNormal[0]= 0.;PlaneNormal[1]= 0.;PlaneNormal[2]=-1.;break;
704 case PLANE_XZ0: PlaneNormal[0]= 0.;PlaneNormal[1]= 1.;PlaneNormal[2]= 1.;break;
705 case PLANE_XZ1: PlaneNormal[0]= 0.;PlaneNormal[1]=-1.;PlaneNormal[2]= 1.;break;
706 case PLANE_YZ0: PlaneNormal[0]= 1.;PlaneNormal[1]= 0.;PlaneNormal[2]= 1.;break;
707 case PLANE_YZ1: PlaneNormal[0]=-1.;PlaneNormal[1]= 0.;PlaneNormal[2]= 1.;break;
714 if(UseDetail) contents += CONTENTS_DETAIL;
715 if(UseLadder) contents += CONTENTS_LADDER;
719 for(i=0; i<NumTris; i++)
724 brush.face[0].v[0][0] = Node[Tri[i].v[0]].p[0];
725 brush.face[0].v[0][1] = Node[Tri[i].v[0]].p[1];
726 brush.face[0].v[0][2] = Node[Tri[i].v[0]].p[2];
728 brush.face[0].v[1][0] = Node[Tri[i].v[2]].p[0];
729 brush.face[0].v[1][1] = Node[Tri[i].v[2]].p[1];
730 brush.face[0].v[1][2] = Node[Tri[i].v[2]].p[2];
732 brush.face[0].v[2][0] = Node[Tri[i].v[1]].p[0];
733 brush.face[0].v[2][1] = Node[Tri[i].v[1]].p[1];
734 brush.face[0].v[2][2] = Node[Tri[i].v[1]].p[2];
741 brush.face[0].v[0][2] += offset;
742 brush.face[0].v[1][2] += offset;
743 brush.face[0].v[1][2] += offset;
746 brush.face[0].v[0][2] -= offset;
747 brush.face[0].v[1][2] -= offset;
748 brush.face[0].v[1][2] -= offset;
751 brush.face[0].v[0][1] += offset;
752 brush.face[0].v[1][1] += offset;
753 brush.face[0].v[1][1] += offset;
756 brush.face[0].v[0][1] -= offset;
757 brush.face[0].v[1][1] -= offset;
758 brush.face[0].v[1][1] -= offset;
761 brush.face[0].v[0][0] += offset;
762 brush.face[0].v[1][0] += offset;
763 brush.face[0].v[1][0] += offset;
766 brush.face[0].v[0][0] -= offset;
767 brush.face[0].v[1][0] -= offset;
768 brush.face[0].v[1][0] -= offset;
777 brush.face[1].v[0][0] = Node[Tri[i].v[0]].p[0];
778 brush.face[1].v[0][1] = (float)backface;
779 brush.face[1].v[0][2] = Node[Tri[i].v[0]].p[2];
781 brush.face[1].v[1][0] = Node[Tri[i].v[1]].p[0];
782 brush.face[1].v[1][1] = (float)backface;
783 brush.face[1].v[1][2] = Node[Tri[i].v[1]].p[2];
785 brush.face[1].v[2][0] = Node[Tri[i].v[2]].p[0];
786 brush.face[1].v[2][1] = (float)backface;
787 brush.face[1].v[2][2] = Node[Tri[i].v[2]].p[2];
790 brush.face[2].v[0][0] = Node[Tri[i].v[0]].p[0];
791 brush.face[2].v[0][1] = Node[Tri[i].v[0]].p[1];
792 brush.face[2].v[0][2] = Node[Tri[i].v[0]].p[2];
794 brush.face[2].v[1][0] = Node[Tri[i].v[1]].p[0];
795 brush.face[2].v[1][1] = Node[Tri[i].v[1]].p[1];
796 brush.face[2].v[1][2] = Node[Tri[i].v[1]].p[2];
798 brush.face[2].v[2][0] = Node[Tri[i].v[1]].p[0];
799 brush.face[2].v[2][1] = (float)backface;
800 brush.face[2].v[2][2] = Node[Tri[i].v[1]].p[2];
803 brush.face[3].v[0][0] = Node[Tri[i].v[1]].p[0];
804 brush.face[3].v[0][1] = Node[Tri[i].v[1]].p[1];
805 brush.face[3].v[0][2] = Node[Tri[i].v[1]].p[2];
807 brush.face[3].v[1][0] = Node[Tri[i].v[2]].p[0];
808 brush.face[3].v[1][1] = Node[Tri[i].v[2]].p[1];
809 brush.face[3].v[1][2] = Node[Tri[i].v[2]].p[2];
811 brush.face[3].v[2][0] = Node[Tri[i].v[2]].p[0];
812 brush.face[3].v[2][1] = (float)backface;
813 brush.face[3].v[2][2] = Node[Tri[i].v[2]].p[2];
816 brush.face[4].v[0][0] = Node[Tri[i].v[2]].p[0];
817 brush.face[4].v[0][1] = Node[Tri[i].v[2]].p[1];
818 brush.face[4].v[0][2] = Node[Tri[i].v[2]].p[2];
820 brush.face[4].v[1][0] = Node[Tri[i].v[0]].p[0];
821 brush.face[4].v[1][1] = Node[Tri[i].v[0]].p[1];
822 brush.face[4].v[1][2] = Node[Tri[i].v[0]].p[2];
824 brush.face[4].v[2][0] = Node[Tri[i].v[0]].p[0];
825 brush.face[4].v[2][1] = (float)backface;
826 brush.face[4].v[2][2] = Node[Tri[i].v[0]].p[2];
831 brush.face[1].v[0][0] = (float)backface;
832 brush.face[1].v[0][1] = Node[Tri[i].v[0]].p[1];
833 brush.face[1].v[0][2] = Node[Tri[i].v[0]].p[2];
835 brush.face[1].v[1][0] = (float)backface;
836 brush.face[1].v[1][1] = Node[Tri[i].v[1]].p[1];
837 brush.face[1].v[1][2] = Node[Tri[i].v[1]].p[2];
839 brush.face[1].v[2][0] = (float)backface;
840 brush.face[1].v[2][1] = Node[Tri[i].v[2]].p[1];
841 brush.face[1].v[2][2] = Node[Tri[i].v[2]].p[2];
844 brush.face[2].v[0][0] = Node[Tri[i].v[0]].p[0];
845 brush.face[2].v[0][1] = Node[Tri[i].v[0]].p[1];
846 brush.face[2].v[0][2] = Node[Tri[i].v[0]].p[2];
848 brush.face[2].v[1][0] = Node[Tri[i].v[1]].p[0];
849 brush.face[2].v[1][1] = Node[Tri[i].v[1]].p[1];
850 brush.face[2].v[1][2] = Node[Tri[i].v[1]].p[2];
852 brush.face[2].v[2][0] = (float)backface;
853 brush.face[2].v[2][1] = Node[Tri[i].v[1]].p[1];
854 brush.face[2].v[2][2] = Node[Tri[i].v[1]].p[2];
857 brush.face[3].v[0][0] = Node[Tri[i].v[1]].p[0];
858 brush.face[3].v[0][1] = Node[Tri[i].v[1]].p[1];
859 brush.face[3].v[0][2] = Node[Tri[i].v[1]].p[2];
861 brush.face[3].v[1][0] = Node[Tri[i].v[2]].p[0];
862 brush.face[3].v[1][1] = Node[Tri[i].v[2]].p[1];
863 brush.face[3].v[1][2] = Node[Tri[i].v[2]].p[2];
865 brush.face[3].v[2][0] = (float)backface;
866 brush.face[3].v[2][1] = Node[Tri[i].v[2]].p[1];
867 brush.face[3].v[2][2] = Node[Tri[i].v[2]].p[2];
870 brush.face[4].v[0][0] = Node[Tri[i].v[2]].p[0];
871 brush.face[4].v[0][1] = Node[Tri[i].v[2]].p[1];
872 brush.face[4].v[0][2] = Node[Tri[i].v[2]].p[2];
874 brush.face[4].v[1][0] = Node[Tri[i].v[0]].p[0];
875 brush.face[4].v[1][1] = Node[Tri[i].v[0]].p[1];
876 brush.face[4].v[1][2] = Node[Tri[i].v[0]].p[2];
878 brush.face[4].v[2][0] = (float)backface;
879 brush.face[4].v[2][1] = Node[Tri[i].v[0]].p[1];
880 brush.face[4].v[2][2] = Node[Tri[i].v[0]].p[2];
884 brush.face[1].v[0][0] = Node[Tri[i].v[0]].p[0];
885 brush.face[1].v[0][1] = Node[Tri[i].v[0]].p[1];
886 brush.face[1].v[0][2] = (float)backface;
888 brush.face[1].v[1][0] = Node[Tri[i].v[1]].p[0];
889 brush.face[1].v[1][1] = Node[Tri[i].v[1]].p[1];
890 brush.face[1].v[1][2] = (float)backface;
892 brush.face[1].v[2][0] = Node[Tri[i].v[2]].p[0];
893 brush.face[1].v[2][1] = Node[Tri[i].v[2]].p[1];
894 brush.face[1].v[2][2] = (float)backface;
897 brush.face[2].v[0][0] = Node[Tri[i].v[0]].p[0];
898 brush.face[2].v[0][1] = Node[Tri[i].v[0]].p[1];
899 brush.face[2].v[0][2] = Node[Tri[i].v[0]].p[2];
901 brush.face[2].v[1][0] = Node[Tri[i].v[1]].p[0];
902 brush.face[2].v[1][1] = Node[Tri[i].v[1]].p[1];
903 brush.face[2].v[1][2] = Node[Tri[i].v[1]].p[2];
905 brush.face[2].v[2][0] = Node[Tri[i].v[1]].p[0];
906 brush.face[2].v[2][1] = Node[Tri[i].v[1]].p[1];
907 brush.face[2].v[2][2] = (float)backface;
910 brush.face[3].v[0][0] = Node[Tri[i].v[1]].p[0];
911 brush.face[3].v[0][1] = Node[Tri[i].v[1]].p[1];
912 brush.face[3].v[0][2] = Node[Tri[i].v[1]].p[2];
914 brush.face[3].v[1][0] = Node[Tri[i].v[2]].p[0];
915 brush.face[3].v[1][1] = Node[Tri[i].v[2]].p[1];
916 brush.face[3].v[1][2] = Node[Tri[i].v[2]].p[2];
918 brush.face[3].v[2][0] = Node[Tri[i].v[2]].p[0];
919 brush.face[3].v[2][1] = Node[Tri[i].v[2]].p[1];
920 brush.face[3].v[2][2] = (float)backface;
923 brush.face[4].v[0][0] = Node[Tri[i].v[2]].p[0];
924 brush.face[4].v[0][1] = Node[Tri[i].v[2]].p[1];
925 brush.face[4].v[0][2] = Node[Tri[i].v[2]].p[2];
927 brush.face[4].v[1][0] = Node[Tri[i].v[0]].p[0];
928 brush.face[4].v[1][1] = Node[Tri[i].v[0]].p[1];
929 brush.face[4].v[1][2] = Node[Tri[i].v[0]].p[2];
931 brush.face[4].v[2][0] = Node[Tri[i].v[0]].p[0];
932 brush.face[4].v[2][1] = Node[Tri[i].v[0]].p[1];
933 brush.face[4].v[2][2] = (float)backface;
938 strcpy(brush.face[j].texture,
939 (strlen(texture1) ? texture1 : texture0));
940 brush.face[j].Shift[0] = (float)TexOffset[0];
941 brush.face[j].Shift[1] = (float)TexOffset[1];
942 brush.face[j].Rotate = 0.;
943 brush.face[j].Scale[0] = (float)TexScale[0];
944 brush.face[j].Scale[1] = (float)TexScale[1];
945 brush.face[j].Contents = contents;
947 brush.face[j].Surface = 0;
949 brush.face[j].Surface = SURF_HINT;
950 brush.face[j].Value = 0;
955 XYZVectorSubtract(brush.face[0].v[2],brush.face[0].v[0],t[0]);
956 XYZVectorSubtract(brush.face[0].v[1],brush.face[0].v[2],t[1]);
957 CrossProduct(t[0],t[1],SurfNormal);
958 VectorNormalize(SurfNormal,SurfNormal);
959 if(DotProduct(SurfNormal,PlaneNormal) < Steep)
960 strcpy(brush.face[0].texture,texture2);
962 strcpy(brush.face[0].texture,texture0);
965 strcpy(brush.face[0].texture,texture0);
967 if(surf) brush.face[0].Value = ArghRad2;
973 //=================================================================
974 void MapOut(int NumNodes,int NumTris, NODE *Node, TRI *Tri)
976 extern double backface;
977 extern double xmin, xmax, ymin, ymax, zmin, zmax;
979 char hint[32], skip[32];
983 ghCursorCurrent = LoadCursor(NULL,IDC_WAIT);
984 SetCursor(ghCursorCurrent);
986 UseDetail = 1; // this is temporary
987 MakeBrushes(NumTris,Node,Tri,TRUE,0,Texture[Game][0],Texture[Game][1],Texture[Game][2]);
989 if(AddHints || GimpHints)
994 strcpy(hint,"generic/misc/hint");
995 strcpy(skip,"generic/misc/skip");
1002 strcpy(hint,"general/hint");
1003 strcpy(skip,"general/skip");
1006 strcpy(hint,"common/0_hint");
1007 strcpy(skip,"common/0_skip");
1010 strcpy(hint,"common/hint");
1011 strcpy(skip,"common/skip");
1014 strcpy(hint,"e1u1/hint");
1015 strcpy(skip,"e1u1/skip");
1020 MakeBrushes(NumTris,Node,Tri,FALSE,HINT_OFFSET,hint,hint,hint);
1024 int j0, j1, j2, k, k0, k1;
1029 int MaxHints; // We don't want a whole slew of hint brushes, which we'd get
1030 // with low decimation values and our current placement scheme.
1031 // Limit number of hint brushes to number of undecimated grid
1037 front = LessThan(zmin,32.);
1040 front = MoreThan(ymax,32.);
1043 front = LessThan(ymin,32.);
1046 front = MoreThan(xmax,32.);
1049 front = LessThan(xmin,32.);
1052 front = MoreThan(zmax,32.);
1055 for(i=0; i<NumTris; i++)
1081 for(w=1; w<min(16,NH) && brush.Number < MaxHints; w++)
1083 for(h=max(1,w/2); h<min(16,NV) && brush.Number < MaxHints; h++)
1085 for(i=0; i<=NH-w && brush.Number < MaxHints; i++)
1087 for(j=0; j<=NV-h && brush.Number < MaxHints; j++)
1090 q[2] = q[0] + w*NVP1 + h;
1103 for(k=0, OK=1; k<NumTris && OK; k++)
1105 if(Tri[k].min[j1] >= max(Node[q[0]].p[j1],Node[q[2]].p[j1])) continue;
1106 if(Tri[k].min[j2] >= max(Node[q[0]].p[j2],Node[q[2]].p[j2])) continue;
1107 if(Tri[k].max[j1] <= min(Node[q[0]].p[j1],Node[q[2]].p[j1])) continue;
1108 if(Tri[k].max[j2] <= min(Node[q[0]].p[j2],Node[q[2]].p[j2])) continue;
1110 for(h0=0; h0<4 && OK; h0++)
1113 for(t=0; t<3 && OK; t++)
1115 s[t] = side(Node[q[h0]].p[j1],Node[q[h0]].p[j2],
1116 Node[q[h1]].p[j1],Node[q[h1]].p[j2],
1117 Node[Tri[k].v[t]].p[j1],Node[Tri[k].v[t]].p[j2]);
1119 if((s[1] > 0 || s[2] > 0) && s[0] < 0) OK=0;
1120 if((s[2] > 0 || s[0] > 0) && s[1] < 0) OK=0;
1121 if((s[0] > 0 || s[1] > 0) && s[2] < 0) OK=0;
1130 brush.face[0].v[0][0] = Node[q[2]].p[0];
1131 brush.face[0].v[0][1] = (float)front;
1132 brush.face[0].v[0][2] = Node[q[2]].p[2];
1134 brush.face[0].v[1][0] = Node[q[1]].p[0];
1135 brush.face[0].v[1][1] = (float)front;
1136 brush.face[0].v[1][2] = Node[q[1]].p[2];
1138 brush.face[0].v[2][0] = Node[q[0]].p[0];
1139 brush.face[0].v[2][1] = (float)front;
1140 brush.face[0].v[2][2] = Node[q[0]].p[2];
1143 brush.face[1].v[0][0] = Node[q[0]].p[0];
1144 brush.face[1].v[0][1] = (float)backface;
1145 brush.face[1].v[0][2] = Node[q[0]].p[2];
1147 brush.face[1].v[1][0] = Node[q[1]].p[0];
1148 brush.face[1].v[1][1] = (float)backface;
1149 brush.face[1].v[1][2] = Node[q[1]].p[2];
1151 brush.face[1].v[2][0] = Node[q[2]].p[0];
1152 brush.face[1].v[2][1] = (float)backface;
1153 brush.face[1].v[2][2] = Node[q[2]].p[2];
1155 for(k0=0; k0<brush.NumFaces-2; k0++)
1158 k1=(k0+1) % (brush.NumFaces-2);
1160 brush.face[k].v[0][0] = Node[q[k0]].p[0];
1161 brush.face[k].v[0][1] = (float)front;
1162 brush.face[k].v[0][2] = Node[q[k0]].p[2];
1164 brush.face[k].v[1][0] = Node[q[k1]].p[0];
1165 brush.face[k].v[1][1] = (float)front;
1166 brush.face[k].v[1][2] = Node[q[k1]].p[2];
1168 brush.face[k].v[2][0] = Node[q[k1]].p[0];
1169 brush.face[k].v[2][1] = (float)backface;
1170 brush.face[k].v[2][2] = Node[q[k1]].p[2];
1176 brush.face[0].v[0][0] = (float)front;
1177 brush.face[0].v[0][1] = Node[q[2]].p[1];
1178 brush.face[0].v[0][2] = Node[q[2]].p[2];
1180 brush.face[0].v[1][0] = (float)front;
1181 brush.face[0].v[1][1] = Node[q[1]].p[1];
1182 brush.face[0].v[1][2] = Node[q[1]].p[2];
1184 brush.face[0].v[2][0] = (float)front;
1185 brush.face[0].v[2][1] = Node[q[0]].p[1];
1186 brush.face[0].v[2][2] = Node[q[0]].p[2];
1189 brush.face[1].v[0][0] = (float)backface;
1190 brush.face[1].v[0][1] = Node[q[0]].p[1];
1191 brush.face[1].v[0][2] = Node[q[0]].p[2];
1193 brush.face[1].v[1][0] = (float)backface;
1194 brush.face[1].v[1][1] = Node[q[1]].p[1];
1195 brush.face[1].v[1][2] = Node[q[1]].p[2];
1197 brush.face[1].v[2][0] = (float)backface;
1198 brush.face[1].v[2][1] = Node[q[2]].p[1];
1199 brush.face[1].v[2][2] = Node[q[2]].p[2];
1201 for(k0=0; k0<brush.NumFaces-2; k0++)
1204 k1=(k0+1) % (brush.NumFaces-2);
1206 brush.face[k].v[0][0] = (float)front;
1207 brush.face[k].v[0][1] = Node[q[k0]].p[1];
1208 brush.face[k].v[0][2] = Node[q[k0]].p[2];
1210 brush.face[k].v[1][0] = (float)front;
1211 brush.face[k].v[1][1] = Node[q[k1]].p[1];
1212 brush.face[k].v[1][2] = Node[q[k1]].p[2];
1214 brush.face[k].v[2][0] = (float)backface;
1215 brush.face[k].v[2][1] = Node[q[k1]].p[1];
1216 brush.face[k].v[2][2] = Node[q[k1]].p[2];
1221 brush.face[0].v[0][0] = Node[q[2]].p[0];
1222 brush.face[0].v[0][1] = Node[q[2]].p[1];
1223 brush.face[0].v[0][2] = (float)front;
1225 brush.face[0].v[1][0] = Node[q[1]].p[0];
1226 brush.face[0].v[1][1] = Node[q[1]].p[1];
1227 brush.face[0].v[1][2] = (float)front;
1229 brush.face[0].v[2][0] = Node[q[0]].p[0];
1230 brush.face[0].v[2][1] = Node[q[0]].p[1];
1231 brush.face[0].v[2][2] = (float)front;
1234 brush.face[1].v[0][0] = Node[q[0]].p[0];
1235 brush.face[1].v[0][1] = Node[q[0]].p[1];
1236 brush.face[1].v[0][2] = (float)backface;
1238 brush.face[1].v[1][0] = Node[q[1]].p[0];
1239 brush.face[1].v[1][1] = Node[q[1]].p[1];
1240 brush.face[1].v[1][2] = (float)backface;
1242 brush.face[1].v[2][0] = Node[q[2]].p[0];
1243 brush.face[1].v[2][1] = Node[q[2]].p[1];
1244 brush.face[1].v[2][2] = (float)backface;
1246 for(k0=0; k0<brush.NumFaces-2; k0++)
1249 k1=(k0+1) % (brush.NumFaces-2);
1251 brush.face[k].v[0][0] = Node[q[k0]].p[0];
1252 brush.face[k].v[0][1] = Node[q[k0]].p[1];
1253 brush.face[k].v[0][2] = (float)front;
1255 brush.face[k].v[1][0] = Node[q[k1]].p[0];
1256 brush.face[k].v[1][1] = Node[q[k1]].p[1];
1257 brush.face[k].v[1][2] = (float)front;
1259 brush.face[k].v[2][0] = Node[q[k1]].p[0];
1260 brush.face[k].v[2][1] = Node[q[k1]].p[1];
1261 brush.face[k].v[2][2] = (float)backface;
1265 for(face=0; face<6; face++)
1267 strcpy(brush.face[face].texture,(face<=1 ? skip : hint));
1268 brush.face[face].Shift[0] = 0;
1269 brush.face[face].Shift[1] = 0;
1270 brush.face[face].Rotate = 0.;
1271 brush.face[face].Scale[0] = 1;
1272 brush.face[face].Scale[1] = 1;
1273 brush.face[face].Contents = CONTENTS_DETAIL;
1274 brush.face[face].Surface = (face<=1 ? SURF_SKIP : SURF_HINT);
1275 brush.face[face].Value = 0;
1277 if(!brush.Number) OpenFuncGroup();
1284 if(brush.Number) CloseFuncGroup();
1287 ghCursorCurrent = ghCursorDefault;
1288 SetCursor(ghCursorCurrent);
1291 //===========================================================================
1292 int CheckBorders(int *NumNodesUsed, int NumNodes, NODE *Node, int *NumTris, TRI **pTri)
1295 int i, j, k0, k1, N;
1299 N = NumNodesUsed[0];
1301 for(i=0; i<NumTris[0]; i++)
1303 EdgeOnSide(Tri[i].v,&k0,&border);
1304 if(border < 0) continue;
1305 CalcAngles(Node, Tri[i].v, angle);
1307 if((angle[k0] < SLIVER_ANGLE) || (angle[k1] < SLIVER_ANGLE))
1309 j = Bisect(Node, border, Tri[i].v[k0], Tri[i].v[k1]);
1312 if(!Node[j].used) // Shouldn't be used, but...
1320 if(NumNodesUsed[0] > N)
1323 tricall(NumNodes, Node, NumTris, NULL, pTri, "cnzBNPY");
1326 return (NumNodesUsed[0] - N);