Debian Codegraph @

Descent3/terrain.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #ifdef NEWEDITOR
20 #include "neweditor\globals.h"
21 #else
22 #include <stdlib.h>
23 #endif
24 #include "vecmat.h"
25 #include "object.h"
26 #include "mono.h"
27 #include "terrain.h"
28 #include "pserror.h"
29 #include "bitmap.h"
30 #include "gametexture.h"
31 #include "lighting.h"
32 #include "lightmap.h"
33 #include "weather.h"
34 #include <string.h>
35 #include "mem.h"
36 #include "dedicated_server.h"
37 #include "psrand.h"
38 #ifdef EDITOR
39 #include "editor\d3edit.h"
40 #endif
41
42 #include <algorithm>
43
44 #define SKY_RADIUS 2500.0
45 #define DEFAULT_LIGHT_SOURCE \
46 { 0, TERRAIN_SIZE * 100, 0 }
47
48 // How far we can see (in world coordinates)
49 float VisibleTerrainZ;
50
51 terrain_segment Terrain_seg[TERRAIN_WIDTH * TERRAIN_DEPTH];
52 terrain_tex_segment Terrain_tex_seg[TERRAIN_TEX_WIDTH * TERRAIN_TEX_DEPTH];
53 terrain_sky Terrain_sky;
54
55 #if (!defined(RELEASE) || defined(NEWEDITOR))
56 // first object to render after cell has been rendered (only used for SW renderer)
57 int16_t Terrain_seg_render_objs[TERRAIN_WIDTH * TERRAIN_DEPTH];
58 #endif
59
60 // Our lighting maps for the terrain, one for each quadrant (starting at lower left)
61 int TerrainLightmaps[4];
62
63 // A list of terrain to render
64 terrain_render_info Terrain_list[MAX_CELLS_TO_RENDER];
65
66 uint16_t *Terrain_rotate_list; // which points have been sub/rotated this frame
67 g3Point *World_point_buffer; // Rotated points
68
69 // The min/max values for a particular region of terrain
70 uint8_t *Terrain_min_height_int[7];
71 uint8_t *Terrain_max_height_int[7];
72 // Texture values for a particular region
73
74 // Terrain dynamic lighting table
75 uint8_t Terrain_dynamic_table[TERRAIN_WIDTH * TERRAIN_DEPTH];
76
77 // Terrain normals depending on LOD
78 terrain_normals *TerrainNormals[MAX_TERRAIN_LOD];
79
80 // Max deltas per terrain lod block
81 float *TerrainDeltaBlocks[MAX_TERRAIN_LOD];
82
83 // Tracks edges of LOD
84 uint8_t TerrainJoinMap[TERRAIN_WIDTH * TERRAIN_DEPTH];
85
86 // Terrain Y values
87 float Terrain_y_values[256];
88
89 #if (defined(EDITOR) || defined(NEWEDITOR))
90 uint8_t TerrainSelected[TERRAIN_WIDTH * TERRAIN_DEPTH];
91 int Num_terrain_selected = 0;
92 int Editor_LOD_engine_off = 1;
93 bool Terrain_render_ext_room_objs = true;
94 #endif
95
96 int TSearch_on = 0, TSearch_found_type, TSearch_x, TSearch_y, TSearch_seg, TSearch_face;
97
98 int Terrain_LOD_engine_off = 0;
99
100 int TerrainEdgeTest[MAX_TERRAIN_LOD][16];
101 uint8_t TerrainEdgeJump[MAX_TERRAIN_LOD];
102
103 // Unique terrain geometry identifier
104 int Terrain_checksum = -1;
105
106 // Occlusion data for knowing what to draw
107 uint8_t Terrain_occlusion_map[256][32];
108 int Terrain_occlusion_checksum = -2;
109
110 // returns the index of the highest float
111 int GetHighestDelta(float *deltas, int count) {
112 int high_index = -999;
113 float high_delta = -99999;
114 int i;
115
116 for (i = 0; i < count; i++) {
117 if ((deltas[i]) > (high_delta)) {
118 high_index = i;
119 high_delta = deltas[i];
120 }
121 }
122
123 ASSERT(high_index < count);
124
125 return high_index;
126 }
127
128 float RecurseLODDeltas(int x1, int y1, int x2, int y2, int lod) {
129 float deltas[6];
130 float maxdelta;
131 float v0, v1, v2, v3;
132 int edgex = x2, edgey = y2;
133 int i, t;
134
135 if (x1 % 2)
136 Int3();
137 if (x2 % 2)
138 Int3();
139 if (y1 % 2)
140 Int3();
141 if (y2 % 2)
142 Int3();
143
144 int midx = ((x2 - x1) / 2) + x1;
145 int midy = ((y2 - y1) / 2) + y1;
146
147 if (x2 == TERRAIN_WIDTH)
148 edgex = TERRAIN_WIDTH - 1;
149 if (y2 == TERRAIN_DEPTH)
150 edgey = TERRAIN_DEPTH - 1;
151
152 // starts from lower left, proceeds clockwise
153 v0 = Terrain_seg[y1 * TERRAIN_WIDTH + x1].y;
154 v1 = Terrain_seg[edgey * TERRAIN_WIDTH + x1].y;
155 v2 = Terrain_seg[edgey * TERRAIN_WIDTH + edgex].y;
156 v3 = Terrain_seg[y1 * TERRAIN_WIDTH + edgex].y;
157
158 deltas[0] = fabs(Terrain_seg[(midy)*TERRAIN_WIDTH + midx].y - (((v2 - v0) / 2) + v0));
159 deltas[1] = fabs(Terrain_seg[(midy)*TERRAIN_WIDTH + midx].y - (((v3 - v1) / 2) + v1));
160
161 // left edge
162 deltas[2] = fabs(Terrain_seg[(midy)*TERRAIN_WIDTH + x1].y - (((v1 - v0) / 2) + v0));
163
164 // top edge
165 deltas[3] = fabs(Terrain_seg[(y2)*TERRAIN_WIDTH + midx].y - (((v2 - v1) / 2) + v1));
166
167 // right edge
168 deltas[4] = fabs(Terrain_seg[(midy)*TERRAIN_WIDTH + x2].y - (((v3 - v2) / 2) + v2));
169
170 // bottom edge
171 deltas[5] = fabs(Terrain_seg[(y1)*TERRAIN_WIDTH + midx].y - (((v3 - v0) / 2) + v0));
172
173 maxdelta = deltas[GetHighestDelta(deltas, 6)];
174
175 if (lod != MAX_TERRAIN_LOD - 2) {
176 deltas[0] = RecurseLODDeltas(x1, midy, midx, y2, lod + 1);
177 deltas[1] = RecurseLODDeltas(midx, midy, x2, y2, lod + 1);
178 deltas[2] = RecurseLODDeltas(midx, y1, x2, midy, lod + 1);
179 deltas[3] = RecurseLODDeltas(x1, y1, midx, midy, lod + 1);
180
181 if (deltas[0] == SHUTOFF_LOD_INVISIBLE && deltas[1] == SHUTOFF_LOD_INVISIBLE &&
182 deltas[2] == SHUTOFF_LOD_INVISIBLE && deltas[3] == SHUTOFF_LOD_INVISIBLE) {
183 maxdelta = SHUTOFF_LOD_INVISIBLE;
184 } else {
185 for (i = 0; i < 4; i++) {
186 if (deltas[i] == SHUTOFF_LOD_INVISIBLE)
187 deltas[i] = SHUTOFF_LOD_DELTA;
188 }
189
190 float maxdelta2 = deltas[GetHighestDelta(deltas, 4)];
191 if (maxdelta2 > maxdelta)
192 maxdelta = maxdelta2;
193 }
194 }
195
196 // Now check if there is anything special about this level of detail that
197 // excludes it from being used in the engine
198 if (lod == MAX_TERRAIN_LOD - 2) {
199 int total_counted = 0, total_invis = 0;
200
201 for (i = y1; i < y2; i++) {
202 for (t = x1; t < x2; t++, total_counted++) {
203 if ((Terrain_seg[i * TERRAIN_WIDTH + t].flags & TF_INVISIBLE)) {
204 maxdelta = SHUTOFF_LOD_DELTA;
205 total_invis++;
206 }
207 }
208 }
209
210 // See if we can completely turn off this LOD
211 if (total_invis == total_counted)
212 maxdelta = SHUTOFF_LOD_INVISIBLE;
213 }
214
215 return (maxdelta);
216 }
217
218 // returns highest slopechange
219 float GetGreatestSlopeChange(float *slopes, int count) {
220 float high_delta = -90000.0f;
221 int i, t;
222
223 for (i = 0; i < count; i++) {
224 for (t = 0; t < count; t++) {
225 if ((fabs(slopes[t] - slopes[i])) > high_delta) {
226 high_delta = fabs(slopes[t] - slopes[i]);
227 }
228 }
229 }
230
231 return high_delta;
232 }
233
234 void GenerateSingleLODDelta(int sx, int sz) {
235 int w, h, i, z, x;
236 int simplemul, rowsize;
237 int chunk_size = 1 << (MAX_TERRAIN_LOD - 1);
238
239 sx *= chunk_size;
240 sz *= chunk_size;
241
242 int save_x = sx;
243 int save_z = sz;
244
245 // Starts from lower-left, going clockwise
246 // 0 is lowest_level_detail (blunt)
247 for (i = 0; i < MAX_TERRAIN_LOD - 1; i++) {
248 w = chunk_size >> ((MAX_TERRAIN_LOD - 1) - i);
249 h = chunk_size >> ((MAX_TERRAIN_LOD - 1) - i);
250
251 simplemul = 1 << ((MAX_TERRAIN_LOD - 1) - i);
252 rowsize = TERRAIN_WIDTH / simplemul;
253
254 sx = save_x;
255 sz = save_z;
256
257 sx /= simplemul;
258 sz /= simplemul;
259
260 for (z = sz; z < sz + h; z++) {
261 for (x = sx; x < sx + w; x++) {
262 float delta;
263
264 delta =
265 RecurseLODDeltas(x * simplemul, (z * simplemul), (x * simplemul) + simplemul, z * simplemul + simplemul, i);
266 TerrainDeltaBlocks[i][z * rowsize + x] = delta;
267 }
268 }
269 }
270 }
271
272 void GenerateLODDeltas() {
273 int w, h, i, z, x;
274 int simplemul, rowsize;
275 // Starts from lower-left, going clockwise
276 for (i = 0; i < MAX_TERRAIN_LOD - 1; i++) {
277 w = TERRAIN_WIDTH >> ((MAX_TERRAIN_LOD - 1) - i);
278 h = TERRAIN_DEPTH >> ((MAX_TERRAIN_LOD - 1) - i);
279
280 simplemul = 1 << ((MAX_TERRAIN_LOD - 1) - i);
281 rowsize = TERRAIN_WIDTH / simplemul;
282
283 for (z = 0; z < h; z++) {
284 for (x = 0; x < w; x++) {
285 float delta;
286
287 delta =
288 RecurseLODDeltas(x * simplemul, (z * simplemul), (x * simplemul) + simplemul, z * simplemul + simplemul, i);
289 TerrainDeltaBlocks[i][z * rowsize + x] = delta;
290 }
291 }
292 }
293 }
294
295 // Returns a unique checksum for the current terrain geometry
296 int GetTerrainGeometryChecksum() {
297 int total = 0;
298
299 for (int i = 0; i < TERRAIN_WIDTH * TERRAIN_DEPTH; i++) {
300 total += (Terrain_seg[i].ypos + i);
301 }
302
303 return total;
304 }
305
306 // Builds the min max quadtree data for terrain VSD
307 void BuildMinMaxTerrain() {
308 int i, w, h, start, x, y, cell;
309 int row_width, xoffset, yoffset, total_rows;
310 int minheight, maxheight, cellheight;
311
312 mprintf(0, "Building min/max terrain table.\n");
313
314 // Calculate our integer y positions (0-255)
315 for (i = 0; i < TERRAIN_WIDTH * TERRAIN_DEPTH; i++) {
316 float fl;
317
318 #if (!defined(RELEASE) || defined(NEWEDITOR))
319 Terrain_seg_render_objs[i] = -1;
320 #endif
321
322 fl = Terrain_seg[i].ypos * TERRAIN_HEIGHT_INCREMENT;
323
324 Terrain_seg[i].y = fl;
325 }
326
327 int check = GetTerrainGeometryChecksum();
328
329 if (check == Terrain_checksum)
330 return;
331
332 Terrain_checksum = check;
333
334 // Generate level of detail deltas
335 if (!Dedicated_server)
336 GenerateLODDeltas();
337
338 for (i = 0; i < 7; i++) {
339 row_width = 1 << i;
340 total_rows = 1 << i;
341 int yspeed_offset = 0;
342
343 for (yoffset = 0; yoffset < total_rows; yoffset++, yspeed_offset += row_width) {
344 for (xoffset = 0; xoffset < row_width; xoffset++) {
345 w = TERRAIN_WIDTH >> i;
346 h = TERRAIN_WIDTH >> i;
347
348 start = ((yoffset * (TERRAIN_WIDTH >> i)) * TERRAIN_WIDTH);
349 start += (xoffset * (TERRAIN_WIDTH >> i));
350
351 minheight = 999;
352 maxheight = 0;
353
354 if (h < TERRAIN_DEPTH)
355 h++;
356 if (w < TERRAIN_DEPTH)
357 w++;
358
359 int terrain_offset = 0;
360 for (y = 0; y < h; y++, terrain_offset += TERRAIN_WIDTH) {
361 for (x = 0; x < w; x++) {
362 cell = start + (terrain_offset) + x;
363 cellheight = Terrain_seg[cell].ypos;
364
365 if (cellheight < minheight)
366 minheight = cellheight;
367 if (cellheight > maxheight)
368 maxheight = cellheight;
369 }
370 }
371
372 if (minheight < 0)
373 minheight = 0;
374 if (maxheight > 255)
375 maxheight = 255;
376
377 Terrain_min_height_int[i][yspeed_offset + xoffset] = (minheight);
378 Terrain_max_height_int[i][yspeed_offset + xoffset] = (maxheight);
379 }
380 }
381 }
382 }
383
384 // Changes the terrain mesh for deformation effect
385 void DeformTerrainPoint(int x, int z, int change_height) {
386 terrain_segment *tseg = &Terrain_seg[z * TERRAIN_WIDTH + x];
387 int i;
388
389 change_height += tseg->ypos;
390
391 change_height = std::clamp(change_height, 0, 255);
392
393 tseg->ypos = change_height;
394 tseg->y = tseg->ypos * TERRAIN_HEIGHT_INCREMENT;
395
396 int sx = std::max(0, x - 1);
397 int sz = std::max(0, z - 1);
398
399 // Update min/max
400 for (i = 0; i < 7; i++) {
401 int row_width = 1 << i;
402 int div = 256 >> i;
403
404 for (int t = sz; t <= z; t++) {
405 for (int k = sx; k <= x; k++) {
406
407 int offset = ((t / div) * row_width) + (k / div);
408
409 if (tseg->ypos > Terrain_max_height_int[i][offset])
410 Terrain_max_height_int[i][offset] = tseg->ypos;
411 if (tseg->ypos < Terrain_min_height_int[i][offset])
412 Terrain_min_height_int[i][offset] = tseg->ypos;
413 }
414 }
415 }
416
417 // Update normals
418
419 for (i = sz; i <= z; i++) {
420 for (int t = sx; t <= x; t++) {
421 vector a, b, c;
422 terrain_segment *tseg0 = &Terrain_seg[i * TERRAIN_WIDTH + t];
423 terrain_segment *tseg1 = &Terrain_seg[(i + 1) * TERRAIN_WIDTH + t];
424 terrain_segment *tseg2 = &Terrain_seg[((i + 1) * TERRAIN_WIDTH) + t + 1];
425 terrain_segment *tseg3 = &Terrain_seg[(i * TERRAIN_WIDTH) + t + 1];
426
427 // Do upper left triangle
428 a.x = t * TERRAIN_SIZE;
429 a.y = tseg0->y;
430 a.z = i * TERRAIN_SIZE;
431
432 b.x = t * TERRAIN_SIZE;
433 b.y = tseg1->y;
434 b.z = (i + 1) * TERRAIN_SIZE;
435
436 c.x = (t + 1) * TERRAIN_SIZE;
437 c.y = tseg2->y;
438 c.z = (i + 1) * TERRAIN_SIZE;
439
440 vm_GetNormal(&TerrainNormals[MAX_TERRAIN_LOD - 1][i * TERRAIN_WIDTH + t].normal1, &a, &b, &c);
441
442 // Now do lower right triangle
443 a.x = t * TERRAIN_SIZE;
444 a.y = tseg0->y;
445 a.z = i * TERRAIN_SIZE;
446
447 b.x = (t + 1) * TERRAIN_SIZE;
448 b.y = tseg2->y;
449 b.z = (i + 1) * TERRAIN_SIZE;
450
451 c.x = (t + 1) * TERRAIN_SIZE;
452 c.y = tseg3->y;
453 c.z = (i)*TERRAIN_SIZE;
454
455 vm_GetNormal(&TerrainNormals[MAX_TERRAIN_LOD - 1][i * TERRAIN_WIDTH + t].normal2, &a, &b, &c);
456 }
457 }
458 }
459
460 void UpdateSingleTerrainLightmap(int which);
461 void DeformTerrain(vector *pos, int depth, float size) {
462 int startx, startz, endx, endz;
463 int changed[4] = {0, 0, 0, 0};
464 vector local_pos = *pos;
465
466 startx = (pos->x / TERRAIN_SIZE) - (size / TERRAIN_SIZE);
467 startz = (pos->z / TERRAIN_SIZE) - (size / TERRAIN_SIZE);
468 endx = (pos->x / TERRAIN_SIZE) + (size / TERRAIN_SIZE);
469 endz = (pos->z / TERRAIN_SIZE) + (size / TERRAIN_SIZE);
470
471 startx = std::max(0, startx);
472 startz = std::max(0, startz);
473 endx = std::min(TERRAIN_WIDTH - 1, endx);
474 endz = std::min(TERRAIN_DEPTH - 1, endz);
475
476 int i, t;
477
478 vector cur_vec;
479 vector up_vec = {0, 1, 0};
480
481 local_pos.y = 0;
482
483 cur_vec.x = startx * TERRAIN_SIZE;
484 cur_vec.y = 0;
485 cur_vec.z = startz * TERRAIN_SIZE;
486
487 float max_dist = vm_VectorDistanceQuick(&local_pos, &cur_vec);
488
489 for (i = startz; i <= endz; i++, cur_vec.z += TERRAIN_SIZE) {
490 cur_vec.x = startx * TERRAIN_SIZE;
491 for (t = startx; t <= endx; t++, cur_vec.x += TERRAIN_SIZE) {
492 terrain_segment *tseg = &Terrain_seg[i * TERRAIN_WIDTH + t];
493
494 if ((up_vec * TerrainNormals[MAX_TERRAIN_LOD - 1][i * TERRAIN_WIDTH + t].normal1) < .5)
495 continue; // not flat enough
496 if ((up_vec * TerrainNormals[MAX_TERRAIN_LOD - 1][i * TERRAIN_WIDTH + t].normal2) < .5)
497 continue; // not flat enough
498
499 float dist = 1.0 - (vm_VectorDistanceQuick(&local_pos, &cur_vec) / max_dist);
500
501 int height_change = -(dist * (float)depth);
502 int light_change = height_change * 2;
503
504 DeformTerrainPoint(t, i, height_change);
505
506 tseg->r = std::max(0, tseg->r + light_change);
507 tseg->g = std::max(0, tseg->g + light_change);
508 tseg->b = std::max(0, tseg->b + light_change);
509
510 int which = ((i / 128) * 2) + (t / 128);
511 changed[which] = 1;
512 }
513 }
514
515 int div = (1 << (MAX_TERRAIN_LOD - 1));
516
517 startx /= div;
518 startz /= div;
519 endx /= div;
520 endz /= div;
521
522 GenerateSingleLODDelta(startx, startz);
523 GenerateSingleLODDelta(endx, startz);
524 GenerateSingleLODDelta(endx, endz);
525 GenerateSingleLODDelta(startx, endz);
526
527 for (i = 0; i < 4; i++) {
528 if (changed[i])
529 UpdateSingleTerrainLightmap(i);
530 }
531 }
532
533 // Builds normals for the currently loaded terrain
534 void BuildTerrainNormals() {
535 int i, t, l, z, x;
536 vector a, b, c;
537 int simplemul;
538 vector up_norm = {0, 1.0, 0};
539
540 // Set all to be initially up
541 for (i = 0; i < TERRAIN_WIDTH * TERRAIN_DEPTH; i++) {
542 TerrainNormals[MAX_TERRAIN_LOD - 1][i].normal1 = up_norm;
543 TerrainNormals[MAX_TERRAIN_LOD - 1][i].normal2 = up_norm;
544 }
545
546 for (l = MAX_TERRAIN_LOD - 1; l < MAX_TERRAIN_LOD; l++) {
547 simplemul = 1 << ((MAX_TERRAIN_LOD - 1) - l);
548
549 for (z = 0, i = 0; i < TERRAIN_DEPTH - simplemul; i += simplemul, z++) {
550 for (x = 0, t = 0; t < TERRAIN_WIDTH - simplemul; t += simplemul, x++) {
551 terrain_segment *tseg = &Terrain_seg[i * TERRAIN_WIDTH + t];
552 terrain_segment *tseg0 = &Terrain_seg[i * TERRAIN_WIDTH + t];
553 terrain_segment *tseg1 = &Terrain_seg[(i + simplemul) * TERRAIN_WIDTH + t];
554 terrain_segment *tseg2 = &Terrain_seg[((i + simplemul) * TERRAIN_WIDTH) + t + simplemul];
555 terrain_segment *tseg3 = &Terrain_seg[(i * TERRAIN_WIDTH) + t + simplemul];
556
557 // Do upper left triangle
558 a.x = t * TERRAIN_SIZE;
559 a.y = tseg0->y;
560 a.z = i * TERRAIN_SIZE;
561
562 b.x = t * TERRAIN_SIZE;
563 b.y = tseg1->y;
564 b.z = (i + simplemul) * TERRAIN_SIZE;
565
566 c.x = (t + simplemul) * TERRAIN_SIZE;
567 c.y = tseg2->y;
568 c.z = (i + simplemul) * TERRAIN_SIZE;
569
570 vm_GetNormal(&TerrainNormals[l][z * (TERRAIN_WIDTH / simplemul) + x].normal1, &a, &b, &c);
571
572 // Now do lower right triangle
573 a.x = t * TERRAIN_SIZE;
574 a.y = tseg0->y;
575 a.z = i * TERRAIN_SIZE;
576
577 b.x = (t + simplemul) * TERRAIN_SIZE;
578 b.y = tseg2->y;
579 b.z = (i + simplemul) * TERRAIN_SIZE;
580
581 c.x = (t + simplemul) * TERRAIN_SIZE;
582 c.y = tseg3->y;
583 c.z = (i)*TERRAIN_SIZE;
584
585 vm_GetNormal(&TerrainNormals[l][z * (TERRAIN_WIDTH / simplemul) + x].normal2, &a, &b, &c);
586 }
587 }
588 }
589 }
590
591 void GenerateTerrainLight() {
592 // Generates a lighting value for every terrain cell
593 int i;
594
595 GenerateLightSource();
596
597 vector camera_light = Terrain_sky.lightsource;
598
599 vm_NormalizeVector(&camera_light);
600
601 for (i = 0; i < TERRAIN_WIDTH * TERRAIN_DEPTH; i++) {
602 float dp = (-vm_DotProduct(&camera_light, &TerrainNormals[MAX_TERRAIN_LOD - 1][i].normal1) + 1.0) / 2;
603 Terrain_seg[i].l = Float_to_ubyte(dp);
604 Terrain_seg[i].r = Terrain_seg[i].l;
605 Terrain_seg[i].g = Terrain_seg[i].l;
606 Terrain_seg[i].b = Terrain_seg[i].l;
607 }
608
609 UpdateTerrainLightmaps();
610 }
611
612 void CloseTerrain(void) {
613 int i;
614
615 for (i = MAX_TERRAIN_LOD - 1; i < MAX_TERRAIN_LOD; i++)
616 mem_free(TerrainNormals[i]);
617
618 for (i = 0; i < 7; i++) {
619 if (Terrain_min_height_int[i])
620 mem_free(Terrain_min_height_int[i]);
621 if (Terrain_max_height_int[i])
622 mem_free(Terrain_max_height_int[i]);
623 }
624
625 if (!Dedicated_server) {
626 mem_free(Terrain_rotate_list);
627 mem_free(World_point_buffer);
628
629 for (i = 0; i < MAX_TERRAIN_LOD - 1; i++)
630 mem_free(TerrainDeltaBlocks[i]);
631 }
632 }
633
634 // Given a 3space triplet, computes the u,v coords for a texture map at that position
635 // on a sphere.
636 void SphereMap(float x, float y, float z, float radius, float *u, float *v, int h) {
637 if (y < 0)
638 Int3();
639
640 float fh = h;
641
642 // produces u from 0 to 1
643 *u = fh / (65536.0 / 16.005); // account for floating point precision error
644
645 *v = acos(y / radius) / (PI / 2); // v=.5 to 1
646 *v /= .5; // v=1 to 2
647 *v -= 1; // v=0 to 1
648 }
649
650 void SetupSkyTexture() {
651
652 int first_top_texture;
653
654 // Find first sky texture
655 first_top_texture = FindTextureName("CloudySky");
656 if (first_top_texture < 0)
657 first_top_texture = 0;
658
659 Terrain_sky.dome_texture = first_top_texture;
660
661 // Figure out the sky color
662
663 Terrain_sky.sky_color = GR_RGB(0, 0, 255);
664
665 if (!Terrain_sky.textured)
666 Terrain_sky.sky_color = GR_RGB(8, 0, 32);
667 Terrain_sky.fog_color = GR_RGB(4, 0, 16);
668 Terrain_sky.horizon_color = GR_RGB(128, 32, 32);
669 Terrain_sky.textured = 1;
670 Terrain_sky.rotate_rate = 0;
671 }
672
673 // Compute a parametric sphere for our sky.
674 void SetupSky(float radius, int flags, uint8_t randit) {
675 int jump = 65536 / MAX_HORIZON_PIECES;
676 int top = ((65536 / 4) * 3) + (65536 / 8);
677
678 int i, t;
679
680 Terrain_sky.radius = radius;
681 Terrain_sky.flags = flags;
682
683 int horizon_r = GR_COLOR_RED(Terrain_sky.horizon_color);
684 int horizon_g = GR_COLOR_GREEN(Terrain_sky.horizon_color);
685 int horizon_b = GR_COLOR_BLUE(Terrain_sky.horizon_color);
686
687 float rad_diff = radius - SKY_RADIUS;
688
689 // Figure out where our points in the inside of the sphere are
690 for (i = 0; i < 6; i++) {
691 const int increment = 16384 / 5;
692 const angle pitch = (65536 - 16384) + (i * increment);
693
694 for (t = 0; t < MAX_HORIZON_PIECES; t++) {
695 vector *vec = &Terrain_sky.horizon_vectors[t][i];
696
697 matrix tempm;
698 vm_AnglesToMatrix(&tempm, pitch, t * jump, 0);
699 vm_ScaleVector(vec, &tempm.fvec, SKY_RADIUS / 2);
700
701 vec->y -= MAX_TERRAIN_HEIGHT;
702 vec->y += (rad_diff / 4);
703 }
704 }
705
706 // Now figure out texture UVS
707 for (i = 0; i < 5; i++) {
708 float scalar = (float)i / 4.0;
709 int angle_increment = 65536 / MAX_HORIZON_PIECES;
710 for (t = 0; t < MAX_HORIZON_PIECES; t++) {
711 float cur_sin = FixSin(t * angle_increment) * scalar;
712 float cur_cos = FixCos(t * angle_increment) * scalar;
713
714 cur_sin = (cur_sin + 1) / 2;
715 cur_cos = (cur_cos + 1) / 2;
716
717 Terrain_sky.horizon_u[t][i] = cur_cos;
718 Terrain_sky.horizon_v[t][i] = cur_sin;
719 }
720 }
721
722 int highcount = 0; // keep track of what stars are close to the top of the sphere
723 // don't draw too many of them
724 if (!randit)
725 return;
726
727 for (i = 0; i < MAX_STARS; i++) {
728 vector starvec;
729 matrix tempm;
730 int p;
731 top = ((65536 / 4) * 3);
732 int highlimit = MAX_STARS / 8;
733
734 p = ps_rand() % (65336 / 4);
735
736 while (highcount > highlimit && p < 6000)
737 p = ps_rand() % (65336 / 4);
738
739 if (p < 6000)
740 highcount++;
741
742 vm_AnglesToMatrix(&tempm, (top + p) % 65336, (ps_rand() * ps_rand()) % 65536, 0);
743 vm_ScaleVector(&starvec, &tempm.fvec, Terrain_sky.radius * 500);
744 Terrain_sky.star_vectors[i] = starvec;
745
746 // Now figure out the color of this star. The closer to horizon it is, the
747 // dimmer it is
748 float ynorm = starvec.y / (Terrain_sky.radius * 500);
749
750 float color_norm = ynorm * 2;
751 color_norm = std::clamp(color_norm, 0.2f, 1.0f);
752 int color = ps_rand() % 6;
753 int r, g, b;
754
755 if (color <= 2) {
756 r = 255;
757 g = 255;
758 b = 255;
759 } else if (color == 3) {
760 r = 255;
761 g = 200;
762 b = 200;
763 } else if (color == 4) {
764 r = 255;
765 g = 200;
766 b = 255;
767 } else {
768 r = 255;
769 g = 255;
770 b = 200;
771 }
772
773 r = ((1.0 - color_norm) * horizon_r) + (color_norm * r);
774 g = ((1.0 - color_norm) * horizon_g) + (color_norm * g);
775 b = ((1.0 - color_norm) * horizon_b) + (color_norm * b);
776
777 Terrain_sky.star_color[i] = GR_RGB(r, g, b);
778 }
779
780 for (i = 0; i < MAX_SATELLITES; i++) {
781 vector satellitevec;
782 matrix tempm;
783
784 int p = ps_rand() % (65336 / 8);
785 top = ((65536 / 4) * 3) + (4096); // don't do satellites that are straight up
786
787 vm_AnglesToMatrix(&tempm, (top + p) % 65336, (ps_rand() * ps_rand()) % 65536, 0);
788 vm_ScaleVector(&satellitevec, &tempm.fvec, Terrain_sky.radius * 3);
789 Terrain_sky.satellite_vectors[i] = satellitevec;
790 Terrain_sky.satellite_size[i] = 500;
791 }
792 }
793
794 // Loads a pcx bitmap to be used as a height map
795 int LoadPCXTerrain(char *filename) {
796 CFILE *infile;
797 int run = 0, i, total, j, n;
798 int16_t xmin, ymin, xmax, ymax;
799 int width, height;
800 uint8_t buf;
801 uint8_t *lando;
802
803 if ((infile = cfopen(filename, "rb")) == NULL)
804 return (0);
805
806 cf_ReadInt(infile);
807 xmin = cf_ReadShort(infile);
808 ymin = cf_ReadShort(infile);
809 xmax = cf_ReadShort(infile);
810 ymax = cf_ReadShort(infile);
811
812 for (n = 0; n < 116; n++)
813 cf_ReadByte(infile);
814
815 width = 1 + xmax - xmin;
816 height = 1 + ymax - ymin;
817
818 total = width * height;
819
820 lando = (uint8_t *)mem_malloc(total);
821
822 mprintf(0, "Heightmap is %d x %d\n", width, height);
823
824 while (run < total) {
825 buf = cf_ReadByte(infile);
826 if (buf >= 192) {
827 uint8_t tb = 0;
828 tb = cf_ReadByte(infile);
829 for (i = 0; i < (buf - 192); i++, run++)
830 lando[run] = tb;
831 } else {
832 lando[run] = buf;
833 run++;
834 }
835 }
836
837 cfclose(infile);
838
839 for (i = 0; i < TERRAIN_DEPTH; i++)
840 for (j = 0; j < TERRAIN_WIDTH; j++) {
841 n = (int)lando[((i % height) * width) + (j % width)];
842
843 Terrain_seg[((TERRAIN_WIDTH - 1) - i) * TERRAIN_WIDTH + j].ypos = n;
844 }
845
846 mem_free(lando);
847 BuildMinMaxTerrain();
848 BuildTerrainNormals();
849 GenerateTerrainLight();
850
851 #if (defined(EDITOR) || defined(NEWEDITOR))
852 memset(TerrainSelected, 0, TERRAIN_WIDTH * TERRAIN_DEPTH);
853 Num_terrain_selected = 0;
854 World_changed = 1;
855 #endif
856
857 return (1);
858 }
859
860 // Called whenever a new level is initted
861 void ResetTerrain(int force) {
862 int i, t;
863 int tex_index = FindTextureName("RainbowTexture");
864
865 if (tex_index < 0)
866 tex_index = 4; // stuff some random value
867
868 for (i = 0; i < TERRAIN_DEPTH; i++) {
869 for (t = 0; t < TERRAIN_WIDTH; t++) {
870 #if (!defined(RELEASE) || defined(NEWEDITOR))
871 Terrain_seg_render_objs[i * TERRAIN_WIDTH + t] = -1;
872 #endif
873 Terrain_seg[i * TERRAIN_WIDTH + t].objects = -1;
874 Terrain_seg[i * TERRAIN_WIDTH + t].flags = 0;
875 Terrain_seg[i * TERRAIN_WIDTH + t].lm_quad = ((i / 128) * 2) + (t / 128);
876 Terrain_seg[i * TERRAIN_WIDTH + t].texseg_index = ((i >> 3) * TERRAIN_TEX_WIDTH) + (t >> 3);
877
878 Terrain_dynamic_table[i * TERRAIN_WIDTH + t] = 0xFF;
879 }
880 }
881
882 for (i = 0; i < TERRAIN_TEX_DEPTH; i++) {
883 for (t = 0; t < TERRAIN_TEX_WIDTH; t++) {
884 int s = i * TERRAIN_TEX_WIDTH + t;
885 Terrain_tex_seg[s].tex_index = tex_index;
886 Terrain_tex_seg[s].rotation = (1 << 4);
887 }
888 }
889
890 if (force) {
891 Terrain_checksum = -1;
892
893 for (i = 0; i < TERRAIN_DEPTH; i++) {
894 for (t = 0; t < TERRAIN_WIDTH; t++) {
895 Terrain_seg[i * TERRAIN_WIDTH + t].y = 0;
896 Terrain_seg[i * TERRAIN_WIDTH + t].ypos = 0;
897 }
898 }
899
900 for (i = 0; i < 7; i++) {
901 int size = 1 << i;
902 memset(Terrain_min_height_int[i], 0, size * size);
903 memset(Terrain_max_height_int[i], 0, size * size);
904 }
905
906 if (!Dedicated_server) {
907 for (i = 0; i < MAX_TERRAIN_LOD - 1; i++) {
908 int w = TERRAIN_WIDTH >> ((MAX_TERRAIN_LOD - 1) - i);
909 int h = TERRAIN_DEPTH >> ((MAX_TERRAIN_LOD - 1) - i);
910
911 memset(TerrainDeltaBlocks[i], 0, w * h * sizeof(float));
912 }
913 }
914 }
915
916 BuildTerrainNormals();
917
918 for (i = 0; i < MAX_SATELLITES; i++) {
919 Terrain_sky.satellite_texture[i] = 111;
920 Terrain_sky.satellite_flags[i] = 0;
921 }
922
923 GenerateTerrainLight();
924
925 #if (defined(EDITOR) || defined(NEWEDITOR))
926 memset(TerrainSelected, 0, TERRAIN_WIDTH * TERRAIN_DEPTH);
927 #endif
928
929 memset(TerrainJoinMap, 0, TERRAIN_WIDTH * TERRAIN_DEPTH);
930
931 Terrain_sky.damage_per_second = 0;
932 Terrain_sky.fog_scalar = .85f;
933 SetupSkyTexture();
934 ResetWeather();
935 }
936
937 // Takes our 1st satellite and fills in the appropriate values in the lightsource vector
938 void GenerateLightSource() {
939 Terrain_sky.lightsource.x = FixCos(Terrain_sky.lightangle);
940 Terrain_sky.lightsource.z = FixSin(Terrain_sky.lightangle);
941 }
942
943 void InitTerrain(void) {
944 int i, w, h, t;
945
946 // set sky texture to unused
947 Terrain_sky.textured = 0;
948
949 // Setup stuff for rendering
950 if (!Dedicated_server) {
951 Terrain_rotate_list = (uint16_t *)mem_malloc(TERRAIN_WIDTH * TERRAIN_DEPTH * sizeof(uint16_t));
952 ASSERT(Terrain_rotate_list);
953
954 World_point_buffer = (g3Point *)mem_malloc(TERRAIN_WIDTH * TERRAIN_DEPTH * sizeof(g3Point));
955 ASSERT(World_point_buffer);
956
957 // Allocate space for lod delta tree and unique texture IDs
958 for (i = 0; i < MAX_TERRAIN_LOD - 1; i++) {
959 w = TERRAIN_WIDTH >> ((MAX_TERRAIN_LOD - 1) - i);
960 h = TERRAIN_DEPTH >> ((MAX_TERRAIN_LOD - 1) - i);
961
962 TerrainDeltaBlocks[i] = (float *)mem_malloc(w * h * sizeof(float));
963 }
964 }
965
966 // Allocate space for lod normals
967
968 for (i = MAX_TERRAIN_LOD - 1; i < MAX_TERRAIN_LOD; i++) {
969 w = TERRAIN_WIDTH >> ((MAX_TERRAIN_LOD - 1) - i);
970 h = TERRAIN_DEPTH >> ((MAX_TERRAIN_LOD - 1) - i);
971
972 TerrainNormals[i] = (terrain_normals *)mem_malloc(w * h * sizeof(terrain_normals));
973 memset(TerrainNormals[i], 0, w * h * sizeof(terrain_normals));
974 }
975
976 // Allocate space for our min/max tables
977 for (i = 0; i < 7; i++) {
978 w = 1 << i;
979 h = 1 << i;
980
981 // Index 1 cuts the whole thing into 4ths, index 2 into 8ths, etc
982 Terrain_min_height_int[i] = (uint8_t *)mem_malloc(w * h * sizeof(uint8_t));
983 Terrain_max_height_int[i] = (uint8_t *)mem_malloc(w * h * sizeof(uint8_t));
984
985 ASSERT(Terrain_min_height_int[i] != NULL);
986 ASSERT(Terrain_max_height_int[i] != NULL);
987 }
988
989 Terrain_sky.lightangle = 0;
990 Terrain_sky.num_satellites = 1;
991 Terrain_sky.damage_per_second = 0;
992
993 SetupSky(SKY_RADIUS, TF_STARS | TF_SATELLITES, 1);
994 GenerateLightSource();
995
996 for (i = 0; i < 4; i++) {
997 TerrainLightmaps[i] = lm_AllocLightmap(128, 128);
998 ASSERT(TerrainLightmaps[i] != BAD_LM_INDEX);
999 GameLightmaps[TerrainLightmaps[i]].flags |= LF_WRAP;
1000 }
1001
1002 for (i = 0; i < MAX_TERRAIN_LOD; i++) {
1003 int simplemul = 1 << ((MAX_TERRAIN_LOD - 1) - i);
1004 int answer;
1005
1006 TerrainEdgeJump[i] = simplemul;
1007
1008 for (t = 0; t < 16; t++) {
1009 if (t == 0)
1010 answer = 1;
1011 else {
1012 if (t % simplemul == 0)
1013 answer = 1;
1014 else
1015 answer = 0;
1016 }
1017 TerrainEdgeTest[i][t] = answer;
1018 }
1019 }
1020
1021 // Setup dome textures
1022 Terrain_sky.dome_texture = 0;
1023
1024 atexit(CloseTerrain);
1025 }
1026
1027 void UpdateSingleTerrainLightmap(int which) {
1028 int w = lm_w(TerrainLightmaps[which]);
1029 int i, t;
1030
1031 GameLightmaps[TerrainLightmaps[which]].flags |= LF_CHANGED;
1032
1033 int sx = (which % 2) * 128;
1034 int sz = (which / 2) * 128;
1035
1036 for (i = sz; i < sz + 128; i++) {
1037 for (t = sx; t < sx + 128; t++) {
1038 int tseg = i * TERRAIN_WIDTH + t;
1039 terrain_segment *tp = &Terrain_seg[tseg];
1040
1041 uint16_t color = GR_RGB16(tp->r, tp->g, tp->b);
1042 uint16_t *data = lm_data(TerrainLightmaps[which]);
1043
1044 int x = t % 128;
1045 int y = 127 - (i % 128);
1046
1047 data[y * w + x] = OPAQUE_FLAG | color;
1048 }
1049 }
1050 }
1051
1052 // Generates lightmaps based on the info given by the .light field of each Terrain_seg
1053 void UpdateTerrainLightmaps() {
1054 int i, t;
1055
1056 // First make the wraparounds work right
1057
1058 // lower-left strip
1059 for (i = 0; i < 128; i++) {
1060 Terrain_seg[i * TERRAIN_WIDTH].r = Terrain_seg[i * TERRAIN_WIDTH + 128].r;
1061 Terrain_seg[i * TERRAIN_WIDTH].g = Terrain_seg[i * TERRAIN_WIDTH + 128].g;
1062 Terrain_seg[i * TERRAIN_WIDTH].b = Terrain_seg[i * TERRAIN_WIDTH + 128].b;
1063
1064 Terrain_seg[i].r = Terrain_seg[128 * TERRAIN_WIDTH + i].r;
1065 Terrain_seg[i].g = Terrain_seg[128 * TERRAIN_WIDTH + i].g;
1066 Terrain_seg[i].b = Terrain_seg[128 * TERRAIN_WIDTH + i].b;
1067 }
1068
1069 // lower-right strip
1070 for (i = 0; i < 128; i++) {
1071 Terrain_seg[i * TERRAIN_WIDTH + 255].r = Terrain_seg[i * TERRAIN_WIDTH + 127].r;
1072 Terrain_seg[i * TERRAIN_WIDTH + 255].g = Terrain_seg[i * TERRAIN_WIDTH + 127].g;
1073 Terrain_seg[i * TERRAIN_WIDTH + 255].b = Terrain_seg[i * TERRAIN_WIDTH + 127].b;
1074
1075 Terrain_seg[i + 128].r = Terrain_seg[128 * TERRAIN_WIDTH + i + 128].r;
1076 Terrain_seg[i + 128].g = Terrain_seg[128 * TERRAIN_WIDTH + i + 128].g;
1077 Terrain_seg[i + 128].b = Terrain_seg[128 * TERRAIN_WIDTH + i + 128].b;
1078 }
1079
1080 // upper-left strip
1081 for (i = 0; i < 128; i++) {
1082 Terrain_seg[(i + 128) * TERRAIN_WIDTH].r = Terrain_seg[(i + 128) * TERRAIN_WIDTH + 128].r;
1083 Terrain_seg[(i + 128) * TERRAIN_WIDTH].g = Terrain_seg[(i + 128) * TERRAIN_WIDTH + 128].g;
1084 Terrain_seg[(i + 128) * TERRAIN_WIDTH].b = Terrain_seg[(i + 128) * TERRAIN_WIDTH + 128].b;
1085
1086 Terrain_seg[255 * TERRAIN_WIDTH + i].r = Terrain_seg[127 * TERRAIN_WIDTH + i].r;
1087 Terrain_seg[255 * TERRAIN_WIDTH + i].g = Terrain_seg[127 * TERRAIN_WIDTH + i].g;
1088 Terrain_seg[255 * TERRAIN_WIDTH + i].b = Terrain_seg[127 * TERRAIN_WIDTH + i].b;
1089 }
1090
1091 // upper-right strip
1092 for (i = 0; i < 128; i++) {
1093 Terrain_seg[(i + 128) * TERRAIN_WIDTH + 255].r = Terrain_seg[(i + 128) * TERRAIN_WIDTH + 127].r;
1094 Terrain_seg[(i + 128) * TERRAIN_WIDTH + 255].g = Terrain_seg[(i + 128) * TERRAIN_WIDTH + 127].g;
1095 Terrain_seg[(i + 128) * TERRAIN_WIDTH + 255].b = Terrain_seg[(i + 128) * TERRAIN_WIDTH + 127].b;
1096
1097 Terrain_seg[255 * TERRAIN_WIDTH + i + 128].r = Terrain_seg[127 * TERRAIN_WIDTH + i + 128].r;
1098 Terrain_seg[255 * TERRAIN_WIDTH + i + 128].g = Terrain_seg[127 * TERRAIN_WIDTH + i + 128].g;
1099 Terrain_seg[255 * TERRAIN_WIDTH + i + 128].b = Terrain_seg[127 * TERRAIN_WIDTH + i + 128].b;
1100 }
1101
1102 int w = lm_w(TerrainLightmaps[0]);
1103
1104 for (i = 0; i < TERRAIN_DEPTH; i++) {
1105 for (t = 0; t < TERRAIN_WIDTH; t++) {
1106 int tseg = i * TERRAIN_WIDTH + t;
1107 terrain_segment *tp = &Terrain_seg[tseg];
1108 int x = t % 128;
1109 int y = 127 - (i % 128);
1110 int which = ((i / 128) * 2) + (t / 128);
1111
1112 uint16_t color = GR_RGB16(tp->r, tp->g, tp->b);
1113 uint16_t *data = lm_data(TerrainLightmaps[which]);
1114
1115 data[y * w + x] = OPAQUE_FLAG | color;
1116 }
1117 }
1118
1119 for (i = 0; i < 4; i++) {
1120 GameLightmaps[TerrainLightmaps[i]].flags |= LF_CHANGED;
1121 GameLightmaps[TerrainLightmaps[i]].flags &= ~LF_LIMITS;
1122 }
1123 }
1124
1125 // Builds the surface normal for terrain segment n
1126 void BuildNormalForTerrainSegment(int n) {
1127 int i, t;
1128
1129 vector a, b, c;
1130
1131 if (n < 0 || n >= (TERRAIN_WIDTH - 1) * (TERRAIN_DEPTH - 1))
1132 return;
1133
1134 i = n / TERRAIN_WIDTH;
1135 t = n % TERRAIN_WIDTH;
1136
1137 a.x = t * TERRAIN_SIZE;
1138 a.y = Terrain_seg[(i * TERRAIN_WIDTH) + (t)].y;
1139 a.z = i * TERRAIN_SIZE;
1140
1141 b.x = t * TERRAIN_SIZE;
1142 b.y = Terrain_seg[(i * TERRAIN_WIDTH) + (t + TERRAIN_WIDTH)].y;
1143 b.z = (i + 1) * TERRAIN_SIZE;
1144
1145 c.x = (t + 1) * TERRAIN_SIZE;
1146 c.y = Terrain_seg[(i * TERRAIN_WIDTH) + (t + TERRAIN_WIDTH + 1)].y;
1147 c.z = (i + 1) * TERRAIN_SIZE;
1148
1149 vm_GetNormal(&TerrainNormals[MAX_TERRAIN_LOD - 1][i * TERRAIN_WIDTH + t].normal1, &a, &b, &c);
1150
1151 a.x = t * TERRAIN_SIZE;
1152 a.y = Terrain_seg[(i * TERRAIN_WIDTH) + (t)].y;
1153 a.z = i * TERRAIN_SIZE;
1154
1155 b.x = (t + 1) * TERRAIN_SIZE;
1156 b.y = Terrain_seg[(i * TERRAIN_WIDTH) + (t + TERRAIN_WIDTH + 1)].y;
1157 b.z = (i + 1) * TERRAIN_SIZE;
1158
1159 c.x = (t + 1) * TERRAIN_SIZE;
1160 c.y = Terrain_seg[(i * TERRAIN_WIDTH) + (t + 1)].y;
1161 c.z = (i)*TERRAIN_SIZE;
1162
1163 vm_GetNormal(&TerrainNormals[MAX_TERRAIN_LOD - 1][i * TERRAIN_WIDTH + t].normal2, &a, &b, &c);
1164 }
1165
1166 // Builds the vertex normal for terrain segment n
1167 void BuildLightingNormalForSegment(int n) {
1168 int i, t, hback, vback;
1169 vector temp;
1170
1171 if (n < 0 || n >= (TERRAIN_WIDTH - 1) * (TERRAIN_DEPTH - 1))
1172 return;
1173
1174 i = n / TERRAIN_WIDTH;
1175 t = n % TERRAIN_WIDTH;
1176
1177 if (i == 0)
1178 vback = TERRAIN_DEPTH - 1;
1179 else
1180 vback = i - 1;
1181
1182 if (t == 0)
1183 hback = TERRAIN_WIDTH - 1;
1184 else
1185 hback = t - 1;
1186
1187 vm_MakeZero(&temp);
1188
1189 vm_AddVectors(&temp, &temp, &TerrainNormals[MAX_TERRAIN_LOD - 1][i * TERRAIN_WIDTH + hback].normal2);
1190 vm_AddVectors(&temp, &temp, &TerrainNormals[MAX_TERRAIN_LOD - 1][i * TERRAIN_WIDTH + t].normal2);
1191 vm_AddVectors(&temp, &temp, &TerrainNormals[MAX_TERRAIN_LOD - 1][(vback)*TERRAIN_WIDTH + hback].normal1);
1192 vm_AddVectors(&temp, &temp, &TerrainNormals[MAX_TERRAIN_LOD - 1][(vback)*TERRAIN_WIDTH + t].normal2);
1193
1194 vm_AverageVector(&temp, 4);
1195 // vm_NormalizeVectorFast (&temp);
1196 }