-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr.py
176 lines (110 loc) · 4.3 KB
/
r.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
import math
from Core import RenderEngine
world = World(320, 200)
world.add_objects(Sphere(center=Vec3(), position=Vec3()))
world.add_objects(Sphere(center=Vec3(), position=Vec3()))
world.add_objects(Sphere(center=Vec3(), position=Vec3()))
world.add_objects(PointLight(center=Vec3(), position=Vec3()))
world.add_objects(AmbientLight(center=Vec3(), position=Vec3()))
world.add_objects(DirectionalLight(center=Vec3(), position=Vec3()))
renderer = RenderEngine(world)
renderer.render('demoscene.ppm')
def compute_light(P, N, V, s):
i = 0.0
for light in scene['lights']:
if light['type'] == 'ambient':
i += light['intensity']
else:
if light['type'] == 'point':
L = light['position'] - P
if light['type'] == 'directional':
L = light['direction']
n_dot_l = dot(N,L)
if n_dot_l > 0:
i += light['intensity']*n_dot_l/(N.mag()*L.mag())
# specularity calculation
if s != -1:
R = 2*N*dot(N,L) - L
r_dot_v = dot(R,V)
if r_dot_v > 0 :
i += light['intensity']*( (r_dot_v / (R.mag()*V.mag()) )**s)
pass
return i
def trace_ray(O, D, t_min, t_max):
closest_t = math.inf
closest_sphere = None
for sphere in scene['spheres']:
t1, t2 = ray_sphere_intersect(O, D, sphere)
if (t_min < t1 < t_max) and t1 < closest_t:
closest_t = t1
closest_sphere = sphere
if (t_min < t2 < t_max) and t2 < closest_t:
closest_t = t2
closest_sphere = sphere
if closest_sphere == None:
return Vec3(173/255, 216/255, 230/255)
else:
P = Vec3(closest_t*D.x , closest_t*D.y, closest_t*D.z)
N = P - closest_sphere['center']
N = N / N.mag()
return closest_sphere['color']*compute_light(P, N, -1*D, sphere['specular'])
def dot(v1, v2):
return v1.x* v2.x + v1.y * v2.y + v1.z * v2.z
def ray_sphere_intersect(O, D, sphere):
C = sphere['center']
r = sphere['radius']
oc = O - C
k1 = dot(D,D)
k2 = 2*dot(oc, D)
k3 = dot(oc, oc) - r*r
discriminant = k2*k2 - 4*k1*k3
if discriminant < 0:
return math.inf, math.inf
t1 = (-k2 + math.sqrt(discriminant))/(2*k1)
t2 = (-k2 - math.sqrt(discriminant))/(2*k1)
return t1, t2
def canvas_to_viewport(x,y):
return Vec3(x*Vw/Cw, y*Vh/Ch, d)
def c_to_i(x, y):
'''canvas to image'''
xnew = xmin + (xmax - xmin)/Cw
ynew = ymin + (ymax - ymin)/Ch
return Vec3(xnew, ynew, d)
# define scene with one sphere
scene = {
'spheres':[
# {'center':Vec3(0, 0 ,0),'radius': 0.3,'color':Vec3(1,0,0)},
# {'center':Vec3(0, 1 ,2),'radius': 0.5,'color':Vec3(0.3,1,1.0)},
# {'center':Vec3(-0.9, 1 ,2),'radius': 0.2,'color':Vec3(0.3,0.3,1.0)},
{'center':Vec3(-1.2 ,0 ,0.9),'radius': 0.6,'color':Vec3(0.7,0.2,0.7), 'specular':1000},
{'center':Vec3(1.2 ,0 ,0.9),'radius': 0.6,'color':Vec3(0.3,1,1.0), 'specular':500},
{'center':Vec3(0 ,1.3 ,0.9),'radius': 0.8,'color':Vec3(0.3,0.3,1.0), 'specular':200},
{'center':Vec3(0 ,5001 ,0.9),'radius': 5000,'color':Vec3(0.1,0.8,0.1), 'specular':10},
],
'lights':[
{'type':'ambient','intensity':0.4,'position':Vec3(0,-0.6,0)},
{'type':'point','intensity':0.5,'position':Vec3(0,-0.3,1.8)},
{'type':'directional','intensity':0.1,'direction':Vec3(-0.5,-0.3,0)}
]
}
if __name__ == '__main__':
WIDTH = 320
HEIGHT = 200
camera = Vec3(0, 0, -1)
aspect_ratio = WIDTH/HEIGHT
xmin = -1
xmax = 1
ymax = 1/aspect_ratio
ymin = -1*ymax
xstep = (xmax - xmin) / (WIDTH-1)
ystep = (ymax - ymin) / (HEIGHT-1)
image = P3Image(WIDTH, HEIGHT)
for j in range(HEIGHT):
y = ymin + j*ystep
for i in range(WIDTH):
x = xmin + i*xstep
ray = Vec3(x,y,0) - camera
color = trace_ray(camera, ray, -1, math.inf)
image.set_pixel(i, j, color)
print(f'rendering block [{i}] [{j}] - [{"{:3.0f}%".format(float(j) / float(HEIGHT) * 100)}] ', end='\r')
image.save('render.ppm')