From 06059eb522f33574680bb75bb73f28e2b0f6e836 Mon Sep 17 00:00:00 2001 From: Tim Vaughan Date: Mon, 20 Mar 2023 11:40:50 +0100 Subject: [PATCH] Colours are now Vectors, added rainbow pigment. --- Colours.lua | 15 +++++++++------ Primitives.lua | 3 ++- Textures.lua | 16 ++++++++++++++++ mandelbulb.lua | 6 ++++-- rainbow_wall.lua | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 rainbow_wall.lua diff --git a/Colours.lua b/Colours.lua index 8a71cbe..eec5bb5 100644 --- a/Colours.lua +++ b/Colours.lua @@ -1,12 +1,15 @@ --- Some pre-defined colours +require "Vector" +local V = Vector + Colours = { - red = {1,0,0}, - green = {0,1,0}, - blue = {0,0,1}, - yellow = {1,1,0}, - magenta = {1,0,1}, - cyan = {0,1,1} + red = V.new{1,0,0}, + green = V.new{0,1,0}, + blue = V.new{0,0,1}, + yellow = V.new{1,1,0}, + magenta = V.new{1,0,1}, + cyan = V.new{0,1,1} } return Colours diff --git a/Primitives.lua b/Primitives.lua index b1efbfb..1648392 100644 --- a/Primitives.lua +++ b/Primitives.lua @@ -63,7 +63,8 @@ end Primitives = { make_sphere = make_sphere, make_plane = make_plane, - make_pipe = make_pipe + make_pipe = make_pipe, + make_mandelbulb = make_mandelbulb } return Primitives diff --git a/Textures.lua b/Textures.lua index ad805a6..324a2d2 100644 --- a/Textures.lua +++ b/Textures.lua @@ -3,6 +3,9 @@ require "Vector" local V = Vector +require "Colours" +local C = Colours + local function make_phong_texture(lights, mapped_pigment, amb, diff, spec, shiny) local normalize = V.normalize @@ -140,6 +143,18 @@ local function make_mandelbrot_pigment(set_pigment, nonset_pigment, max_iter) end end +local function make_rainbow_pigment(scale) + return function (x,y) + + local Ired = math.sin(x/math.pi/scale)^2 * math.sin(y/math.pi/scale)^2 + local Igreen = math.cos(x/math.pi/scale)^2 * math.sin(y/math.pi/scale)^2 + local Iblue = math.sin(x/math.pi/scale)^2 * math.cos(y/math.pi/scale)^2 + + return C.red*Ired + C.green*Igreen + C.blue*Iblue + end +end + + -- Mapping functions -- These functions define mappings from 3D world coordinates to 2D @@ -170,6 +185,7 @@ Textures = { make_solid_pigment = make_solid_pigment, make_checkered_pigment = make_checkered_pigment, make_image_pigment = make_image_pigment, + make_rainbow_pigment = make_rainbow_pigment, make_mandelbrot_pigment = make_mandelbrot_pigment, map_rectangular = map_rectangular, map_spherical = map_spherical diff --git a/mandelbulb.lua b/mandelbulb.lua index 7d7a011..f39c67b 100644 --- a/mandelbulb.lua +++ b/mandelbulb.lua @@ -18,7 +18,8 @@ local scene = { sdf = P.make_mandelbulb(V.origin, 100, 8, T.make_phong_texture( {V.new{3,-5,3}}, - T.make_solid_pigment{0,0.5,1.0}, + -- T.make_solid_pigment{0,0.5,1.0}, + T.map_spherical(T.make_rainbow_pigment(0.1), V.origin, 1, 1), 0.1, 1.0, 0.0, 30)), camera = {location = V.new{0,-5,0}, @@ -28,4 +29,5 @@ local scene = { Render.eps = 0.001 Render.bg_col = {0,0,0} -Render.render(scene, 2560, 1440, "out/mandelbulb.ppm") +Render.render(scene, 2560, 1440, "out/mandelbulb_rainbow.ppm") +-- Render.render(scene, 2560, 1440, "out/mandelbulb_rainbow.ppm") diff --git a/rainbow_wall.lua b/rainbow_wall.lua new file mode 100644 index 0000000..ad84b68 --- /dev/null +++ b/rainbow_wall.lua @@ -0,0 +1,34 @@ +require "Vector" +local V = Vector + +require "Primitives" +local P = Primitives + +require "Operations" +local O = Operations + +require "Textures" +local T = Textures + +require "Render" + +local lights = {V.new{1,-1,1}} + +local plane_normal = V.normalize(V.new{0.0,-1,0.0}) +local texture_y = V.normalize(V.cross(plane_normal,V.x)) +local texture_x = V.cross(texture_y,plane_normal) + +local scene = { + sdf = P.make_plane(V.new{0,0,0}, plane_normal, + T.make_phong_texture(lights, + T.map_rectangular( + T.make_rainbow_pigment(0.1), + texture_x, texture_y), + 0.2, 1.0, 0, 1)), + + camera = {location = V.new{-0.5,-5,0}, + point_at = V.new{-0.5,0,0}, + right = V.x, + fov = 1}} + +Render.render(scene, 320, 200, "out/rainbow_wall.ppm") -- 2.20.1