-
Notifications
You must be signed in to change notification settings - Fork 2
/
RayTracer.pck.st
632 lines (518 loc) · 16.9 KB
/
RayTracer.pck.st
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
'From Cuis 5.0 of 7 November 2016 [latest update: #3741] on 13 May 2019 at 1:12:41 am'!
'Description Please enter a description for this package'!
!provides: 'RayTracer' 1 4!
!requires: 'Math 3D' 1 21 nil!
SystemOrganization addCategory: #RayTracer!
!classDefinition: #Camera category: #RayTracer!
Object subclass: #Camera
instanceVariableNames: 'location direction up right image imagePoker'
classVariableNames: ''
poolDictionaries: ''
category: 'RayTracer'!
!classDefinition: 'Camera class' category: #RayTracer!
Camera class
instanceVariableNames: ''!
!classDefinition: #CompoundFinish category: #RayTracer!
Object subclass: #CompoundFinish
instanceVariableNames: 'finishes'
classVariableNames: ''
poolDictionaries: ''
category: 'RayTracer'!
!classDefinition: 'CompoundFinish class' category: #RayTracer!
CompoundFinish class
instanceVariableNames: ''!
!classDefinition: #FlatColorPigment category: #RayTracer!
Object subclass: #FlatColorPigment
instanceVariableNames: 'color'
classVariableNames: ''
poolDictionaries: ''
category: 'RayTracer'!
!classDefinition: 'FlatColorPigment class' category: #RayTracer!
FlatColorPigment class
instanceVariableNames: ''!
!classDefinition: #Intersection category: #RayTracer!
Object subclass: #Intersection
instanceVariableNames: 'object ray distance intersectionPoint normal scene'
classVariableNames: ''
poolDictionaries: ''
category: 'RayTracer'!
!classDefinition: 'Intersection class' category: #RayTracer!
Intersection class
instanceVariableNames: ''!
!classDefinition: #LightSource category: #RayTracer!
Object subclass: #LightSource
instanceVariableNames: 'location color'
classVariableNames: ''
poolDictionaries: ''
category: 'RayTracer'!
!classDefinition: 'LightSource class' category: #RayTracer!
LightSource class
instanceVariableNames: ''!
!classDefinition: #PhongLightingModelFinish category: #RayTracer!
Object subclass: #PhongLightingModelFinish
instanceVariableNames: 'ambient diffuse specular specularPower'
classVariableNames: ''
poolDictionaries: ''
category: 'RayTracer'!
!classDefinition: 'PhongLightingModelFinish class' category: #RayTracer!
PhongLightingModelFinish class
instanceVariableNames: ''!
!classDefinition: #Ray category: #RayTracer!
Object subclass: #Ray
instanceVariableNames: 'origin direction directionLengthSquared'
classVariableNames: ''
poolDictionaries: ''
category: 'RayTracer'!
!classDefinition: 'Ray class' category: #RayTracer!
Ray class
instanceVariableNames: ''!
!classDefinition: #RayTraceObject category: #RayTracer!
Object subclass: #RayTraceObject
instanceVariableNames: 'texture'
classVariableNames: ''
poolDictionaries: ''
category: 'RayTracer'!
!classDefinition: 'RayTraceObject class' category: #RayTracer!
RayTraceObject class
instanceVariableNames: ''!
!classDefinition: #ReflectionFinish category: #RayTracer!
Object subclass: #ReflectionFinish
instanceVariableNames: 'reflection'
classVariableNames: ''
poolDictionaries: ''
category: 'RayTracer'!
!classDefinition: 'ReflectionFinish class' category: #RayTracer!
ReflectionFinish class
instanceVariableNames: ''!
!classDefinition: #Scene category: #RayTracer!
Object subclass: #Scene
instanceVariableNames: 'objects lightSources camera'
classVariableNames: ''
poolDictionaries: ''
category: 'RayTracer'!
!classDefinition: 'Scene class' category: #RayTracer!
Scene class
instanceVariableNames: ''!
!classDefinition: #Texture category: #RayTracer!
Object subclass: #Texture
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'RayTracer'!
!classDefinition: 'Texture class' category: #RayTracer!
Texture class
instanceVariableNames: ''!
!classDefinition: #TextureWithFinish category: #RayTracer!
Texture subclass: #TextureWithFinish
instanceVariableNames: 'pigment finish'
classVariableNames: ''
poolDictionaries: ''
category: 'RayTracer'!
!classDefinition: 'TextureWithFinish class' category: #RayTracer!
TextureWithFinish class
instanceVariableNames: ''!
!Camera methodsFor: 'rendering' stamp: 'len 1/18/2017 21:22:53'!
allPixelsDo: aBlock
0 to: self image extent y - 1
do:
[:y |
0 to: self image extent x - 1
do:
[:x |
aBlock value: x value: y]].
^self image! !
!Camera methodsFor: 'rendering' stamp: 'len 1/18/2017 21:26:18'!
createRayForPixel: aPoint
| rayDirection scaledX scaledY |
scaledX _ aPoint x asFloat / image width - 0.5.
scaledY _ (image height - aPoint y) / image height - 0.5.
rayDirection _ self direction + (self up * scaledY) += (self right * scaledX).
^Ray new
origin: self location;
direction: rayDirection.! !
!Camera methodsFor: 'accessing'!
direction
^direction! !
!Camera methodsFor: 'accessing'!
direction: aVector
direction _ aVector! !
!Camera methodsFor: 'accessing'!
image
^image! !
!Camera methodsFor: 'accessing'!
image: anObject
image _ anObject! !
!Camera methodsFor: 'accessing'!
location
^location! !
!Camera methodsFor: 'accessing'!
location: aVector
location _ aVector! !
!Camera methodsFor: 'accessing'!
right
^right! !
!Camera methodsFor: 'accessing'!
right: aVector
right _ aVector! !
!Camera methodsFor: 'accessing'!
up
^up! !
!Camera methodsFor: 'accessing'!
up: aVector
up _ aVector! !
!Camera methodsFor: 'dialect-specific' stamp: 'len 1/18/2017 21:23:12'!
imageAt: aPoint putColor: color
imagePoker
pixelAt: aPoint
put: (color pixelValueForDepth: image depth)! !
!Camera methodsFor: 'dialect-specific' stamp: 'avi 10/12/2003 00:44'!
imageOfSize: aPoint
^Form
extent: aPoint
depth: 32! !
!Camera methodsFor: 'dialect-specific' stamp: 'len 1/20/2017 21:56:25'!
imageSize: aPoint
(image notNil and: [image extent = aPoint]) ifTrue: [^ self].
image _ self imageOfSize: aPoint.
imagePoker _ BitBlt bitPokerToForm: image.! !
!Camera methodsFor: 'initializing'!
initialize
location _ -10 z.
direction _ 1 z.
up _ 1 y.
right _ 1 x.! !
!Camera methodsFor: 'initializing' stamp: 'len 5/13/2019 00:55:11'!
renderScene: aScene
self allPixelsDo:
[:x :y |
| point ray color |
point _ x @ y.
ray _ self createRayForPixel: point.
color _ aScene trace: ray.
self imageAt: point putColor: (color as: Color)].
^self image! !
!CompoundFinish methodsFor: 'adding'!
addFinish: aFinish
finishes add: aFinish! !
!CompoundFinish methodsFor: 'rendering' stamp: 'len 5/13/2019 01:09:48'!
colorAt: anIntersection
| color |
color _ Color new.
finishes do: [:each| color += (each colorAt: anIntersection)].
^color! !
!CompoundFinish methodsFor: 'initializing'!
initialize
finishes _ OrderedCollection new! !
!FlatColorPigment methodsFor: 'accessing'!
color
^color! !
!FlatColorPigment methodsFor: 'accessing' stamp: 'len 5/13/2019 01:09:54'!
color: aColor
color _ aColor! !
!FlatColorPigment methodsFor: 'rendering'!
colorAt: anIntersection
^color! !
!Intersection methodsFor: 'rendering'!
colorInScene: aScene
scene _ aScene.
^self object colorAt: self! !
!Intersection methodsFor: 'rendering' stamp: 'len 1/18/2017 21:27:19'!
colorOfReflectedRay
^self scene trace: (Ray new
origin: self intersectionPoint;
direction: self reflectedDirection)! !
!Intersection methodsFor: 'rendering'!
eyeRayDirection
^self ray direction! !
!Intersection methodsFor: 'rendering' stamp: 'len 1/21/2017 19:44:17'!
normal
normal ifNotNil: [^ normal].
normal _ self object normalAt: self intersectionPoint.
(normal dot: self eyeRayDirection) positive ifTrue: [normal _ normal negated].
^ normal! !
!Intersection methodsFor: 'rendering' stamp: 'len 1/21/2017 19:39:46'!
normalComponentForLightSource: lightSource
| lightSourceVector |
lightSourceVector _ self intersectionPoint unitVectorTowards: lightSource location.
^ self normal dot: lightSourceVector! !
!Intersection methodsFor: 'rendering'!
reflectedDirection
^self normal reflectRay: self eyeRayDirection! !
!Intersection methodsFor: 'accessing'!
distance
^distance! !
!Intersection methodsFor: 'accessing'!
distance: aNumber
distance _ aNumber! !
!Intersection methodsFor: 'accessing' stamp: 'len 1/21/2017 19:56:07'!
intersectionPoint
^intersectionPoint ifNil: [intersectionPoint _ ray pointAtDistance: distance]! !
!Intersection methodsFor: 'accessing'!
object
^object! !
!Intersection methodsFor: 'accessing'!
object: aRayTraceObject
object _ aRayTraceObject! !
!Intersection methodsFor: 'accessing'!
pigmentColor
^self object texture pigment colorAt: self! !
!Intersection methodsFor: 'accessing'!
ray
^ray! !
!Intersection methodsFor: 'accessing'!
ray: aRay
ray _ aRay! !
!Intersection methodsFor: 'accessing'!
scene
^scene! !
!Intersection methodsFor: 'accessing'!
scene: anObject
scene _ anObject! !
!Intersection class methodsFor: 'instance creation'!
for: anObject ray: aRay distance: aNumber
^self new
object: anObject;
ray: aRay;
distance: aNumber;
yourself! !
!LightSource methodsFor: 'accessing'!
color
^color! !
!LightSource methodsFor: 'accessing' stamp: 'len 5/13/2019 01:10:11'!
color: aColor
color _ aColor! !
!LightSource methodsFor: 'accessing'!
location
^location! !
!LightSource methodsFor: 'accessing'!
location: aVector
location _ aVector! !
!LightSource methodsFor: 'initializing' stamp: 'len 5/13/2019 01:10:21'!
initialize
self
color: Color white;
location: 0@10@-10! !
!LightSource methodsFor: 'rendering' stamp: 'len 1/18/2017 21:34:21'!
isIntersectionVisible: anIntersection
| lightSourceVector lightSourceDistance ray |
lightSourceVector _ self location - anIntersection intersectionPoint.
lightSourceDistance _ lightSourceVector length.
ray _ (Ray new)
origin: anIntersection intersectionPoint;
direction: lightSourceVector /= lightSourceDistance.
anIntersection scene intersectionsWith: ray
do: [:intersection | intersection distance < lightSourceDistance ifTrue: [^false]].
^true! !
!PhongLightingModelFinish methodsFor: 'accessing'!
ambient
^ambient! !
!PhongLightingModelFinish methodsFor: 'accessing' stamp: 'len 1/17/2017 17:41:31'!
ambient: anObject
ambient _ anObject asFloat! !
!PhongLightingModelFinish methodsFor: 'accessing'!
diffuse
^diffuse! !
!PhongLightingModelFinish methodsFor: 'accessing' stamp: 'len 1/17/2017 17:41:58'!
diffuse: anObject
diffuse _ anObject asFloat! !
!PhongLightingModelFinish methodsFor: 'accessing'!
specular
^specular! !
!PhongLightingModelFinish methodsFor: 'accessing' stamp: 'len 1/17/2017 17:42:02'!
specular: anObject
specular _ anObject asFloat! !
!PhongLightingModelFinish methodsFor: 'accessing'!
specularPower
^specularPower! !
!PhongLightingModelFinish methodsFor: 'accessing' stamp: 'len 1/17/2017 17:42:06'!
specularPower: anObject
specularPower _ anObject asFloat! !
!PhongLightingModelFinish methodsFor: 'rendering'!
ambientColorAt: anIntersection
^anIntersection pigmentColor * self ambient! !
!PhongLightingModelFinish methodsFor: 'rendering' stamp: 'len 1/18/2017 21:16:15'!
diffuseColorAt: anIntersection fromLightSource: lightSource
| dot |
dot _ anIntersection normalComponentForLightSource: lightSource.
^anIntersection pigmentColor * lightSource color *= (dot * self diffuse)! !
!PhongLightingModelFinish methodsFor: 'rendering' stamp: 'len 5/13/2019 01:10:28'!
specularColorAt: anIntersection lightSource: lightSource
| halfLength cosAngleOfIncidence halfVector intensity lightSourceVector |
lightSourceVector _ anIntersection intersectionPoint
unitVectorTowards: lightSource location.
halfVector _ anIntersection eyeRayDirection negated
halfwayVectorTo: lightSourceVector.
halfLength _ halfVector length.
halfLength > 0.0
ifTrue:
[cosAngleOfIncidence _ (halfVector dot: anIntersection normal) / halfLength.
cosAngleOfIncidence > 0.0
ifTrue:
[intensity _ (cosAngleOfIncidence raisedTo: self specularPower)
* self specular.
^lightSource color * intensity]].
^Color black! !
!PhongLightingModelFinish methodsFor: 'initializing' stamp: 'len 1/19/2017 20:38:12'!
colorAt: anIntersection
| color dot |
color _ self ambientColorAt: anIntersection.
anIntersection scene lightSources do:
[:lightSource |
(lightSource isIntersectionVisible: anIntersection)
ifTrue:
[dot _ anIntersection normalComponentForLightSource: lightSource.
dot > 0.0
ifTrue:
[color _ color
+ (self diffuseColorAt: anIntersection fromLightSource: lightSource)
+= (self specularColorAt: anIntersection lightSource: lightSource)]]].
^color! !
!PhongLightingModelFinish methodsFor: 'initializing' stamp: 'len 1/17/2017 17:39:59'!
initialize
self
ambient: 0.2;
diffuse: 0.8;
specular: 0.0;
specularPower: 20.0! !
!Ray methodsFor: 'accessing'!
direction
^direction! !
!Ray methodsFor: 'accessing'!
direction: anObject
direction _ anObject! !
!Ray methodsFor: 'accessing'!
origin
^origin! !
!Ray methodsFor: 'accessing'!
origin: anObject
origin _ anObject! !
!Ray methodsFor: 'accessing' stamp: 'len 1/22/2017 09:37:30'!
pointAtDistance: d
^self direction * d + self origin! !
!Ray methodsFor: 'tracing' stamp: 'len 1/18/2017 21:18:06'!
directionLengthSquared
^directionLengthSquared ifNil: [directionLengthSquared _ direction dot: direction]! !
!RayTraceObject methodsFor: 'tracing'!
colorAt: anIntersection
^self texture colorAt: anIntersection! !
!RayTraceObject methodsFor: 'tracing'!
normalAt: aPoint
self subclassResponsibility! !
!RayTraceObject methodsFor: 'tracing'!
writeIntersectionsWith: aRay into: aCollection
^self! !
!RayTraceObject methodsFor: 'initializing'!
initialize
self
texture: Texture new! !
!RayTraceObject methodsFor: 'accessing'!
texture
^texture! !
!RayTraceObject methodsFor: 'accessing'!
texture: aRayTraceTexture
texture _ aRayTraceTexture! !
!ReflectionFinish methodsFor: 'rendering'!
colorAt: anIntersection
^anIntersection colorOfReflectedRay * reflection! !
!ReflectionFinish methodsFor: 'accessing'!
reflection
^reflection! !
!ReflectionFinish methodsFor: 'accessing' stamp: 'len 1/17/2017 17:42:13'!
reflection: aNumber
reflection _ aNumber asFloat! !
!Scene methodsFor: 'scene creation'!
addLightSource: aLightSource
lightSources add: aLightSource! !
!Scene methodsFor: 'scene creation'!
addObject: anObject
objects add: anObject! !
!Scene methodsFor: 'tracing'!
allIntersectionsWith: aRay
| intersections |
intersections _ OrderedCollection new.
self objects
do: [:each | each writeIntersectionsWith: aRay into: intersections].
^intersections asSortedCollection: [:x :y | x distance < y distance].! !
!Scene methodsFor: 'tracing'!
closestIntersectionWith: aRay
| intersections |
intersections _ self allIntersectionsWith: aRay.
^intersections isEmpty ifTrue: [nil] ifFalse: [intersections first]! !
!Scene methodsFor: 'tracing'!
intersectionsWith: aRay do: aBlock
| intersections |
intersections _ OrderedCollection new.
self objects
do: [:each | each writeIntersectionsWith: aRay into: intersections.
intersections do: aBlock.
intersections _ OrderedCollection new].! !
!Scene methodsFor: 'tracing' stamp: 'len 5/13/2019 01:12:00'!
trace: aRay
| intersection |
intersection _ self closestIntersectionWith: aRay.
intersection isNil ifTrue: [^Color black].
^intersection colorInScene: self! !
!Scene methodsFor: 'initializing'!
initialize
self
objects: OrderedCollection new;
lightSources: OrderedCollection new! !
!Scene methodsFor: 'accessing'!
lightSources
^lightSources! !
!Scene methodsFor: 'accessing'!
lightSources: aCollection
lightSources _ aCollection! !
!Scene methodsFor: 'accessing'!
objects
^objects! !
!Scene methodsFor: 'accessing'!
objects: aCollection
objects _ aCollection! !
!Texture methodsFor: 'rendering' stamp: 'len 5/13/2019 01:12:06'!
colorAt: anIntersection
^Color white! !
!TextureWithFinish methodsFor: 'rendering'!
colorAt: anIntersection
^self finish colorAt: anIntersection! !
!TextureWithFinish methodsFor: 'accessing'!
finish
^finish! !
!TextureWithFinish methodsFor: 'accessing'!
finish: anObject
finish _ anObject! !
!TextureWithFinish methodsFor: 'accessing'!
pigment
^pigment! !
!TextureWithFinish methodsFor: 'accessing'!
pigment: anObject
pigment _ anObject! !
!TextureWithFinish methodsFor: 'initializing'!
initialize
self
pigment: FlatColorPigment new;
finish: PhongLightingModelFinish new! !
!Number methodsFor: '*RayTracer' stamp: 'len 1/17/2017 18:00:54'!
x
^ Float32Vector3 x: self y: 0 z: 0! !
!Number methodsFor: '*RayTracer' stamp: 'len 1/17/2017 18:01:00'!
y
^ Float32Vector3 x: 0 y: self z: 0! !
!Number methodsFor: '*RayTracer' stamp: 'len 1/17/2017 18:01:04'!
z
^ Float32Vector3 x: 0 y: 0 z: self! !
!Float32Vector3 methodsFor: '*RayTracer' stamp: 'len 1/18/2017 21:38:12'!
halfwayVectorTo: aVector
^(self + aVector) /= 2.0! !
!Float32Vector3 methodsFor: '*RayTracer' stamp: 'len 1/18/2017 21:37:28'!
reflectRay: aVector
^self * ((self dot: aVector) * -2.0) -= aVector! !
!Float32Vector3 methodsFor: '*RayTracer' stamp: 'len 1/18/2017 21:32:27'!
unitVector
^self copy normalize! !
!Float32Vector3 methodsFor: '*RayTracer' stamp: 'len 1/18/2017 21:30:13'!
unitVectorTowards: aVector
^(aVector - self) normalize! !
!Point methodsFor: '*RayTracer' stamp: 'len 1/17/2017 18:01:56'!
@ aNumber
^Float32Vector3 x: x y: y z: aNumber! !