From 1d72ec6163d1843edc9b63e60c684b2619e2f326 Mon Sep 17 00:00:00 2001 From: Miles Bader Date: Tue, 18 Mar 2008 09:30:15 +0900 Subject: [PATCH] Add new ArithTex operations FLOOR, CEIL, TRUNC, SIN, COS, TAN, ATAN2 --- arith-tex.h | 5 +++- arith-tex.tcc | 17 +++++++++++++ snogray.lua | 66 +++++++++++++++++++++++---------------------------- 3 files changed, 51 insertions(+), 37 deletions(-) diff --git a/arith-tex.h b/arith-tex.h index 87ff8a72..420a1111 100644 --- a/arith-tex.h +++ b/arith-tex.h @@ -29,8 +29,11 @@ class ArithTex : public Tex enum Op { ADD, SUB, MUL, DIV, MOD, POW, + FLOOR, CEIL, TRUNC, // floor/ceil/trunc (X / Y) * Y MIN, MAX, AVG, - MIRROR, // abs (x - y) + MIRROR, // abs (X - Y) + SIN, COS, TAN, // sin/cos/tan (X * 2 * PI / Y) + ATAN2 }; ArithTex (Op _op, const TexVal &_arg1, const TexVal &_arg2) diff --git a/arith-tex.tcc b/arith-tex.tcc index 4d747f58..18d9ec44 100644 --- a/arith-tex.tcc +++ b/arith-tex.tcc @@ -16,6 +16,7 @@ #include "config.h" #include "color.h" +#include "color-math.h" namespace snogray { @@ -44,6 +45,12 @@ ArithTex::eval (const TexCoords &tex_coords) const return fmod (val1, val2); case POW: return pow (val1, val2); + case FLOOR: + return floor (val1 / val2) * val2; + case CEIL: + return ceil (val1 / val2) * val2; + case TRUNC: + return trunc (val1 / val2) * val2; case MIN: return min (val1, val2); case MAX: @@ -52,6 +59,16 @@ ArithTex::eval (const TexCoords &tex_coords) const return (val1 + val2) / 2; case MIRROR: return abs (val1 - val2); + case SIN: + return sin (val1 * 2 * PI / val2); + case COS: + return cos (val1 * 2 * PI / val2); + case TAN: + return tan (val1 * 2 * PI / val2); + case ATAN2: + return atan2 (val1, val2); + default: + return val1; // no meaning, just cheap }; } diff --git a/snogray.lua b/snogray.lua index 9ec14809..97b1205a 100644 --- a/snogray.lua +++ b/snogray.lua @@ -982,8 +982,11 @@ rot_tex = rotate_tex -- local arith_tex_ops = { ADD = 0, SUB = 1, MUL = 2, DIV = 3, MOD = 4, POW = 5, - MIN = 6, MAX = 7, AVG = 8, - MIRROR = 9, -- abs (x - y) + FLOOR = 6, CEIL = 7, TRUNC = 8, -- floor/ceil/trunc (X / Y) * Y + MIN = 9, MAX = 10, AVG = 11, + MIRROR = 12, -- abs (X - Y) + SIN = 13, COS = 14, TAN = 15, -- sin/cos/tan (X * 2 * PI / Y) + ATAN2 = 16 } -- Return a texture which performs operation OP on input textures ARG1 @@ -996,14 +999,11 @@ function arith_tex (op, arg1, arg2) return raw.arith_tex (op, tex_vals (arg1, arg2)) end --- Convenient aliases for the various arith_tex operations. +-- Alias for the arith_tex MUL operation. This function treats the +-- second operand specially because it is used to overload the "*" +-- operator for textures, which we want to work for texture-xform +-- operations too. -- -function add_tex (tex1, tex2) - return arith_tex ('ADD', tex1, tex2) -end -function sub_tex (tex1, tex2) - return arith_tex ('SUB', tex1, tex2) -end function mul_tex (tex1, tex2_or_xform) if is_xform (tex2_or_xform) then return xform_tex (tex2_or_xform, tex1) @@ -1011,33 +1011,27 @@ function mul_tex (tex1, tex2_or_xform) return arith_tex ('MUL', tex1, tex2_or_xform) end end -function div_tex (tex1, tex2) - return arith_tex ('DIV', tex1, tex2) -end -function mod_tex (tex1, tex2) - return arith_tex ('MOD', tex1, tex2) -end -function pow_tex (tex1, tex2) - return arith_tex ('POW', tex1, tex2) -end -function min_tex (tex1, tex2) - return arith_tex ('MIN', tex1, tex2) -end -function max_tex (tex1, tex2) - return arith_tex ('MAX', tex1, tex2) -end -function avg_tex (tex1, tex2) - return arith_tex ('AVG', tex1, tex2) -end -function mirror_tex (tex1, tex2) - return arith_tex ('MIRROR', tex1, tex2) -end -function abs_tex (tex) - return arith_tex ('MIRROR', tex, 0) -end -function neg_tex (tex) - return arith_tex ('SUB', 0, tex) -end + +-- Convenient aliases for the various other arith_tex operations. +-- +function add_tex (...) return arith_tex ('ADD', ...) end +function sub_tex (...) return arith_tex ('SUB', ...) end +function div_tex (...) return arith_tex ('DIV', ...) end +function mod_tex (...) return arith_tex ('MOD', ...) end +function pow_tex (...) return arith_tex ('POW', ...) end +function floor_tex (x, y) return arith_tex ('FLOOR', x, y or 1) end +function ceil_tex (x, y) return arith_tex ('CEIL', x, y or 1) end +function trunc_tex (x, y) return arith_tex ('TRUNC', x, y or 1) end +function min_tex (...) return arith_tex ('MIN', ...) end +function max_tex (...) return arith_tex ('MAX', ...) end +function avg_tex (...) return arith_tex ('AVG', ...) end +function mirror_tex (...) return arith_tex ('MIRROR', ...) end +function abs_tex (tex) return arith_tex ('MIRROR', tex, 0) end +function neg_tex (tex) return arith_tex ('SUB', 0, tex) end +function sin_tex (x, y) return arith_tex ('SIN', x, y or 2*math.pi) end +function cos_tex (x, y) return arith_tex ('COS', x, y or 2*math.pi) end +function tan_tex (x, y) return arith_tex ('TAN', x, y or 2*math.pi) end +function atan2_tex (...) return arith_tex ('ATAN2', ...) end -- Install operator overloads for the texture metatable MT. --