rodecomp/Source/Contents/Ground/GroundLightingInitialization.cpp
ragnarok-clanker e307fb6bc6 Decompile fn_17558 (GroundGeometry::InitializeLighting)
Batch #15776 by Yommy: 0 byte-identical, 1 confirmed by peer review.

- fn_17558 GroundGeometry::InitializeLighting: logic confirmed by peer review

Peer-reviewed-by: eAthena, Toshiro
Accepted without human review: peers confirmed the logic ("same" ahead by at least 3, with 70% of the votes).
2026-11-10 11:35:52 +00:00

74 lines
2.3 KiB
C++

#include "Contents/Ground/GroundGeometry.h"
#include "Resource/World/TerrainResource.h"
// OFFSET: 0x00ABFB10
// FUNCTION: fn_17558
void GroundGeometry::InitializeLighting() {
if (!this->IsNewVer()) {
return;
}
// STATIC: 0x01348128
// GLOBAL: gl_25590
static constexpr int offsets[5][2] = {{0, 0}, {0, -1}, {0, 1}, {1, 0}, {-1, 0}};
for (int row = 0; row < 2 * this->height; ++row) {
for (int column = 0; column < 2 * this->width; ++column) {
float average = 0.0f;
const TerrainHeightCell* cell = this->terrain->CellAt(column, row);
if (cell) {
average = ((cell->heights[1] + cell->heights[0]) + cell->heights[2] + cell->heights[3]) * 0.25f;
}
float minimum;
float maximum;
this->CellBounds(column / 2, row / 2, minimum, maximum);
float red = 1.0f;
float green = 1.0f;
float blue = 1.0f;
if (average >= minimum - 2.5f) {
int alphaSum = 0;
int redSum = 0;
int greenSum = 0;
int blueSum = 0;
for (const auto& offset : offsets) {
unsigned char alpha;
unsigned char sampleRed;
unsigned char sampleGreen;
unsigned char sampleBlue;
this->LightmapSample(column + offset[0], row + offset[1], alpha, sampleRed, sampleGreen, sampleBlue);
alphaSum += alpha;
redSum += sampleRed;
greenSum += sampleGreen;
blueSum += sampleBlue;
}
const float alpha = (static_cast<float>(alphaSum) / 5.0f) / 255.0f;
red = ((static_cast<float>(redSum) / 5.0f) * 0.0078125f) + alpha;
green = ((static_cast<float>(greenSum) / 5.0f) * 0.0078125f) + alpha;
blue = ((static_cast<float>(blueSum) / 5.0f) * 0.0078125f) + alpha;
const float first = green > red ? green : red;
const float largest = blue > first ? blue : first;
if (largest > 1.0f) {
red = red / largest;
green = green / largest;
blue = blue / largest;
}
}
if (!this->lighting.empty()) {
const auto index = static_cast<unsigned>(column) + 2u * static_cast<unsigned>(row) * static_cast<unsigned>(this->width);
this->lighting[index] = {red, green, blue};
}
}
}
}
// OFFSET: 0x00ABFD50
// FUNCTION: fn_17559
void GroundBase::InitializeLighting() {
}