forked from nineties/raytracer
/
raytracer.rb
286 lines (261 loc) · 7.74 KB
/
raytracer.rb
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# raytracer -
# $Id: raytracer.rb 2010-08-18 20:48:53 nineties $
class Vector
def initialize(x, y, z)
@x = x
@y = y
@z = z
end
def length
Math.sqrt(@x * @x + @y * @y + @z * @z)
end
def normalize
l = 1 / length()
@x *= l
@y *= l
@z *= l
self
end
def + (v)
Vector.new(@x + v.x, @y + v.y, @z + v.z)
end
def - (v)
Vector.new(@x - v.x, @y - v.y, @z - v.z)
end
def * (t)
if t.kind_of?(Vector) then
Vector.new(@x * t.x, @y * t.y, @z * t.z)
else
Vector.new(@x * t, @y * t, @z * t)
end
end
attr_accessor :x, :y, :z
end
def iprod(u, v)
u.x * v.x + u.y * v.y + u.z * v.z
end
Color = Vector
# objects
class Material
def initialize(color, diffuse = 0.0, reflection = 0.0, refraction = 0.0, specular = -1.0)
@color = color
@diffuse = diffuse
@reflection = reflection
@refraction = refraction
@specular = specular
@specular = 1.0 - @diffuse if specular < 0
end
attr_accessor :color, :diffuse, :reflection, :refraction, :specular
end
class Ray
def initialize(origin, direction, distance = Float::MAX)
@origin = origin
@direction = direction.normalize
@distance = distance
@inside = false
@hit = nil
end
def terminal
@origin + @direction * @distance
end
attr_accessor :origin, :direction, :distance, :inside, :hit
end
class Primitive
attr_accessor :material
end
class Sphere < Primitive
def initialize(mat, center, radius)
@material = mat
@center = center
@radius = radius
end
def intersect(ray)
c = @center - ray.origin
cd = iprod(c, ray.direction)
det = @radius * @radius - iprod(c, c) + cd * cd
if det > 0 then
det = Math.sqrt(det)
d1 = cd + det
d2 = cd - det
return if d1 < 0
if d2 < 0 then
if d1 < ray.distance then
ray.distance = d1
ray.hit = self
ray.inside = true
end
else
if d2 < ray.distance then
ray.distance = d2
ray.hit = self
ray.inside = false
end
end
end
end
def normal(point)
n = point - @center
n.normalize
end
attr_reader :center, :radius
end
class Plane < Primitive
def initialize(mat, normal, point)
@material = mat
@normal = normal.normalize
@const = iprod(normal, point)
end
def intersect(ray)
d = iprod(@normal, ray.direction)
if d != 0 then
l = (@const - iprod(ray.origin, @normal))/d
return nil if l < 0
if l < ray.distance then
ray.distance = l
ray.hit = self
ray.inside = false
end
end
end
def normal(point)
@normal
end
end
class Light < Sphere
def initialize(color, center, radius)
super(Material.new(color), center, radius)
end
end
class Raytracer
def initialize
@window_width = 800
@window_height = 600
@view_width = 7.0
@view_height = 5.25
@origin = Vector.new(0.0, 0.0, -5.0)
@primitives = []
end
attr_accessor :window_width, :window_height, :view_width, :view_height
attr_accessor :origin
def setup_mapping
@dx = @view_width / @window_width
@dy = @view_height / @window_height
@left = -@view_width/2
@top = @view_height/2
end
def << (prim)
@primitives << prim
end
def render
setup_mapping
print "P3\n#{@window_width} #{@window_height}\n255\n"
y = @top
@window_height.times do
x = @left
@window_width.times do
dir = Vector.new(x, y, 0.0) - @origin
ray = Ray.new(@origin, dir)
color = trace(ray, 1, 1.0)
if color then
r = (color.x * 255).to_i
g = (color.y * 255).to_i
b = (color.z * 255).to_i
r = 255 if r > 255
g = 255 if g > 255
b = 255 if b > 255
print "#{r} #{g} #{b} "
else
print "0 0 0 "
end
x += @dx
end
print "\n"
y -= @dy
end
end
DEPTH_MAX = 6
EPSILON = 0.0001
def trace(ray, depth, cur_refr)
return nil if depth > DEPTH_MAX
ray.hit = nil
@primitives.each do |prim|
prim.intersect(ray)
end
return nil unless ray.hit
prim = ray.hit
if prim.kind_of?(Light) then
return Color.new(1.0, 1.0, 1.0)
else
color = Color.new(0.0, 0.0, 0.0)
# interaction point
ip = ray.terminal
# handle lights
@primitives.each do |light|
next unless light.kind_of?(Light)
l = light.center - ip
r = Ray.new(ip + l * EPSILON, l, l.length)
shade = 1.0
@primitives.each do |prim|
next if prim == light
prim.intersect(r)
if r.hit then
shade = 0.0
break
end
end
n = prim.normal(ip)
l.normalize
# diffuse shading
if prim.material.diffuse > 0 then
d = iprod(n, l)
if d > 0 then
diffuse = d * prim.material.diffuse * shade
color += prim.material.color * light.material.color * diffuse
end
end
# specular
if prim.material.specular > 0 then
r = l - n * 2.0 * iprod(l, n)
d = iprod(ray.direction, r)
if d > 0 then
specular = d**20 * prim.material.specular * shade
color += light.material.color * specular
end
end
end
# reflection
refl = prim.material.reflection
if refl > 0 then
n = prim.normal(ip)
r = ray.direction - n * 2.0 * iprod(ray.direction, n)
c = trace(Ray.new(ip + r * EPSILON, r), depth + 1, cur_refr)
color += c * prim.material.color * refl if c
end
# refraction
refr = prim.material.refraction
if refr > 0 then
n = cur_refr / refr
norm = prim.normal(ip)
norm *= -1 if ray.inside
cosI = iprod(norm, ray.direction)
cosT2 = 1.0 - n*n*(1.0 - cosI * cosI)
if cosT2 > 0 then
t = ray.direction * n + norm * (n * cosI - Math.sqrt(cosT2))
ray = Ray.new(ip + t * EPSILON, t)
c = trace(ray, depth + 1, refr)
if c then
# Beer's law
absorbance = prim.material.color * 0.15 * (-ray.distance)
transparency = Color.new(
Math.exp(absorbance.x),
Math.exp(absorbance.y),
Math.exp(absorbance.z)
)
color += transparency * c
end
end
end
color
end
end
end