| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#include "qbsp.h" |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
#define PORTALFILE "PRT1" |
| 35 |
|
| 36 |
FILE *pf; |
| 37 |
int num_visclusters; |
| 38 |
int num_visportals; |
| 39 |
|
| 40 |
void WriteFloat( FILE *f, vec_t v ){ |
| 41 |
if ( fabs( v - Q_rint( v ) ) < 0.001 ) { |
| 42 |
fprintf( f,"%i ",(int)Q_rint( v ) ); |
| 43 |
} |
| 44 |
else{ |
| 45 |
fprintf( f,"%f ",v ); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
void WritePortalFile_r( node_t *node ){ |
| 55 |
int i, s; |
| 56 |
portal_t *p; |
| 57 |
winding_t *w; |
| 58 |
vec3_t normal; |
| 59 |
vec_t dist; |
| 60 |
|
| 61 |
|
| 62 |
if ( node->planenum != PLANENUM_LEAF && !node->detail_seperator ) { |
| 63 |
WritePortalFile_r( node->children[0] ); |
| 64 |
WritePortalFile_r( node->children[1] ); |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
if ( node->contents & CONTENTS_SOLID ) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
for ( p = node->portals ; p ; p = p->next[s] ) |
| 73 |
{ |
| 74 |
w = p->winding; |
| 75 |
s = ( p->nodes[1] == node ); |
| 76 |
if ( w && p->nodes[0] == node ) { |
| 77 |
if ( !Portal_VisFlood( p ) ) { |
| 78 |
continue; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
WindingPlane( w, normal, &dist ); |
| 87 |
if ( DotProduct( p->plane.normal, normal ) < 0.99 ) { |
| 88 |
fprintf( pf,"%i %i %i ",w->numpoints, p->nodes[1]->cluster, p->nodes[0]->cluster ); |
| 89 |
} |
| 90 |
else{ |
| 91 |
fprintf( pf,"%i %i %i ",w->numpoints, p->nodes[0]->cluster, p->nodes[1]->cluster ); |
| 92 |
} |
| 93 |
for ( i = 0 ; i < w->numpoints ; i++ ) |
| 94 |
{ |
| 95 |
fprintf( pf,"(" ); |
| 96 |
WriteFloat( pf, w->p[i][0] ); |
| 97 |
WriteFloat( pf, w->p[i][1] ); |
| 98 |
WriteFloat( pf, w->p[i][2] ); |
| 99 |
fprintf( pf,") " ); |
| 100 |
} |
| 101 |
fprintf( pf,"\n" ); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
void FillLeafNumbers_r( node_t *node, int num ){ |
| 115 |
if ( node->planenum == PLANENUM_LEAF ) { |
| 116 |
if ( node->contents & CONTENTS_SOLID ) { |
| 117 |
node->cluster = -1; |
| 118 |
} |
| 119 |
else{ |
| 120 |
node->cluster = num; |
| 121 |
} |
| 122 |
return; |
| 123 |
} |
| 124 |
node->cluster = num; |
| 125 |
FillLeafNumbers_r( node->children[0], num ); |
| 126 |
FillLeafNumbers_r( node->children[1], num ); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
void NumberLeafs_r( node_t *node ){ |
| 135 |
portal_t *p; |
| 136 |
|
| 137 |
if ( node->planenum != PLANENUM_LEAF && !node->detail_seperator ) { |
| 138 |
node->cluster = -99; |
| 139 |
NumberLeafs_r( node->children[0] ); |
| 140 |
NumberLeafs_r( node->children[1] ); |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
if ( node->contents & CONTENTS_SOLID ) { |
| 147 |
node->cluster = -1; |
| 148 |
return; |
| 149 |
} |
| 150 |
|
| 151 |
FillLeafNumbers_r( node, num_visclusters ); |
| 152 |
num_visclusters++; |
| 153 |
|
| 154 |
|
| 155 |
for ( p = node->portals ; p ; ) |
| 156 |
{ |
| 157 |
if ( p->nodes[0] == node ) { |
| 158 |
if ( Portal_VisFlood( p ) ) { |
| 159 |
num_visportals++; |
| 160 |
} |
| 161 |
p = p->next[0]; |
| 162 |
} |
| 163 |
else{ |
| 164 |
p = p->next[1]; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
void CreateVisPortals_r( node_t *node ){ |
| 177 |
|
| 178 |
|
| 179 |
if ( node->planenum == PLANENUM_LEAF || node->detail_seperator ) { |
| 180 |
return; |
| 181 |
} |
| 182 |
|
| 183 |
MakeNodePortal( node ); |
| 184 |
SplitNodePortals( node ); |
| 185 |
|
| 186 |
CreateVisPortals_r( node->children[0] ); |
| 187 |
CreateVisPortals_r( node->children[1] ); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
void FinishVisPortals2_r( node_t *node ){ |
| 196 |
if ( node->planenum == PLANENUM_LEAF ) { |
| 197 |
return; |
| 198 |
} |
| 199 |
|
| 200 |
MakeNodePortal( node ); |
| 201 |
SplitNodePortals( node ); |
| 202 |
|
| 203 |
FinishVisPortals2_r( node->children[0] ); |
| 204 |
FinishVisPortals2_r( node->children[1] ); |
| 205 |
} |
| 206 |
|
| 207 |
void FinishVisPortals_r( node_t *node ){ |
| 208 |
if ( node->planenum == PLANENUM_LEAF ) { |
| 209 |
return; |
| 210 |
} |
| 211 |
|
| 212 |
if ( node->detail_seperator ) { |
| 213 |
FinishVisPortals2_r( node ); |
| 214 |
return; |
| 215 |
} |
| 216 |
|
| 217 |
FinishVisPortals_r( node->children[0] ); |
| 218 |
FinishVisPortals_r( node->children[1] ); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
int clusterleaf; |
| 223 |
void SaveClusters_r( node_t *node ){ |
| 224 |
if ( node->planenum == PLANENUM_LEAF ) { |
| 225 |
dleafs[clusterleaf++].cluster = node->cluster; |
| 226 |
return; |
| 227 |
} |
| 228 |
SaveClusters_r( node->children[0] ); |
| 229 |
SaveClusters_r( node->children[1] ); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
void WritePortalFile( tree_t *tree ){ |
| 238 |
char filename[1024]; |
| 239 |
node_t *headnode; |
| 240 |
|
| 241 |
Sys_FPrintf( SYS_VRB, "--- WritePortalFile ---\n" ); |
| 242 |
|
| 243 |
headnode = tree->headnode; |
| 244 |
num_visclusters = 0; |
| 245 |
num_visportals = 0; |
| 246 |
|
| 247 |
FreeTreePortals_r( headnode ); |
| 248 |
|
| 249 |
MakeHeadnodePortals( tree ); |
| 250 |
|
| 251 |
CreateVisPortals_r( headnode ); |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
NumberLeafs_r( headnode ); |
| 256 |
|
| 257 |
|
| 258 |
sprintf( filename, "%s.prt", source ); |
| 259 |
Sys_Printf( "writing %s\n", filename ); |
| 260 |
pf = fopen( filename, "w" ); |
| 261 |
if ( !pf ) { |
| 262 |
Error( "Error opening %s", filename ); |
| 263 |
} |
| 264 |
|
| 265 |
fprintf( pf, "%s\n", PORTALFILE ); |
| 266 |
fprintf( pf, "%i\n", num_visclusters ); |
| 267 |
fprintf( pf, "%i\n", num_visportals ); |
| 268 |
|
| 269 |
Sys_FPrintf( SYS_VRB, "%5i visclusters\n", num_visclusters ); |
| 270 |
Sys_FPrintf( SYS_VRB, "%5i visportals\n", num_visportals ); |
| 271 |
|
| 272 |
WritePortalFile_r( headnode ); |
| 273 |
|
| 274 |
fclose( pf ); |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
clusterleaf = 1; |
| 279 |
SaveClusters_r( headnode ); |
| 280 |
} |