-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.lua
167 lines (148 loc) · 5.89 KB
/
main.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
--[[
ray tracing program made by following through "Ray Tracing in One Weekend" by Peter Shirley
https://raytracing.github.io/
license: GPL 3.0. written by: github/return5
Copyright (C) <2020> <return5>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
--]]
local Camera = require "Camera"
local Material = require "Material"
local function addSphere(world,center,diameter,material)
world[#world + 1] = SPHERE:new(center,diameter,material)
end
local function populateWorldGIFs(world)
<<<<<<< HEAD
--addSphere(world,VECTOR:new(-1,0,-1),0.5,DIELECTRIC:new(1.5))
=======
>>>>>>> 245d3820cf0bee1865b5ff6e2ffc99c9873e5df1
addSphere(world,VECTOR:new(0,0,-1),0.5,LAMBERTIAN:new(VECTOR:new(0.1,0.2,0.5)))
addSphere(world,VECTOR:new(0,-100.5,-1),100,LAMBERTIAN:new(VECTOR:new(0.8,0.8,0.0)))
addSphere(world,VECTOR:new(1,0,-1),0.5,METAL:new(VECTOR:new(0.8,0.6,0.2),0.3))
addSphere(world,VECTOR:new(-1,0,-1),0.5,DIELECTRIC:new(1.5))
<<<<<<< HEAD
addSphere(world,VECTOR:new(-1,0,-1),-0.47,DIELECTRIC:new(1.5))
=======
addSphere(world,VECTOR:new(-1,0,-1),-0.45,DIELECTRIC:new(1.5))
>>>>>>> 245d3820cf0bee1865b5ff6e2ffc99c9873e5df1
end
local function populateWorldStatic(world)
addSphere(world,VECTOR:new(0,1,0),1.0,DIELECTRIC:new(1.5))
addSphere(world,VECTOR:new(-4,1,0),1.0,LAMBERTIAN:new(VECTOR:new(0.4,0.2,0.1)))
addSphere(world,VECTOR:new(4,1,0),1.0,METAL:new(VECTOR:new(0.7,0.6,0.5),0.0))
addSphere(world,VECTOR:new(0,-1000,0),1000,LAMBERTIAN:new(VECTOR:new(0.5,0.5,0.5)))
end
local function randScene(world)
local rand = math.random
local randD = randDouble
local vec = VECTOR:new(4,0.2,0)
local randVec = randomVec
for a = -11,11,1 do
for b = -11,11,1 do
local choose_mat = rand()
local material = nil
local center = VECTOR:new( a + 0.9 * rand(), 0.2, b + 0.9 * rand())
if((center - vec):mag() > 0.9) then
if(choose_mat < 0.8) then
local albedo = randVec(0,1) * randVec(0,1)
material = LAMBERTIAN:new(albedo)
elseif(choose_mat < 0.95) then
material = METAL:new(randVec(0.5,1),randD(0,0.5))
else
material = DIELECTRIC:new(1.5)
end
addSphere(world,center,0.2,material)
end
end
end
end
local function makeCamera(lookfrom,lookat,dist)
local vup = VECTOR:new(0,1,0)
local aperture = 0.1
local aspect_ratio = 16.0 / 9.0
return CAMERA:new(lookfrom,lookat,vup,20,aspect_ratio,aperture,dist)
end
local function rayTracePic(camera,world,height,width,file)
local file = io.open(file,"w")
local samples_pixel = 100
local max_depth = 50
local rand = math.random
local color = VECTOR:new(0,0,0)
io.output(file)
io.write("P3\n",width + 1,' ',height + 1,'\n',"255\n")
for j = height,0,-1 do
for i = 0,width,1 do
color[1] = 0
color[2] = 0
color[3] = 0
for s = 0,samples_pixel,1 do
color = color + (camera:getRay((i + rand()) / width,(j + rand()) / height):color(world,max_depth))
end
color:writeColor(samples_pixel)
end
end
file:close()
end
local function flyIn(height,width,world)
local lookat = VECTOR:new(0,0,-1)
local str = tostring
for i=10,-0.3,-0.3 do
rayTracePic(makeCamera(VECTOR:new(0,0,i),lookat,i + 1),world,height,width,"flyin_"..str(10 - i)..".ppm")
end
end
local function dropBall(height,width,world)
local lookat = VECTOR:new(0,0,-1)
local str = tostring
local camera = makeCamera(VECTOR:new(0,0,4),lookat,5)
for i=2.0,-0.3,-0.3 do
world[1].center[2] = i
rayTracePic(camera,world,height,width,"drop_"..str((2.0 - i))..".ppm")
end
end
local function panLeft(height,width,world)
local lookfrom = VECTOR:new(0,0,4)
local str = tostring
local lookat = VECTOR:new(3,0,-1)
for i=3,-2.7,-0.3 do
lookat[1] = i
rayTracePic(makeCamera(lookfrom,lookat,5),world,height,width,"pan_"..str(3 - i)..".ppm")
end
end
--generates a series of images which are then combined into animated GIFs
local function makeGIFs(height,width)
local world = HIT_LIST:new()
populateWorldGIFs(world)
flyIn(height,width,world)
panLeft(height,width,world)
dropBall(height,width,world)
end
--produces the image with lots of tiny balls as seen in Ray Tracing in One Weekend
local function makeStaticScene(height,width)
local camera = makeCamera(VECTOR:new(13,2,3),VECTOR:new(0,0,0),10)
local world = HIT_LIST:new()
randScene(world)
populateWorldStatic(world)
rayTracePic(camera,world,height,width,"sample.ppm")
end
local function main()
math.randomseed(os.time())
local aspect_ratio = 16.0 / 9.0
local width = 384
local height = toInteger((width) / aspect_ratio)
<<<<<<< HEAD
makeGIFs(height - 1,width - 1)
--makeStaticScene(height - 1,width - 1)
=======
--makeGIFs(height - 1,width - 1)
makeStaticScene(height - 1,width - 1)
>>>>>>> 245d3820cf0bee1865b5ff6e2ffc99c9873e5df1
end
main()