X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=blobdiff_plain;f=Textures.lua;h=cee3dd9f715d371071ad053acfab3ea386e480cf;hb=46c1311e546ae5cefc4f596ed13476c649f12f66;hp=b1d1cb308dda091f93cf2e59a9009c099eb04013;hpb=c86e8c7637a7368950add912b9bce244bbf9e689;p=raymarcher.git diff --git a/Textures.lua b/Textures.lua index b1d1cb3..cee3dd9 100644 --- a/Textures.lua +++ b/Textures.lua @@ -2,26 +2,61 @@ require "Vector" local V = Vector -local function make_phong(lights, colour, amb, spec, shiny) +local function make_phong_texture(lights, pigment, amb, diff, spec, shiny) + local normalize = V.normalize return function (location, ray_dir, normal, count) - local thiscol = {0, 0, 0} - local reflected_ray = V.normalize(normal*(ray_dir*normal*2) - ray_dir) + local colour = pigment(location[1],location[3]) + local thiscol = {colour[1]*amb, colour[2]*amb, colour[3]*amb} + + local reflected_ray = ray_dir - normal*(ray_dir*normal*2) for _,light in ipairs(lights) do local light_dir = V.normalize(light-location) - local Iamb = math.max(normal*light_dir, 0) + local Idiff = math.max(normal*light_dir, 0) local Ispec = math.pow(math.max(light_dir*reflected_ray,0),shiny) - thiscol[1] = thiscol[1] + colour[1]*Iamb*amb + Ispec*spec - thiscol[2] = thiscol[2] + colour[2]*Iamb*amb + Ispec*spec - thiscol[3] = thiscol[3] + colour[3]*Iamb*amb + Ispec*spec + thiscol[1] = thiscol[1] + colour[1]*Idiff*diff + Ispec*spec + thiscol[2] = thiscol[2] + colour[2]*Idiff*diff + Ispec*spec + thiscol[3] = thiscol[3] + colour[3]*Idiff*diff + Ispec*spec end return thiscol end end + +local function make_count_texture(scale) + return function (location, ray_dir, normal, count) + return {count*scale, count*scale, count*scale} + end +end + +local function make_flat_texture(pigment) + return function (location, ray_dir, normal, count) + return pigment(location[1], location[2]) + end +end + +local function make_solid_pigment(colour) + return function (x,y) + return colour + end +end + +local function make_checkered_pigment(colour1, colour2) + return function (x,y) + if (x%1 < 0.5 and y%1 < 0.5) or (x%1 > 0.5 and y%1 > 0.5) then + return colour1 + else + return colour2 + end + end +end Textures = { - make_phong = make_phong + make_phong_texture = make_phong_texture, + make_flat_texture = make_flat_texture, + make_count_texture = make_count_texture, + make_solid_pigment = make_solid_pigment, + make_checkered_pigment = make_checkered_pigment } return Textures