T&C LAB-AI
Robotics
Computer Graphics and Programming
Lecture 11
Ray Tracing
Jeong-Yean Yang
2020/12/8
1
T&C LAB-AI
Basic Concept of Ray Tracing
Light, Color, and Magic with Math.
1
2
T&C LAB-AI
Robotics
What is a Ray Tracing?
• Calculate Color as we see in a Physical world.
• Everything in a Ray tracing is Math and Math.
• 3D Geometries such as sphere, cylinder, plane and
line are Perfectly Calculated.
• Ray tracing is Entirely 3D Euclidean Mathematics.
• Thus, it shows very Realistic scene.
3
T&C LAB-AI
Robotics
How to Calculate Colors?
• What we see in everyday is What?
– We can see an Apple. It is red.
• In a Physical world, the Arrow direction is True?
4
Apple
Is Red
T&C LAB-AI
Robotics
What We Intend to See is Not the Truth.
The Light is coming on Our Eyes
• Seeing Mechanism
• The Light source is the origin.
5
Step 1.
Light source fires Rays
Step 2.
Rays are Reflected
(We CANNOT see this Ray)
Step 3.
We can some rays that are reflected
on the apple surface
T&C LAB-AI
Robotics
What We See is
a Set of Reflected Rays from Light Sources
6
Hey,
Is It Easy?
Our World is more complex than you think
T&C LAB-AI
Robotics
Everything Reflects Rays.
(Without a Black hole)
7
T&C LAB-AI
Robotics
What You See is
Reflected Rays from Light Source
• Can you believe it? Think the Sun Disappears
8
T&C LAB-AI
Ray Tracing in the Reversed Way
2
9
T&C LAB-AI
Robotics
Ray + Tracing
• What is a Tracing?
– Tracing follows where the color comes from?
10
Reflected ray
Scene Image
(Ray tracing result)
Ray
Normal
Vector
Light source
T&C LAB-AI
Robotics
Lambertian Reflection Model
• Lambertian model defines Diffuse color
– by Only Normal vector
• OpenGL rendering calculates cosine for diffuse color11
ˆ ˆ
cos
i n
ˆn
surface
ˆi
ˆr
camera
ˆ : Illumination(light source)
ˆ : Reflection
ˆ :
i
r
n normal
ˆv
Remind
pp. 53
Lecture 8
T&C LAB-AI
Robotics
Illumination Model
with Reflection and Refraction
• Illumination model in Ray tracing
– Reflection and Refraction
12
ˆn
surface
ˆi
ˆr
camera
ˆ : Illumination(light source)
ˆ : Reflection
ˆ :
ˆ : Refraction
i
r
n normal
t
ˆv
Lambertian
model
(lecture8 pp.53)
+
Refraction
ˆt
'
T&C LAB-AI
Robotics
Ray Tracing finds Colors
Step 1. Eye fires Ray
• Scene image = width x height
• Eye fires rays for each pixel
13
Scene Image
(Ray tracing result)
Ray
(0,0)
(640,480)
Get pixel color
From the apple object
T&C LAB-AI
Robotics
Ray Tracing finds Colors
Step 2. Calculate Reflection and Refraction
14
Scene Image
(Ray tracing result)
Diffuse and
Specular
(0,0)
(640,480)
Illumination
vector
Color from
light source
+
Red from apple +
yellow from sun
=
T&C LAB-AI
Robotics
Ray Tracing finds Colors
Step 3. Calculate Reflection and Refraction
• Refraction
15
object
Ray
Reflection
Refraction
object
Total
color
Reflected
color
Refracted
Color
illumination
illumination
T&C LAB-AI
Robotics
Ray Casting Vs. Ray Tracing
• Ray Casting has NO Reflection and Refraction
• Ray Tracing does with Reflection and Refraction
16
Ray Tracing
Ray
Normal
Vector
Light source
Ray Casting
Ray
T&C LAB-AI
Robotics
Example of Ray Tracing
17
What is the difference with OpenGL?
- There are two lights.
- Shadow, Transparency, Refracted
image, and mirror
- Realistic scene rather than polygon-
based OpenGL
Two
shadows
Reflected image
from floor
Lenz effect from
a sphere
Mirror
Refraction
T&C LAB-AI
Robotics
Ray through Z= -1 (Near) plane
• Calculate Ray
18
Z=-1
(0,0)
2
( , )
[0, )
[0, )
x y
R
x
w
y
h
(640,480)
X=-1
Y=1
Z=-1
X=1
Y=-1
Z=-1
(
320) / 320
(
240) / 240
1
X
x
Y
y
Z
ˆ
,
[ , , ] o
Ray v
X Y Z
z
y
origin
T&C LAB-AI
Robotics
Modifying Viewpoint in RT Example
((-)Z is somewhat confused..)
19
Z=-1
z
y
origin
z
y
Z=-2
Z=-1
Z=0
Z=0
T&C LAB-AI
Robotics
uRT-05-RT1-Buffer
Basic Buffering
• Load 640x480 image, “dummy.jpg”
20
Red
JPG is based on
BGR color map
Green
Blue
Result
640x480 red screen
T&C LAB-AI
Robotics
Ray Vector Calculation
640x480 = 307200 rays
• Ex) uRT-06-RT2-2DLight
21
(
320) / 320
(
240) / 240
1
'
640 / 480
X
x
Y
y
Z
X
X
ˆ
ˆ
,
[
', , ] o
Ray v
X Y Z
Z= -1
( , )
x y
ˆv
T&C LAB-AI
Robotics
uRT-06-RT2-2DLight
Normal vector calculation
• Remind Lambertian Diffuse Model
• Use normal vector (0,0,1)
• Lambertian diffuse uses illumination source, i.
• If we use a ray, v, what will happen?
22
ˆ ˆ
cos
i n
ˆn
surface
ˆi
ˆr
camera
ˆ : Illumination(light source)
ˆ : Reflection
ˆ :
i
r
n normal
ˆv
ˆ
ˆ
ˆ ˆ
, cos
v
i
v n
T&C LAB-AI
Robotics
uRT-06-RT2-2DLight
Normal vector calculation
23
Viewpoint
Position, o
ˆ ˆ
cos
v n
ˆ
ˆv i
ˆn
Result
Light source
is on o, too.
T&C LAB-AI
Robotics
Object Modeling in uObj
• 3D graphics uses Object Modeling in two ways.
• 1. Polygon-based modeling
– Vertices and faces
• 2. Parametric Modeling
– Sphere ( radius and center position)
– Plane( normal vector and position)
– and so on
• Ray tracing uses Parametric Modeling
24
T&C LAB-AI
Robotics
Example of Sphere
Math of 3Dim. Vector Space
• The minimum distance is easy
25
ˆv
ˆo
ˆx
2
2
2
2
2
2
2
2
2
2
2
ˆ
| | 1
ˆ ˆ
ˆ
ˆ
ˆ ˆ
ˆ
ˆ ˆ
ˆ ˆ
ˆ
ˆ
d
|
| |
| |
| |
|
ˆ
ˆ
ˆ
ˆ
ˆ
ˆ
ˆ
| |
| |
2
| |
2
ˆ ˆ
d
2
2
0
ˆ ˆ
ˆ ˆ ˆ
(
)
v
x
o
v
x c
o
v c
o c
v
b
v
b
v
b v
b
b v
d
b v
d
b v
o c
v
ˆc
ˆ :
ˆ :
ˆ :
o starting position
v direction
c center
ˆ ˆ
ˆ ˆ ˆ ˆ
(
)
x
o
o c
v v
T&C LAB-AI
Robotics
Example of Sphere
Intersection
• Get Intersection point for Ray Tracing
26
ˆv
ˆo
1
ˆp
ˆc
2
ˆp
2
2
2
2
2
2
2
2
2
2
2
2
2
2
1,2
ˆ
| | 1
ˆ ˆ
ˆ
ˆ
ˆ ˆ
ˆ ˆ
ˆ
ˆ
,
|
| |
| |
|
ˆ
ˆ
ˆ
ˆ
| |
| |
2
ˆ
ˆ
ˆ
2
| |
0
ˆ
ˆ
ˆ
ˆ
ˆ
2
4
4 | |
4
2
2
v
x
o
v
radius r
x c
o c
v
b
v
b
v
b v
b v
b
r
b v
b v
b
r
b
b
ac
a
T&C LAB-AI
Robotics
If Ray passes the Sphere or Not
• 3Dim space is in a REAL Space
27
2
2
2
1,2
ˆ ˆ
ˆ
ˆ
ˆ
ˆ
ˆ
ˆ
| |
x
o
v
b v
b v
b
r
2
2
2
2
2
2
2
2
2
1,2
1
1
2
2
ˆ
ˆ
ˆ
| |
0 :
Intersection
ˆ
ˆ
ˆ
| |
0 : Intersection
ˆ
ˆ
ˆ
ˆ
ˆ
| |
ˆ
ˆ
ˆ ˆ
ˆ
ˆ
,
D
b v
b
r
No
D
b v
b
r
b v
b v
b
r
p
o
v p
o
v
T&C LAB-AI
Robotics
Alternative Solution by Geometry
28
ˆv
ˆo
ˆc
ˆ ˆ ˆ
b
c o
ˆ
ˆ
ˆ
v b v
d
2
2
ˆ ˆ
ˆ
ˆ
ˆ
:
Intersection
d
b b
v b v
d
r No
d
ˆv
ˆ
ˆ
ˆ
v b v
d
ˆ ˆ ˆ
b
c o
2
2
2
2
2
1
2
2
2
ˆ
ˆ
ˆ
ˆ
ˆ
ˆ
ˆ
ˆ
ˆ
ˆ
r
d
e
p
v b v
e
v
p
v b v
e
v
r
e
T&C LAB-AI
Robotics
Ray Tracing with a Sphere
uRT-07-RT3-Object
• Assume that Light source is on Viewpoint, o.
• Step 1) Fire ray
• Step 2) Find the intersection point, p1
• Step 3) Get Unit Normal
• Step 4) Color =
29
Viewpoint
Position, o
z
y
ˆv
ˆn
Sphere
Object
1
ˆp
ˆ ˆ
cos
i n
1
ˆ
ˆ
ˆ
(
)
u
n
p
c
T&C LAB-AI
Robotics
uRT-07-RT3-Object
30
T&C LAB-AI
Robotics
Break Time
• Lambertian Model works as Smoothing effect
• Black hole images are Rendered by Mathematical
Calculation( It is NOT an optical image)
31
Black hole by X ray images
T&C LAB-AI
Robotics
Ray Tracing Depth Sorting
• It is similar to Z-buffer method
• Depth sorting finds which one is nearest to viewpoint.
uRT-08-RT4-MultiObject-ZProb
32
Bad Case
uRT-09-RT4-MultiObject-ZOrder
Good case
T&C LAB-AI
Robotics
Modified FindRGB
33
Obj2
Obj1
Ray
Find which one
has minimum
distance from
viewpoint, o
ˆo
Obj2 is
closed to
position, o
Obj2
Obj1
Ray
ˆo
Obj1 is
closed to
position, o
T&C LAB-AI
Robotics
Diffuse, Ambient, and Specular
in Ray Casting
• Ray get color from Object
34
Ray
ˆo
Ray
ˆo
Ambient=0.1,0.1,0.1
Diffuse=1,0,0
Specular=0.3,0.3,0.3
Ambient=0.1,0.1,0.1
Diffuse=0,0,1
Specular=0.1,0.1,0.5
T&C LAB-AI
Robotics
uRT-10-RT5-Colors-Ambient_diffuse
35
One Step Ray Tracing
RGB=ambient+diffuse cos +specular
T&C LAB-AI
Robotics
Light Position
• Light object: uLight
– The previous examples has the light at the viewpoint
36
Light position
Point light
Directional light
,
(distance)
'
Attenuation
func
RGB
RGB
T&C LAB-AI
Robotics
How to Calculate the Distance to
Point Lights
• Step 1. calculate intersection point, p
• Step 2. calculate illumination vector,
• Step 3. calculate normal vector
• Step 4. calculate Reflection vector
37
Viewpoint
Position, o
Object
ˆp
ˆn
ˆr
ˆn
surface
ˆi
ˆr
camera
ˆ : Illumination(light source)
ˆ : Reflection
ˆ :
i
r
n normal
ˆv
Light source, k
ˆi
ˆ
ˆ
ˆ
i
k
p
ˆ
ˆ
ˆ
2 -
r
n i
T&C LAB-AI
Robotics
Math of Reflection Vector
pp. 54 in Lecture 8
• What is the difference?
• Think if illumination vector is normalized,
– The result is same.
38
ˆ ˆ
ˆ ˆ ˆ
2
ˆ
ˆ
ˆ
ˆ ˆ
2
i
r
i n n
r
i n n i
Reflection vector
Reflection vector
ˆ ˆ
ˆ
2
ˆ
ˆ
ˆ
2
i
r
n
r
n i
ˆi
T&C LAB-AI
Robotics
Ex) Bad and Good Case
39
Bad
uRT-11-RT5-Lights-position-Prob-badcolor
Good
uRT-11-RT5-Lights-position-Sol-badcolor
T&C LAB-AI
Robotics
Phong Effect
pp. 54 in lecture 8
• Color is determined by
40
ˆn
surface
ˆi
ˆr
camera
ˆ : Illumination(light source)
ˆ : Reflection
ˆ :
i
r
n normal
ˆv
ˆ ˆ
cos
r v
S( )
cos
s
cos
( )
ˆ ˆ
ˆ ˆ
(
)
(
)
s
RGB
Ambient
Diffuse
Specular S
Ambient
Diffuse i n
Specular r v
T&C LAB-AI
Robotics
Phong’s effect Result
uRT-12-RT6-Colors-Specular-Phong
41
specular
Phong’s specular
10
ˆ ˆ
r v
Phong’s specular
100
ˆ ˆ
r v
T&C LAB-AI
Robotics
OpenGL with GLSL is same with
Ray Tracing with
Phong’s effect
42
• Ray casting does not cover Phong’s effect.
• You finished Background Knowledge of OpenGL by
learning Ray Tracing
What you learn here
is close to
Ray Casting
OpenGL GLSL is
Nearly
Semi Ray Tracing
(or Ray casting)
T&C LAB-AI
Over OpenGL Technologies
Ray Tracing with Transparency
3
43
T&C LAB-AI
Robotics
Ray Tracing with Transparent Ray
• Each Ray is divided by Two Rays, such as Reflection
and Refraction
• Reflected and Refracted rays are repeatedly divided
by other two rays such as reflection and Refraction
44
Ray Casting
Ray
Ray Tracing
Object
Color
Color
Ray
Reflection
Refraction
T&C LAB-AI
Robotics
Object
Ray Tracing Concept.
Step1. Ray Start from Eye
45
Ray
Object
T&C LAB-AI
Robotics
Object
Ray Tracing Concept.
Step2. Ray generates Reflection and Refraction
46
Reflection
Refraction
Object
T&C LAB-AI
Robotics
Object
Ray Tracing Concept.
Step3. Each Ray generates Reflection and
Refraction
47
Refraction
Object
Refraction
Reflection
Reflection
Nothing
T&C LAB-AI
Robotics
Object
Object
Ray Tracing Concept.
Step4. More and More Rays
48
Refraction
Nothing
Reflection
Reflection
Refraction
Reflection
T&C LAB-AI
Robotics
Ray Tracing has three features
• 1. Nothing to meet an object or a light
– The Ray goes astray.
– I means that the ray meets No light It is removed
• 2. It is called, Infinite Ray Generation Infinite Loop
– One ray is divided into Reflection and Refraction.
– Set limitation of the Ray Separation( or Generation)
– Three for simple example Low quality.
– One ray with three steps generates 8 rays.
• 3. Light Intensity is needed.
– Light CANNOT go infinite distance.
– Light intensity is Inverse proportional to the distance
49
T&C LAB-AI
Robotics
Refraction
• Snell’s Law
– Light velocity varies when passing materials
50
ˆn
surface
ˆi
1
2
ˆt
camera
ˆ : Illumination
ˆ :
ˆ : Reflection
i
n normal
t
ˆv
1
1
2
2
2
1
sin
Refractive Factor
sin
v
n
n
v
n
T&C LAB-AI
Robotics
Refraction Vector Calculation
51
ˆn
surface
ˆi
1
2
ˆt
ˆp
ˆn
surface
ˆi
2
ˆt
ˆp
ˆp
ˆ
ˆ
ˆ
ˆ ˆ
ˆ
ˆ
ˆ
/ |
|
u
p
i n n i
p
p
p
ˆn
2
2
ˆ
ˆ
ˆ
sin
cos
(
)
u
t
p
n
ˆ
u
p
T&C LAB-AI
Robotics
Get Refraction Vector
52
1
2
sin
Refractive Factor
.
sin
geo tr
ˆ
ˆ
ˆ
ˆ ˆ
ˆ
ˆ
ˆ
/ |
|
u
p
i n n i
p
p
p
2
2
ˆ
ˆ
ˆ
sin
cos
(
)
ˆ
ˆ ˆ
/ | |
u
u
t
p
n
t
t
t
ˆ
|
, | 1
ˆ
ˆ
ray v
i
v
ˆ
ˆ
ˆ
ˆ ˆ
ˆ ˆ ˆ ˆ
ˆ
ˆ
ˆ
/ |
|
u
p
i n n i
v n n v
p
p
p
T&C LAB-AI
Robotics
Refraction with geo.tr vector
• Three steps Ray tracing.
53
Ray
Reflection
Refraction
Ret = Diffuse + Ambient
Ref = Reflected Color * Specular
Tra = Transparent Color * Transparent
Final Ret = ret+ Ref + Tra
T&C LAB-AI
Robotics
Various Types of Transparency
54
No Transparency
Only Red is Transparent
Both are transparent
T&C LAB-AI
Robotics
Mirror with No Transparency
55
It is Not a Perfect Mirror
It is a Perfect Mirror