Lunatic: rewrite shadexfog.create_128_trans to allow creating fewer tables.

It must be a power of two in [2 .. 128]. Rewrite test/sprite_access.con's
liztroop example to account for that.
Also, add function shadexfog.create_trans() which aceepts a function
(r,g,b,R,G,B) -> blended (r,g,b) and rewrite shadexfog.create_additive_trans()
in terms of it.

git-svn-id: https://svn.eduke32.com/eduke32@4333 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
helixhorned 2014-02-16 19:16:01 +00:00
parent e68d5a14b3
commit 37ffae149e
2 changed files with 48 additions and 11 deletions

View file

@ -9,6 +9,7 @@
local error = error
local bit = require("bit")
local math = require("math")
local min, max = math.min, math.max
local floor = math.floor
@ -257,16 +258,33 @@ end
---------- Blending table tests ----------
function shadexfog.create_128_trans(startblendidx)
for alpha=1,128 do
local f = alpha/256
-- shadexfog.create_128_trans(startblendidx [, numtabs])
--
-- 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)
end
if (bit.band(numtabs, numtabs-1) ~= 0) then
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 i=0,255 do
local r,g,b = engine.getrgb(i)
for j=0,255 do
local r,g,b = engine.getrgb(i)
local R,G,B = engine.getrgb(j)
tab[i][j] = engine.nearcolor(f*r+F*R, f*g+F*G, f*b+F*B)
@ -277,21 +295,35 @@ function shadexfog.create_128_trans(startblendidx)
end
end
function shadexfog.create_additive_trans(blendidx)
-- 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(i)
local R,G,B = engine.getrgb(j)
tab[i][j] = engine.nearcolor(min(r+R, 63), min(g+G, 63), min(b+B, 63), 255-16)
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_trans(
blendidx,
function(r,g,b,R,G,B)
return min(r+R, 63), min(g+G, 63), min(b+B, 63)
end
)
end
do
return shadexfog

View file

@ -76,6 +76,10 @@ onevent EVENT_GAME // XXX: better: on spawn + loadactor
}
endevent
define NUMALPHATABS 128
define C1 0 //must be log2(128/NUMALPHATABS)
define C2 257 // must be 2*NUMALPHATABS+1
onevent EVENT_ANIMATESPRITES
setvarvar tmp totalclock
shiftvarl tmp 2
@ -83,6 +87,7 @@ onevent EVENT_ANIMATESPRITES
sin alpha tmp // alpha is now in [-2^14 .. 2^14]
shiftvarr alpha 7 // [-2^7 .. 2^7]
addvar alpha 128 // [0 .. 256]
shiftvarr alpha C1 // [0 .. 2*NUMALPHATABS]
ifvare alpha 0
{
@ -97,11 +102,11 @@ onevent EVENT_ANIMATESPRITES
orvar tmp 2
settspr[THISACTOR].tsprcstat tmp
// Assume blending tables [1 .. 128] are installed, like generated by
// shadexfog.lua:create_128_trans(1).
ifvarg alpha 128
// Assume blending tables [1 .. NUMALPHATABS] are installed, like
// generated by shadexfog.lua:create_128_trans(1, NUMALPHATABS).
ifvarg alpha NUMALPHATABS
{
setvarvar tmp 257
setvarvar tmp C2
subvarvar tmp alpha
setvarvar alpha tmp