MCPcopy Create free account
hub / github.com/DescentDevelopers/Descent3 / DeformTerrainPoint

Function DeformTerrainPoint

Descent3/terrain.cpp:385–458  ·  view source on GitHub ↗

Changes the terrain mesh for deformation effect

Source from the content-addressed store, hash-verified

383
384// Changes the terrain mesh for deformation effect
385void 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

Callers 1

DeformTerrainFunction · 0.85

Calls 1

vm_GetNormalFunction · 0.50

Tested by

no test coverage detected