Skip to content

Commit

Permalink
GND: Implement error correction for lightmap textures
Browse files Browse the repository at this point in the history
The blending stage interpolates colors linearly when multiplying with the higher-precision inputs (same as for diffuse textures).
  • Loading branch information
rdw-software committed Mar 18, 2024
1 parent a2a1bfd commit 781c39a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
18 changes: 11 additions & 7 deletions Core/FileFormats/RagnarokGND.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ local ffi = require("ffi")
local uv = require("uv")

local assert = assert
local math_floor = math.floor
local floor = math.floor
local format = string.format
local table_insert = table.insert
local tonumber = tonumber
Expand Down Expand Up @@ -800,9 +800,9 @@ function RagnarokGND:GenerateLightmapTextureImage(posterizationLevel)
local rgbaImageBytes = buffer.new(width * height * 4)
local bufferStartPointer, reservedlength = rgbaImageBytes:reserve(width * height * 4)
for pixelV = 0, height - 1, 1 do
local sliceV = math_floor(pixelV / 8)
local sliceV = floor(pixelV / 8)
for pixelU = 0, width - 1, 1 do
local sliceU = math_floor(pixelU / 8)
local sliceU = floor(pixelU / 8)
local sliceID = sliceV * numSlicesPerRow + sliceU
local offsetU = pixelU % 8
local offsetV = pixelV % 8
Expand All @@ -827,10 +827,14 @@ function RagnarokGND:GenerateLightmapTextureImage(posterizationLevel)
posterizedBlue = bit.lshift(posterizedBlue, posterizationLevel)
posterizedAlpha = bit.lshift(posterizedAlpha, posterizationLevel)

bufferStartPointer[writableAreaStartIndex + 0] = posterizedRed
bufferStartPointer[writableAreaStartIndex + 1] = posterizedGreen
bufferStartPointer[writableAreaStartIndex + 2] = posterizedBlue
bufferStartPointer[writableAreaStartIndex + 3] = posterizedAlpha
local remainingColorDepthInBitsPerPixel = math.max(1, 8 - posterizationLevel)
local numAvailableColorValues = math.pow(2, remainingColorDepthInBitsPerPixel) -- 256, 128, 64, 32, 16, 8
local errorCorrection = 1 / numAvailableColorValues

bufferStartPointer[writableAreaStartIndex + 0] = posterizedRed + floor(errorCorrection * red)
bufferStartPointer[writableAreaStartIndex + 1] = posterizedGreen + floor(errorCorrection * green)
bufferStartPointer[writableAreaStartIndex + 2] = posterizedBlue + floor(errorCorrection * blue)
bufferStartPointer[writableAreaStartIndex + 3] = posterizedAlpha + floor(errorCorrection * alpha)
else -- Slightly wasteful, but the determinism enables testing (and it's barely noticeable anyway)
bufferStartPointer[writableAreaStartIndex + 0] = 255
bufferStartPointer[writableAreaStartIndex + 1] = 0
Expand Down
2 changes: 1 addition & 1 deletion Tests/FileFormats/RagnarokGND.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ describe("RagnarokGND", function()
assertEquals(lightmapTextureImage.height, 8)

local rawImageBytes = tostring(lightmapTextureImage.rgbaImageBytes)
local expectedTextureChecksum = 288753058
local expectedTextureChecksum = 3839217290
local generatedTextureChecksum = miniz.crc32(rawImageBytes)
assertEquals(generatedTextureChecksum, expectedTextureChecksum)
end)
Expand Down
2 changes: 1 addition & 1 deletion Tests/Tools/RagnarokTools.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe("RagnarokTools", function()
it("should export the GND lightmap data in a human-readable format if a valid GND buffer is passed", function()
local expectedLightmapChecksum = "be735bab85771a54892d4129d7aba3126e0f7f41f2c9891a28aa8dcfc897d2fa"
local expectedShadowmapChecksum = "4a7a36bedbb8e73797b91b2f568b59b8d4600dc3975e2265114339a5142e9175"
local expectedTextureChecksum = "dfbff3a58a516c0990147567388431dee2d05dae12a01bd5fb4d30230bb79573"
local expectedTextureChecksum = "b8323aa6889476237edea7d1ce894c889e3bb7dbd464bbe1068568994999c659"

local gndFileContents = C_FileSystem.ReadFile(path.join("Tests", "Fixtures", "no-water-plane.gnd"))
RagnarokTools:ExportLightmapsFromGND(gndFileContents)
Expand Down

0 comments on commit 781c39a

Please sign in to comment.