T&C LAB-AI
Robotics
Computer Graphics and Programming
Lecture 12
GLSL
by extending Ray Tracing
GLSL: OpenGL Shading Language
Jeong-Yean Yang
2020/12/8
1
T&C LAB-AI
GLSL by Ray Marching
1
2
T&C LAB-AI
Robotics
OpenGL Shading Library (GLSL)
• We have learned OpenGL and Ray Tracing
– How to Add Ray Casting or Semi Ray Tracing on OpenGL’s
environment?
– OpenGL is based on Polygon-based Rendering
• Ray Tracing determines Pixel’s Color by calculating color
with respect to
– Normal vector of surfaces
– Light position, reflection, and refraction vector
• OpenGL permits One pixel’s color by GLSL programming
3
T&C LAB-AI
Robotics
Computational Burden in Ray Tracing
• What is the most Painful works in Ray Tracing?
• Finding Intersection point is complex and slow.
• Sampling becomes the Approximation Technique for
Intersection by fast computation.
Ray Marching technique is used for OpenGL rendering
4
Ray Casting
Ray
Ray Tracing
Object
Color
Color
Ray
Reflection
Refraction
T&C LAB-AI
Robotics
Ray Marching for Volumetric Rendering
• OpenGL uses Z buffer for Volume Rendering
• Z buffering projects all data onto one scene image
• Rendering requires volumetric operation
5
Z=1
Z=-1
Perspective Mapping
Scene
Z=1
Z=-1
Order
Red is above blue
T&C LAB-AI
Robotics
Ray Marching for Fast Speed
• While doing volumetric rendering,
– first sphere s0 meets object B and fills the volume
– Second sphere s1 and s2 meets object B and fill the volume
– Final sphere, s3 meets object A and Calculate Intersection Point
– It is Faster than Ray tracing
6
A
B
Viewpoint
Ray
s0
s1
s2
s3
T&C LAB-AI
GLSL Structure
2
7
T&C LAB-AI
Robotics
How to OpenGL calculate One Pixel Color?
Vertex shader and Fragment Shader
8
Ref: uGL-39-GLSL-Basic
PhongTex.vsh
PhongTex.fsh
T&C LAB-AI
Robotics
GLSL Architecture
• Script(vsh and fsh) is compiled and uploaded by
uShader class
9
Vertex Shader
(*.vsh)
Fragment Shader
(*.fsh)
VBO
OpenGL’s
Compiler
GLSL
Program
GPU
Memory
Rendering
Our Program
Vertex Transform
Set Color value
GLSL (vsh, fsh)
uShader
Find RGB
With vsh
and fsh
Pixel Color
T&C LAB-AI
Robotics
App VSH FSH
• Our App transfers VBO handle to vsh
– Vertices, normal, and textures.
– Ambient, diffuse, and specular is given to vsh
• VSH file : do transform of vertices and normal.
– Gl’s result = P*H*vertices
– Given color is not used here pass colors into FSH
• FSH file: do calculation of colors
– VSH provides color and geometry information.
– FSH calculate RGB.
10
T&C LAB-AI
Robotics
GLSL Basic Variable Types
1. attribute
Connected with VBO
2. varying
Variables in vsh is transferred
to variables in fsh
3. uniform
Connected with my program
11
T&C LAB-AI
Robotics
GLSL Grammar
• It is similar to C language
• Caution: some variables are very different
– Ex) A= 1 A=1.0
– Ex) vec4 a = vec4(0,0,0,1), a.xyz = vec3(1,2,3)
– Ex) You cannot modify “varying variable”
• varying vec4 diffuse;
• vec4 v = vec4(1,2,3,0);
• diffuse = v Error
• See example of solid.fsh in “Blue example” of uGL-45-Sphere-
Gouraud-GLSL
• Reference
– https://www.khronos.org/opengl/wiki/Data_Type_(GLSL)#Ve
ctors
12
T&C LAB-AI
Robotics
APPVSHFSH
Variable Connection
13
VBO handle
Our App
Screen(P)
Our App
Model(H)
Our App
Color
Our App
VSH
attribute
Vertices,
Normals
textures
uniform
screen
model
uniform
colors
GL’s
Internal
variables
varying
variables
FSH
texture
Our App
uniform
texture
Rendering
varying
variables
T&C LAB-AI
Robotics
See Example uGL-39-GLSL-Basic
attributes in vsh
14
VSH file
Lecture 9
pp. 26
uVertex uses vertex, normal, and texture, u and v
Three Attributes are defined.
T&C LAB-AI
Robotics
See Example uGL-39-GLSL-Basic
uniform in vsh
15
uObj::Draw()
uShader::Load()
Our Program
T&C LAB-AI
Robotics
See Example uGL-39-GLSL-Basic
uniform in vsh
16
uObj::Draw()
uShader::Load()
Our Program
T&C LAB-AI
Robotics
See Example uGL-39-GLSL-Basic
uniform in vsh
17
uObj::Draw()
uShader::Load()
Our Program
T&C LAB-AI
Robotics
See Example uGL-39-GLSL-Basic
uniform in vsh
18
screen
model
Perspective
mapping,
H transform
uShader’s
variable
Our App
GLSL in
GPU
T&C LAB-AI
Vertex and Fragment Shader
(VSH and FSH)
3
19
T&C LAB-AI
Robotics
Meaning of “screen and model” in vsh
• gl_Position is rendering result on 2D display
– Screen(perspective mapping) is required
• eyePosition(Viewpoint) and normal vector
– need not perspective mapping(screen)
20
Cam.P
H
model
screen
object
H
P
T&C LAB-AI
Robotics
Vertex shader
Vs.
Fragment shader
• VSH for only Transform
– Bypassing Our App’s parameter into FSH
– APP VSH FSH
• VSH is not so unique in many examples
• FSH is designed to calculate Each Pixel Color
• Keep it in your mind
– VSH and FSH are Not for Objects,
– But for Each Pixel Color of objects
21
T&C LAB-AI
Robotics
App, VBO, VSH, and FSH
Connection Diagram
22
App
VSH
FSH
VBO
Position,
normal,
UV
Texture
texture
varying
uniform
attribute
P, H, color
position, normal, color
uniform
T&C LAB-AI
Robotics
FSH EX1) uGL-02-Polygon-Color
solid.vsh and solid.fsh
23
Solid.vsh
Solid.fsh
result
uObj.cpp
T&C LAB-AI
Robotics
FSH Ex2) uGL-03-Object-Camera
solid.vsh and solid.fsh
24
Solid.vsh
T&C LAB-AI
Robotics
Lambertian Diffuse and
Phong’s Specular Effect
FSH Ex 3) uGL-10-uWnd-Box-Gouraud
25
Solid.vsh
Solid.fsh
T&C LAB-AI
Robotics
Whenever OpenGL draws one pixel,
GLSL(Vsh and Fsh) is called by GPU
• As a result,
• eyePosition by model*vertices is considered as
“Intersection Point” by the Ray from viewpoint(0,0,0)
26
Solid.vsh
OpenGL calculates intersection
point for painting as in Ray Tracing
T&C LAB-AI
Robotics
Think as in Ray Tracing,
Can you Read it Now?
27
ˆn
surface
ˆi
ˆr
camera
ˆ : Illumination(light source)
ˆ : Reflection
ˆ :
i
r
n normal
ˆv
P
T&C LAB-AI
Robotics
Think as in Ray Tracing,
Can you Read it Now?
28
ˆn
surface
ˆi
ˆr
camera
ˆ : Illumination(light source)
ˆ : Reflection
ˆ :
i
r
n normal
ˆv
P
Light source
ˆ
L i
T&C LAB-AI
Robotics
Lambertian Dot Product for Diffuse
29
ˆn
surface
ˆi
ˆr
camera
ˆ : Illumination(light source)
ˆ : Reflection
ˆ :
i
r
n normal
ˆv
P
ˆ ˆ
cos
i n
T&C LAB-AI
Robotics
Lambertian Dot Product for Diffuse
30
ˆn
surface
ˆi
ˆr
camera
ˆ : Illumination(light source)
ˆ : Reflection
ˆ :
i
r
n normal
ˆv
P
cos
0 : Hidden surface
T&C LAB-AI
Robotics
Phong’s Specular Color with Reflected Vector
31
ˆn
surface
ˆi
ˆr
camera
ˆ : Illumination(light source)
ˆ : Reflection
ˆ :
i
r
n normal
ˆv
P
If It is Not a Hidden surface
the Ray from viewpoint(0,0,0)
T&C LAB-AI
Robotics
32
ˆn
surface
ˆi
ˆr
camera
ˆ : Illumination(light source)
ˆ : Reflection
ˆ :
i
r
n normal
ˆv
P
If It is Not a Hidden surface
the Ray from viewpoint(0,0,0)
ˆ ˆ
cos
r v
S( )
cos
s
Phong’s Specular Color with Reflected Vector
T&C LAB-AI
Robotics
33
ˆn
surface
ˆi
ˆr
camera
ˆ : Illumination(light source)
ˆ : Reflection
ˆ :
i
r
n normal
ˆv
P
Phong’s Specular Color with Reflected Vector
Color = ambient + diffuse * cos(q) + specular * pow(angle,100)
T&C LAB-AI
Robotics
Ex4) uGL-40-GLSL-Box-Gouraud
Two types of Diffuse
34
Control
diffuse by
GLSL
T&C LAB-AI
Robotics
Ex5) uGL-40-GLSL-Box-Gouraud
Ambient Effect
35
Basic Type
Q: Why lower ambient becomes darker?
T&C LAB-AI
Robotics
Ex6) uGL-40-GLSL-Box-Gouraud
More Specular by Power and by Factor
36
Pow by 100
Pow by 1000
Specular * 5.0
Specular * 1.0
Specular * 1.0
Pow by 100
T&C LAB-AI
Robotics
Simple Cartoon Rendering
• Game with Oriental ink painting
37
T&C LAB-AI
Robotics
Edge is Over colored using Dot Product
ex)uGL-41-GLSL-Dog-Rendering
38
View point
(light source)
ˆn
ˆv
Black
color
L 0
ˆ
ˆ
cos
P
n L
T&C LAB-AI
Robotics
Ex.7) uGL-41-GLSL-Dog-Rendering
Cartoon-Rendering
39
L 0
ˆ
ˆ
cos
P
n L
View point
75deg
cos
0.25
Alpha = 1-dot
66.4 deg
cos
0.4
Alpha = 0.9