Debian Codegraph @

code/renderergl1/tr_curve.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
23 #include "tr_local.h"
24
25 /*
26
27 This file does all of the processing necessary to turn a raw grid of points
28 read from the map file into a srfGridMesh_t ready for rendering.
29
30 The level of detail solution is direction independent, based only on subdivided
31 distance from the true curve.
32
33 Only a single entry point:
34
35 srfGridMesh_t *R_SubdividePatchToGrid( int width, int height,
36 drawVert_t points[MAX_PATCH_SIZE*MAX_PATCH_SIZE] ) {
37
38 */
39
40
41 /*
42 ============
43 LerpDrawVert
44 ============
45 */
46 static void LerpDrawVert( drawVert_t *a, drawVert_t *b, drawVert_t *out ) {
47 out->xyz[0] = 0.5f * (a->xyz[0] + b->xyz[0]);
48 out->xyz[1] = 0.5f * (a->xyz[1] + b->xyz[1]);
49 out->xyz[2] = 0.5f * (a->xyz[2] + b->xyz[2]);
50
51 out->st[0] = 0.5f * (a->st[0] + b->st[0]);
52 out->st[1] = 0.5f * (a->st[1] + b->st[1]);
53
54 out->lightmap[0] = 0.5f * (a->lightmap[0] + b->lightmap[0]);
55 out->lightmap[1] = 0.5f * (a->lightmap[1] + b->lightmap[1]);
56
57 out->color[0] = (a->color[0] + b->color[0]) >> 1;
58 out->color[1] = (a->color[1] + b->color[1]) >> 1;
59 out->color[2] = (a->color[2] + b->color[2]) >> 1;
60 out->color[3] = (a->color[3] + b->color[3]) >> 1;
61 }
62
63 /*
64 ============
65 Transpose
66 ============
67 */
68 static void Transpose( int width, int height, drawVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE] ) {
69 int i, j;
70 drawVert_t temp;
71
72 if ( width > height ) {
73 for ( i = 0 ; i < height ; i++ ) {
74 for ( j = i + 1 ; j < width ; j++ ) {
75 if ( j < height ) {
76 // swap the value
77 temp = ctrl[j][i];
78 ctrl[j][i] = ctrl[i][j];
79 ctrl[i][j] = temp;
80 } else {
81 // just copy
82 ctrl[j][i] = ctrl[i][j];
83 }
84 }
85 }
86 } else {
87 for ( i = 0 ; i < width ; i++ ) {
88 for ( j = i + 1 ; j < height ; j++ ) {
89 if ( j < width ) {
90 // swap the value
91 temp = ctrl[i][j];
92 ctrl[i][j] = ctrl[j][i];
93 ctrl[j][i] = temp;
94 } else {
95 // just copy
96 ctrl[i][j] = ctrl[j][i];
97 }
98 }
99 }
100 }
101
102 }
103
104
105 /*
106 =================
107 MakeMeshNormals
108
109 Handles all the complicated wrapping and degenerate cases
110 =================
111 */
112 static void MakeMeshNormals( int width, int height, drawVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE] ) {
113 int i, j, k, dist;
114 vec3_t normal;
115 vec3_t sum;
116 vec3_t base;
117 vec3_t delta;
118 int x, y;
119 drawVert_t *dv;
120 vec3_t around[8], temp;
121 qboolean good[8];
122 qboolean wrapWidth, wrapHeight;
123 float len;
124 static int neighbors[8][2] = {
125 {0,1}, {1,1}, {1,0}, {1,-1}, {0,-1}, {-1,-1}, {-1,0}, {-1,1}
126 };
127
128 wrapWidth = qfalse;
129 for ( i = 0 ; i < height ; i++ ) {
130 VectorSubtract( ctrl[i][0].xyz, ctrl[i][width-1].xyz, delta );
131 len = VectorLengthSquared( delta );
132 if ( len > 1.0 ) {
133 break;
134 }
135 }
136 if ( i == height ) {
137 wrapWidth = qtrue;
138 }
139
140 wrapHeight = qfalse;
141 for ( i = 0 ; i < width ; i++ ) {
142 VectorSubtract( ctrl[0][i].xyz, ctrl[height-1][i].xyz, delta );
143 len = VectorLengthSquared( delta );
144 if ( len > 1.0 ) {
145 break;
146 }
147 }
148 if ( i == width) {
149 wrapHeight = qtrue;
150 }
151
152
153 for ( i = 0 ; i < width ; i++ ) {
154 for ( j = 0 ; j < height ; j++ ) {
155 dv = &ctrl[j][i];
156 VectorCopy( dv->xyz, base );
157 for ( k = 0 ; k < 8 ; k++ ) {
158 VectorClear( around[k] );
159 good[k] = qfalse;
160
161 for ( dist = 1 ; dist <= 3 ; dist++ ) {
162 x = i + neighbors[k][0] * dist;
163 y = j + neighbors[k][1] * dist;
164 if ( wrapWidth ) {
165 if ( x < 0 ) {
166 x = width - 1 + x;
167 } else if ( x >= width ) {
168 x = 1 + x - width;
169 }
170 }
171 if ( wrapHeight ) {
172 if ( y < 0 ) {
173 y = height - 1 + y;
174 } else if ( y >= height ) {
175 y = 1 + y - height;
176 }
177 }
178
179 if ( x < 0 || x >= width || y < 0 || y >= height ) {
180 break; // edge of patch
181 }
182 VectorSubtract( ctrl[y][x].xyz, base, temp );
183 if ( VectorNormalize( temp ) < 0.001f ) {
184 continue; // degenerate edge, get more dist
185 } else {
186 good[k] = qtrue;
187 VectorCopy( temp, around[k] );
188 break; // good edge
189 }
190 }
191 }
192
193 VectorClear( sum );
194 for ( k = 0 ; k < 8 ; k++ ) {
195 if ( !good[k] || !good[(k+1)&7] ) {
196 continue; // didn't get two points
197 }
198 CrossProduct( around[(k+1)&7], around[k], normal );
199 if ( VectorNormalize( normal ) < 0.001f ) {
200 continue;
201 }
202 VectorAdd( normal, sum, sum );
203 }
204 //if ( count == 0 ) {
205 // printf("bad normal\n");
206 //}
207 VectorNormalize2( sum, dv->normal );
208 }
209 }
210 }
211
212
213 /*
214 ============
215 InvertCtrl
216 ============
217 */
218 static void InvertCtrl( int width, int height, drawVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE] ) {
219 int i, j;
220 drawVert_t temp;
221
222 for ( i = 0 ; i < height ; i++ ) {
223 for ( j = 0 ; j < width/2 ; j++ ) {
224 temp = ctrl[i][j];
225 ctrl[i][j] = ctrl[i][width-1-j];
226 ctrl[i][width-1-j] = temp;
227 }
228 }
229 }
230
231
232 /*
233 =================
234 InvertErrorTable
235 =================
236 */
237 static void InvertErrorTable( float errorTable[2][MAX_GRID_SIZE], int width, int height ) {
238 int i;
239 float copy[2][MAX_GRID_SIZE];
240
241 Com_Memcpy( copy, errorTable, sizeof( copy ) );
242
243 for ( i = 0 ; i < width ; i++ ) {
244 errorTable[1][i] = copy[0][i]; //[width-1-i];
245 }
246
247 for ( i = 0 ; i < height ; i++ ) {
248 errorTable[0][i] = copy[1][height-1-i];
249 }
250
251 }
252
253 /*
254 ==================
255 PutPointsOnCurve
256 ==================
257 */
258 static void PutPointsOnCurve( drawVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE],
259 int width, int height ) {
260 int i, j;
261 drawVert_t prev, next;
262
263 for ( i = 0 ; i < width ; i++ ) {
264 for ( j = 1 ; j < height ; j += 2 ) {
265 LerpDrawVert( &ctrl[j][i], &ctrl[j+1][i], &prev );
266 LerpDrawVert( &ctrl[j][i], &ctrl[j-1][i], &next );
267 LerpDrawVert( &prev, &next, &ctrl[j][i] );
268 }
269 }
270
271
272 for ( j = 0 ; j < height ; j++ ) {
273 for ( i = 1 ; i < width ; i += 2 ) {
274 LerpDrawVert( &ctrl[j][i], &ctrl[j][i+1], &prev );
275 LerpDrawVert( &ctrl[j][i], &ctrl[j][i-1], &next );
276 LerpDrawVert( &prev, &next, &ctrl[j][i] );
277 }
278 }
279 }
280
281 /*
282 =================
283 R_CreateSurfaceGridMesh
284 =================
285 */
286 srfGridMesh_t *R_CreateSurfaceGridMesh(int width, int height,
287 drawVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE], float errorTable[2][MAX_GRID_SIZE] ) {
288 int i, j, size;
289 drawVert_t *vert;
290 vec3_t tmpVec;
291 srfGridMesh_t *grid;
292
293 // copy the results out to a grid
294 size = (width * height - 1) * sizeof( drawVert_t ) + sizeof( *grid );
295
296 #ifdef PATCH_STITCHING
297 grid = /*ri.Hunk_Alloc*/ ri.Malloc( size );
298 Com_Memset(grid, 0, size);
299
300 grid->widthLodError = /*ri.Hunk_Alloc*/ ri.Malloc( width * 4 );
301 Com_Memcpy( grid->widthLodError, errorTable[0], width * 4 );
302
303 grid->heightLodError = /*ri.Hunk_Alloc*/ ri.Malloc( height * 4 );
304 Com_Memcpy( grid->heightLodError, errorTable[1], height * 4 );
305 #else
306 grid = ri.Hunk_Alloc( size, h_low );
307 Com_Memset(grid, 0, size);
308
309 grid->widthLodError = ri.Hunk_Alloc( width * 4, h_low );
310 Com_Memcpy( grid->widthLodError, errorTable[0], width * 4 );
311
312 grid->heightLodError = ri.Hunk_Alloc( height * 4, h_low );
313 Com_Memcpy( grid->heightLodError, errorTable[1], height * 4 );
314 #endif
315
316 grid->width = width;
317 grid->height = height;
318 grid->surfaceType = SF_GRID;
319 ClearBounds( grid->meshBounds[0], grid->meshBounds[1] );
320 for ( i = 0 ; i < width ; i++ ) {
321 for ( j = 0 ; j < height ; j++ ) {
322 vert = &grid->verts[j*width+i];
323 *vert = ctrl[j][i];
324 AddPointToBounds( vert->xyz, grid->meshBounds[0], grid->meshBounds[1] );
325 }
326 }
327
328 // compute local origin and bounds
329 VectorAdd( grid->meshBounds[0], grid->meshBounds[1], grid->localOrigin );
330 VectorScale( grid->localOrigin, 0.5f, grid->localOrigin );
331 VectorSubtract( grid->meshBounds[0], grid->localOrigin, tmpVec );
332 grid->meshRadius = VectorLength( tmpVec );
333
334 VectorCopy( grid->localOrigin, grid->lodOrigin );
335 grid->lodRadius = grid->meshRadius;
336 //
337 return grid;
338 }
339
340 /*
341 =================
342 R_FreeSurfaceGridMesh
343 =================
344 */
345 void R_FreeSurfaceGridMesh( srfGridMesh_t *grid ) {
346 ri.Free(grid->widthLodError);
347 ri.Free(grid->heightLodError);
348 ri.Free(grid);
349 }
350
351 /*
352 =================
353 R_SubdividePatchToGrid
354 =================
355 */
356 srfGridMesh_t *R_SubdividePatchToGrid( int width, int height,
357 drawVert_t points[MAX_PATCH_SIZE*MAX_PATCH_SIZE] ) {
358 int i, j, k, l;
359 drawVert_t_cleared( prev );
360 drawVert_t_cleared( next );
361 drawVert_t_cleared( mid );
362 float len, maxLen;
363 int dir;
364 int t;
365 drawVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE];
366 float errorTable[2][MAX_GRID_SIZE];
367
368 for ( i = 0 ; i < width ; i++ ) {
369 for ( j = 0 ; j < height ; j++ ) {
370 ctrl[j][i] = points[j*width+i];
371 }
372 }
373
374 for ( dir = 0 ; dir < 2 ; dir++ ) {
375
376 for ( j = 0 ; j < MAX_GRID_SIZE ; j++ ) {
377 errorTable[dir][j] = 0;
378 }
379
380 // horizontal subdivisions
381 for ( j = 0 ; j + 2 < width ; j += 2 ) {
382 // check subdivided midpoints against control points
383
384 // FIXME: also check midpoints of adjacent patches against the control points
385 // this would basically stitch all patches in the same LOD group together.
386
387 maxLen = 0;
388 for ( i = 0 ; i < height ; i++ ) {
389 vec3_t midxyz;
390 vec3_t midxyz2;
391 vec3_t dirVector;
392 vec3_t projected;
393 float d;
394
395 // calculate the point on the curve
396 for ( l = 0 ; l < 3 ; l++ ) {
397 midxyz[l] = (ctrl[i][j].xyz[l] + ctrl[i][j+1].xyz[l] * 2
398 + ctrl[i][j+2].xyz[l] ) * 0.25f;
399 }
400
401 // see how far off the line it is
402 // using dist-from-line will not account for internal
403 // texture warping, but it gives a lot less polygons than
404 // dist-from-midpoint
405 VectorSubtract( midxyz, ctrl[i][j].xyz, midxyz );
406 VectorSubtract( ctrl[i][j+2].xyz, ctrl[i][j].xyz, dirVector );
407 VectorNormalize( dirVector );
408
409 d = DotProduct( midxyz, dirVector );
410 VectorScale( dirVector, d, projected );
411 VectorSubtract( midxyz, projected, midxyz2);
412 len = VectorLengthSquared( midxyz2 ); // we will do the sqrt later
413 if ( len > maxLen ) {
414 maxLen = len;
415 }
416 }
417
418 maxLen = sqrt(maxLen);
419
420 // if all the points are on the lines, remove the entire columns
421 if ( maxLen < 0.1f ) {
422 errorTable[dir][j+1] = 999;
423 continue;
424 }
425
426 // see if we want to insert subdivided columns
427 if ( width + 2 > MAX_GRID_SIZE ) {
428 errorTable[dir][j+1] = 1.0f/maxLen;
429 continue; // can't subdivide any more
430 }
431
432 if ( maxLen <= r_subdivisions->value ) {
433 errorTable[dir][j+1] = 1.0f/maxLen;
434 continue; // didn't need subdivision
435 }
436
437 errorTable[dir][j+2] = 1.0f/maxLen;
438
439 // insert two columns and replace the peak
440 width += 2;
441 for ( i = 0 ; i < height ; i++ ) {
442 LerpDrawVert( &ctrl[i][j], &ctrl[i][j+1], &prev );
443 LerpDrawVert( &ctrl[i][j+1], &ctrl[i][j+2], &next );
444 LerpDrawVert( &prev, &next, &mid );
445
446 for ( k = width - 1 ; k > j + 3 ; k-- ) {
447 ctrl[i][k] = ctrl[i][k-2];
448 }
449 ctrl[i][j + 1] = prev;
450 ctrl[i][j + 2] = mid;
451 ctrl[i][j + 3] = next;
452 }
453
454 // back up and recheck this set again, it may need more subdivision
455 j -= 2;
456
457 }
458
459 Transpose( width, height, ctrl );
460 t = width;
461 width = height;
462 height = t;
463 }
464
465
466 // put all the aproximating points on the curve
467 PutPointsOnCurve( ctrl, width, height );
468
469 // cull out any rows or columns that are colinear
470 for ( i = 1 ; i < width-1 ; i++ ) {
471 if ( errorTable[0][i] != 999 ) {
472 continue;
473 }
474 for ( j = i+1 ; j < width ; j++ ) {
475 for ( k = 0 ; k < height ; k++ ) {
476 ctrl[k][j-1] = ctrl[k][j];
477 }
478 errorTable[0][j-1] = errorTable[0][j];
479 }
480 width--;
481 }
482
483 for ( i = 1 ; i < height-1 ; i++ ) {
484 if ( errorTable[1][i] != 999 ) {
485 continue;
486 }
487 for ( j = i+1 ; j < height ; j++ ) {
488 for ( k = 0 ; k < width ; k++ ) {
489 ctrl[j-1][k] = ctrl[j][k];
490 }
491 errorTable[1][j-1] = errorTable[1][j];
492 }
493 height--;
494 }
495
496 #if 1
497 // flip for longest tristrips as an optimization
498 // the results should be visually identical with or
499 // without this step
500 if ( height > width ) {
501 Transpose( width, height, ctrl );
502 InvertErrorTable( errorTable, width, height );
503 t = width;
504 width = height;
505 height = t;
506 InvertCtrl( width, height, ctrl );
507 }
508 #endif
509
510 // calculate normals
511 MakeMeshNormals( width, height, ctrl );
512
513 return R_CreateSurfaceGridMesh( width, height, ctrl, errorTable );
514 }
515
516 /*
517 ===============
518 R_GridInsertColumn
519 ===============
520 */
521 srfGridMesh_t *R_GridInsertColumn( srfGridMesh_t *grid, int column, int row, vec3_t point, float loderror ) {
522 int i, j;
523 int width, height, oldwidth;
524 drawVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE];
525 float errorTable[2][MAX_GRID_SIZE];
526 float lodRadius;
527 vec3_t lodOrigin;
528
529 oldwidth = 0;
530 width = grid->width + 1;
531 if (width > MAX_GRID_SIZE)
532 return NULL;
533 height = grid->height;
534 for (i = 0; i < width; i++) {
535 if (i == column) {
536 //insert new column
537 for (j = 0; j < grid->height; j++) {
538 LerpDrawVert( &grid->verts[j * grid->width + i-1], &grid->verts[j * grid->width + i], &ctrl[j][i] );
539 if (j == row)
540 VectorCopy(point, ctrl[j][i].xyz);
541 }
542 errorTable[0][i] = loderror;
543 continue;
544 }
545 errorTable[0][i] = grid->widthLodError[oldwidth];
546 for (j = 0; j < grid->height; j++) {
547 ctrl[j][i] = grid->verts[j * grid->width + oldwidth];
548 }
549 oldwidth++;
550 }
551 for (j = 0; j < grid->height; j++) {
552 errorTable[1][j] = grid->heightLodError[j];
553 }
554 // put all the aproximating points on the curve
555 //PutPointsOnCurve( ctrl, width, height );
556 // calculate normals
557 MakeMeshNormals( width, height, ctrl );
558
559 VectorCopy(grid->lodOrigin, lodOrigin);
560 lodRadius = grid->lodRadius;
561 // free the old grid
562 R_FreeSurfaceGridMesh(grid);
563 // create a new grid
564 grid = R_CreateSurfaceGridMesh( width, height, ctrl, errorTable );
565 grid->lodRadius = lodRadius;
566 VectorCopy(lodOrigin, grid->lodOrigin);
567 return grid;
568 }
569
570 /*
571 ===============
572 R_GridInsertRow
573 ===============
574 */
575 srfGridMesh_t *R_GridInsertRow( srfGridMesh_t *grid, int row, int column, vec3_t point, float loderror ) {
576 int i, j;
577 int width, height, oldheight;
578 drawVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE];
579 float errorTable[2][MAX_GRID_SIZE];
580 float lodRadius;
581 vec3_t lodOrigin;
582
583 oldheight = 0;
584 width = grid->width;
585 height = grid->height + 1;
586 if (height > MAX_GRID_SIZE)
587 return NULL;
588 for (i = 0; i < height; i++) {
589 if (i == row) {
590 //insert new row
591 for (j = 0; j < grid->width; j++) {
592 LerpDrawVert( &grid->verts[(i-1) * grid->width + j], &grid->verts[i * grid->width + j], &ctrl[i][j] );
593 if (j == column)
594 VectorCopy(point, ctrl[i][j].xyz);
595 }
596 errorTable[1][i] = loderror;
597 continue;
598 }
599 errorTable[1][i] = grid->heightLodError[oldheight];
600 for (j = 0; j < grid->width; j++) {
601 ctrl[i][j] = grid->verts[oldheight * grid->width + j];
602 }
603 oldheight++;
604 }
605 for (j = 0; j < grid->width; j++) {
606 errorTable[0][j] = grid->widthLodError[j];
607 }
608 // put all the aproximating points on the curve
609 //PutPointsOnCurve( ctrl, width, height );
610 // calculate normals
611 MakeMeshNormals( width, height, ctrl );
612
613 VectorCopy(grid->lodOrigin, lodOrigin);
614 lodRadius = grid->lodRadius;
615 // free the old grid
616 R_FreeSurfaceGridMesh(grid);
617 // create a new grid
618 grid = R_CreateSurfaceGridMesh( width, height, ctrl, errorTable );
619 grid->lodRadius = lodRadius;
620 VectorCopy(lodOrigin, grid->lodOrigin);
621 return grid;
622 }