doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
tr_polytope.cpp
Go to the documentation of this file.
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 "../idlib/precompiled.h"
30 #pragma hdrstop
31 
32 #include "tr_local.h"
33 
34 #define MAX_POLYTOPE_PLANES 6
35 
36 /*
37 =====================
38 R_PolytopeSurface
39 
40 Generate vertexes and indexes for a polytope, and optionally returns the polygon windings.
41 The positive sides of the planes will be visible.
42 =====================
43 */
44 srfTriangles_t *R_PolytopeSurface( int numPlanes, const idPlane *planes, idWinding **windings ) {
45  int i, j;
47  idFixedWinding planeWindings[MAX_POLYTOPE_PLANES];
48  int numVerts, numIndexes;
49 
50  if ( numPlanes > MAX_POLYTOPE_PLANES ) {
51  common->Error( "R_PolytopeSurface: more than %d planes", MAX_POLYTOPE_PLANES );
52  }
53 
54  numVerts = 0;
55  numIndexes = 0;
56  for ( i = 0; i < numPlanes; i++ ) {
57  const idPlane &plane = planes[i];
58  idFixedWinding &w = planeWindings[i];
59 
60  w.BaseForPlane( plane );
61  for ( j = 0; j < numPlanes; j++ ) {
62  const idPlane &plane2 = planes[j];
63  if ( j == i ) {
64  continue;
65  }
66  if ( !w.ClipInPlace( -plane2, ON_EPSILON ) ) {
67  break;
68  }
69  }
70  if ( w.GetNumPoints() <= 2 ) {
71  continue;
72  }
73  numVerts += w.GetNumPoints();
74  numIndexes += ( w.GetNumPoints() - 2 ) * 3;
75  }
76 
77  // allocate the surface
78  tri = R_AllocStaticTriSurf();
79  R_AllocStaticTriSurfVerts( tri, numVerts );
80  R_AllocStaticTriSurfIndexes( tri, numIndexes );
81 
82  // copy the data from the windings
83  for ( i = 0; i < numPlanes; i++ ) {
84  idFixedWinding &w = planeWindings[i];
85  if ( !w.GetNumPoints() ) {
86  continue;
87  }
88  for ( j = 0 ; j < w.GetNumPoints() ; j++ ) {
89  tri->verts[tri->numVerts + j ].Clear();
90  tri->verts[tri->numVerts + j ].xyz = w[j].ToVec3();
91  }
92 
93  for ( j = 1 ; j < w.GetNumPoints() - 1 ; j++ ) {
94  tri->indexes[ tri->numIndexes + 0 ] = tri->numVerts;
95  tri->indexes[ tri->numIndexes + 1 ] = tri->numVerts + j;
96  tri->indexes[ tri->numIndexes + 2 ] = tri->numVerts + j + 1;
97  tri->numIndexes += 3;
98  }
99  tri->numVerts += w.GetNumPoints();
100 
101  // optionally save the winding
102  if ( windings ) {
103  windings[i] = new idWinding( w.GetNumPoints() );
104  *windings[i] = w;
105  }
106  }
107 
108  R_BoundTriSurf( tri );
109 
110  return tri;
111 }
srfTriangles_t * R_PolytopeSurface(int numPlanes, const idPlane *planes, idWinding **windings)
Definition: tr_polytope.cpp:44
#define MAX_POLYTOPE_PLANES
Definition: tr_polytope.cpp:34
void Clear(void)
Definition: DrawVert.h:73
int numVerts
Definition: Model.h:98
idVec3 xyz
Definition: DrawVert.h:42
srfTriangles_t * R_AllocStaticTriSurf(void)
Definition: tr_trisurf.cpp:523
int i
Definition: process.py:33
int GetNumPoints(void) const
Definition: Winding.h:238
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:3454
idCommon * common
Definition: Common.cpp:206
Definition: Plane.h:71
void R_BoundTriSurf(srfTriangles_t *tri)
Definition: tr_trisurf.cpp:704
#define ON_EPSILON
Definition: Plane.h:44
void BaseForPlane(const idVec3 &normal, const float dist)
Definition: Winding.cpp:66
glIndex_t * indexes
Definition: Model.h:102
void R_AllocStaticTriSurfIndexes(srfTriangles_t *tri, int numIndexes)
Definition: tr_trisurf.cpp:565
GLint j
Definition: qgl.h:264
int numIndexes
Definition: Model.h:101
virtual void Error(const char *fmt,...) id_attribute((format(printf
bool ClipInPlace(const idPlane &plane, const float epsilon=ON_EPSILON, const bool keepOn=false)
Definition: Winding.cpp:349
void R_AllocStaticTriSurfVerts(srfTriangles_t *tri, int numVerts)
Definition: tr_trisurf.cpp:555
idDrawVert * verts
Definition: Model.h:99