File: tritools.cpp

package info (click to toggle)
dhewm3 1.5.1~pre%2Bgit20200905%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: bullseye
  • size: 21,664 kB
  • sloc: cpp: 408,868; ansic: 1,188; objc: 1,034; python: 330; sh: 94; makefile: 11
file content (386 lines) | stat: -rw-r--r-- 7,936 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
===========================================================================

Doom 3 GPL Source Code
Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.

This file is part of the Doom 3 GPL Source Code ("Doom 3 Source Code").

Doom 3 Source Code 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 3 of the License, or
(at your option) any later version.

Doom 3 Source Code 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.

You should have received a copy of the GNU General Public License
along with Doom 3 Source Code.  If not, see <http://www.gnu.org/licenses/>.

In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code.  If not, please request a copy in writing from id Software at the address below.

If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.

===========================================================================
*/

#include "sys/platform.h"

#include "tools/compilers/dmap/dmap.h"

/*

  All triangle list functions should behave reasonably with NULL lists.

*/

/*
===============
AllocTri
===============
*/
mapTri_t	*AllocTri( void ) {
	mapTri_t	*tri;

	tri = (mapTri_t *)Mem_Alloc( sizeof( *tri ) );
	memset( tri, 0, sizeof( *tri ) );
	return tri;
}

/*
===============
FreeTri
===============
*/
void		FreeTri( mapTri_t *tri ) {
	Mem_Free( tri );
}


/*
===============
MergeTriLists

This does not copy any tris, it just relinks them
===============
*/
mapTri_t	*MergeTriLists( mapTri_t *a, mapTri_t *b ) {
	mapTri_t	**prev;

	prev = &a;
	while ( *prev ) {
		prev = &(*prev)->next;
	}

	*prev = b;

	return a;
}


/*
===============
FreeTriList
===============
*/
void FreeTriList( mapTri_t *a ) {
	mapTri_t	*next;

	for ( ; a ; a = next ) {
		next = a->next;
		Mem_Free( a );
	}
}

/*
===============
CopyTriList
===============
*/
mapTri_t	*CopyTriList( const mapTri_t *a ) {
	mapTri_t	*testList;
	const mapTri_t	*tri;

	testList = NULL;
	for ( tri = a ; tri ; tri = tri->next ) {
		mapTri_t	*copy;

		copy = CopyMapTri( tri );
		copy ->next = testList;
		testList = copy;
	}

	return testList;
}


/*
=============
CountTriList
=============
*/
int	CountTriList( const mapTri_t *tri ) {
	int		c;

	c = 0;
	while ( tri ) {
		c++;
		tri = tri->next;
	}

	return c;
}


/*
===============
CopyMapTri
===============
*/
mapTri_t	*CopyMapTri( const mapTri_t *tri ) {
	mapTri_t		*t;

	t = (mapTri_t *)Mem_Alloc( sizeof( *t ) );
	*t = *tri;

	return t;
}

/*
===============
MapTriArea
===============
*/
float MapTriArea( const mapTri_t *tri ) {
	return idWinding::TriangleArea( tri->v[0].xyz, tri->v[1].xyz, tri->v[2].xyz );
}

/*
===============
RemoveBadTris

Return a new list with any zero or negative area triangles removed
===============
*/
mapTri_t	*RemoveBadTris( const mapTri_t *list ) {
	mapTri_t	*newList;
	mapTri_t	*copy;
	const mapTri_t	*tri;

	newList = NULL;

	for ( tri = list ; tri ; tri = tri->next ) {
		if ( MapTriArea( tri ) > 0 ) {
			copy = CopyMapTri( tri );
			copy->next = newList;
			newList = copy;
		}
	}

	return newList;
}

/*
================
BoundTriList
================
*/
void BoundTriList( const mapTri_t *list, idBounds &b ) {
	b.Clear();
	for ( ; list ; list = list->next ) {
		b.AddPoint( list->v[0].xyz );
		b.AddPoint( list->v[1].xyz );
		b.AddPoint( list->v[2].xyz );
	}
}

/*
================
DrawTri
================
*/
void DrawTri( const mapTri_t *tri ) {
	idWinding w;

	w.SetNumPoints( 3 );
	VectorCopy( tri->v[0].xyz, w[0] );
	VectorCopy( tri->v[1].xyz, w[1] );
	VectorCopy( tri->v[2].xyz, w[2] );
	DrawWinding( &w );
}


/*
================
FlipTriList

Swaps the vertex order
================
*/
void	FlipTriList( mapTri_t *tris ) {
	mapTri_t	*tri;

	for ( tri = tris ; tri ; tri = tri->next ) {
		idDrawVert	v;
		const struct hashVert_s *hv;
		struct optVertex_s	*ov;

		v = tri->v[0];
		tri->v[0] = tri->v[2];
		tri->v[2] = v;

		hv = tri->hashVert[0];
		tri->hashVert[0] = tri->hashVert[2];
		tri->hashVert[2] = hv;

		ov = tri->optVert[0];
		tri->optVert[0] = tri->optVert[2];
		tri->optVert[2] = ov;
	}
}

/*
================
WindingForTri
================
*/
idWinding *WindingForTri( const mapTri_t *tri ) {
	idWinding	*w;

	w = new idWinding( 3 );
	w->SetNumPoints( 3 );
	VectorCopy( tri->v[0].xyz, (*w)[0] );
	VectorCopy( tri->v[1].xyz, (*w)[1] );
	VectorCopy( tri->v[2].xyz, (*w)[2] );

	return w;
}

/*
================
TriVertsFromOriginal

Regenerate the texcoords and colors on a fragmented tri from the plane equations
================
*/
void		TriVertsFromOriginal( mapTri_t *tri, const mapTri_t *original ) {
	int		i, j;
	float	denom;

	denom = idWinding::TriangleArea( original->v[0].xyz, original->v[1].xyz, original->v[2].xyz );
	if ( denom == 0 ) {
		return;		// original was degenerate, so it doesn't matter
	}

	for ( i = 0 ; i < 3 ; i++ ) {
		float	a, b, c;

		// find the barycentric coordinates
		a = idWinding::TriangleArea( tri->v[i].xyz, original->v[1].xyz, original->v[2].xyz ) / denom;
		b = idWinding::TriangleArea( tri->v[i].xyz, original->v[2].xyz, original->v[0].xyz ) / denom;
		c = idWinding::TriangleArea( tri->v[i].xyz, original->v[0].xyz, original->v[1].xyz ) / denom;

		// regenerate the interpolated values
		tri->v[i].st[0] = a * original->v[0].st[0]
			 + b * original->v[1].st[0] + c * original->v[2].st[0];
		tri->v[i].st[1] = a * original->v[0].st[1]
			 + b * original->v[1].st[1] + c * original->v[2].st[1];

		for ( j = 0 ; j < 3 ; j++ ) {
			tri->v[i].normal[j] = a * original->v[0].normal[j]
				 + b * original->v[1].normal[j] + c * original->v[2].normal[j];
		}
		tri->v[i].normal.Normalize();
	}
}

/*
================
WindingToTriList

Generates a new list of triangles with proper texcoords from a winding
created by clipping the originalTri

OriginalTri can be NULL if you don't care about texCoords
================
*/
mapTri_t *WindingToTriList( const idWinding *w, const mapTri_t *originalTri ) {
	mapTri_t	*tri;
	mapTri_t	*triList;
	int			i, j;
	const idVec3	*vec;

	if ( !w ) {
		return NULL;
	}

	triList = NULL;
	for ( i = 2 ; i < w->GetNumPoints() ; i++ ) {
		tri = AllocTri();
		if ( !originalTri ) {
			memset( tri, 0, sizeof( *tri ) );
		} else {
			*tri = *originalTri;
		}
		tri->next = triList;
		triList = tri;

		for ( j = 0 ; j < 3 ; j++ ) {
			if ( j == 0 ) {
				vec = &((*w)[0]).ToVec3();
			} else if ( j == 1 ) {
				vec = &((*w)[i-1]).ToVec3();
			} else {
				vec = &((*w)[i]).ToVec3();
			}

			VectorCopy( *vec, tri->v[j].xyz );
		}
		if ( originalTri ) {
			TriVertsFromOriginal( tri, originalTri );
		}
	}

	return triList;
}


/*
==================
ClipTriList
==================
*/
void	ClipTriList( const mapTri_t *list, const idPlane &plane, float epsilon,
						mapTri_t **front, mapTri_t **back ) {
	const mapTri_t *tri;
	mapTri_t		*newList;
	idWinding		*w, *frontW, *backW;

	*front = NULL;
	*back = NULL;

	for ( tri = list ; tri ; tri = tri->next ) {
		w = WindingForTri( tri );
		w->Split( plane, epsilon, &frontW, &backW );

		newList = WindingToTriList( frontW, tri );
		*front = MergeTriLists( *front, newList );

		newList = WindingToTriList( backW, tri );
		*back = MergeTriLists( *back, newList );

		delete w;
	}

}

/*
==================
PlaneForTri
==================
*/
void	PlaneForTri( const mapTri_t *tri, idPlane &plane ) {
	plane.FromPoints( tri->v[0].xyz, tri->v[1].xyz, tri->v[2].xyz );
}