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

Function BuildTerrainNormals

Descent3/terrain.cpp:534–589  ·  view source on GitHub ↗

Builds normals for the currently loaded terrain

Source from the content-addressed store, hash-verified

532
533// Builds normals for the currently loaded terrain
534void 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
591void GenerateTerrainLight() {

Callers 4

OnTerrpadRenormalizeMethod · 0.85
ReadTerrainChunksFunction · 0.85
LoadPCXTerrainFunction · 0.85
ResetTerrainFunction · 0.85

Calls 1

vm_GetNormalFunction · 0.50

Tested by

no test coverage detected