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