Debian Codegraph @

code/renderergl2/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 srfBspSurface_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 srfBspSurface_t *R_SubdividePatchToGrid( int width, int height,
36 srfVert_t points[MAX_PATCH_SIZE*MAX_PATCH_SIZE] ) {
37
38 */
39
40
41 /*
42 ============
43 LerpDrawVert
44 ============
45 */
46 static void LerpDrawVert( srfVert_t *a, srfVert_t *b, srfVert_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] = ((int)a->color[0] + (int)b->color[0]) >> 1;
58 out->color[1] = ((int)a->color[1] + (int)b->color[1]) >> 1;
59 out->color[2] = ((int)a->color[2] + (int)b->color[2]) >> 1;
60 out->color[3] = ((int)a->color[3] + (int)b->color[3]) >> 1;
61 }
62
63 /*
64 ============
65 Transpose
66 ============
67 */
68 static void Transpose( int width, int height, srfVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE] ) {
69 int i, j;
70 srfVert_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, srfVert_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 srfVert_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 {
208 vec3_t fNormal;
209 VectorNormalize2(sum, fNormal);
210 R_VaoPackNormal(dv->normal, fNormal);
211 }
212 }
213 }
214 }
215
216 static void MakeMeshTangentVectors(int width, int height, srfVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE], int numIndexes,
217 glIndex_t indexes[(MAX_GRID_SIZE-1)*(MAX_GRID_SIZE-1)*2*3])
218 {
219 int i, j;
220 srfVert_t *dv[3];
221 static srfVert_t ctrl2[MAX_GRID_SIZE * MAX_GRID_SIZE];
222 glIndex_t *tri;
223
224 // FIXME: use more elegant way
225 for(i = 0; i < width; i++)
226 {
227 for(j = 0; j < height; j++)
228 {
229 dv[0] = &ctrl2[j * width + i];
230 *dv[0] = ctrl[j][i];
231 }
232 }
233
234 for(i = 0, tri = indexes; i < numIndexes; i += 3, tri += 3)
235 {
236 dv[0] = &ctrl2[tri[0]];
237 dv[1] = &ctrl2[tri[1]];
238 dv[2] = &ctrl2[tri[2]];
239
240 R_CalcTangentVectors(dv);
241 }
242
243 for(i = 0; i < width; i++)
244 {
245 for(j = 0; j < height; j++)
246 {
247 dv[0] = &ctrl2[j * width + i];
248 dv[1] = &ctrl[j][i];
249
250 VectorCopy4(dv[0]->tangent, dv[1]->tangent);
251 }
252 }
253 }
254
255
256 static int MakeMeshIndexes(int width, int height, glIndex_t indexes[(MAX_GRID_SIZE-1)*(MAX_GRID_SIZE-1)*2*3])
257 {
258 int i, j;
259 int numIndexes;
260 int w, h;
261
262 h = height - 1;
263 w = width - 1;
264 numIndexes = 0;
265 for(i = 0; i < h; i++)
266 {
267 for(j = 0; j < w; j++)
268 {
269 int v1, v2, v3, v4;
270
271 // vertex order to be reckognized as tristrips
272 v1 = i * width + j + 1;
273 v2 = v1 - 1;
274 v3 = v2 + width;
275 v4 = v3 + 1;
276
277 indexes[numIndexes++] = v2;
278 indexes[numIndexes++] = v3;
279 indexes[numIndexes++] = v1;
280
281 indexes[numIndexes++] = v1;
282 indexes[numIndexes++] = v3;
283 indexes[numIndexes++] = v4;
284 }
285 }
286
287 return numIndexes;
288 }
289
290
291 /*
292 ============
293 InvertCtrl
294 ============
295 */
296 static void InvertCtrl( int width, int height, srfVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE] ) {
297 int i, j;
298 srfVert_t temp;
299
300 for ( i = 0 ; i < height ; i++ ) {
301 for ( j = 0 ; j < width/2 ; j++ ) {
302 temp = ctrl[i][j];
303 ctrl[i][j] = ctrl[i][width-1-j];
304 ctrl[i][width-1-j] = temp;
305 }
306 }
307 }
308
309
310 /*
311 =================
312 InvertErrorTable
313 =================
314 */
315 static void InvertErrorTable( float errorTable[2][MAX_GRID_SIZE], int width, int height ) {
316 int i;
317 float copy[2][MAX_GRID_SIZE];
318
319 Com_Memcpy( copy, errorTable, sizeof( copy ) );
320
321 for ( i = 0 ; i < width ; i++ ) {
322 errorTable[1][i] = copy[0][i]; //[width-1-i];
323 }
324
325 for ( i = 0 ; i < height ; i++ ) {
326 errorTable[0][i] = copy[1][height-1-i];
327 }
328
329 }
330
331 /*
332 ==================
333 PutPointsOnCurve
334 ==================
335 */
336 static void PutPointsOnCurve( srfVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE],
337 int width, int height ) {
338 int i, j;
339 srfVert_t prev, next;
340
341 for ( i = 0 ; i < width ; i++ ) {
342 for ( j = 1 ; j < height ; j += 2 ) {
343 LerpDrawVert( &ctrl[j][i], &ctrl[j+1][i], &prev );
344 LerpDrawVert( &ctrl[j][i], &ctrl[j-1][i], &next );
345 LerpDrawVert( &prev, &next, &ctrl[j][i] );
346 }
347 }
348
349
350 for ( j = 0 ; j < height ; j++ ) {
351 for ( i = 1 ; i < width ; i += 2 ) {
352 LerpDrawVert( &ctrl[j][i], &ctrl[j][i+1], &prev );
353 LerpDrawVert( &ctrl[j][i], &ctrl[j][i-1], &next );
354 LerpDrawVert( &prev, &next, &ctrl[j][i] );
355 }
356 }
357 }
358
359 /*
360 =================
361 R_CreateSurfaceGridMesh
362 =================
363 */
364 void R_CreateSurfaceGridMesh(srfBspSurface_t *grid, int width, int height,
365 srfVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE], float errorTable[2][MAX_GRID_SIZE],
366 int numIndexes, glIndex_t indexes[(MAX_GRID_SIZE-1)*(MAX_GRID_SIZE-1)*2*3]) {
367 int i, j;
368 srfVert_t *vert;
369 vec3_t tmpVec;
370
371 // copy the results out to a grid
372 Com_Memset(grid, 0, sizeof(*grid));
373
374 #ifdef PATCH_STITCHING
375 grid->widthLodError = /*ri.Hunk_Alloc*/ ri.Malloc( width * 4 );
376 Com_Memcpy( grid->widthLodError, errorTable[0], width * 4 );
377
378 grid->heightLodError = /*ri.Hunk_Alloc*/ ri.Malloc( height * 4 );
379 Com_Memcpy( grid->heightLodError, errorTable[1], height * 4 );
380
381 grid->numIndexes = numIndexes;
382 grid->indexes = ri.Malloc(grid->numIndexes * sizeof(glIndex_t));
383 Com_Memcpy(grid->indexes, indexes, numIndexes * sizeof(glIndex_t));
384
385 grid->numVerts = (width * height);
386 grid->verts = ri.Malloc(grid->numVerts * sizeof(srfVert_t));
387 #else
388 grid->widthLodError = ri.Hunk_Alloc( width * 4, h_low );
389 Com_Memcpy( grid->widthLodError, errorTable[0], width * 4 );
390
391 grid->heightLodError = ri.Hunk_Alloc( height * 4, h_low );
392 Com_Memcpy( grid->heightLodError, errorTable[1], height * 4 );
393
394 grid->numIndexes = numIndexes;
395 grid->indexes = ri.Hunk_Alloc(grid->numIndexes * sizeof(glIndex_t), h_low);
396 Com_Memcpy(grid->indexes, indexes, numIndexes * sizeof(glIndex_t));
397
398 grid->numVerts = (width * height);
399 grid->verts = ri.Hunk_Alloc(grid->numVerts * sizeof(srfVert_t), h_low);
400 #endif
401
402 grid->width = width;
403 grid->height = height;
404 grid->surfaceType = SF_GRID;
405 ClearBounds( grid->cullBounds[0], grid->cullBounds[1] );
406 for ( i = 0 ; i < width ; i++ ) {
407 for ( j = 0 ; j < height ; j++ ) {
408 vert = &grid->verts[j*width+i];
409 *vert = ctrl[j][i];
410 AddPointToBounds( vert->xyz, grid->cullBounds[0], grid->cullBounds[1] );
411 }
412 }
413
414 // compute local origin and bounds
415 VectorAdd( grid->cullBounds[0], grid->cullBounds[1], grid->cullOrigin );
416 VectorScale( grid->cullOrigin, 0.5f, grid->cullOrigin );
417 VectorSubtract( grid->cullBounds[0], grid->cullOrigin, tmpVec );
418 grid->cullRadius = VectorLength( tmpVec );
419
420 VectorCopy( grid->cullOrigin, grid->lodOrigin );
421 grid->lodRadius = grid->cullRadius;
422 //
423 }
424
425 /*
426 =================
427 R_FreeSurfaceGridMesh
428 =================
429 */
430 static void R_FreeSurfaceGridMeshData( srfBspSurface_t *grid ) {
431 ri.Free(grid->widthLodError);
432 ri.Free(grid->heightLodError);
433 ri.Free(grid->indexes);
434 ri.Free(grid->verts);
435 }
436
437 /*
438 =================
439 R_SubdividePatchToGrid
440 =================
441 */
442 void R_SubdividePatchToGrid( srfBspSurface_t *grid, int width, int height,
443 srfVert_t points[MAX_PATCH_SIZE*MAX_PATCH_SIZE] ) {
444 int i, j, k, l;
445 srfVert_t_cleared( prev );
446 srfVert_t_cleared( next );
447 srfVert_t_cleared( mid );
448 float len, maxLen;
449 int dir;
450 int t;
451 srfVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE];
452 float errorTable[2][MAX_GRID_SIZE];
453 int numIndexes;
454 static glIndex_t indexes[(MAX_GRID_SIZE-1)*(MAX_GRID_SIZE-1)*2*3];
455 int consecutiveComplete;
456
457 for ( i = 0 ; i < width ; i++ ) {
458 for ( j = 0 ; j < height ; j++ ) {
459 ctrl[j][i] = points[j*width+i];
460 }
461 }
462
463 for ( dir = 0 ; dir < 2 ; dir++ ) {
464
465 for ( j = 0 ; j < MAX_GRID_SIZE ; j++ ) {
466 errorTable[dir][j] = 0;
467 }
468
469 consecutiveComplete = 0;
470
471 // horizontal subdivisions
472 for ( j = 0 ; ; j = (j + 2) % (width - 1) ) {
473 // check subdivided midpoints against control points
474
475 // FIXME: also check midpoints of adjacent patches against the control points
476 // this would basically stitch all patches in the same LOD group together.
477
478 maxLen = 0;
479 for ( i = 0 ; i < height ; i++ ) {
480 vec3_t midxyz;
481 vec3_t midxyz2;
482 vec3_t dirVector;
483 vec3_t projected;
484 float d;
485
486 // calculate the point on the curve
487 for ( l = 0 ; l < 3 ; l++ ) {
488 midxyz[l] = (ctrl[i][j].xyz[l] + ctrl[i][j+1].xyz[l] * 2
489 + ctrl[i][j+2].xyz[l] ) * 0.25f;
490 }
491
492 // see how far off the line it is
493 // using dist-from-line will not account for internal
494 // texture warping, but it gives a lot less polygons than
495 // dist-from-midpoint
496 VectorSubtract( midxyz, ctrl[i][j].xyz, midxyz );
497 VectorSubtract( ctrl[i][j+2].xyz, ctrl[i][j].xyz, dirVector );
498 VectorNormalize( dirVector );
499
500 d = DotProduct( midxyz, dirVector );
501 VectorScale( dirVector, d, projected );
502 VectorSubtract( midxyz, projected, midxyz2);
503 len = VectorLengthSquared( midxyz2 ); // we will do the sqrt later
504 if ( len > maxLen ) {
505 maxLen = len;
506 }
507 }
508
509 maxLen = sqrt(maxLen);
510
511 // if all the points are on the lines, remove the entire columns
512 if ( maxLen < 0.1f ) {
513 errorTable[dir][j+1] = 999;
514 // if we go over the whole grid twice without adding any columns, stop
515 if (++consecutiveComplete >= width)
516 break;
517 continue;
518 }
519
520 // see if we want to insert subdivided columns
521 if ( width + 2 > MAX_GRID_SIZE ) {
522 errorTable[dir][j+1] = 1.0f/maxLen;
523 break; // can't subdivide any more
524 }
525
526 if ( maxLen <= r_subdivisions->value ) {
527 errorTable[dir][j+1] = 1.0f/maxLen;
528 // if we go over the whole grid twice without adding any columns, stop
529 if (++consecutiveComplete >= width)
530 break;
531 continue; // didn't need subdivision
532 }
533
534 errorTable[dir][j+2] = 1.0f/maxLen;
535
536 consecutiveComplete = 0;
537
538 // insert two columns and replace the peak
539 width += 2;
540 for ( i = 0 ; i < height ; i++ ) {
541 LerpDrawVert( &ctrl[i][j], &ctrl[i][j+1], &prev );
542 LerpDrawVert( &ctrl[i][j+1], &ctrl[i][j+2], &next );
543 LerpDrawVert( &prev, &next, &mid );
544
545 for ( k = width - 1 ; k > j + 3 ; k-- ) {
546 ctrl[i][k] = ctrl[i][k-2];
547 }
548 ctrl[i][j + 1] = prev;
549 ctrl[i][j + 2] = mid;
550 ctrl[i][j + 3] = next;
551 }
552
553 // skip the new one, we'll get it on the next pass
554 j += 2;
555 }
556
557 Transpose( width, height, ctrl );
558 t = width;
559 width = height;
560 height = t;
561 }
562
563
564 // put all the aproximating points on the curve
565 PutPointsOnCurve( ctrl, width, height );
566
567 // cull out any rows or columns that are colinear
568 for ( i = 1 ; i < width-1 ; i++ ) {
569 if ( errorTable[0][i] != 999 ) {
570 continue;
571 }
572 for ( j = i+1 ; j < width ; j++ ) {
573 for ( k = 0 ; k < height ; k++ ) {
574 ctrl[k][j-1] = ctrl[k][j];
575 }
576 errorTable[0][j-1] = errorTable[0][j];
577 }
578 width--;
579 }
580
581 for ( i = 1 ; i < height-1 ; i++ ) {
582 if ( errorTable[1][i] != 999 ) {
583 continue;
584 }
585 for ( j = i+1 ; j < height ; j++ ) {
586 for ( k = 0 ; k < width ; k++ ) {
587 ctrl[j-1][k] = ctrl[j][k];
588 }
589 errorTable[1][j-1] = errorTable[1][j];
590 }
591 height--;
592 }
593
594 #if 1
595 // flip for longest tristrips as an optimization
596 // the results should be visually identical with or
597 // without this step
598 if ( height > width ) {
599 Transpose( width, height, ctrl );
600 InvertErrorTable( errorTable, width, height );
601 t = width;
602 width = height;
603 height = t;
604 InvertCtrl( width, height, ctrl );
605 }
606 #endif
607
608 // calculate indexes
609 numIndexes = MakeMeshIndexes(width, height, indexes);
610
611 // calculate normals
612 MakeMeshNormals( width, height, ctrl );
613 MakeMeshTangentVectors(width, height, ctrl, numIndexes, indexes);
614
615 R_CreateSurfaceGridMesh(grid, width, height, ctrl, errorTable, numIndexes, indexes);
616 }
617
618 /*
619 ===============
620 R_GridInsertColumn
621 ===============
622 */
623 void R_GridInsertColumn( srfBspSurface_t *grid, int column, int row, vec3_t point, float loderror ) {
624 int i, j;
625 int width, height, oldwidth;
626 srfVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE];
627 float errorTable[2][MAX_GRID_SIZE];
628 float lodRadius;
629 vec3_t lodOrigin;
630 int numIndexes;
631 static glIndex_t indexes[(MAX_GRID_SIZE-1)*(MAX_GRID_SIZE-1)*2*3];
632
633 oldwidth = 0;
634 width = grid->width + 1;
635 if (width > MAX_GRID_SIZE)
636 return;
637 height = grid->height;
638 for (i = 0; i < width; i++) {
639 if (i == column) {
640 //insert new column
641 for (j = 0; j < grid->height; j++) {
642 LerpDrawVert( &grid->verts[j * grid->width + i-1], &grid->verts[j * grid->width + i], &ctrl[j][i] );
643 if (j == row)
644 VectorCopy(point, ctrl[j][i].xyz);
645 }
646 errorTable[0][i] = loderror;
647 continue;
648 }
649 errorTable[0][i] = grid->widthLodError[oldwidth];
650 for (j = 0; j < grid->height; j++) {
651 ctrl[j][i] = grid->verts[j * grid->width + oldwidth];
652 }
653 oldwidth++;
654 }
655 for (j = 0; j < grid->height; j++) {
656 errorTable[1][j] = grid->heightLodError[j];
657 }
658 // put all the aproximating points on the curve
659 //PutPointsOnCurve( ctrl, width, height );
660
661 // calculate indexes
662 numIndexes = MakeMeshIndexes(width, height, indexes);
663
664 // calculate normals
665 MakeMeshNormals( width, height, ctrl );
666 MakeMeshTangentVectors(width, height, ctrl, numIndexes, indexes);
667
668 VectorCopy(grid->lodOrigin, lodOrigin);
669 lodRadius = grid->lodRadius;
670 // free the old grid
671 R_FreeSurfaceGridMeshData(grid);
672 // create a new grid
673 R_CreateSurfaceGridMesh(grid, width, height, ctrl, errorTable, numIndexes, indexes);
674 grid->lodRadius = lodRadius;
675 VectorCopy(lodOrigin, grid->lodOrigin);
676 }
677
678 /*
679 ===============
680 R_GridInsertRow
681 ===============
682 */
683 void R_GridInsertRow( srfBspSurface_t *grid, int row, int column, vec3_t point, float loderror ) {
684 int i, j;
685 int width, height, oldheight;
686 srfVert_t ctrl[MAX_GRID_SIZE][MAX_GRID_SIZE];
687 float errorTable[2][MAX_GRID_SIZE];
688 float lodRadius;
689 vec3_t lodOrigin;
690 int numIndexes;
691 static glIndex_t indexes[(MAX_GRID_SIZE-1)*(MAX_GRID_SIZE-1)*2*3];
692
693 oldheight = 0;
694 width = grid->width;
695 height = grid->height + 1;
696 if (height > MAX_GRID_SIZE)
697 return;
698 for (i = 0; i < height; i++) {
699 if (i == row) {
700 //insert new row
701 for (j = 0; j < grid->width; j++) {
702 LerpDrawVert( &grid->verts[(i-1) * grid->width + j], &grid->verts[i * grid->width + j], &ctrl[i][j] );
703 if (j == column)
704 VectorCopy(point, ctrl[i][j].xyz);
705 }
706 errorTable[1][i] = loderror;
707 continue;
708 }
709 errorTable[1][i] = grid->heightLodError[oldheight];
710 for (j = 0; j < grid->width; j++) {
711 ctrl[i][j] = grid->verts[oldheight * grid->width + j];
712 }
713 oldheight++;
714 }
715 for (j = 0; j < grid->width; j++) {
716 errorTable[0][j] = grid->widthLodError[j];
717 }
718 // put all the aproximating points on the curve
719 //PutPointsOnCurve( ctrl, width, height );
720
721 // calculate indexes
722 numIndexes = MakeMeshIndexes(width, height, indexes);
723
724 // calculate normals
725 MakeMeshNormals( width, height, ctrl );
726 MakeMeshTangentVectors(width, height, ctrl, numIndexes, indexes);
727
728 VectorCopy(grid->lodOrigin, lodOrigin);
729 lodRadius = grid->lodRadius;
730 // free the old grid
731 R_FreeSurfaceGridMeshData(grid);
732 // create a new grid
733 R_CreateSurfaceGridMesh(grid, width, height, ctrl, errorTable, numIndexes, indexes);
734 grid->lodRadius = lodRadius;
735 VectorCopy(lodOrigin, grid->lodOrigin);
736 }