Debian Codegraph @

code/renderergl1/tr_bsp.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22 // tr_map.c
23
24 #include "tr_local.h"
25
26 /*
27
28 Loads and prepares a map file for scene rendering.
29
30 A single entry point:
31
32 void RE_LoadWorldMap( const char *name );
33
34 */
35
36 static world_t s_worldData;
37 static byte *fileBase;
38
39 int c_subdivisions;
40 int c_gridVerts;
41
42 //===============================================================================
43
44 static void HSVtoRGB( float h, float s, float v, float rgb[3] )
45 {
46 int i;
47 float f;
48 float p, q, t;
49
50 h *= 5;
51
52 i = floor( h );
53 f = h - i;
54
55 p = v * ( 1 - s );
56 q = v * ( 1 - s * f );
57 t = v * ( 1 - s * ( 1 - f ) );
58
59 switch ( i )
60 {
61 case 0:
62 rgb[0] = v;
63 rgb[1] = t;
64 rgb[2] = p;
65 break;
66 case 1:
67 rgb[0] = q;
68 rgb[1] = v;
69 rgb[2] = p;
70 break;
71 case 2:
72 rgb[0] = p;
73 rgb[1] = v;
74 rgb[2] = t;
75 break;
76 case 3:
77 rgb[0] = p;
78 rgb[1] = q;
79 rgb[2] = v;
80 break;
81 case 4:
82 rgb[0] = t;
83 rgb[1] = p;
84 rgb[2] = v;
85 break;
86 case 5:
87 rgb[0] = v;
88 rgb[1] = p;
89 rgb[2] = q;
90 break;
91 }
92 }
93
94 /*
95 ===============
96 R_ColorShiftLightingBytes
97
98 ===============
99 */
100 static void R_ColorShiftLightingBytes( byte in[4], byte out[4] ) {
101 int shift, r, g, b;
102
103 // shift the color data based on overbright range
104 shift = r_mapOverBrightBits->integer - tr.overbrightBits;
105
106 // shift the data based on overbright range
107 r = in[0] << shift;
108 g = in[1] << shift;
109 b = in[2] << shift;
110
111 // normalize by color instead of saturating to white
112 if ( ( r | g | b ) > 255 ) {
113 int max;
114
115 max = r > g ? r : g;
116 max = max > b ? max : b;
117 r = r * 255 / max;
118 g = g * 255 / max;
119 b = b * 255 / max;
120 }
121
122 out[0] = r;
123 out[1] = g;
124 out[2] = b;
125 out[3] = in[3];
126 }
127
128 /*
129 ===============
130 R_LoadLightmaps
131
132 ===============
133 */
134 #define LIGHTMAP_SIZE 128
135 static void R_LoadLightmaps( lump_t *l ) {
136 byte *buf, *buf_p;
137 int len;
138 byte image[LIGHTMAP_SIZE*LIGHTMAP_SIZE*4];
139 int i, j;
140 float maxIntensity = 0;
141
142 len = l->filelen;
143 if ( !len ) {
144 return;
145 }
146 buf = fileBase + l->fileofs;
147
148 // we are about to upload textures
149 R_IssuePendingRenderCommands();
150
151 // create all the lightmaps
152 tr.numLightmaps = len / (LIGHTMAP_SIZE * LIGHTMAP_SIZE * 3);
153 if ( tr.numLightmaps == 1 ) {
154 //FIXME: HACK: maps with only one lightmap turn up fullbright for some reason.
155 //this avoids this, but isn't the correct solution.
156 tr.numLightmaps++;
157 }
158
159 // if we are in r_vertexLight mode, we don't need the lightmaps at all
160 if ( r_vertexLight->integer || glConfig.hardwareType == GLHW_PERMEDIA2 ) {
161 return;
162 }
163
164 tr.lightmaps = ri.Hunk_Alloc( tr.numLightmaps * sizeof(image_t *), h_low );
165 for ( i = 0 ; i < tr.numLightmaps ; i++ ) {
166 // expand the 24 bit on-disk to 32 bit
167 buf_p = buf + i * LIGHTMAP_SIZE*LIGHTMAP_SIZE * 3;
168
169 if ( r_lightmap->integer == 2 )
170 { // color code by intensity as development tool (FIXME: check range)
171 for ( j = 0; j < LIGHTMAP_SIZE * LIGHTMAP_SIZE; j++ )
172 {
173 float r = buf_p[j*3+0];
174 float g = buf_p[j*3+1];
175 float b = buf_p[j*3+2];
176 float intensity;
177 float out[3] = {0.0, 0.0, 0.0};
178
179 intensity = 0.33f * r + 0.685f * g + 0.063f * b;
180
181 if ( intensity > 255 )
182 intensity = 1.0f;
183 else
184 intensity /= 255.0f;
185
186 if ( intensity > maxIntensity )
187 maxIntensity = intensity;
188
189 HSVtoRGB( intensity, 1.00, 0.50, out );
190
191 image[j*4+0] = out[0] * 255;
192 image[j*4+1] = out[1] * 255;
193 image[j*4+2] = out[2] * 255;
194 image[j*4+3] = 255;
195 }
196 } else {
197 for ( j = 0 ; j < LIGHTMAP_SIZE * LIGHTMAP_SIZE; j++ ) {
198 R_ColorShiftLightingBytes( &buf_p[j*3], &image[j*4] );
199 image[j*4+3] = 255;
200 }
201 }
202 tr.lightmaps[i] = R_CreateImage( va("*lightmap%d",i), image,
203 LIGHTMAP_SIZE, LIGHTMAP_SIZE, IMGTYPE_COLORALPHA,
204 IMGFLAG_NOLIGHTSCALE | IMGFLAG_NO_COMPRESSION | IMGFLAG_CLAMPTOEDGE, 0 );
205 }
206
207 if ( r_lightmap->integer == 2 ) {
208 ri.Printf( PRINT_ALL, "Brightest lightmap value: %d\n", ( int ) ( maxIntensity * 255 ) );
209 }
210 }
211
212
213 /*
214 =================
215 RE_SetWorldVisData
216
217 This is called by the clipmodel subsystem so we can share the 1.8 megs of
218 space in big maps...
219 =================
220 */
221 void RE_SetWorldVisData( const byte *vis ) {
222 tr.externalVisData = vis;
223 }
224
225
226 /*
227 =================
228 R_LoadVisibility
229 =================
230 */
231 static void R_LoadVisibility( lump_t *l ) {
232 int len;
233 byte *buf;
234
235 len = ( s_worldData.numClusters + 63 ) & ~63;
236 s_worldData.novis = ri.Hunk_Alloc( len, h_low );
237 Com_Memset( s_worldData.novis, 0xff, len );
238
239 len = l->filelen;
240 if ( !len ) {
241 return;
242 }
243 buf = fileBase + l->fileofs;
244
245 s_worldData.numClusters = LittleLong( ((int *)buf)[0] );
246 s_worldData.clusterBytes = LittleLong( ((int *)buf)[1] );
247
248 // CM_Load should have given us the vis data to share, so
249 // we don't need to allocate another copy
250 if ( tr.externalVisData ) {
251 s_worldData.vis = tr.externalVisData;
252 } else {
253 byte *dest;
254
255 dest = ri.Hunk_Alloc( len - 8, h_low );
256 Com_Memcpy( dest, buf + 8, len - 8 );
257 s_worldData.vis = dest;
258 }
259 }
260
261 //===============================================================================
262
263
264 /*
265 ===============
266 ShaderForShaderNum
267 ===============
268 */
269 static shader_t *ShaderForShaderNum( int shaderNum, int lightmapNum ) {
270 shader_t *shader;
271 dshader_t *dsh;
272
273 int _shaderNum = LittleLong( shaderNum );
274 if ( _shaderNum < 0 || _shaderNum >= s_worldData.numShaders ) {
275 ri.Error( ERR_DROP, "ShaderForShaderNum: bad num %i", _shaderNum );
276 }
277 dsh = &s_worldData.shaders[ _shaderNum ];
278
279 if ( r_vertexLight->integer || glConfig.hardwareType == GLHW_PERMEDIA2 ) {
280 lightmapNum = LIGHTMAP_BY_VERTEX;
281 }
282
283 if ( r_fullbright->integer ) {
284 lightmapNum = LIGHTMAP_WHITEIMAGE;
285 }
286
287 shader = R_FindShader( dsh->shader, lightmapNum, qtrue );
288
289 // if the shader had errors, just use default shader
290 if ( shader->defaultShader ) {
291 return tr.defaultShader;
292 }
293
294 return shader;
295 }
296
297 /*
298 ===============
299 ParseFace
300 ===============
301 */
302 static void ParseFace( dsurface_t *ds, drawVert_t *verts, msurface_t *surf, int *indexes ) {
303 int i, j;
304 srfSurfaceFace_t *cv;
305 int numPoints, numIndexes;
306 int lightmapNum;
307 int sfaceSize, ofsIndexes;
308
309 lightmapNum = LittleLong( ds->lightmapNum );
310
311 // get fog volume
312 surf->fogIndex = LittleLong( ds->fogNum ) + 1;
313
314 // get shader value
315 surf->shader = ShaderForShaderNum( ds->shaderNum, lightmapNum );
316 if ( r_singleShader->integer && !surf->shader->isSky ) {
317 surf->shader = tr.defaultShader;
318 }
319
320 numPoints = LittleLong( ds->numVerts );
321 if (numPoints > MAX_FACE_POINTS) {
322 ri.Printf( PRINT_WARNING, "WARNING: MAX_FACE_POINTS exceeded: %i\n", numPoints);
323 numPoints = MAX_FACE_POINTS;
324 surf->shader = tr.defaultShader;
325 }
326
327 numIndexes = LittleLong( ds->numIndexes );
328
329 // create the srfSurfaceFace_t
330 sfaceSize = offsetof( srfSurfaceFace_t, points ) + sizeof( *cv->points ) * numPoints;
331 ofsIndexes = sfaceSize;
332 sfaceSize += sizeof( int ) * numIndexes;
333
334 cv = ri.Hunk_Alloc( sfaceSize, h_low );
335 cv->surfaceType = SF_FACE;
336 cv->numPoints = numPoints;
337 cv->numIndices = numIndexes;
338 cv->ofsIndices = ofsIndexes;
339
340 verts += LittleLong( ds->firstVert );
341 for ( i = 0 ; i < numPoints ; i++ ) {
342 for ( j = 0 ; j < 3 ; j++ ) {
343 cv->points[i][j] = LittleFloat( verts[i].xyz[j] );
344 }
345 for ( j = 0 ; j < 2 ; j++ ) {
346 cv->points[i][3+j] = LittleFloat( verts[i].st[j] );
347 cv->points[i][5+j] = LittleFloat( verts[i].lightmap[j] );
348 }
349 R_ColorShiftLightingBytes( verts[i].color, (byte *)&cv->points[i][7] );
350 }
351
352 indexes += LittleLong( ds->firstIndex );
353 for ( i = 0 ; i < numIndexes ; i++ ) {
354 ((int *)((byte *)cv + cv->ofsIndices ))[i] = LittleLong( indexes[ i ] );
355 }
356
357 // take the plane information from the lightmap vector
358 for ( i = 0 ; i < 3 ; i++ ) {
359 cv->plane.normal[i] = LittleFloat( ds->lightmapVecs[2][i] );
360 }
361 cv->plane.dist = DotProduct( cv->points[0], cv->plane.normal );
362 SetPlaneSignbits( &cv->plane );
363 cv->plane.type = PlaneTypeForNormal( cv->plane.normal );
364
365 surf->data = (surfaceType_t *)cv;
366 }
367
368
369 /*
370 ===============
371 ParseMesh
372 ===============
373 */
374 static void ParseMesh ( dsurface_t *ds, drawVert_t *verts, msurface_t *surf ) {
375 srfGridMesh_t *grid;
376 int i, j;
377 int width, height, numPoints;
378 drawVert_t points[MAX_PATCH_SIZE*MAX_PATCH_SIZE];
379 int lightmapNum;
380 vec3_t bounds[2];
381 vec3_t tmpVec;
382 static surfaceType_t skipData = SF_SKIP;
383
384 lightmapNum = LittleLong( ds->lightmapNum );
385
386 // get fog volume
387 surf->fogIndex = LittleLong( ds->fogNum ) + 1;
388
389 // get shader value
390 surf->shader = ShaderForShaderNum( ds->shaderNum, lightmapNum );
391 if ( r_singleShader->integer && !surf->shader->isSky ) {
392 surf->shader = tr.defaultShader;
393 }
394
395 // we may have a nodraw surface, because they might still need to
396 // be around for movement clipping
397 if ( s_worldData.shaders[ LittleLong( ds->shaderNum ) ].surfaceFlags & SURF_NODRAW ) {
398 surf->data = &skipData;
399 return;
400 }
401
402 width = LittleLong( ds->patchWidth );
403 height = LittleLong( ds->patchHeight );
404
405 verts += LittleLong( ds->firstVert );
406 numPoints = width * height;
407 for ( i = 0 ; i < numPoints ; i++ ) {
408 for ( j = 0 ; j < 3 ; j++ ) {
409 points[i].xyz[j] = LittleFloat( verts[i].xyz[j] );
410 points[i].normal[j] = LittleFloat( verts[i].normal[j] );
411 }
412 for ( j = 0 ; j < 2 ; j++ ) {
413 points[i].st[j] = LittleFloat( verts[i].st[j] );
414 points[i].lightmap[j] = LittleFloat( verts[i].lightmap[j] );
415 }
416 R_ColorShiftLightingBytes( verts[i].color, points[i].color );
417 }
418
419 // pre-tesseleate
420 grid = R_SubdividePatchToGrid( width, height, points );
421 surf->data = (surfaceType_t *)grid;
422
423 // copy the level of detail origin, which is the center
424 // of the group of all curves that must subdivide the same
425 // to avoid cracking
426 for ( i = 0 ; i < 3 ; i++ ) {
427 bounds[0][i] = LittleFloat( ds->lightmapVecs[0][i] );
428 bounds[1][i] = LittleFloat( ds->lightmapVecs[1][i] );
429 }
430 VectorAdd( bounds[0], bounds[1], bounds[1] );
431 VectorScale( bounds[1], 0.5f, grid->lodOrigin );
432 VectorSubtract( bounds[0], grid->lodOrigin, tmpVec );
433 grid->lodRadius = VectorLength( tmpVec );
434 }
435
436 /*
437 ===============
438 ParseTriSurf
439 ===============
440 */
441 static void ParseTriSurf( dsurface_t *ds, drawVert_t *verts, msurface_t *surf, int *indexes ) {
442 srfTriangles_t *tri;
443 int i, j;
444 int numVerts, numIndexes;
445
446 // get fog volume
447 surf->fogIndex = LittleLong( ds->fogNum ) + 1;
448
449 // get shader
450 surf->shader = ShaderForShaderNum( ds->shaderNum, LIGHTMAP_BY_VERTEX );
451 if ( r_singleShader->integer && !surf->shader->isSky ) {
452 surf->shader = tr.defaultShader;
453 }
454
455 numVerts = LittleLong( ds->numVerts );
456 numIndexes = LittleLong( ds->numIndexes );
457
458 tri = ri.Hunk_Alloc( sizeof( *tri ) + numVerts * sizeof( tri->verts[0] )
459 + numIndexes * sizeof( tri->indexes[0] ), h_low );
460 tri->surfaceType = SF_TRIANGLES;
461 tri->numVerts = numVerts;
462 tri->numIndexes = numIndexes;
463 tri->verts = (drawVert_t *)(tri + 1);
464 tri->indexes = (int *)(tri->verts + tri->numVerts );
465
466 surf->data = (surfaceType_t *)tri;
467
468 // copy vertexes
469 ClearBounds( tri->bounds[0], tri->bounds[1] );
470 verts += LittleLong( ds->firstVert );
471 for ( i = 0 ; i < numVerts ; i++ ) {
472 for ( j = 0 ; j < 3 ; j++ ) {
473 tri->verts[i].xyz[j] = LittleFloat( verts[i].xyz[j] );
474 tri->verts[i].normal[j] = LittleFloat( verts[i].normal[j] );
475 }
476 AddPointToBounds( tri->verts[i].xyz, tri->bounds[0], tri->bounds[1] );
477 for ( j = 0 ; j < 2 ; j++ ) {
478 tri->verts[i].st[j] = LittleFloat( verts[i].st[j] );
479 tri->verts[i].lightmap[j] = LittleFloat( verts[i].lightmap[j] );
480 }
481
482 R_ColorShiftLightingBytes( verts[i].color, tri->verts[i].color );
483 }
484
485 // copy indexes
486 indexes += LittleLong( ds->firstIndex );
487 for ( i = 0 ; i < numIndexes ; i++ ) {
488 tri->indexes[i] = LittleLong( indexes[i] );
489 if ( tri->indexes[i] < 0 || tri->indexes[i] >= numVerts ) {
490 ri.Error( ERR_DROP, "Bad index in triangle surface" );
491 }
492 }
493 }
494
495 /*
496 ===============
497 ParseFlare
498 ===============
499 */
500 static void ParseFlare( dsurface_t *ds, drawVert_t *verts, msurface_t *surf, int *indexes ) {
501 srfFlare_t *flare;
502 int i;
503
504 // get fog volume
505 surf->fogIndex = LittleLong( ds->fogNum ) + 1;
506
507 // get shader
508 surf->shader = ShaderForShaderNum( ds->shaderNum, LIGHTMAP_BY_VERTEX );
509 if ( r_singleShader->integer && !surf->shader->isSky ) {
510 surf->shader = tr.defaultShader;
511 }
512
513 flare = ri.Hunk_Alloc( sizeof( *flare ), h_low );
514 flare->surfaceType = SF_FLARE;
515
516 surf->data = (surfaceType_t *)flare;
517
518 for ( i = 0 ; i < 3 ; i++ ) {
519 flare->origin[i] = LittleFloat( ds->lightmapOrigin[i] );
520 flare->color[i] = LittleFloat( ds->lightmapVecs[0][i] );
521 flare->normal[i] = LittleFloat( ds->lightmapVecs[2][i] );
522 }
523 }
524
525
526 /*
527 =================
528 R_MergedWidthPoints
529
530 returns true if there are grid points merged on a width edge
531 =================
532 */
533 int R_MergedWidthPoints(srfGridMesh_t *grid, int offset) {
534 int i, j;
535
536 for (i = 1; i < grid->width-1; i++) {
537 for (j = i + 1; j < grid->width-1; j++) {
538 if ( fabs(grid->verts[i + offset].xyz[0] - grid->verts[j + offset].xyz[0]) > .1) continue;
539 if ( fabs(grid->verts[i + offset].xyz[1] - grid->verts[j + offset].xyz[1]) > .1) continue;
540 if ( fabs(grid->verts[i + offset].xyz[2] - grid->verts[j + offset].xyz[2]) > .1) continue;
541 return qtrue;
542 }
543 }
544 return qfalse;
545 }
546
547 /*
548 =================
549 R_MergedHeightPoints
550
551 returns true if there are grid points merged on a height edge
552 =================
553 */
554 int R_MergedHeightPoints(srfGridMesh_t *grid, int offset) {
555 int i, j;
556
557 for (i = 1; i < grid->height-1; i++) {
558 for (j = i + 1; j < grid->height-1; j++) {
559 if ( fabs(grid->verts[grid->width * i + offset].xyz[0] - grid->verts[grid->width * j + offset].xyz[0]) > .1) continue;
560 if ( fabs(grid->verts[grid->width * i + offset].xyz[1] - grid->verts[grid->width * j + offset].xyz[1]) > .1) continue;
561 if ( fabs(grid->verts[grid->width * i + offset].xyz[2] - grid->verts[grid->width * j + offset].xyz[2]) > .1) continue;
562 return qtrue;
563 }
564 }
565 return qfalse;
566 }
567
568 /*
569 =================
570 R_FixSharedVertexLodError_r
571
572 NOTE: never sync LoD through grid edges with merged points!
573
574 FIXME: write generalized version that also avoids cracks between a patch and one that meets half way?
575 =================
576 */
577 void R_FixSharedVertexLodError_r( int start, srfGridMesh_t *grid1 ) {
578 int j, k, l, m, n, offset1, offset2, touch;
579 srfGridMesh_t *grid2;
580
581 for ( j = start; j < s_worldData.numsurfaces; j++ ) {
582 //
583 grid2 = (srfGridMesh_t *) s_worldData.surfaces[j].data;
584 // if this surface is not a grid
585 if ( grid2->surfaceType != SF_GRID ) continue;
586 // if the LOD errors are already fixed for this patch
587 if ( grid2->lodFixed == 2 ) continue;
588 // grids in the same LOD group should have the exact same lod radius
589 if ( grid1->lodRadius != grid2->lodRadius ) continue;
590 // grids in the same LOD group should have the exact same lod origin
591 if ( grid1->lodOrigin[0] != grid2->lodOrigin[0] ) continue;
592 if ( grid1->lodOrigin[1] != grid2->lodOrigin[1] ) continue;
593 if ( grid1->lodOrigin[2] != grid2->lodOrigin[2] ) continue;
594 //
595 touch = qfalse;
596 for (n = 0; n < 2; n++) {
597 //
598 if (n) offset1 = (grid1->height-1) * grid1->width;
599 else offset1 = 0;
600 if (R_MergedWidthPoints(grid1, offset1)) continue;
601 for (k = 1; k < grid1->width-1; k++) {
602 for (m = 0; m < 2; m++) {
603
604 if (m) offset2 = (grid2->height-1) * grid2->width;
605 else offset2 = 0;
606 if (R_MergedWidthPoints(grid2, offset2)) continue;
607 for ( l = 1; l < grid2->width-1; l++) {
608 //
609 if ( fabs(grid1->verts[k + offset1].xyz[0] - grid2->verts[l + offset2].xyz[0]) > .1) continue;
610 if ( fabs(grid1->verts[k + offset1].xyz[1] - grid2->verts[l + offset2].xyz[1]) > .1) continue;
611 if ( fabs(grid1->verts[k + offset1].xyz[2] - grid2->verts[l + offset2].xyz[2]) > .1) continue;
612 // ok the points are equal and should have the same lod error
613 grid2->widthLodError[l] = grid1->widthLodError[k];
614 touch = qtrue;
615 }
616 }
617 for (m = 0; m < 2; m++) {
618
619 if (m) offset2 = grid2->width-1;
620 else offset2 = 0;
621 if (R_MergedHeightPoints(grid2, offset2)) continue;
622 for ( l = 1; l < grid2->height-1; l++) {
623 //
624 if ( fabs(grid1->verts[k + offset1].xyz[0] - grid2->verts[grid2->width * l + offset2].xyz[0]) > .1) continue;
625 if ( fabs(grid1->verts[k + offset1].xyz[1] - grid2->verts[grid2->width * l + offset2].xyz[1]) > .1) continue;
626 if ( fabs(grid1->verts[k + offset1].xyz[2] - grid2->verts[grid2->width * l + offset2].xyz[2]) > .1) continue;
627 // ok the points are equal and should have the same lod error
628 grid2->heightLodError[l] = grid1->widthLodError[k];
629 touch = qtrue;
630 }
631 }
632 }
633 }
634 for (n = 0; n < 2; n++) {
635 //
636 if (n) offset1 = grid1->width-1;
637 else offset1 = 0;
638 if (R_MergedHeightPoints(grid1, offset1)) continue;
639 for (k = 1; k < grid1->height-1; k++) {
640 for (m = 0; m < 2; m++) {
641
642 if (m) offset2 = (grid2->height-1) * grid2->width;
643 else offset2 = 0;
644 if (R_MergedWidthPoints(grid2, offset2)) continue;
645 for ( l = 1; l < grid2->width-1; l++) {
646 //
647 if ( fabs(grid1->verts[grid1->width * k + offset1].xyz[0] - grid2->verts[l + offset2].xyz[0]) > .1) continue;
648 if ( fabs(grid1->verts[grid1->width * k + offset1].xyz[1] - grid2->verts[l + offset2].xyz[1]) > .1) continue;
649 if ( fabs(grid1->verts[grid1->width * k + offset1].xyz[2] - grid2->verts[l + offset2].xyz[2]) > .1) continue;
650 // ok the points are equal and should have the same lod error
651 grid2->widthLodError[l] = grid1->heightLodError[k];
652 touch = qtrue;
653 }
654 }
655 for (m = 0; m < 2; m++) {
656
657 if (m) offset2 = grid2->width-1;
658 else offset2 = 0;
659 if (R_MergedHeightPoints(grid2, offset2)) continue;
660 for ( l = 1; l < grid2->height-1; l++) {
661 //
662 if ( fabs(grid1->verts[grid1->width * k + offset1].xyz[0] - grid2->verts[grid2->width * l + offset2].xyz[0]) > .1) continue;
663 if ( fabs(grid1->verts[grid1->width * k + offset1].xyz[1] - grid2->verts[grid2->width * l + offset2].xyz[1]) > .1) continue;
664 if ( fabs(grid1->verts[grid1->width * k + offset1].xyz[2] - grid2->verts[grid2->width * l + offset2].xyz[2]) > .1) continue;
665 // ok the points are equal and should have the same lod error
666 grid2->heightLodError[l] = grid1->heightLodError[k];
667 touch = qtrue;
668 }
669 }
670 }
671 }
672 if (touch) {
673 grid2->lodFixed = 2;
674 R_FixSharedVertexLodError_r ( start, grid2 );
675 //NOTE: this would be correct but makes things really slow
676 //grid2->lodFixed = 1;
677 }
678 }
679 }
680
681 /*
682 =================
683 R_FixSharedVertexLodError
684
685 This function assumes that all patches in one group are nicely stitched together for the highest LoD.
686 If this is not the case this function will still do its job but won't fix the highest LoD cracks.
687 =================
688 */
689 void R_FixSharedVertexLodError( void ) {
690 int i;
691 srfGridMesh_t *grid1;
692
693 for ( i = 0; i < s_worldData.numsurfaces; i++ ) {
694 //
695 grid1 = (srfGridMesh_t *) s_worldData.surfaces[i].data;
696 // if this surface is not a grid
697 if ( grid1->surfaceType != SF_GRID )
698 continue;
699 //
700 if ( grid1->lodFixed )
701 continue;
702 //
703 grid1->lodFixed = 2;
704 // recursively fix other patches in the same LOD group
705 R_FixSharedVertexLodError_r( i + 1, grid1);
706 }
707 }
708
709
710 /*
711 ===============
712 R_StitchPatches
713 ===============
714 */
715 int R_StitchPatches( int grid1num, int grid2num ) {
716 float *v1, *v2;
717 srfGridMesh_t *grid1, *grid2;
718 int k, l, m, n, offset1, offset2, row, column;
719
720 grid1 = (srfGridMesh_t *) s_worldData.surfaces[grid1num].data;
721 grid2 = (srfGridMesh_t *) s_worldData.surfaces[grid2num].data;
722 for (n = 0; n < 2; n++) {
723 //
724 if (n) offset1 = (grid1->height-1) * grid1->width;
725 else offset1 = 0;
726 if (R_MergedWidthPoints(grid1, offset1))
727 continue;
728 for (k = 0; k < grid1->width-2; k += 2) {
729
730 for (m = 0; m < 2; m++) {
731
732 if ( grid2->width >= MAX_GRID_SIZE )
733 break;
734 if (m) offset2 = (grid2->height-1) * grid2->width;
735 else offset2 = 0;
736 for ( l = 0; l < grid2->width-1; l++) {
737 //
738 v1 = grid1->verts[k + offset1].xyz;
739 v2 = grid2->verts[l + offset2].xyz;
740 if ( fabs(v1[0] - v2[0]) > .1)
741 continue;
742 if ( fabs(v1[1] - v2[1]) > .1)
743 continue;
744 if ( fabs(v1[2] - v2[2]) > .1)
745 continue;
746
747 v1 = grid1->verts[k + 2 + offset1].xyz;
748 v2 = grid2->verts[l + 1 + offset2].xyz;
749 if ( fabs(v1[0] - v2[0]) > .1)
750 continue;
751 if ( fabs(v1[1] - v2[1]) > .1)
752 continue;
753 if ( fabs(v1[2] - v2[2]) > .1)
754 continue;
755 //
756 v1 = grid2->verts[l + offset2].xyz;
757 v2 = grid2->verts[l + 1 + offset2].xyz;
758 if ( fabs(v1[0] - v2[0]) < .01 &&
759 fabs(v1[1] - v2[1]) < .01 &&
760 fabs(v1[2] - v2[2]) < .01)
761 continue;
762 //
763 //ri.Printf( PRINT_ALL, "found highest LoD crack between two patches\n" );
764 // insert column into grid2 right after after column l
765 if (m) row = grid2->height-1;
766 else row = 0;
767 grid2 = R_GridInsertColumn( grid2, l+1, row,
768 grid1->verts[k + 1 + offset1].xyz, grid1->widthLodError[k+1]);
769 grid2->lodStitched = qfalse;
770 s_worldData.surfaces[grid2num].data = (void *) grid2;
771 return qtrue;
772 }
773 }
774 for (m = 0; m < 2; m++) {
775
776 if (grid2->height >= MAX_GRID_SIZE)
777 break;
778 if (m) offset2 = grid2->width-1;
779 else offset2 = 0;
780 for ( l = 0; l < grid2->height-1; l++) {
781 //
782 v1 = grid1->verts[k + offset1].xyz;
783 v2 = grid2->verts[grid2->width * l + offset2].xyz;
784 if ( fabs(v1[0] - v2[0]) > .1)
785 continue;
786 if ( fabs(v1[1] - v2[1]) > .1)
787 continue;
788 if ( fabs(v1[2] - v2[2]) > .1)
789 continue;
790
791 v1 = grid1->verts[k + 2 + offset1].xyz;
792 v2 = grid2->verts[grid2->width * (l + 1) + offset2].xyz;
793 if ( fabs(v1[0] - v2[0]) > .1)
794 continue;
795 if ( fabs(v1[1] - v2[1]) > .1)
796 continue;
797 if ( fabs(v1[2] - v2[2]) > .1)
798 continue;
799 //
800 v1 = grid2->verts[grid2->width * l + offset2].xyz;
801 v2 = grid2->verts[grid2->width * (l + 1) + offset2].xyz;
802 if ( fabs(v1[0] - v2[0]) < .01 &&
803 fabs(v1[1] - v2[1]) < .01 &&
804 fabs(v1[2] - v2[2]) < .01)
805 continue;
806 //
807 //ri.Printf( PRINT_ALL, "found highest LoD crack between two patches\n" );
808 // insert row into grid2 right after after row l
809 if (m) column = grid2->width-1;
810 else column = 0;
811 grid2 = R_GridInsertRow( grid2, l+1, column,
812 grid1->verts[k + 1 + offset1].xyz, grid1->widthLodError[k+1]);
813 grid2->lodStitched = qfalse;
814 s_worldData.surfaces[grid2num].data = (void *) grid2;
815 return qtrue;
816 }
817 }
818 }
819 }
820 for (n = 0; n < 2; n++) {
821 //
822 if (n) offset1 = grid1->width-1;
823 else offset1 = 0;
824 if (R_MergedHeightPoints(grid1, offset1))
825 continue;
826 for (k = 0; k < grid1->height-2; k += 2) {
827 for (m = 0; m < 2; m++) {
828
829 if ( grid2->width >= MAX_GRID_SIZE )
830 break;
831 if (m) offset2 = (grid2->height-1) * grid2->width;
832 else offset2 = 0;
833 for ( l = 0; l < grid2->width-1; l++) {
834 //
835 v1 = grid1->verts[grid1->width * k + offset1].xyz;
836 v2 = grid2->verts[l + offset2].xyz;
837 if ( fabs(v1[0] - v2[0]) > .1)
838 continue;
839 if ( fabs(v1[1] - v2[1]) > .1)
840 continue;
841 if ( fabs(v1[2] - v2[2]) > .1)
842 continue;
843
844 v1 = grid1->verts[grid1->width * (k + 2) + offset1].xyz;
845 v2 = grid2->verts[l + 1 + offset2].xyz;
846 if ( fabs(v1[0] - v2[0]) > .1)
847 continue;
848 if ( fabs(v1[1] - v2[1]) > .1)
849 continue;
850 if ( fabs(v1[2] - v2[2]) > .1)
851 continue;
852 //
853 v1 = grid2->verts[l + offset2].xyz;
854 v2 = grid2->verts[(l + 1) + offset2].xyz;
855 if ( fabs(v1[0] - v2[0]) < .01 &&
856 fabs(v1[1] - v2[1]) < .01 &&
857 fabs(v1[2] - v2[2]) < .01)
858 continue;
859 //
860 //ri.Printf( PRINT_ALL, "found highest LoD crack between two patches\n" );
861 // insert column into grid2 right after after column l
862 if (m) row = grid2->height-1;
863 else row = 0;
864 grid2 = R_GridInsertColumn( grid2, l+1, row,
865 grid1->verts[grid1->width * (k + 1) + offset1].xyz, grid1->heightLodError[k+1]);
866 grid2->lodStitched = qfalse;
867 s_worldData.surfaces[grid2num].data = (void *) grid2;
868 return qtrue;
869 }
870 }
871 for (m = 0; m < 2; m++) {
872
873 if (grid2->height >= MAX_GRID_SIZE)
874 break;
875 if (m) offset2 = grid2->width-1;
876 else offset2 = 0;
877 for ( l = 0; l < grid2->height-1; l++) {
878 //
879 v1 = grid1->verts[grid1->width * k + offset1].xyz;
880 v2 = grid2->verts[grid2->width * l + offset2].xyz;
881 if ( fabs(v1[0] - v2[0]) > .1)
882 continue;
883 if ( fabs(v1[1] - v2[1]) > .1)
884 continue;
885 if ( fabs(v1[2] - v2[2]) > .1)
886 continue;
887
888 v1 = grid1->verts[grid1->width * (k + 2) + offset1].xyz;
889 v2 = grid2->verts[grid2->width * (l + 1) + offset2].xyz;
890 if ( fabs(v1[0] - v2[0]) > .1)
891 continue;
892 if ( fabs(v1[1] - v2[1]) > .1)
893 continue;
894 if ( fabs(v1[2] - v2[2]) > .1)
895 continue;
896 //
897 v1 = grid2->verts[grid2->width * l + offset2].xyz;
898 v2 = grid2->verts[grid2->width * (l + 1) + offset2].xyz;
899 if ( fabs(v1[0] - v2[0]) < .01 &&
900 fabs(v1[1] - v2[1]) < .01 &&
901 fabs(v1[2] - v2[2]) < .01)
902 continue;
903 //
904 //ri.Printf( PRINT_ALL, "found highest LoD crack between two patches\n" );
905 // insert row into grid2 right after after row l
906 if (m) column = grid2->width-1;
907 else column = 0;
908 grid2 = R_GridInsertRow( grid2, l+1, column,
909 grid1->verts[grid1->width * (k + 1) + offset1].xyz, grid1->heightLodError[k+1]);
910 grid2->lodStitched = qfalse;
911 s_worldData.surfaces[grid2num].data = (void *) grid2;
912 return qtrue;
913 }
914 }
915 }
916 }
917 for (n = 0; n < 2; n++) {
918 //
919 if (n) offset1 = (grid1->height-1) * grid1->width;
920 else offset1 = 0;
921 if (R_MergedWidthPoints(grid1, offset1))
922 continue;
923 for (k = grid1->width-1; k > 1; k -= 2) {
924
925 for (m = 0; m < 2; m++) {
926
927 if ( !grid2 || grid2->width >= MAX_GRID_SIZE )
928 break;
929 if (m) offset2 = (grid2->height-1) * grid2->width;
930 else offset2 = 0;
931 for ( l = 0; l < grid2->width-1; l++) {
932 //
933 v1 = grid1->verts[k + offset1].xyz;
934 v2 = grid2->verts[l + offset2].xyz;
935 if ( fabs(v1[0] - v2[0]) > .1)
936 continue;
937 if ( fabs(v1[1] - v2[1]) > .1)
938 continue;
939 if ( fabs(v1[2] - v2[2]) > .1)
940 continue;
941
942 v1 = grid1->verts[k - 2 + offset1].xyz;
943 v2 = grid2->verts[l + 1 + offset2].xyz;
944 if ( fabs(v1[0] - v2[0]) > .1)
945 continue;
946 if ( fabs(v1[1] - v2[1]) > .1)
947 continue;
948 if ( fabs(v1[2] - v2[2]) > .1)
949 continue;
950 //
951 v1 = grid2->verts[l + offset2].xyz;
952 v2 = grid2->verts[(l + 1) + offset2].xyz;
953 if ( fabs(v1[0] - v2[0]) < .01 &&
954 fabs(v1[1] - v2[1]) < .01 &&
955 fabs(v1[2] - v2[2]) < .01)
956 continue;
957 //
958 //ri.Printf( PRINT_ALL, "found highest LoD crack between two patches\n" );
959 // insert column into grid2 right after after column l
960 if (m) row = grid2->height-1;
961 else row = 0;
962 grid2 = R_GridInsertColumn( grid2, l+1, row,
963 grid1->verts[k - 1 + offset1].xyz, grid1->widthLodError[k+1]);
964 grid2->lodStitched = qfalse;
965 s_worldData.surfaces[grid2num].data = (void *) grid2;
966 return qtrue;
967 }
968 }
969 for (m = 0; m < 2; m++) {
970
971 if (!grid2 || grid2->height >= MAX_GRID_SIZE)
972 break;
973 if (m) offset2 = grid2->width-1;
974 else offset2 = 0;
975 for ( l = 0; l < grid2->height-1; l++) {
976 //
977 v1 = grid1->verts[k + offset1].xyz;
978 v2 = grid2->verts[grid2->width * l + offset2].xyz;
979 if ( fabs(v1[0] - v2[0]) > .1)
980 continue;
981 if ( fabs(v1[1] - v2[1]) > .1)
982 continue;
983 if ( fabs(v1[2] - v2[2]) > .1)
984 continue;
985
986 v1 = grid1->verts[k - 2 + offset1].xyz;
987 v2 = grid2->verts[grid2->width * (l + 1) + offset2].xyz;
988 if ( fabs(v1[0] - v2[0]) > .1)
989 continue;
990 if ( fabs(v1[1] - v2[1]) > .1)
991 continue;
992 if ( fabs(v1[2] - v2[2]) > .1)
993 continue;
994 //
995 v1 = grid2->verts[grid2->width * l + offset2].xyz;
996 v2 = grid2->verts[grid2->width * (l + 1) + offset2].xyz;
997 if ( fabs(v1[0] - v2[0]) < .01 &&
998 fabs(v1[1] - v2[1]) < .01 &&
999 fabs(v1[2] - v2[2]) < .01)
1000 continue;
1001 //
1002 //ri.Printf( PRINT_ALL, "found highest LoD crack between two patches\n" );
1003 // insert row into grid2 right after after row l
1004 if (m) column = grid2->width-1;
1005 else column = 0;
1006 grid2 = R_GridInsertRow( grid2, l+1, column,
1007 grid1->verts[k - 1 + offset1].xyz, grid1->widthLodError[k+1]);
1008 if (!grid2)
1009 break;
1010 grid2->lodStitched = qfalse;
1011 s_worldData.surfaces[grid2num].data = (void *) grid2;
1012 return qtrue;
1013 }
1014 }
1015 }
1016 }
1017 for (n = 0; n < 2; n++) {
1018 //
1019 if (n) offset1 = grid1->width-1;
1020 else offset1 = 0;
1021 if (R_MergedHeightPoints(grid1, offset1))
1022 continue;
1023 for (k = grid1->height-1; k > 1; k -= 2) {
1024 for (m = 0; m < 2; m++) {
1025
1026 if ( !grid2 || grid2->width >= MAX_GRID_SIZE )
1027 break;
1028 if (m) offset2 = (grid2->height-1) * grid2->width;
1029 else offset2 = 0;
1030 for ( l = 0; l < grid2->width-1; l++) {
1031 //
1032 v1 = grid1->verts[grid1->width * k + offset1].xyz;
1033 v2 = grid2->verts[l + offset2].xyz;
1034 if ( fabs(v1[0] - v2[0]) > .1)
1035 continue;
1036 if ( fabs(v1[1] - v2[1]) > .1)
1037 continue;
1038 if ( fabs(v1[2] - v2[2]) > .1)
1039 continue;
1040
1041 v1 = grid1->verts[grid1->width * (k - 2) + offset1].xyz;
1042 v2 = grid2->verts[l + 1 + offset2].xyz;
1043 if ( fabs(v1[0] - v2[0]) > .1)
1044 continue;
1045 if ( fabs(v1[1] - v2[1]) > .1)
1046 continue;
1047 if ( fabs(v1[2] - v2[2]) > .1)
1048 continue;
1049 //
1050 v1 = grid2->verts[l + offset2].xyz;
1051 v2 = grid2->verts[(l + 1) + offset2].xyz;
1052 if ( fabs(v1[0] - v2[0]) < .01 &&
1053 fabs(v1[1] - v2[1]) < .01 &&
1054 fabs(v1[2] - v2[2]) < .01)
1055 continue;
1056 //
1057 //ri.Printf( PRINT_ALL, "found highest LoD crack between two patches\n" );
1058 // insert column into grid2 right after after column l
1059 if (m) row = grid2->height-1;
1060 else row = 0;
1061 grid2 = R_GridInsertColumn( grid2, l+1, row,
1062 grid1->verts[grid1->width * (k - 1) + offset1].xyz, grid1->heightLodError[k+1]);
1063 grid2->lodStitched = qfalse;
1064 s_worldData.surfaces[grid2num].data = (void *) grid2;
1065 return qtrue;
1066 }
1067 }
1068 for (m = 0; m < 2; m++) {
1069
1070 if (!grid2 || grid2->height >= MAX_GRID_SIZE)
1071 break;
1072 if (m) offset2 = grid2->width-1;
1073 else offset2 = 0;
1074 for ( l = 0; l < grid2->height-1; l++) {
1075 //
1076 v1 = grid1->verts[grid1->width * k + offset1].xyz;
1077 v2 = grid2->verts[grid2->width * l + offset2].xyz;
1078 if ( fabs(v1[0] - v2[0]) > .1)
1079 continue;
1080 if ( fabs(v1[1] - v2[1]) > .1)
1081 continue;
1082 if ( fabs(v1[2] - v2[2]) > .1)
1083 continue;
1084
1085 v1 = grid1->verts[grid1->width * (k - 2) + offset1].xyz;
1086 v2 = grid2->verts[grid2->width * (l + 1) + offset2].xyz;
1087 if ( fabs(v1[0] - v2[0]) > .1)
1088 continue;
1089 if ( fabs(v1[1] - v2[1]) > .1)
1090 continue;
1091 if ( fabs(v1[2] - v2[2]) > .1)
1092 continue;
1093 //
1094 v1 = grid2->verts[grid2->width * l + offset2].xyz;
1095 v2 = grid2->verts[grid2->width * (l + 1) + offset2].xyz;
1096 if ( fabs(v1[0] - v2[0]) < .01 &&
1097 fabs(v1[1] - v2[1]) < .01 &&
1098 fabs(v1[2] - v2[2]) < .01)
1099 continue;
1100 //
1101 //ri.Printf( PRINT_ALL, "found highest LoD crack between two patches\n" );
1102 // insert row into grid2 right after after row l
1103 if (m) column = grid2->width-1;
1104 else column = 0;
1105 grid2 = R_GridInsertRow( grid2, l+1, column,
1106 grid1->verts[grid1->width * (k - 1) + offset1].xyz, grid1->heightLodError[k+1]);
1107 grid2->lodStitched = qfalse;
1108 s_worldData.surfaces[grid2num].data = (void *) grid2;
1109 return qtrue;
1110 }
1111 }
1112 }
1113 }
1114 return qfalse;
1115 }
1116
1117 /*
1118 ===============
1119 R_TryStitchPatch
1120
1121 This function will try to stitch patches in the same LoD group together for the highest LoD.
1122
1123 Only single missing vertice cracks will be fixed.
1124
1125 Vertices will be joined at the patch side a crack is first found, at the other side
1126 of the patch (on the same row or column) the vertices will not be joined and cracks
1127 might still appear at that side.
1128 ===============
1129 */
1130 int R_TryStitchingPatch( int grid1num ) {
1131 int j, numstitches;
1132 srfGridMesh_t *grid1, *grid2;
1133
1134 numstitches = 0;
1135 grid1 = (srfGridMesh_t *) s_worldData.surfaces[grid1num].data;
1136 for ( j = 0; j < s_worldData.numsurfaces; j++ ) {
1137 //
1138 grid2 = (srfGridMesh_t *) s_worldData.surfaces[j].data;
1139 // if this surface is not a grid
1140 if ( grid2->surfaceType != SF_GRID ) continue;
1141 // grids in the same LOD group should have the exact same lod radius
1142 if ( grid1->lodRadius != grid2->lodRadius ) continue;
1143 // grids in the same LOD group should have the exact same lod origin
1144 if ( grid1->lodOrigin[0] != grid2->lodOrigin[0] ) continue;
1145 if ( grid1->lodOrigin[1] != grid2->lodOrigin[1] ) continue;
1146 if ( grid1->lodOrigin[2] != grid2->lodOrigin[2] ) continue;
1147 //
1148 while (R_StitchPatches(grid1num, j))
1149 {
1150 numstitches++;
1151 }
1152 }
1153 return numstitches;
1154 }
1155
1156 /*
1157 ===============
1158 R_StitchAllPatches
1159 ===============
1160 */
1161 void R_StitchAllPatches( void ) {
1162 int i, stitched, numstitches;
1163 srfGridMesh_t *grid1;
1164
1165 numstitches = 0;
1166 do
1167 {
1168 stitched = qfalse;
1169 for ( i = 0; i < s_worldData.numsurfaces; i++ ) {
1170 //
1171 grid1 = (srfGridMesh_t *) s_worldData.surfaces[i].data;
1172 // if this surface is not a grid
1173 if ( grid1->surfaceType != SF_GRID )
1174 continue;
1175 //
1176 if ( grid1->lodStitched )
1177 continue;
1178 //
1179 grid1->lodStitched = qtrue;
1180 stitched = qtrue;
1181 //
1182 numstitches += R_TryStitchingPatch( i );
1183 }
1184 }
1185 while (stitched);
1186 ri.Printf( PRINT_ALL, "stitched %d LoD cracks\n", numstitches );
1187 }
1188
1189 /*
1190 ===============
1191 R_MovePatchSurfacesToHunk
1192 ===============
1193 */
1194 void R_MovePatchSurfacesToHunk(void) {
1195 int i, size;
1196 srfGridMesh_t *grid, *hunkgrid;
1197
1198 for ( i = 0; i < s_worldData.numsurfaces; i++ ) {
1199 //
1200 grid = (srfGridMesh_t *) s_worldData.surfaces[i].data;
1201 // if this surface is not a grid
1202 if ( grid->surfaceType != SF_GRID )
1203 continue;
1204 //
1205 size = (grid->width * grid->height - 1) * sizeof( drawVert_t ) + sizeof( *grid );
1206 hunkgrid = ri.Hunk_Alloc( size, h_low );
1207 Com_Memcpy(hunkgrid, grid, size);
1208
1209 hunkgrid->widthLodError = ri.Hunk_Alloc( grid->width * 4, h_low );
1210 Com_Memcpy( hunkgrid->widthLodError, grid->widthLodError, grid->width * 4 );
1211
1212 hunkgrid->heightLodError = ri.Hunk_Alloc( grid->height * 4, h_low );
1213 Com_Memcpy( hunkgrid->heightLodError, grid->heightLodError, grid->height * 4 );
1214
1215 R_FreeSurfaceGridMesh( grid );
1216
1217 s_worldData.surfaces[i].data = (void *) hunkgrid;
1218 }
1219 }
1220
1221 /*
1222 ===============
1223 R_LoadSurfaces
1224 ===============
1225 */
1226 static void R_LoadSurfaces( lump_t *surfs, lump_t *verts, lump_t *indexLump ) {
1227 dsurface_t *in;
1228 msurface_t *out;
1229 drawVert_t *dv;
1230 int *indexes;
1231 int count;
1232 int numFaces, numMeshes, numTriSurfs, numFlares;
1233 int i;
1234
1235 numFaces = 0;
1236 numMeshes = 0;
1237 numTriSurfs = 0;
1238 numFlares = 0;
1239
1240 in = (void *)(fileBase + surfs->fileofs);
1241 if (surfs->filelen % sizeof(*in))
1242 ri.Error (ERR_DROP, "LoadMap: funny lump size in %s",s_worldData.name);
1243 count = surfs->filelen / sizeof(*in);
1244
1245 dv = (void *)(fileBase + verts->fileofs);
1246 if (verts->filelen % sizeof(*dv))
1247 ri.Error (ERR_DROP, "LoadMap: funny lump size in %s",s_worldData.name);
1248
1249 indexes = (void *)(fileBase + indexLump->fileofs);
1250 if ( indexLump->filelen % sizeof(*indexes))
1251 ri.Error (ERR_DROP, "LoadMap: funny lump size in %s",s_worldData.name);
1252
1253 out = ri.Hunk_Alloc ( count * sizeof(*out), h_low );
1254
1255 s_worldData.surfaces = out;
1256 s_worldData.numsurfaces = count;
1257
1258 for ( i = 0 ; i < count ; i++, in++, out++ ) {
1259 switch ( LittleLong( in->surfaceType ) ) {
1260 case MST_PATCH:
1261 ParseMesh ( in, dv, out );
1262 numMeshes++;
1263 break;
1264 case MST_TRIANGLE_SOUP:
1265 ParseTriSurf( in, dv, out, indexes );
1266 numTriSurfs++;
1267 break;
1268 case MST_PLANAR:
1269 ParseFace( in, dv, out, indexes );
1270 numFaces++;
1271 break;
1272 case MST_FLARE:
1273 ParseFlare( in, dv, out, indexes );
1274 numFlares++;
1275 break;
1276 default:
1277 ri.Error( ERR_DROP, "Bad surfaceType" );
1278 }
1279 }
1280
1281 #ifdef PATCH_STITCHING
1282 R_StitchAllPatches();
1283 #endif
1284
1285 R_FixSharedVertexLodError();
1286
1287 #ifdef PATCH_STITCHING
1288 R_MovePatchSurfacesToHunk();
1289 #endif
1290
1291 ri.Printf( PRINT_ALL, "...loaded %d faces, %i meshes, %i trisurfs, %i flares\n",
1292 numFaces, numMeshes, numTriSurfs, numFlares );
1293 }
1294
1295
1296
1297 /*
1298 =================
1299 R_LoadSubmodels
1300 =================
1301 */
1302 static void R_LoadSubmodels( lump_t *l ) {
1303 dmodel_t *in;
1304 bmodel_t *out;
1305 int i, j, count;
1306
1307 in = (void *)(fileBase + l->fileofs);
1308 if (l->filelen % sizeof(*in))
1309 ri.Error (ERR_DROP, "LoadMap: funny lump size in %s",s_worldData.name);
1310 count = l->filelen / sizeof(*in);
1311
1312 s_worldData.bmodels = out = ri.Hunk_Alloc( count * sizeof(*out), h_low );
1313
1314 for ( i=0 ; i<count ; i++, in++, out++ ) {
1315 model_t *model;
1316
1317 model = R_AllocModel();
1318
1319 assert( model != NULL ); // this should never happen
1320 if ( model == NULL ) {
1321 ri.Error(ERR_DROP, "R_LoadSubmodels: R_AllocModel() failed");
1322 }
1323
1324 model->type = MOD_BRUSH;
1325 model->bmodel = out;
1326 Com_sprintf( model->name, sizeof( model->name ), "*%d", i );
1327
1328 for (j=0 ; j<3 ; j++) {
1329 out->bounds[0][j] = LittleFloat (in->mins[j]);
1330 out->bounds[1][j] = LittleFloat (in->maxs[j]);
1331 }
1332
1333 out->firstSurface = s_worldData.surfaces + LittleLong( in->firstSurface );
1334 out->numSurfaces = LittleLong( in->numSurfaces );
1335 }
1336 }
1337
1338
1339
1340 //==================================================================
1341
1342 /*
1343 =================
1344 R_SetParent
1345 =================
1346 */
1347 static void R_SetParent (mnode_t *node, mnode_t *parent)
1348 {
1349 node->parent = parent;
1350 if (node->contents != -1)
1351 return;
1352 R_SetParent (node->children[0], node);
1353 R_SetParent (node->children[1], node);
1354 }
1355
1356 /*
1357 =================
1358 R_LoadNodesAndLeafs
1359 =================
1360 */
1361 static void R_LoadNodesAndLeafs (lump_t *nodeLump, lump_t *leafLump) {
1362 int i, j, p;
1363 dnode_t *in;
1364 dleaf_t *inLeaf;
1365 mnode_t *out;
1366 int numNodes, numLeafs;
1367
1368 in = (void *)(fileBase + nodeLump->fileofs);
1369 if (nodeLump->filelen % sizeof(dnode_t) ||
1370 leafLump->filelen % sizeof(dleaf_t) ) {
1371 ri.Error (ERR_DROP, "LoadMap: funny lump size in %s",s_worldData.name);
1372 }
1373 numNodes = nodeLump->filelen / sizeof(dnode_t);
1374 numLeafs = leafLump->filelen / sizeof(dleaf_t);
1375
1376 out = ri.Hunk_Alloc ( (numNodes + numLeafs) * sizeof(*out), h_low);
1377
1378 s_worldData.nodes = out;
1379 s_worldData.numnodes = numNodes + numLeafs;
1380 s_worldData.numDecisionNodes = numNodes;
1381
1382 // load nodes
1383 for ( i=0 ; i<numNodes; i++, in++, out++)
1384 {
1385 for (j=0 ; j<3 ; j++)
1386 {
1387 out->mins[j] = LittleLong (in->mins[j]);
1388 out->maxs[j] = LittleLong (in->maxs[j]);
1389 }
1390
1391 p = LittleLong(in->planeNum);
1392 out->plane = s_worldData.planes + p;
1393
1394 out->contents = CONTENTS_NODE; // differentiate from leafs
1395
1396 for (j=0 ; j<2 ; j++)
1397 {
1398 p = LittleLong (in->children[j]);
1399 if (p >= 0)
1400 out->children[j] = s_worldData.nodes + p;
1401 else
1402 out->children[j] = s_worldData.nodes + numNodes + (-1 - p);
1403 }
1404 }
1405
1406 // load leafs
1407 inLeaf = (void *)(fileBase + leafLump->fileofs);
1408 for ( i=0 ; i<numLeafs ; i++, inLeaf++, out++)
1409 {
1410 for (j=0 ; j<3 ; j++)
1411 {
1412 out->mins[j] = LittleLong (inLeaf->mins[j]);
1413 out->maxs[j] = LittleLong (inLeaf->maxs[j]);
1414 }
1415
1416 out->cluster = LittleLong(inLeaf->cluster);
1417 out->area = LittleLong(inLeaf->area);
1418
1419 if ( out->cluster >= s_worldData.numClusters ) {
1420 s_worldData.numClusters = out->cluster + 1;
1421 }
1422
1423 out->firstmarksurface = s_worldData.marksurfaces +
1424 LittleLong(inLeaf->firstLeafSurface);
1425 out->nummarksurfaces = LittleLong(inLeaf->numLeafSurfaces);
1426 }
1427
1428 // chain descendants
1429 R_SetParent (s_worldData.nodes, NULL);
1430 }
1431
1432 //=============================================================================
1433
1434 /*
1435 =================
1436 R_LoadShaders
1437 =================
1438 */
1439 static void R_LoadShaders( lump_t *l ) {
1440 int i, count;
1441 dshader_t *in, *out;
1442
1443 in = (void *)(fileBase + l->fileofs);
1444 if (l->filelen % sizeof(*in))
1445 ri.Error (ERR_DROP, "LoadMap: funny lump size in %s",s_worldData.name);
1446 count = l->filelen / sizeof(*in);
1447 out = ri.Hunk_Alloc ( count*sizeof(*out), h_low );
1448
1449 s_worldData.shaders = out;
1450 s_worldData.numShaders = count;
1451
1452 Com_Memcpy( out, in, count*sizeof(*out) );
1453
1454 for ( i=0 ; i<count ; i++ ) {
1455 out[i].surfaceFlags = LittleLong( out[i].surfaceFlags );
1456 out[i].contentFlags = LittleLong( out[i].contentFlags );
1457 }
1458 }
1459
1460
1461 /*
1462 =================
1463 R_LoadMarksurfaces
1464 =================
1465 */
1466 static void R_LoadMarksurfaces (lump_t *l)
1467 {
1468 int i, j, count;
1469 int *in;
1470 msurface_t **out;
1471
1472 in = (void *)(fileBase + l->fileofs);
1473 if (l->filelen % sizeof(*in))
1474 ri.Error (ERR_DROP, "LoadMap: funny lump size in %s",s_worldData.name);
1475 count = l->filelen / sizeof(*in);
1476 out = ri.Hunk_Alloc ( count*sizeof(*out), h_low);
1477
1478 s_worldData.marksurfaces = out;
1479 s_worldData.nummarksurfaces = count;
1480
1481 for ( i=0 ; i<count ; i++)
1482 {
1483 j = LittleLong(in[i]);
1484 out[i] = s_worldData.surfaces + j;
1485 }
1486 }
1487
1488
1489 /*
1490 =================
1491 R_LoadPlanes
1492 =================
1493 */
1494 static void R_LoadPlanes( lump_t *l ) {
1495 int i, j;
1496 cplane_t *out;
1497 dplane_t *in;
1498 int count;
1499 int bits;
1500
1501 in = (void *)(fileBase + l->fileofs);
1502 if (l->filelen % sizeof(*in))
1503 ri.Error (ERR_DROP, "LoadMap: funny lump size in %s",s_worldData.name);
1504 count = l->filelen / sizeof(*in);
1505 out = ri.Hunk_Alloc ( count*2*sizeof(*out), h_low);
1506
1507 s_worldData.planes = out;
1508 s_worldData.numplanes = count;
1509
1510 for ( i=0 ; i<count ; i++, in++, out++) {
1511 bits = 0;
1512 for (j=0 ; j<3 ; j++) {
1513 out->normal[j] = LittleFloat (in->normal[j]);
1514 if (out->normal[j] < 0) {
1515 bits |= 1<<j;
1516 }
1517 }
1518
1519 out->dist = LittleFloat (in->dist);
1520 out->type = PlaneTypeForNormal( out->normal );
1521 out->signbits = bits;
1522 }
1523 }
1524
1525 /*
1526 =================
1527 R_LoadFogs
1528
1529 =================
1530 */
1531 static void R_LoadFogs( lump_t *l, lump_t *brushesLump, lump_t *sidesLump ) {
1532 int i;
1533 fog_t *out;
1534 dfog_t *fogs;
1535 dbrush_t *brushes, *brush;
1536 dbrushside_t *sides;
1537 int count, brushesCount, sidesCount;
1538 int sideNum;
1539 int planeNum;
1540 shader_t *shader;
1541 float d;
1542 int firstSide;
1543
1544 fogs = (void *)(fileBase + l->fileofs);
1545 if (l->filelen % sizeof(*fogs)) {
1546 ri.Error (ERR_DROP, "LoadMap: funny lump size in %s",s_worldData.name);
1547 }
1548 count = l->filelen / sizeof(*fogs);
1549
1550 // create fog structures for them
1551 s_worldData.numfogs = count + 1;
1552 s_worldData.fogs = ri.Hunk_Alloc ( s_worldData.numfogs*sizeof(*out), h_low);
1553 out = s_worldData.fogs + 1;
1554
1555 if ( !count ) {
1556 return;
1557 }
1558
1559 brushes = (void *)(fileBase + brushesLump->fileofs);
1560 if (brushesLump->filelen % sizeof(*brushes)) {
1561 ri.Error (ERR_DROP, "LoadMap: funny lump size in %s",s_worldData.name);
1562 }
1563 brushesCount = brushesLump->filelen / sizeof(*brushes);
1564
1565 sides = (void *)(fileBase + sidesLump->fileofs);
1566 if (sidesLump->filelen % sizeof(*sides)) {
1567 ri.Error (ERR_DROP, "LoadMap: funny lump size in %s",s_worldData.name);
1568 }
1569 sidesCount = sidesLump->filelen / sizeof(*sides);
1570
1571 for ( i=0 ; i<count ; i++, fogs++) {
1572 out->originalBrushNumber = LittleLong( fogs->brushNum );
1573
1574 if ( (unsigned)out->originalBrushNumber >= brushesCount ) {
1575 ri.Error( ERR_DROP, "fog brushNumber out of range" );
1576 }
1577 brush = brushes + out->originalBrushNumber;
1578
1579 firstSide = LittleLong( brush->firstSide );
1580
1581 if ( (unsigned)firstSide > sidesCount - 6 ) {
1582 ri.Error( ERR_DROP, "fog brush sideNumber out of range" );
1583 }
1584
1585 // brushes are always sorted with the axial sides first
1586 sideNum = firstSide + 0;
1587 planeNum = LittleLong( sides[ sideNum ].planeNum );
1588 out->bounds[0][0] = -s_worldData.planes[ planeNum ].dist;
1589
1590 sideNum = firstSide + 1;
1591 planeNum = LittleLong( sides[ sideNum ].planeNum );
1592 out->bounds[1][0] = s_worldData.planes[ planeNum ].dist;
1593
1594 sideNum = firstSide + 2;
1595 planeNum = LittleLong( sides[ sideNum ].planeNum );
1596 out->bounds[0][1] = -s_worldData.planes[ planeNum ].dist;
1597
1598 sideNum = firstSide + 3;
1599 planeNum = LittleLong( sides[ sideNum ].planeNum );
1600 out->bounds[1][1] = s_worldData.planes[ planeNum ].dist;
1601
1602 sideNum = firstSide + 4;
1603 planeNum = LittleLong( sides[ sideNum ].planeNum );
1604 out->bounds[0][2] = -s_worldData.planes[ planeNum ].dist;
1605
1606 sideNum = firstSide + 5;
1607 planeNum = LittleLong( sides[ sideNum ].planeNum );
1608 out->bounds[1][2] = s_worldData.planes[ planeNum ].dist;
1609
1610 // get information from the shader for fog parameters
1611 shader = R_FindShader( fogs->shader, LIGHTMAP_NONE, qtrue );
1612
1613 out->parms = shader->fogParms;
1614
1615 out->colorInt = ColorBytes4 ( shader->fogParms.color[0] * tr.identityLight,
1616 shader->fogParms.color[1] * tr.identityLight,
1617 shader->fogParms.color[2] * tr.identityLight, 1.0 );
1618
1619 d = shader->fogParms.depthForOpaque < 1 ? 1 : shader->fogParms.depthForOpaque;
1620 out->tcScale = 1.0f / ( d * 8 );
1621
1622 // set the gradient vector
1623 sideNum = LittleLong( fogs->visibleSide );
1624
1625 if ( sideNum == -1 ) {
1626 out->hasSurface = qfalse;
1627 } else {
1628 out->hasSurface = qtrue;
1629 planeNum = LittleLong( sides[ firstSide + sideNum ].planeNum );
1630 VectorSubtract( vec3_origin, s_worldData.planes[ planeNum ].normal, out->surface );
1631 out->surface[3] = -s_worldData.planes[ planeNum ].dist;
1632 }
1633
1634 out++;
1635 }
1636
1637 }
1638
1639
1640 /*
1641 ================
1642 R_LoadLightGrid
1643
1644 ================
1645 */
1646 void R_LoadLightGrid( lump_t *l ) {
1647 int i;
1648 vec3_t maxs;
1649 int numGridPoints;
1650 world_t *w;
1651 float *wMins, *wMaxs;
1652
1653 w = &s_worldData;
1654
1655 w->lightGridInverseSize[0] = 1.0f / w->lightGridSize[0];
1656 w->lightGridInverseSize[1] = 1.0f / w->lightGridSize[1];
1657 w->lightGridInverseSize[2] = 1.0f / w->lightGridSize[2];
1658
1659 wMins = w->bmodels[0].bounds[0];
1660 wMaxs = w->bmodels[0].bounds[1];
1661
1662 for ( i = 0 ; i < 3 ; i++ ) {
1663 w->lightGridOrigin[i] = w->lightGridSize[i] * ceil( wMins[i] / w->lightGridSize[i] );
1664 maxs[i] = w->lightGridSize[i] * floor( wMaxs[i] / w->lightGridSize[i] );
1665 w->lightGridBounds[i] = (maxs[i] - w->lightGridOrigin[i])/w->lightGridSize[i] + 1;
1666 }
1667
1668 numGridPoints = w->lightGridBounds[0] * w->lightGridBounds[1] * w->lightGridBounds[2];
1669
1670 if ( l->filelen != numGridPoints * 8 ) {
1671 ri.Printf( PRINT_WARNING, "WARNING: light grid mismatch\n" );
1672 w->lightGridData = NULL;
1673 return;
1674 }
1675
1676 w->lightGridData = ri.Hunk_Alloc( l->filelen, h_low );
1677 Com_Memcpy( w->lightGridData, (void *)(fileBase + l->fileofs), l->filelen );
1678
1679 // deal with overbright bits
1680 for ( i = 0 ; i < numGridPoints ; i++ ) {
1681 R_ColorShiftLightingBytes( &w->lightGridData[i*8], &w->lightGridData[i*8] );
1682 R_ColorShiftLightingBytes( &w->lightGridData[i*8+3], &w->lightGridData[i*8+3] );
1683 }
1684 }
1685
1686 /*
1687 ================
1688 R_LoadEntities
1689 ================
1690 */
1691 void R_LoadEntities( lump_t *l ) {
1692 char *p, *token, *s;
1693 char keyname[MAX_TOKEN_CHARS];
1694 char value[MAX_TOKEN_CHARS];
1695 world_t *w;
1696
1697 w = &s_worldData;
1698 w->lightGridSize[0] = 64;
1699 w->lightGridSize[1] = 64;
1700 w->lightGridSize[2] = 128;
1701
1702 p = (char *)(fileBase + l->fileofs);
1703
1704 // store for reference by the cgame
1705 w->entityString = ri.Hunk_Alloc( l->filelen + 1, h_low );
1706 strcpy( w->entityString, p );
1707 w->entityParsePoint = w->entityString;
1708
1709 token = COM_ParseExt( &p, qtrue );
1710 if (!*token || *token != '{') {
1711 return;
1712 }
1713
1714 // only parse the world spawn
1715 while ( 1 ) {
1716 // parse key
1717 token = COM_ParseExt( &p, qtrue );
1718
1719 if ( !*token || *token == '}' ) {
1720 break;
1721 }
1722 Q_strncpyz(keyname, token, sizeof(keyname));
1723
1724 // parse value
1725 token = COM_ParseExt( &p, qtrue );
1726
1727 if ( !*token || *token == '}' ) {
1728 break;
1729 }
1730 Q_strncpyz(value, token, sizeof(value));
1731
1732 // check for remapping of shaders for vertex lighting
1733 s = "vertexremapshader";
1734 if (!Q_strncmp(keyname, s, strlen(s)) ) {
1735 s = strchr(value, ';');
1736 if (!s) {
1737 ri.Printf( PRINT_WARNING, "WARNING: no semi colon in vertexshaderremap '%s'\n", value );
1738 break;
1739 }
1740 *s++ = 0;
1741 if (r_vertexLight->integer) {
1742 R_RemapShader(value, s, "0");
1743 }
1744 continue;
1745 }
1746 // check for remapping of shaders
1747 s = "remapshader";
1748 if (!Q_strncmp(keyname, s, strlen(s)) ) {
1749 s = strchr(value, ';');
1750 if (!s) {
1751 ri.Printf( PRINT_WARNING, "WARNING: no semi colon in shaderremap '%s'\n", value );
1752 break;
1753 }
1754 *s++ = 0;
1755 R_RemapShader(value, s, "0");
1756 continue;
1757 }
1758 // check for a different grid size
1759 if (!Q_stricmp(keyname, "gridsize")) {
1760 sscanf(value, "%f %f %f", &w->lightGridSize[0], &w->lightGridSize[1], &w->lightGridSize[2] );
1761 continue;
1762 }
1763 }
1764 }
1765
1766 /*
1767 =================
1768 R_GetEntityToken
1769 =================
1770 */
1771 qboolean R_GetEntityToken( char *buffer, int size ) {
1772 const char *s;
1773
1774 s = COM_Parse( &s_worldData.entityParsePoint );
1775 Q_strncpyz( buffer, s, size );
1776 if ( !s_worldData.entityParsePoint && !s[0] ) {
1777 s_worldData.entityParsePoint = s_worldData.entityString;
1778 return qfalse;
1779 } else {
1780 return qtrue;
1781 }
1782 }
1783
1784 /*
1785 =================
1786 RE_LoadWorldMap
1787
1788 Called directly from cgame
1789 =================
1790 */
1791 void RE_LoadWorldMap( const char *name ) {
1792 int i;
1793 dheader_t *header;
1794 union {
1795 byte *b;
1796 void *v;
1797 } buffer;
1798 byte *startMarker;
1799
1800 if ( tr.worldMapLoaded ) {
1801 ri.Error( ERR_DROP, "ERROR: attempted to redundantly load world map" );
1802 }
1803
1804 // set default sun direction to be used if it isn't
1805 // overridden by a shader
1806 tr.sunDirection[0] = 0.45f;
1807 tr.sunDirection[1] = 0.3f;
1808 tr.sunDirection[2] = 0.9f;
1809
1810 VectorNormalize( tr.sunDirection );
1811
1812 tr.worldMapLoaded = qtrue;
1813
1814 // load it
1815 ri.FS_ReadFile( name, &buffer.v );
1816 if ( !buffer.b ) {
1817 ri.Error (ERR_DROP, "RE_LoadWorldMap: %s not found", name);
1818 }
1819
1820 // clear tr.world so if the level fails to load, the next
1821 // try will not look at the partially loaded version
1822 tr.world = NULL;
1823
1824 Com_Memset( &s_worldData, 0, sizeof( s_worldData ) );
1825 Q_strncpyz( s_worldData.name, name, sizeof( s_worldData.name ) );
1826
1827 Q_strncpyz( s_worldData.baseName, COM_SkipPath( s_worldData.name ), sizeof( s_worldData.name ) );
1828 COM_StripExtension(s_worldData.baseName, s_worldData.baseName, sizeof(s_worldData.baseName));
1829
1830 startMarker = ri.Hunk_Alloc(0, h_low);
1831 c_gridVerts = 0;
1832
1833 header = (dheader_t *)buffer.b;
1834 fileBase = (byte *)header;
1835
1836 i = LittleLong (header->version);
1837 if ( i != BSP_VERSION ) {
1838 ri.Error (ERR_DROP, "RE_LoadWorldMap: %s has wrong version number (%i should be %i)",
1839 name, i, BSP_VERSION);
1840 }
1841
1842 // swap all the lumps
1843 for (i=0 ; i<sizeof(dheader_t)/4 ; i++) {
1844 ((int *)header)[i] = LittleLong ( ((int *)header)[i]);
1845 }
1846
1847 // load into heap
1848 R_LoadShaders( &header->lumps[LUMP_SHADERS] );
1849 R_LoadLightmaps( &header->lumps[LUMP_LIGHTMAPS] );
1850 R_LoadPlanes (&header->lumps[LUMP_PLANES]);
1851 R_LoadFogs( &header->lumps[LUMP_FOGS], &header->lumps[LUMP_BRUSHES], &header->lumps[LUMP_BRUSHSIDES] );
1852 R_LoadSurfaces( &header->lumps[LUMP_SURFACES], &header->lumps[LUMP_DRAWVERTS], &header->lumps[LUMP_DRAWINDEXES] );
1853 R_LoadMarksurfaces (&header->lumps[LUMP_LEAFSURFACES]);
1854 R_LoadNodesAndLeafs (&header->lumps[LUMP_NODES], &header->lumps[LUMP_LEAFS]);
1855 R_LoadSubmodels (&header->lumps[LUMP_MODELS]);
1856 R_LoadVisibility( &header->lumps[LUMP_VISIBILITY] );
1857 R_LoadEntities( &header->lumps[LUMP_ENTITIES] );
1858 R_LoadLightGrid( &header->lumps[LUMP_LIGHTGRID] );
1859
1860 s_worldData.dataSize = (byte *)ri.Hunk_Alloc(0, h_low) - startMarker;
1861
1862 // only set tr.world now that we know the entire level has loaded properly
1863 tr.world = &s_worldData;
1864
1865 ri.FS_FreeFile( buffer.v );
1866 }
1867