Reverse axes order for heightmaps (iterate in Z direction first instead of X)

This commit is contained in:
Gaël de Sailly 2020-07-21 12:46:23 +02:00
parent 6f43430574
commit 25c5cb2e1f
3 changed files with 41 additions and 40 deletions

View File

@ -7,6 +7,8 @@ local blocksize = mapgen_rivers.blocksize
local sea_level = mapgen_rivers.sea_level local sea_level = mapgen_rivers.sea_level
local riverbed_slope = mapgen_rivers.riverbed_slope local riverbed_slope = mapgen_rivers.riverbed_slope
local MAP_BOTTOM = -31000
-- Linear interpolation -- Linear interpolation
local function interp(v00, v01, v11, v10, xf, zf) local function interp(v00, v01, v11, v10, xf, zf)
local v0 = v01*xf + v00*(1-xf) local v0 = v01*xf + v00*(1-xf)
@ -17,15 +19,14 @@ end
local function heightmaps(minp, maxp) local function heightmaps(minp, maxp)
local polygons = make_polygons(minp, maxp) local polygons = make_polygons(minp, maxp)
local incr = maxp.x-minp.x+1 local incr = maxp.z-minp.z+1
local i0 = (minp.z-minp.z) * incr + (minp.x-minp.x) + 1
local terrain_height_map = {} local terrain_height_map = {}
local lake_height_map = {} local lake_height_map = {}
local i = 1 local i = 1
for x=minp.x, maxp.x do
for z=minp.z, maxp.z do for z=minp.z, maxp.z do
for x=minp.x, maxp.x do
local poly = polygons[i] local poly = polygons[i]
if poly then if poly then
@ -106,8 +107,8 @@ local function heightmaps(minp, maxp)
terrain_height_map[i] = terrain_height terrain_height_map[i] = terrain_height
lake_height_map[i] = lake_height lake_height_map[i] = lake_height
else else
terrain_height_map[i] = -31000 terrain_height_map[i] = MAP_BOTTOM
lake_height_map[i] = -31000 lake_height_map[i] = MAP_BOTTOM
end end
i = i + 1 i = i + 1
end end

View File

@ -87,8 +87,8 @@ local function generate(minp, maxp, seed)
local pminp = {x=math.floor(xmin), z=math.floor(zmin)} local pminp = {x=math.floor(xmin), z=math.floor(zmin)}
local pmaxp = {x=math.floor(xmax)+1, z=math.floor(zmax)+1} local pmaxp = {x=math.floor(xmax)+1, z=math.floor(zmax)+1}
local incr = pmaxp.z-pminp.z+1 local incr = pmaxp.x-pminp.x+1
local i_origin = 1 - pminp.x*incr - pminp.z local i_origin = 1 - pminp.z*incr - pminp.x
local terrain_map, lake_map = heightmaps(pminp, pmaxp) local terrain_map, lake_map = heightmaps(pminp, pmaxp)
local c_stone = minetest.get_content_id("default:stone") local c_stone = minetest.get_content_id("default:stone")
@ -125,10 +125,10 @@ local function generate(minp, maxp, seed)
local x0 = math.floor(xn) local x0 = math.floor(xn)
local z0 = math.floor(zn) local z0 = math.floor(zn)
local i0 = i_origin + x0*incr + z0 local i0 = i_origin + z0*incr + x0
local i1 = i0+incr local i1 = i0+1
local i2 = i1+1 local i2 = i1+incr
local i3 = i0+1 local i3 = i2-1
local terrain = interp(terrain_map[i0], terrain_map[i1], terrain_map[i2], terrain_map[i3], xn-x0, zn-z0) local terrain = interp(terrain_map[i0], terrain_map[i1], terrain_map[i2], terrain_map[i3], xn-x0, zn-z0)
if y <= maxp.y then if y <= maxp.y then

View File

@ -96,7 +96,7 @@ local function make_polygons(minp, maxp)
init = true init = true
end end
local chulens = maxp.z - minp.z + 1 local chulens = maxp.x - minp.x + 1
local polygons = {} local polygons = {}
-- Determine the minimum and maximum coordinates of the polygons that could be on the chunk, knowing that they have an average size of 'blocksize' and a maximal offset of 0.5 blocksize. -- Determine the minimum and maximum coordinates of the polygons that could be on the chunk, knowing that they have an average size of 'blocksize' and a maximal offset of 0.5 blocksize.
@ -115,44 +115,44 @@ local function make_polygons(minp, maxp)
local poly_z = {offset_z[iA]+zp, offset_z[iB]+zp, offset_z[iC]+zp+1, offset_z[iD]+zp+1} local poly_z = {offset_z[iA]+zp, offset_z[iB]+zp, offset_z[iC]+zp+1, offset_z[iD]+zp+1}
local polygon = {x=poly_x, z=poly_z, i={iA, iB, iC, iD}} local polygon = {x=poly_x, z=poly_z, i={iA, iB, iC, iD}}
local bounds = {} -- Will be a list of the intercepts of polygon edges for every X position (scanline algorithm) local bounds = {} -- Will be a list of the intercepts of polygon edges for every Z position (scanline algorithm)
-- Calculate the min and max X positions -- Calculate the min and max Z positions
local xmin = math.max(math.floor(blocksize*math.min(unpack(poly_x)))+1, minp.x) local zmin = math.max(math.floor(blocksize*math.min(unpack(poly_z)))+1, minp.z)
local xmax = math.min(math.floor(blocksize*math.max(unpack(poly_x))), maxp.x) local zmax = math.min(math.floor(blocksize*math.max(unpack(poly_z))), maxp.z)
-- And initialize the arrays -- And initialize the arrays
for x=xmin, xmax do for z=zmin, zmax do
bounds[x] = {} bounds[z] = {}
end end
local i1 = 4 local i1 = 4
for i2=1, 4 do -- Loop on 4 edges for i2=1, 4 do -- Loop on 4 edges
local x1, x2 = poly_x[i1], poly_x[i2]
-- Calculate the integer X positions over which this edge spans
local lxmin = math.floor(blocksize*math.min(x1, x2))+1
local lxmax = math.floor(blocksize*math.max(x1, x2))
if lxmin <= lxmax then -- If there is at least one position in it
local z1, z2 = poly_z[i1], poly_z[i2] local z1, z2 = poly_z[i1], poly_z[i2]
-- Calculate coefficient of the equation defining the edge: Z=aX+b -- Calculate the integer Z positions over which this edge spans
local a = (z1-z2) / (x1-x2) local lzmin = math.floor(blocksize*math.min(z1, z2))+1
local b = blocksize*(z1 - a*x1) local lzmax = math.floor(blocksize*math.max(z1, z2))
for x=math.max(lxmin, minp.x), math.min(lxmax, maxp.x) do if lzmin <= lzmax then -- If there is at least one position in it
-- For every X position involved, add the intercepted Z position in the table local x1, x2 = poly_x[i1], poly_x[i2]
table.insert(bounds[x], a*x+b) -- Calculate coefficient of the equation defining the edge: X=aZ+b
local a = (x1-x2) / (z1-z2)
local b = blocksize*(x1 - a*z1)
for z=math.max(lzmin, minp.z), math.min(lzmax, maxp.z) do
-- For every Z position involved, add the intercepted X position in the table
table.insert(bounds[z], a*z+b)
end end
end end
i1 = i2 i1 = i2
end end
for x=xmin, xmax do
-- Now sort the bounds list
local xlist = bounds[x]
table.sort(xlist)
local c = math.floor(#xlist/2)
for l=1, c do
-- Take pairs of Z coordinates: all positions between them belong to the polygon.
local zmin = math.max(math.floor(xlist[l*2-1])+1, minp.z)
local zmax = math.min(math.floor(xlist[l*2]), maxp.z)
local i = (x-minp.x) * chulens + (zmin-minp.z) + 1
for z=zmin, zmax do for z=zmin, zmax do
-- Now sort the bounds list
local zlist = bounds[z]
table.sort(zlist)
local c = math.floor(#zlist/2)
for l=1, c do
-- Take pairs of X coordinates: all positions between them belong to the polygon.
local xmin = math.max(math.floor(zlist[l*2-1])+1, minp.x)
local xmax = math.min(math.floor(zlist[l*2]), maxp.x)
local i = (z-minp.z) * chulens + (xmin-minp.x) + 1
for x=xmin, xmax do
-- Fill the map at these places -- Fill the map at these places
polygons[i] = polygon polygons[i] = polygon
i = i + 1 i = i + 1