1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code ("Doom 3 Source Code").
8 
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code.  If not, see <http://www.gnu.org/licenses/>.
21 
22 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.
23 
24 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.
25 
26 ===========================================================================
27 */
28 
29 #include "sys/platform.h"
30 
31 #include "renderer/tr_local.h"
32 
33 #define MAX_POLYTOPE_PLANES		6
34 
35 /*
36 =====================
37 R_PolytopeSurface
38 
39 Generate vertexes and indexes for a polytope, and optionally returns the polygon windings.
40 The positive sides of the planes will be visible.
41 =====================
42 */
R_PolytopeSurface(int numPlanes,const idPlane * planes,idWinding ** windings)43 srfTriangles_t *R_PolytopeSurface( int numPlanes, const idPlane *planes, idWinding **windings ) {
44 	int i, j;
45 	srfTriangles_t *tri;
46 	idFixedWinding planeWindings[MAX_POLYTOPE_PLANES];
47 	int numVerts, numIndexes;
48 
49 	if ( numPlanes > MAX_POLYTOPE_PLANES ) {
50 		common->Error( "R_PolytopeSurface: more than %d planes", MAX_POLYTOPE_PLANES );
51 	}
52 
53 	numVerts = 0;
54 	numIndexes = 0;
55 	for ( i = 0; i < numPlanes; i++ ) {
56 		const idPlane &plane = planes[i];
57 		idFixedWinding &w = planeWindings[i];
58 
59 		w.BaseForPlane( plane );
60 		for ( j = 0; j < numPlanes; j++ ) {
61 			const idPlane &plane2 = planes[j];
62 			if ( j == i ) {
63 				continue;
64 			}
65 			if ( !w.ClipInPlace( -plane2, ON_EPSILON ) ) {
66 				break;
67 			}
68 		}
69 		if ( w.GetNumPoints() <= 2 ) {
70 			continue;
71 		}
72 		numVerts += w.GetNumPoints();
73 		numIndexes += ( w.GetNumPoints() - 2 ) * 3;
74 	}
75 
76 	// allocate the surface
77 	tri = R_AllocStaticTriSurf();
78 	R_AllocStaticTriSurfVerts( tri, numVerts );
79 	R_AllocStaticTriSurfIndexes( tri, numIndexes );
80 
81 	// copy the data from the windings
82 	for ( i = 0; i < numPlanes; i++ ) {
83 		idFixedWinding &w = planeWindings[i];
84 		if ( !w.GetNumPoints() ) {
85 			continue;
86 		}
87 		for ( j = 0 ; j < w.GetNumPoints() ; j++ ) {
88 			tri->verts[tri->numVerts + j ].Clear();
89 			tri->verts[tri->numVerts + j ].xyz = w[j].ToVec3();
90 		}
91 
92 		for ( j = 1 ; j < w.GetNumPoints() - 1 ; j++ ) {
93 			tri->indexes[ tri->numIndexes + 0 ] = tri->numVerts;
94 			tri->indexes[ tri->numIndexes + 1 ] = tri->numVerts + j;
95 			tri->indexes[ tri->numIndexes + 2 ] = tri->numVerts + j + 1;
96 			tri->numIndexes += 3;
97 		}
98 		tri->numVerts += w.GetNumPoints();
99 
100 		// optionally save the winding
101 		if ( windings ) {
102 			windings[i] = new idWinding( w.GetNumPoints() );
103 			*windings[i] = w;
104 		}
105 	}
106 
107 	R_BoundTriSurf( tri );
108 
109 	return tri;
110 }
111