X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=blobdiff_plain;ds=sidebyside;f=Textures.lua;h=ccc7dbcb453f24d2f324fda17a31b6a003f42263;hb=16d14ce7ffc1e70269fda8d375f1da11c3907af8;hp=2b2e70151f24eaec9150152a01ebc27792ccab5c;hpb=035227716eaa7bd36dad6c33a6e3addda84cc4d7;p=raymarcher.git diff --git a/Textures.lua b/Textures.lua index 2b2e701..ccc7dbc 100644 --- a/Textures.lua +++ b/Textures.lua @@ -63,16 +63,63 @@ local function make_solid_pigment(colour) end end -local function make_checkered_pigment(colour1, colour2) +local function make_checkered_pigment(pigment1, pigment2) 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 + return pigment1(x,y) else - return colour2 + return pigment2(x,y) end end end +local function make_image_pigment(filename,scale) + + print("Loading image pigmant from '" .. filename .. "'...") + + local f = assert(io.open(filename, "rb")) + local data = f:read("*all") + f:close() + + local start,_,wstr,hstr,dstr,raster = + string.find(data, "^P6[%s%c]+(%d+)[%s%c]+(%d+)[%s%c]+(%d+)[%s%c](.*)$") + + if start ~= 1 then + error("Error reading image. Is it in PPM(P6) format?") + end + + if dstr ~= "255" then + error("Only images with a depth of 255 are supported.") + end + + local width = tonumber(wstr) + local height = tonumber(hstr) + local depth = tonumber(dstr) + + local pixels = {} + local i = 1 + for triple in string.gmatch(raster, "...") do + local _,_,r,g,b = string.find(triple, "(.)(.)(.)") + pixels[i] = {string.byte(r)/depth, string.byte(g)/depth, string.byte(b)/depth} + i = i + 1 + end + + print("done.") + print("(Read " .. tostring(#pixels) .. " pixels.)") + + local pixels_per_unit = math.max(width,height) + + return function(x,y) + local xi = math.floor(((x+0.5)*pixels_per_unit/scale)%width) + 1 + local yi = math.floor(((y+0.5)*pixels_per_unit/scale)%height) + 1 + local i = width*(yi-1) + xi + if pixels[i] == nil then + error("Pixel not found at xi=" .. tostring(xi) .. " yi=" .. tostring(yi)) + end + return pixels[i] + end +end + local function make_mandelbrot_pigment(set_pigment, nonset_pigment, max_iter) local function get_col(x,y,cx,cy,iter)