-
Notifications
You must be signed in to change notification settings - Fork 0
/
entry.py
215 lines (134 loc) · 5.99 KB
/
entry.py
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import sys
import vector
import hittable
import material
import util
import math
import camera
import random
def random_scene(seed):
random.seed(seed)
world = hittable.HittableList()
world.add(hittable.Sphere(vector.Vec3(0, -1000, 0), 1000, material.Lambertian(vector.Vec3(0.5, 0.5, 0.5))))
hitList = []
for a in range(-11, 11):
for b in range(-11, 11):
choose_mat = util.random_double()
center = vector.Vec3(a + 0.9*util.random_double(), 0.2, b + 0.9*util.random_double())
if (center - vector.Vec3(4, 0.2, 0)).length() > 0.9:
if choose_mat < 0.8:
# diffuse
albedo = vector.random() * vector.random()
# world.add(hittable.Sphere(center, 0.2, material.Lambertian(albedo)))
center2 = center + vector.Vec3(0, util.random_double_range(0,.5), 0)
hitList.append(hittable.MovingSphere(center, center2, 0.0, 1.0, 0.2, material.Lambertian(albedo)))
elif choose_mat < 0.95:
# metal
albedo = vector.random_in_range(.5, 1)
fuzz = util.random_double_range(0, .5)
hitList.append(hittable.Sphere(center, 0.2, material.Metal(albedo, fuzz)))
else:
# glass
hitList.append(hittable.Sphere(center, 0.2, material.Dielectric(1.5)))
world.add(hittable.bvh_node(hitList, 0,1, 0, 1))
world.add(hittable.Sphere(vector.Vec3(0, 1, 0), 1.0, material.Dielectric(1.5)))
world.add(hittable.Sphere(vector.Vec3(-4, 1, 0), 1.0, material.Lambertian(vector.Vec3(.4, .2, .1))))
world.add(hittable.Sphere(vector.Vec3(4, 1, 0), 1.0, material.Metal(vector.Vec3(.7, .6, .5), 0.0)))
return world
def test_scene(seed):
random.seed(seed)
world = hittable.HittableList()
R = math.cos(util.PI/4)
hit_list = []
world.add(hittable.Sphere(vector.Vec3(0, -1000, 0), 1000, material.Lambertian(vector.Vec3(0.6, 0.6, 0.6))))
for _ in range(200):
ranx = util.random_double_range(-10,10)
ranz = util.random_double_range(-10,10)
sp1 = hittable.Sphere(vector.Vec3(ranx, 0.3, ranz), 0.3, material.Lambertian(vector.Vec3(.4, .2, .1)))
hit_list.append(sp1)
# world.add(hittable.Sphere(vector.Vec3(1, 0, -1), 0.5, material.Metal(vector.Vec3(.8, .6, .2), 0.0)))
#
bvh = hittable.bvh_node(hit_list,0,1)
world.add(bvh)
return world
def test_scene2(seed):
random.seed(seed)
world = hittable.HittableList()
R = math.cos(util.PI/4)
hit_list = []
world.add(hittable.Sphere(vector.Vec3(0, -1000, 0), 1000, material.Lambertian(vector.Vec3(0.6, 0.6, 0.6))))
sp1 = hittable.Sphere(vector.Vec3(-0.7, 0.5, 0), 0.5, material.Lambertian(vector.Vec3(.4, .2, .81)))
hit_list.append(sp1)
sp2 = hittable.Sphere(vector.Vec3(0.0, 0.5, 0), 0.5, material.Lambertian(vector.Vec3(.4, .2, .81)))
hit_list.append(sp2)
sp3 = hittable.Sphere(vector.Vec3(0.7, 0.5, 0), 0.5, material.Lambertian(vector.Vec3(.4, .2, .81)))
hit_list.append(sp3)
# world.add(hittable.Sphere(vector.Vec3(1, 0, -1), 0.5, material.Metal(vector.Vec3(.8, .6, .2), 0.0)))
#
bvh = hittable.bvh_node(hit_list,0,1)
world.add(bvh)
return world
'''
Writes pixel color to the output file
'''
def write_color(outfile, pixel_color: vector.Vec3, samples_per_pixel: int):
r = pixel_color.x
g = pixel_color.y
b = pixel_color.z
scale = 1.0 / samples_per_pixel
r = math.sqrt(r * scale)
g = math.sqrt(g * scale)
b = math.sqrt(b * scale)
outfile.write("" + str(int(255.999 * util.clamp(r, 0.0, 0.999))) + " " + str(int(255.999 * util.clamp(g, 0.0, 0.999))) + " " + str(int(255.999 * util.clamp(b, 0.0, 0.999))))
outfile.write("\n")
def ray_color(ray: vector.Ray, world: hittable.Hittable, depth):
if depth <= 0:
return vector.Vec3(0.0, 0.0, 0.0)
did_hit, hit_rec = world.hit(ray, 0.001, util.INFINITY)
if did_hit:
tempmaterial = hit_rec.material
didscatter, albedo, scattered = tempmaterial.scatter(ray, hit_rec)
if didscatter:
return albedo * ray_color(scattered, world, depth-1)
return vector.Vec3(0, 0, 0)
unit_dir = vector.unit_vector(ray.direction)
t = (unit_dir.y + 1.0)*0.5
return vector.Vec3(1.0, 1.0, 1.0).times(1.0-t) + vector.Vec3(0.5, 0.7, 1.0).times(t)
def entry(filename, instanceid):
aspect_ratio = 16.0 / 9.0
# image_width = 384
image_width = 800
image_height = int(image_width / aspect_ratio)
samples_per_pixel = 60
max_depth = 50
lookfrom = vector.Vec3(0, 1.5, -8)
lookat = vector.Vec3(0, 0.5, 0)
dist_to_focus = 8.0
aperture = 0.1
vup = vector.Vec3(0, 1, 0)
cam = camera.Camera(lookfrom, lookat, vup, 20, aspect_ratio, aperture, dist_to_focus,0.0, 1.0)
world = test_scene2(-1)
random.seed(instanceid)
outfile = open(filename, mode='w', encoding='utf-8', buffering=1)
outfile.write("P3\n")
outfile.write("" + str(image_width) + " " + str(image_height) + "\n")
outfile.write("255\n")
for j in range(image_height-1, -1, -1):
#print(sys.stdout.encoding,file=sys.stderr)
if instanceid == 0:
sys.stderr.write("\rScanlines remaining: " + str(j) + " ")
for i in range(0, image_width, 1):
pixel_color = vector.Vec3(0.0, 0.0, 0.0)
for _ in range(samples_per_pixel):
u1 = (i + util.random_double()) / (image_width-1)
v1 = (j + util.random_double()) / (image_height-1)
r = cam.get_ray(u1, v1)
pixel_color = pixel_color + ray_color(r, world, max_depth)
write_color(outfile,pixel_color, samples_per_pixel)
outfile.flush()
outfile.close()
sys.stderr.write("\nDone (instance: " + str(instanceid) + ")")
if __name__ == "__main__":
# execute only if run as a script
# for debugging
entry("temp", 1)