Lunatic: generalize shadexfog.create_trans() further.

It now accepts a number of tables to create as 3rd arg. The function that
create_trans() takes as the 2nd argument is now being passed
(r,g,b, R,G,B, tablei, numtables).
Rewrite create_additive_trans() in terms of it.
Rename create_128_trans() -> create_alpha_trans().

git-svn-id: https://svn.eduke32.com/eduke32@4347 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
helixhorned 2014-02-22 19:38:48 +00:00
parent 1b1cb73e1c
commit b02aa43036

View file

@ -7,7 +7,11 @@
In EDuke32, simply pass this module at the command line. In EDuke32, simply pass this module at the command line.
--]] --]]
local assert = assert
local error = error local error = error
local print = print
local printf = printf
local type = type
local bit = require("bit") local bit = require("bit")
local math = require("math") local math = require("math")
@ -300,69 +304,77 @@ end
---------- Blending table tests ---------- ---------- Blending table tests ----------
-- shadexfog.create_128_trans(startblendidx [, numtabs]) -- shadexfog.create_trans(startblendidx, func [, numtables [, fullbrightsOK]])
-- --
-- Creates <numtabs> blending tables of smooth alpha translucency, with -- <func>: must be
-- fractions 1/(2*numtabs), 2/(2*numtabs) ... numtabs/(2*numtabs). -- rr, gg, bb = f(r, g, b, R, G, B, level, numtables)
-- <numtabs> must be a power of two in [2 .. 128]. -- ('level' is the table index, from 1 to <numtables>)
function shadexfog.create_128_trans(startblendidx, numtabs) -- <numtables>: number of tables to create, from <startblendidx> on. Default: 1
if (numtabs == nil) then function shadexfog.create_trans(startblendidx, func, numtables, fullbrightsOK)
numtabs = 128 numtables = numtables or 1
else local lastokcol = fullbrightsOK and 255 or 255-16
if (type(numtabs) ~= "number" or not (numtabs >= 2 and numtabs <= 128)) then
error("invalid argument #2: must be a number in [2 .. 128]", 2)
end
if (bit.band(numtabs, numtabs-1) ~= 0) then local tab = engine.blendtab()
error("invalid argument #2: must be a power of two", 2)
end
end
for alpha=1,numtabs do
local f = alpha/(2*numtabs)
local F = 1-f
local tab = engine.blendtab()
for level=1,numtables do
for i=0,255 do for i=0,255 do
local r,g,b = engine.getrgb(i) local r,g,b = engine.getrgb(i)
for j=0,255 do for j=0,255 do
local R,G,B = engine.getrgb(j) local R,G,B = engine.getrgb(j)
tab[i][j] = engine.nearcolor(f*r+F*R, f*g+F*G, f*b+F*B) local rr, gg, bb = func(r,g,b, R,G,B, level, numtables)
tab[i][j] = engine.nearcolor(rr, gg, bb, lastokcol)
end end
end end
engine.setblendtab(startblendidx + alpha-1, tab) engine.setblendtab(startblendidx + level-1, tab)
end end
end end
-- shadexfog.create_trans(blendidx, func [, fullbrightsOK]) local function check_numtables(numtables)
if (numtables ~= nil) then
if (type(numtables) ~= "number" or not (numtables >= 1 and numtables <= 128)) then
error("invalid argument #2: must be a number in [1 .. 128]", 2)
end
if (bit.band(numtables, numtables-1) ~= 0) then
error("invalid argument #2: must be a power of two", 2)
end
end
end
-- shadexfog.create_alpha_trans(startblendidx [, numtables [, fullbrightsOK]])
-- --
-- <func>: must be -- Creates <numtables> blending tables of smooth alpha translucency, with
-- rr, gg, bb = f(r, g, b, R, G, B) -- fractions 1/(2*numtables), 2/(2*numtables) ... numtables/(2*numtables).
function shadexfog.create_trans(blendidx, func, fullbrightsOK) -- <numtables> must be a power of two in [1 .. 128].
local tab = engine.blendtab() function shadexfog.create_alpha_trans(startblendidx, numtables, fullbrightsOK)
check_numtables(numtables)
for i=0,255 do shadexfog.create_trans(
local r,g,b = engine.getrgb(i) startblendidx,
for j=0,255 do
local R,G,B = engine.getrgb(j)
local rr, gg, bb = func(r, g, b, R, G, B) function(r,g,b, R,G,B, alpha, numtabs)
tab[i][j] = engine.nearcolor(rr, gg, bb, fullbrightsOK and 255 or 255-16) local f = alpha/(2*numtabs)
end local F = 1-f
end return f*r+F*R, f*g+F*G, f*b+F*B
end,
engine.setblendtab(blendidx, tab) numtables, fullbrightsOK
)
end end
function shadexfog.create_additive_trans(blendidx) -- shadexfog.create_additive_trans(startblendidx [, numtables [, fullbrightsOK]])
function shadexfog.create_additive_trans(startblendidx, numtables, fullbrightsOK)
shadexfog.create_trans( shadexfog.create_trans(
blendidx, startblendidx,
function(r,g,b,R,G,B)
return min(r+R, 63), min(g+G, 63), min(b+B, 63) function(r,g,b, R,G,B, level, numtabs)
end local f = level/numtabs
return min(f*r+R, 63), min(f*g+G, 63), min(f*b+B, 63)
end,
numtables, fullbrightsOK
) )
end end