Introduction to Graphics Part2 - cl.cam.ac.uk

44
Introduction to Graphics Computer Science Tripos Part 1A/1B Michaelmas Term 2021/2022 Department of Computer Science and Technology The Computer Laboratory William Gates Building 15 JJ Thomson Avenue Cambridge CB3 0FD www.cst.cam.ac.uk

Transcript of Introduction to Graphics Part2 - cl.cam.ac.uk

Page 1: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics

Computer Science Tripos Part 1A/1B

Michaelmas Term 2021/2022

Department of

Computer Science

and Technology

The Computer Laboratory

William Gates Building

15 JJ Thomson Avenue

Cambridge

CB3 0FD

www.cst.cam.ac.uk

Page 2: Introduction to Graphics Part2 - cl.cam.ac.uk

This handout includes copies of the slides that will be used in lectures.

These notes do not constitute a complete transcript of all the lectures and

they are not a substitute for text books. They are intended to give a

reasonable synopsis of the subjects discussed, but they give neither

complete descriptions nor all the background material.

Selected slides contain a reference to the relevant section in the

recommended textbook for this course: Fundamentals of Computer

Graphics by Marschner & Shirley, CRC Press 2015 (4th edition). The

references are in the format [FCG N.M], where N.M is the section number.

Material is copyright © Neil A Dodgson, Peter Robinson & Rafał Mantiuk, 1996‐2021, except where otherwise noted.

All other copyright material is made available under the University’s licence. All rights reserved.

Page 3: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafał Mantiuk 1

Michaelmas Term 2021/2022

Introduction to Computer GraphicsRafał Mantiuk

www.cl.cam.ac.uk/~rkm38

Eight lectures & two practical tasksPart IA 75% CST, Part IB 50% CST

Two supervisions suggestedTwo exam questions on Paper 3

2What are Computer Graphics &

Image Processing?

Scenedescription

Digitalimage

Computergraphics

Image analysis &computer vision

Image processing

Imagecapture

Imagedisplay

3

Why bother with CG?All visual computer output depends on CG

printed output (laser/ink jet/phototypesetter) monitor (CRT/LCD/OLED/DMD) all visual computer output consists of real images generated

by the computer from some internal digital image

Much other visual imagery depends on CG TV & movie special effects & post-production most books, magazines,

catalogues, brochures, junk mail, newspapers, packaging, posters, flyers

Course StructureBackground

What is an image? Resolution and quantisation. Storage of images in memory. [1 lecture]

Rendering Perspective. Reflection of light from surfaces and shading. Geometric

models. Ray tracing. [2 lectures]Graphics pipeline

Polygonal mesh models. Transformations using matrices in 2D and 3D. Homogeneous coordinates. Projection: orthographic and perspective. Rasterisation. [2 lectures]

Graphics hardware and modern OpenGL GPU APIs. Vertex processing. Fragment processing. Working with

meshes and textures. [1 lectures]Human vision, colour and tone mapping

Colour perception. Colour spaces. Tone mapping [2 lectures]

4

5

Course booksFundamentals of Computer Graphics

Shirley & MarschnerCRC Press 2015 (4th or 5th edition)

[FCG 3.1] – reference to section 3.1 (4th edition)Computer Graphics: Principles & Practice

Hughes, van Dam, McGuire, Sklar et al.Addison-Wesley 2013 (3rd edition)

OpenGL Programming Guide:The Official Guide to Learning OpenGL Version 4.5 with SPIR-V Kessenich, Sellers & Shreiner

Addison Wesley 2016 (7th edition and later)

6

Introduction to Computer Graphics

Background What is an image? Resolution and quantisation Storage of images in memory

RenderingGraphics pipelineRasterizationGraphics hardware and modern OpenGLHuman vision and colour & tone mapping

1

2

3

4

5

6

Page 4: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafał Mantiuk 2

Michaelmas Term 2021/2022

What is a (digital) image?A digital photograph? (“JPEG”)A snapshot of real-world lighting?

Image

2D array of pixels 2D function

From computing perspective(discrete)

From mathematicalperspective(continuous)

•To represent images in memory

•To create image processing software

•To express image processing as a mathematical problem

•To develop (and understand) algorithms

7

Image 2D array of pixels In most cases, each pixel takes 3 bytes: one for each red, green and blue But how to store a 2D array in memory?

8

Stride

Calculating the pixel component index in memory For row-major order (grayscale)𝑖(𝑥, 𝑦) = 𝑥 + 𝑦 𝑛 For column-major order (grayscale)𝑖(𝑥, 𝑦) = 𝑥 𝑛 + 𝑦 For interleaved row-major (colour)𝑖 𝑥, 𝑦, 𝑐 = 𝑥 3 + 𝑦 3 𝑛 + 𝑐 General case𝑖 𝑥, 𝑦, 𝑐 = 𝑥 𝑠 + 𝑦 𝑠 + 𝑐 𝑠

where 𝑠 , 𝑠 and 𝑠 are the strides for the x, y and colour dimensions

9

Padded images and stride Sometimes it is desirable to “pad” image with extra pixels

for example when using operators that need to access pixels outside the image border

Or to define a region of interest (ROI)

How to address pixels for such an image and the ROI?

Allocated memory spaceImage

Region of Interest(ROI)

10

Padded images and stride

𝑖 𝑥, 𝑦, 𝑐 = 𝑖 + 𝑥 𝑠 + 𝑦 𝑠 + 𝑐 𝑠 For row-major, interleaved, grayscale

𝑖 = 𝑠 = 𝑠 = 𝑠 =

Allocated memory spaceImage

Region of Interest(ROI)

11

Pixel (PIcture ELement)

Each pixel (usually) consist of three values describing the color

(red, green, blue)For example

(255, 255, 255) for white (0, 0, 0) for black (255, 0, 0) for red

Why are the values in the 0-255 range?How many bytes are needed to store 5MPixel image?

(uncompressed)

12

7

8

9

10

11

12

Page 5: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafał Mantiuk 3

Michaelmas Term 2021/2022

Pixel formats, bits per pixel, bit-depthGrayscale – single color channel, 8 bits (1 byte)Highcolor – 216=65,536 colors (2 bytes)

Truecolor – 224 = 16,8 million colors (3 bytes)Deepcolor – even more colors (>= 4 bytes)

But why? 13

Color bandingIf there are not

enough bits to represent color

Looks worse because of the Mach band orChevreul illusion

Dithering (added noise) can reduce banding Printers but also

some LCD displays

Mac

h ba

nds

Intensity profile14

What is a (computer) image?A digital photograph? (“JPEG”)A snapshot of real-world lighting?

Image

2D array of pixels 2D function

From computing perspective(discrete)

From mathematicalperspective(continuous)

•To represent images in memory

•To create image processing software

•To express image processing as a mathematical problem

•To develop (and understand) algorithms

15

Image – 2D functionImage can be seen as a function I(x,y), that gives

intensity value for any given coordinate (x,y)

16

Sampling an image

The image can be sampled on a rectangular sampling grid to yield a set of samples. These samples are pixels.

17

What is a pixel? (math)A pixel is not

a box a disk a teeny light

A pixel is a point it has no dimension it occupies no area it cannot be seen it has coordinates

A pixel is a sample

From: http://groups.csail.mit.edu/graphics/classes/6.837/F01/Lecture05/lecture05.pdf18

13

14

15

16

17

18

Page 6: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafał Mantiuk 4

Michaelmas Term 2021/2022

Sampling and quantization Physical world is described in terms of continuous quantities But computers work only with discrete numbers Sampling – process of mapping continuous function to a

discrete oneQuantization – process of mapping continuous variable to a

discrete one

19

20

Computer Graphics & Image Processing

BackgroundRendering

Perspective Reflection of light from surfaces and shading Geometric models Ray tracing

Graphics pipelineGraphics hardware and modern OpenGLHuman vision and colour & tone mapping

Depth cues21

Rendering depth22

23

Perspective in photographs

Gates Building – the rounded version(Stanford)

Gates Building – the rectilinear version(Cambridge)

Early perspective

Presentation at the Temple

Ambrogio Lorenzetti 1342Uffizi Gallery

Florence

24

19

20

21

22

23

24

Page 7: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafał Mantiuk 5

Michaelmas Term 2021/2022

Wrong perspective

Adoring saintsLorenzo Monaco

1407-09National Gallery

London

25

Renaissance perspective

Geometrical perspectiveFilippo Brunelleschi 1413

Holy Trinity frescoMasaccio (Tommaso di Ser Giovanni

di Simone) 1425Santa Maria Novella

FlorenceDe pictura (On painting)

textbook by Leon Battista Alberti1435

26

More perspective

The Annunciationwith Saint Emidius

Carlo Crivelli 1486National Gallery London

27

False perspective28

Calculating perspective

29

Ray tracing

Identify point on surface and calculate illuminationGiven a set of 3D objects, shoot a ray from the eye

through the centre of every pixel and see what surfaces it hits

30

shoot a ray through each pixel whatever the ray hits determines the colour of that pixel

[FCG 4]

25

26

27

28

29

30

Page 8: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafał Mantiuk 6

Michaelmas Term 2021/2022

31Ray tracing: examples

ray tracing easily handles reflection, refraction, shadows and blur

ray tracing is computationally expensive

32

Ray tracing algorithm

select an eye point and a screen plane

FOR every pixel in the screen planedetermine the ray from the eye through the pixel’s centreFOR each object in the scene

IF the object is intersected by the rayIF the intersection is the closest (so far) to the eye

record intersection point and objectEND IF ;

END IF ;END FOR ;set pixel’s colour to that of the object at the closest intersection point

END FOR ;

33

Intersection of a ray with an object 1 plane

polygon or disc intersection the ray with the plane of the polygon

as above then check to see whether the intersection point lies inside the polygon

a 2D geometry problem (which is simple for a disc)

OD

rayplane

: ,:

P O sD sP N d

00

N

s d N ON D

34

Intersection of a ray with an object 2 sphere

cylinder, cone, torus all similar to sphere try them as an exercise

OD C

r

adbs

adbs

acbd

rCOCOcCODb

DDa

2

2

4

2

2

1

2

2

d real d imaginary

0)()( :sphere0,:ray

2

rCPCPssDOP

35

Ray tracing: shading once you have the intersection of a

ray with the nearest object you can also: calculate the normal to the object at

that intersection point shoot rays from that point to all of the

light sources, and calculate the diffuse and specular reflections off the object at that point

this (plus ambient illumination) gives the colour of the object (at that point)

OD C

r

N

light 1

light 2

36

Ray tracing: shadows because you are tracing

rays from the intersection point to the light, you can check whether another object is between the intersection and the light and is hence casting a shadow also need to watch for self-

shadowingO

D Cr

N

light 1

light 2

light 3

31

32

33

34

35

36

Page 9: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafał Mantiuk 7

Michaelmas Term 2021/2022

37

Ray tracing: reflection if a surface is totally or

partially reflective then new rays can be spawned to find the contribution to the pixel’s colour given by the reflection this is perfect (mirror)

reflection

O

N1

lightN2

38

Ray tracing: transparency & refraction objects can be totally or

partially transparent this allows objects behind the

current one to be seen through it

transparent objects can have refractive indices bending the rays as they pass

through the objects

transparency + reflection means that a ray can split into two parts

O

light

D0

D1

D'1

D'2

D2

Illumination and shading

Dürer’s method allows us to calculate what part of the scene is visible in any pixel

But what colour should it be?Depends on:

lighting shadows properties of surface material

39

[FCG 4.5-4.8]

40

How do surfaces reflect light?

perfect specularreflection(mirror)

Imperfect specularreflection

diffuse reflection(Lambertian reflection)

Johann Lambert, 18th century German mathematician

the surface of a specular reflector is facetted, each facet reflects perfectly but in a slightly different direction to the other facets

41

Comments on reflection

the surface can absorb some wavelengths of light e.g. shiny gold or shiny copper

specular reflection has “interesting” properties at glancing angles owing to occlusion of micro-facets by one another

plastics are good examples of surfaces with: specular reflection in the light’s colour diffuse reflection in the plastic’s colour

42

Calculating the shading of a surface gross assumptions:

there is only diffuse (Lambertian) reflection all light falling on a surface comes directly from a light source

there is no interaction between objects no object casts shadows on any other

so can treat each surface as if it were the only object in the scene light sources are considered to be infinitely distant from the object

the vector to the light is the same across the whole surface

observation: the colour of a flat surface will be uniform across it, dependent only on the

colour & position of the object and the colour & position of the light sources

37

38

39

40

41

42

Page 10: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafał Mantiuk 8

Michaelmas Term 2021/2022

43

Diffuse shading calculation

L is a normalised vector pointing in the direction of the light source

N is the normal to the surface

Il is the intensity of the light source

kd is the proportion of light which is diffusely reflected by the surface

I is the intensity of the light reflected by the surface

LN

I I kI k N L

l d

l d

cos( )

use this equation to calculate the colour of a pixel

44

Diffuse shading: comments can have different Il and different kd for different wavelengths

(colours) watch out for cos < 0

implies that the light is behind the polygon and so it cannot illuminate this side of the polygon

do you use one-sided or two-sided surfaces? one sided: only the side in the direction of the normal vector can be

illuminated if cos < 0 then both sides are black

two sided: the sign of cos determines which side of the polygon is illuminated

need to invert the sign of the intensity for the back side this is essentially a simple one-parameter ( BRDF

45Specular reflection

Phong developed an easy-to-calculate approximation to specular reflection

NR

V

L

L is a normalised vector pointing in the direction of the light source

R is the vector of perfect reflectionN is the normal to the surfaceV is a normalised vector pointing at the

viewerIl is the intensity of the light sourceks is the proportion of light which is

specularly reflected by the surfacen is Phong’s ad hoc “roughness” coefficientI is the intensity of the specularly reflected

light

I I kI k R V

l sn

l sn

cos( )

Phong Bui-Tuong, “Illumination for computer generated pictures”, CACM, 18(6), 1975, 311–7

n=1 n=3 n=7 n=20 n=40

46

Examples

diffuse reflection

specular reflection

100% 75% 50% 25% 0%

100%

75%

50%

25%

0%

47

Shading: overall equation the overall shading equation can thus be considered to be the

ambient illumination plus the diffuse and specular reflections from each light source

the more lights there are in the scene, the longer this calculation will take

NRi

V

Li

I I k I k L N I k R Va a i d i i s in

ii

( ) ( )

48

The gross assumptions revisited diffuse reflection approximate specular reflection no shadows

need to do ray tracing or shadow mapping to get shadows

lights at infinity can add local lights at the expense of more calculation

need to interpolate the L vector

no interaction between surfaces cheat!

assume that all light reflected off all other surfaces onto a given surface can be amalgamated into a single constant term: “ambient illumination”, add this onto the diffuse and specular illumination

43

44

45

46

47

48

Page 11: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafał Mantiuk 9

Michaelmas Term 2021/2022

49

Sampling we have assumed so far that each ray

passes through the centre of a pixel i.e. the value for each pixel is the colour of

the object which happens to lie exactly under the centre of the pixel

this leads to: stair step (jagged) edges to objects small objects being missed completely thin objects being missed completely or

split into small pieces

50

Anti-aliasing these artefacts (and others) are jointly known as aliasing methods of ameliorating the effects of aliasing are known as

anti-aliasing

in signal processing aliasing is a precisely defined technical term for a particular kind of artefact

in computer graphics its meaning has expanded to include most undesirable effects that can occur in the image

this is because the same anti-aliasing techniques which ameliorate true aliasing artefacts also ameliorate most of the other artefacts

51

Sampling in ray tracing single point

shoot a single ray through the pixel’s centre

super-sampling for anti-aliasing shoot multiple rays through the pixel

and average the result regular grid, random, jittered, Poisson

disc

adaptive super-sampling shoot a few rays through the pixel,

check the variance of the resulting values, if similar enough stop, otherwise shoot some more rays

52

Types of super-sampling 1 regular grid

divide the pixel into a number of sub-pixels and shoot a ray through the centre of each

problem: can still lead to noticable aliasing unless a very high resolution sub-pixel grid is used

random shoot N rays at random points in the pixel replaces aliasing artefacts with noise artefacts

the eye is far less sensitive to noise than to aliasing

12 8 4

53

Types of super-sampling 2 Poisson disc

shoot N rays at random points in the pixel with the proviso that no two rays shall pass through the pixel closer than e to one another

for N rays this produces a better looking image than pure random sampling

very hard to implement properly

Poisson disc pure random

54

Types of super-sampling 3 jittered

divide pixel into N sub-pixels and shoot one ray at a random point in each sub-pixel

an approximation to Poisson disc sampling

for N rays it is better than pure random sampling

easy to implement

jittered pure randomPoisson disc

49

50

51

52

53

54

Page 12: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafał Mantiuk 10

Michaelmas Term 2021/2022

55More reasons for wanting to takemultiple samples per pixel

super-sampling is only one reason why we might want to take multiple samples per pixel

many effects can be achieved by distributing the multiple samples over some range called distributed ray tracing

N.B. distributed means distributed over a range of values

can work in two wayseach of the multiple rays shot through a pixel is allocated a random value from

the relevant distribution(s) all effects can be achieved this way with sufficient rays per pixel

each ray spawns multiple rays when it hits an object this alternative can be used, for example, for area lights

56

Examples of distributed ray tracing distribute the samples for a pixel over the pixel area

get random (or jittered) super-sampling used for anti-aliasing

distribute the rays going to a light source over some area allows area light sources in addition to point and directional light sources produces soft shadows with penumbrae

distribute the camera position over some area allows simulation of a camera with a finite aperture lens produces depth of field effects

distribute the samples in time produces motion blur effects on any moving objects

57

Anti-aliasing

one sample per pixel multiple samples per pixel

58

Area vs point light source

an area light source produces soft shadows a point light source produces hard shadows

59Finite aperture

1, 120

left, a pinhole camera

below, a finite aperture camera

below left, 12 samples per pixel

below right, 120 samples per pixel

note the depth of field blur: only objects at the correct distance are in focus

60

Introduction to Computer Graphics

BackgroundRenderingGraphics pipeline

Polygonal mesh models Transformations using matrices in 2D and 3D Homogeneous coordinates Projection: orthographic and perspective

RasterizationGraphics hardware and modern OpenGLHuman vision, colour and tone mapping

55

56

57

58

59

60

Page 13: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafał Mantiuk 11

Michaelmas Term 2021/2022

Unfortunately…Ray tracing is computationally expensive

used for super-high visual qualityVideo games and user interfaces need something fasterMost real-time applications rely on rasterization

Model surfaces as polyhedra – meshes of polygons Use composition to build scenes Apply perspective transformation

and project into plane of screen Work out which surface was closest Fill pixels with colour of nearest visible polygon

Modern graphics cards have hardware to support thisRay tracing starts to appear in real-time rendering

The latest generation of GPUs offers accelerated ray-tracing But it still not as efficient as rasterization

61

Three-dimensional objects Polyhedral surfaces are made up from

meshes of multiple connected polygons

Polygonal meshes open or closed manifold or non-manifold

Curved surfaces must be converted to polygons to be drawn

62

63

Surfaces in 3D: polygons

Easier to consider planar polygons 3 vertices (triangle) must be planar > 3 vertices, not necessarily planar

this vertex is in front of the other

three, which are all in the same plane

a non-planar “polygon” rotate the polygon

about the vertical axis

should the result be thisor this?

64

Splitting polygons into triangles Most Graphics Processing Units (GPUs) are optimised to

draw triangles Split polygons with more than three vertices into triangles

which is preferable?

?

65

2D transformations scale

rotate

translate

(shear)

why? it is extremely useful to be

able to transform predefined objects to an arbitrary location, orientation, and size

any reasonable graphics package will include transforms 2D Postscript 3D OpenGL

[FCG 6]

66

Basic 2D transformations scale

about origin by factor m

rotate about origin by angle

translate along vector (xo,yo)

shear parallel to x axis by factor a

x mxy my''

x x yy x y' cos sin' sin cos

x x xy y y

o

o

''

x x ayy y''

61

62

63

64

65

66

Page 14: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafał Mantiuk 12

Michaelmas Term 2021/2022

67

Matrix representation of transformations scale

about origin, factor m

do nothing identity

xy

mm

xy

''

0

0

xy

xy

''

1 00 1

xy

a xy

''

10 1

rotate about origin, angle

shear parallel to x axis, factor a

xy

xy

''

cos sinsin cos

68

Homogeneous 2D co-ordinates translations cannot be represented using simple 2D matrix

multiplication on 2D vectors, so we switch to homogeneous co-ordinates

an infinite number of homogeneous co-ordinates map to every 2D point

w=0 represents a point at infinity usually take the inverse transform to be:

( , , ) ,x y w xw

yw

( , ) ( , , )x y x y 1

[FCG 6.3]

69

Matrices in homogeneous co-ordinates scale

about origin, factor m

do nothing identity

xyw

mm

xyw

'''

0 00 00 0 1

rotate about origin, angle

shear parallel to x axis, factor a

xyw

xyw

'''

cos sinsin cos

00

0 0 1

xyw

a xyw

'''

1 00 1 00 0 1

xyw

xyw

'''

1 0 00 1 00 0 1

70

Translation by matrix algebra

xyw

xy

xyw

o'''

1 00 10 0 1

0

w w'y y wyo' x x wxo'

xw

xw

x'' 0 0'

' ywy

wy

In conventional coordinates

In homogeneous coordinates

71

Concatenating transformations often necessary to perform more than one transformation on the

same object can concatenate transformations by multiplying their matrices

e.g. a shear followed by a scaling:

xyw

mm

xyw

xyw

a xyw

' '' '' '

'''

'''

0 00 00 0 1

1 00 1 00 0 1

xyw

mm

a xyw

m mam

xyw

' '' '' '

0 00 00 0 1

1 00 1 00 0 1

00 00 0 1

shearscale

shearscale both

72

Transformation are not commutative

be careful of the order in which you concatenate transformations

rotate by 45°

scale by 2along x axis

rotate by 45°

scale by 2along x axis

22

22

12

12

22

12

22

12

12

12

12

12

00

0 0 1

2 0 00 1 00 0 1

00

0 0 1

00

0 0 1

scale

rotatescale then rotate

rotate then scale

67

68

69

70

71

72

Page 15: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafał Mantiuk 13

Michaelmas Term 2021/2022

73

Scaling about an arbitrary point scale by a factor m about point (xo,yo)

translate point (xo,yo) to the originscale by a factor m about the origintranslate the origin to (xo,yo)

(xo,yo)

(0,0)

xyw

xy

xyw

o

o

'''

1 00 10 0 1

xyw

mm

xyw

' '' '' '

'''

0 00 00 0 1

xyw

xy

xyw

o

o

' ' '' ' '' ' '

' '' '' '

1 00 10 0 1

xyw

xy

mm

xy

xyw

o

o

o

o

' ' '' ' '' ' '

1 00 10 0 1

0 00 00 0 1

1 00 10 0 1

Exercise: show how to perform rotation about an arbitrary point

74

3D transformations 3D homogeneous co-ordinates

3D transformation matrices

( , , , ) ( , , )x y z w xw

yw

zw

1 0 0 00 1 0 00 0 1 00 0 0 1

mm

m

x

y

z

0 0 00 0 00 0 00 0 0 1

1 0 00 1 00 0 10 0 0 1

ttt

x

y

z

cos sinsin cos

0 00 0

0 0 1 00 0 0 1

1 0 0 00 00 00 0 0 1

cos sinsin cos

cos sin

sin cos

0 00 1 0 0

0 00 0 0 1

translation identity

scale

rotation about x-axis

rotation about y-axisrotation about z-axis

75

3D transformations are not commutative

x

yz

x

xz

z

x

yz

90° rotation about z-axis

90° rotation about x-axis

90° rotation about z-axis

90° rotation about x-axis

opposite faces

76

Model transformation 1 the graphics package Open Inventor defines a cylinder to be:

centre at the origin, (0,0,0) radius 1 unit height 2 units, aligned along the y-axis

this is the only cylinder that can be drawn,but the package has a complete set of 3D transformations

we want to draw a cylinder of: radius 2 units the centres of its two ends

located at (1,2,3) and (2,4,5) its length is thus 3 units

what transforms are required?and in what order should they be applied?

x

y

2

2

Model transformation 2

order is important: scale first rotate translate last

scaling and translation are straightforward

77

x

y

2

2

x

y

3

4

10000200005.100002

S

1000410030105.1001

T

translate centre of cylinder from (0,0,0) to halfway between (1,2,3)

and (2,4,5)

scale fromsize (2,2,2)

to size (4,3,4)

S

Model transformation 3

rotation is a multi-step process break the rotation into steps, each of which is rotation

about a principal axis work these out by taking the desired orientation back to

the original axis-aligned position

the centres of its two ends located at (1,2,3) and (2,4,5)

desired axis: (2,4,5)–(1,2,3) = (1,2,2)

original axis: y-axis = (0,1,0)

78

73

74

75

76

77

78

Page 16: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafał Mantiuk 14

Michaelmas Term 2021/2022

Model transformation 4 desired axis: (2,4,5)–(1,2,3) = (1,2,2) original axis: y-axis = (0,1,0)

zero the z-coordinate by rotating about the x-axis

79

22

1

222arcsinθ

10000θcosθsin00θsinθcos00001

R

y

z

)2,2,1(

)0,8,1(

0,22,1 22

Model transformation 5

then zero the x-coordinate by rotating about the z-axis we now have the object’s axis pointing along the y-axis

80

22

2

81

1arcsinφ

1000010000φcosφsin00φsinφcos

R

x

y)0,8,1(

)0,3,0(

0,81,022

Model transformation 6

the overall transformation is: first scale then take the inverse of the rotation we just calculated finally translate to the correct position

81

wzyx

wzyx

SRRT 12

11

''''

Application: display multiple instances transformations allow you to define an object at one

location and then place multiple instances in your scene

82

83

3D 2D projectionto make a picture

3D world is projected to a 2D image like a camera taking a photograph the three dimensional world is projected onto a plane

The 3D world is described as a set of (mathematical) objects

e.g. sphere radius (3.4)centre (0,2,9)

e.g. box size (2,4,3)centre (7, 2, 9)orientation (27º, 156º)

84

Types of projection

parallel e.g. useful in CAD, architecture, etc looks unrealistic

perspective e.g. things get smaller as they get farther away looks realistic

this is how cameras work

( , , ) ( , )x y z x y

( , , ) ( , )x y z xz

yz

79

80

81

82

83

84

Page 17: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafał Mantiuk 15

Michaelmas Term 2021/2022

85

Geometry of perspective projection

y

z

d

( , , )x y z( ' , ' , )x y d

x x dz

y y dz

'

'

( , , )0 0 0

86

Projection as a matrix operation

x x dz

y y dz

'

'

10/100/100000100001

//1 z

yx

dd

dzd

yx

zz 1'

This is useful in the z-buffer algorithm where we need to interpolate 1/z values rather than z values.

wzwywx

wzyx

///

remember

87Perspective projection

with an arbitrary camera we have assumed that:

screen centre at (0,0,d) screen parallel to xy-plane z-axis into screen y-axis up and x-axis to the right eye (camera) at origin (0,0,0)

for an arbitrary camera we can either: work out equations for projecting objects about an arbitrary point

onto an arbitrary plane transform all objects into our standard co-ordinate system (viewing

co-ordinates) and use the above assumptions

88

A variety of transformations

the modelling transform and viewing transform can be multiplied together to produce a single matrix taking an object directly from object co-ordinates into viewing co-ordinates

either or both of the modelling transform and viewing transform matrices can be the identity matrix

e.g. objects can be specified directly in viewing co-ordinates, or directly in world co-ordinates

this is a useful set of transforms, not a hard and fast model of how things should be done

object inworld

co-ordinates

object inviewing

co-ordinatesviewing transform

object in2D screen

co-ordinatesprojection

object inobject

co-ordinates modelling transform

Model, View, Projection matrices

Object coordinates

Object centred at the origin

World coordinates

Model matrix

To position each object in the scene. Could be different for each object.

89

Model, View, Projection matrices

World coordinates

View matrix

View (camera) coordinates

Camera at the origin, pointing at -z

To position all objects relative to the camera

90

90

85

86

87

88

89

90

Page 18: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafał Mantiuk 16

Michaelmas Term 2021/2022

Model, View, Projection matrices

Projection matrix

View (camera) coordinates

Screen coordinates

x and y must be in the range-1 and 1

To project 3D coordinates to a 2D plane. Note that zcoordinate is retained for depth testing.

91

The default OpenGL coordinate system is

right-handed

91

All together

3D world vertex

coordinates

Screen coordinates

xs/ws and ys/ws must be

between -1 and 1

Projection, view and model matrices

92

92

𝑥𝑦𝑧𝑤 = 𝑃 𝑉 𝑀 𝑥𝑦𝑧𝑤

93

Viewing transformation 1

the problem: to transform an arbitrary co-ordinate system to the

default viewing co-ordinate system

camera specification in world co-ordinates eye (camera) at (ex,ey,ez) look point (centre of screen) at (lx,ly,lz) up along vector (ux,uy,uz)

perpendicular to

worldco-ordinates

viewingco-ordinatesviewing

transform

u

e

l

el

94

Viewing transformation 2 translate eye point, (ex,ey,ez), to origin, (0,0,0)

scale so that eye point to look point distance, , is distance from origin to screen centre, d

el

T

1 0 00 1 00 0 10 0 0 1

eee

x

y

z

el S

el

el

el

( ) ( ) ( )l e l e l ex x y y z z

d

d

d

2 2 2

0 0 00 0 00 0 00 0 0 1

95

Viewing transformation 3 need to align line with z-axis

first transform e and l into new co-ordinate system

then rotate e''l'' into yz-plane, rotating about y-axis

el

e S T e 0 l S T l'' ''

𝐑 = cos θ 0 sin θ 00 1 0 0−sin θ 0 cos θ 00 0 0 1x

z

( ' ' , ' ' , ' ' )l l lx y z

0 2 2, ' ' , ' ' ' 'l l ly x z

θ = arccos 𝑙′′𝑙′′ + 𝑙′′

96

Viewing transformation 4 having rotated the viewing vector onto the yz plane, rotate it

about the x-axis so that it aligns with the z-axis

22

2

'''''''''arccosφ

10000φcosφsin00φsinφcos00001

zy

z

lll

R

y

z

( , ' ' ' , ' ' ' )0 l ly z

0 0

0 0

2 2, , ' ' ' ' ' '

( , , )

l l

dy z

l R l''' '' 1

91

92

93

94

95

96

Page 19: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafał Mantiuk 17

Michaelmas Term 2021/2022

97

Viewing transformation 5 the final step is to ensure that the up vector actually points up,

i.e. along the positive y-axis actually need to rotate the up vector about the z-axis so that it lies in the

positive y half of the yz plane

u R R u'''' 2 1why don’t we need to multiply u by S or T?

u is a vector rather than a point, vectors do not get translated

scaling u by a uniform scaling matrix would make no difference to the direction in which it points

22

3

''''''''

''''arccosψ

1000010000ψcosψsin00ψsinψcos

yx

y

uu

u

R

98

Viewing transformation 6

we can now transform any point in world co-ordinates to the equivalent point in viewing co-ordinate

in particular: the matrices depend only on e, l, and u, so they can be pre-

multiplied together

worldco-ordinates

viewingco-ordinatesviewing

transform

xyzw

xyzw

''''

R R R S T3 2 1

e l ( , , ) ( , , )0 0 0 0 0 d

M R R R S T 3 2 1

Transforming normal vectors Transformation by

a nonorthogonal matrixdoes not preserve angles

Since:

We can find that: Derivation shown on the visualizer

99

𝑁 𝑇 = 0𝑁 𝑇 = 𝐺𝑁 MT = 0Normal transform

Vertex position transformTransformed normal

and tangent vector 𝐺 = 𝑀

1 0 00 2 00 0 1

99

[FCG 6.2.2]

Scene constructionWe will build a robot from

basic partsBody transformation𝑀 =?Arm1 transformation𝑀 =?Arm 2 transformation𝑀 =?

100

Body

Arm1Arm2

Part

s

Fina

l sce

ne

Object coordinates

World coordinates

Scene constructionBody transformation𝐸 = 𝑠𝑐𝑎𝑙𝑒 12𝑇 = 𝑡𝑟𝑎𝑛𝑠𝑙𝑎𝑡𝑒 𝑥𝑦 𝑟𝑜𝑡𝑎𝑡𝑒(30 )𝑀 = 𝑇 𝐸Arm1 transformation𝑇 = 𝑡𝑟𝑎𝑛𝑠𝑙𝑎𝑡𝑒 11.75 𝑟𝑜𝑡𝑎𝑡𝑒(−90 )𝑀 = 𝑇 𝑇Arm2 transformation𝑇 = 𝑡𝑟𝑎𝑛𝑠𝑙𝑎𝑡𝑒 02 𝑟𝑜𝑡𝑎𝑡𝑒(−90 )𝑀 = 𝑇 𝑇 𝑇

101

Body

Arm1Arm2

Scene GraphA scene can be drawn by

traversing a scene graph:

traverse( node, T_parent ) {M = T_parent * node.T * node.Enode.draw(M)for each child {

traverse( child, T_parent * node.T ) }

}

102

[FCG 12.2]

97

98

99

100

101

102

Page 20: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics Michaelmas Term 2021/2022

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafal Mantiuk

Introduction to Computer Graphics

103

BackgroundRenderingGraphics pipelineRasterizationGraphics hardware and modern OpenGLHuman vision and colour & tone mapping

Rasterization algorithm(*)

104

Set model, view and projection (MVP) transformations

FOR every triangle in the scenetransform its vertices using MVP matricesIF the triangle is within a view frustum

clip the triangle to the screen borderFOR each fragment in the triangle

interpolate fragment position and attributes between verticescompute fragment colourIF the fragment is closer to the camera than any pixel drawn so far

update the screen pixel with the fragment colourEND IF ;

END FOR ;END IF ;

END FOR ;

(*) simplified

fragment – a candidate pixel in the triangle

Illumination & shading Drawing polygons with uniform colours gives poor results Interpolate colours across polygons

105

Rasterization Efficiently draw (thousands of) triangles

Interpolate vertex attributes inside the triangle

106

Homogenous barycentric coordinates are used to interpolate colours, normals, texture coordinates and other attributesinside the triangle

[FCG 2.7]

Homogenous barycentric coordinates Find barycentric

coordinates of the point (x,y) Given the coordinates of

the vertices Derivation in the lecture

107

𝛼 = ( , )( , ) 𝛽 = ( , )( , )𝑓 𝑥, 𝑦 is the implicit line equation:𝑓 𝑥, 𝑦 = 𝑦 − 𝑦 𝑥 + 𝑥 − 𝑥 𝑦 + 𝑥 𝑦 − 𝑥 𝑦

Triangle rasterization

Optimization: the barycentric coordinates will change by the same amount when moving one pixel right (or one pixel down) regardless of the position Precompute increments Δ𝛼, Δ𝛽, Δ𝛾 and use them instead of

computing barycentric coordinates when drawing pixels sequentially

108

for y=ymin to ymax dofor x=xmin to xmax do𝛼 = 𝑓 (𝑥, 𝑦)/𝑓 (𝑥 , 𝑦 )𝛽 = 𝑓 (𝑥, 𝑦)/𝑓 (𝑥 , 𝑦 )𝛾 = 1 − 𝛼 − 𝛽

if (𝛼 > 0 and 𝛽 > 0 and 𝛾 > 0 ) then𝑐 = 𝛼𝑐 + 𝛽𝑐 + 𝛾𝑐draw pixels (x,y) with colour c

103 104

105 106

107 108

Page 21: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics Michaelmas Term 2021/2022

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafal Mantiuk

Surface normal vector interpolation for a polygonal model, interpolate normal vector between the

vertices Calculate colour (Phong reflection model) for each pixel Diffuse component can be either interpolated or computed for each

pixel

N.B. Phong’s approximation tospecular reflection ignores(amongst other things) theeffects of glancing incidence(the Fresnel term)

109

[( ' , ' ), , ( , , ), ]x y z r g b1 1 1 1 1 1 1N

[( ' , ' ), ,( , , ) , ]x y zr g b2 2 2

2 2 2 2N

[ ( ' , ' ) , , ( , , ) , ]x y z r g b3 3 3 3 3 3 3N

Occlusions (hidden surfaces)

110

Simple case

More difficult cases

[FCG 8.2.3]

Z-Buffer - algorithm

Initialize the depth buffer and image buffer for all pixelscolour(x, y) = Background_colour,depth(x, y) = zmax // position of the far clipping plane

For every triangle in a scene do For every fragment (x, y) in this triangle do

Calculate z for current (x, y) if (z < depth(x, y)) and (z > zmin) then

depth(x, y) = z colour(x, y) = fragment_colour(x, y)

111

Colourbuffer

Depthbuffer

View frustum Controlled by camera parameters: near-, far-clipping

planes and field-of-view

Far-clipping plane

FOV

Near-clipping plane

Z-buffer must store all these values

112

Introduction to Computer Graphics

113

BackgroundRenderingGraphics pipelineRasterizationGraphics hardware and modern OpenGL

GPU & APIs OpenGL Rendering pipeline Example OpenGL code GLSL Textures Raster buffers

Human vision, colour & tone mapping

What is a GPU? Graphics Processing Unit Like CPU (Central

Processing Unit) but for processing graphics

Optimized for floating point operations on large arrays of data Vertices, normals, pixels, etc.

114

109 110

111 112

113 114

Page 22: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics Michaelmas Term 2021/2022

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafal Mantiuk

What does a GPU do Performs all low-level tasks & a lot of high-level tasks

Clipping, rasterisation, hidden surface removal, … Essentially draws millions of triangles very efficiently

Procedural shading, texturing, animation, simulation, … Video rendering, de- and encoding, deinterlacing, ... Physics engines

Full programmability at several pipeline stages fully programmable but optimized for massively parallel operations

115

What makes GPU so fast? 3D rendering can be very efficiently parallelized

Millions of pixels Millions of triangles Many operations executed independently at the same time

This is why modern GPUs Contain between hundreds and thousands of SIMD processors

Single Instruction Multiple Data – operate on large arrays of data

>>400 GB/s memory access This is much higher bandwidth than CPU But peak performance can be expected for very specific operations

116

GPU APIs (Application Programming Interfaces)

OpenGL Multi-platform Open standard API Focus on general 3D

applications Open GL driver manages

the resources

DirectX Microsoft Windows / Xbox Proprietary API Focus on games

Application manages resources

117

One more API Vulkan – cross platform, open standard Low-overhead API for high performance 3D graphics Compared to OpenGL / DirectX

Reduces CPU load Better support of multi-CPU-core architectures Finer control of GPU

But The code for drawing a few primitives can take 1000s line of

code Intended for game engines and code that must be very well

optimized

118

And one more Metal (Apple iOS8)

low-level, low-overhead 3D GFX and compute shaders API Support for Apple A7, Intel HD and Iris, AMD, Nvidia Similar design as modern APIs, such as Vulcan Swift or Objective-C API Used mostly on iOS

119

GPGPU - general purpose computing OpenGL and DirectX are not meant to be used for

general purpose computing Example: physical simulation, machine learning

CUDA – Nvidia’s architecture for parallel computing C-like programming language With special API for parallel instructions Requires Nvidia GPU

OpenCL – Similar to CUDA, but open standard Can run on both GPU and CPU Supported by AMD, Intel and NVidia, Qualcomm, Apple, …

120

115 116

117 118

119 120

Page 23: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics Michaelmas Term 2021/2022

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafal Mantiuk

GPU and mobile devices OpenGL ES 1.0-3.2

Stripped version of OpenGL Removed functionality that is not strictly necessary on mobile

devices

Devices iOS: iPhone, iPad Android phones PlayStation 3 Nintendo 3DS and many more

OpenGL ES 2.0 rendering (iOS)

121

WebGL JavaScript library for 3D rendering in a web browser WebGL 1.0 - based on OpenGL ES 2.0 WebGL 2.0 – based on OpenGL ES 3.0

Chrome and Firefox (2017)

Most modern browsers support WebGL

Potentially could be used to create 3D games in a browser and replace Adobe Flash

http://zygotebody.com/

122

OpenGL in Java Standard Java API does not include OpenGL interface But several wrapper libraries exist

Java OpenGL – JOGL Lightweight Java Game Library - LWJGL

We will use LWJGL 3 Seems to be better maintained Access to other APIs (OpenCL, OpenAL, …)

We also need a linear algebra library JOML – Java OpenGL Math Library Operations on 2, 3, 4-dimensional vectors and matrices

123

OpenGL History Proprietary library IRIS GL by SGI

OpenGL 1.0 (1992)

OpenGL 1.2 (1998)

OpenGL 2.0 (2004) GLSL Non-power-of-two (NPOT)

textures

OpenGL 3.0 (2008) Major overhaul of the API Many features from previous

versions depreciated

OpenGL 3.2 (2009) Core and Compatibility profiles

Geometry shaders

OpenGL 4.0 (2010) Catching up with Direct3D 11

OpenGL 4.5 (2014) OpenGL 4.6 (2017)

SPIR-V shaders

124

How to learn OpenGL? Lectures – algorithms behind OpenGL, general principles Tick 2 – detailed tutorial, learning by doing References

OpenGL Programming Guide: The Official Guide to Learning OpenGL, Version 4.5 with SPIR-V by John Kessenich, Graham Sellers, Dave Shreiner ISBN-10: 0134495497

OpenGL quick reference guidehttps://www.opengl.org/documentation/glsl/

Google search: „man gl......”

125

OpenGL rendering pipeline

121 122

123 124

125 126

Page 24: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics Michaelmas Term 2021/2022

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafal Mantiuk

OpenGL programming model

CPU code GPU code

gl* functions that Create OpenGL objects Copy data CPU<->GPU Modify OpenGL state Enqueue operations Synchronize CPU & GPU

C99 library Wrappers in most

programming language

Fragment shaders Vertex shaders and other shaders Written in GLSL

Similar to C From OpenGL 4.6 could be

written in other language and compiled to SPIR-V

127

OpenGL rendering pipeline

Vertex data

Vertex shader

Tessellation control shader

Tessellation evaluation shader

Geometry shaderClippingRasterization

Fragment shader

Screen buffer

Programmable stages

Fixed stages

Primitive assembly

128

OpenGL rendering pipeline

Vertex data

Vertex shader

Tessellation control shader

Tessellation evaluation shader

Geometry shader

ClippingRasterization

Fragment shader

Screen buffer

Programmable stages Fixed stages

Primitive assembly

Processing of vertices, normals, uv texture coordinates.

129

OpenGL rendering pipeline

Vertex data

Vertex shader

Tessellation control shader

Tessellation evaluation shader

Geometry shader

ClippingRasterization

Fragment shader

Screen buffer

Programmable stages Fixed stages

Primitive setup[Optional] Create new

primitives by tessellating existing primitives (patches).

130

OpenGL rendering pipeline

Vertex data

Vertex shader

Tessellation control shader

Tessellation evaluation shader

Geometry shader

ClippingRasterization

Fragment shader

Screen buffer

Programmable stages Fixed stages

Primitive assembly

[Optional] Operate on tessellated geometry. Can create new primitives.

131

fur shadow volumes

OpenGL rendering pipeline

Vertex data

Vertex shader

Tessellation control shader

Tessellation evaluation shader

Geometry shader

ClippingRasterization

Fragment shader

Screen buffer

Programmable stages Fixed stages

Primitive assembly

Organizes vertices into primitives and prepares them for

rendering.

132

127 128

129 130

131 132

Page 25: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics Michaelmas Term 2021/2022

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafal Mantiuk

OpenGL rendering pipeline

Vertex data

Vertex shader

Tessellation control shader

Tessellation evaluation shader

Geometry shaderClippingRasterization

Fragment shader

Screen buffer

Programmable stages

Fixed stages

Primitive assembly

Remove or modify vertices so that they all lie within the viewport (view frustum).

133

OpenGL rendering pipeline

Vertex data

Vertex shader

Tessellation control shader

Tessellation evaluation shader

Geometry shaderClippingRasterization

Fragment shader

Screen buffer

Programmable stages

Fixed stages

Primitive assembly

Generates fragments (pixels) to be drawn for each primitive.

Interpolates vertex attributes.

scanlines

134

OpenGL rendering pipeline

Vertex data

Vertex shader

Tessellation control shader

Tessellation evaluation shader

Geometry shaderClipping

Rasterization

Fragment shader

Screen buffer

Programmable stages Fixed stages

Primitive assembly

Computes colour per each fragment (pixel). Can lookup colour in the texture. Can modify pixels’ depth value.

Also used for tone mapping.

135

Non-Photorealistic-Renderingshader

Physically accuratematerials

Example: preparing vertex data for a cube

136

Indices

0, 1, 2

Ind Positions Normals

0 0, 0, 0 0, 0, -1

… … …

Vertex attributes

Primitives (triangles)

Geometry objects in OpenGL (OO view)

137

GLSL - fundamentals

133 134

135 136

137 138

Page 26: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics Michaelmas Term 2021/2022

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafal Mantiuk

Shaders Shaders are small programs executed on a GPU

Executed for each vertex, each pixel (fragment), etc.

They are written in GLSL (OpenGL Shading Language) Similar to C and Java Primitive (int, float) and aggregate data types (ivec3, vec3) Structures and arrays Arithmetic operations on scalars, vectors and matrices Flow control: if, switch, for, while Functions

139

Example of a vertex shader#version 330

in vec3 position; // vertex position in local space

in vec3 normal; // vertex normal in local space

out vec3 frag_normal; // fragment normal in world space

uniform mat4 mvp_matrix; // model-view-projection matrix

void main()

{

// Typicaly normal is transformed by the model matrix

// Since the model matrix is identity in our case, we do not modify normals

frag_normal = normal;

// The position is projected to the screen coordinates using mvp_matrix

gl_Position = mvp_matrix * vec4(position, 1.0);

}Why is this piece of code needed?

140

Data types Basic types

float, double, int, uint, bool

Aggregate types float: vec2, vec3, vec4; mat2, mat3, mat4 double: dvec2, dvec3, dvec4; dmat2, dmat3, dmat4 int: ivec2, ivec3, ivec4 uint: uvec2, uvec3, uvec4 bool: bvec2, bvec3, bvec4

vec3 V = vec3( 1.0, 2.0, 3.0 ); mat3 M = mat3( 1.0, 2.0, 3.0,4.0, 5.0, 6.0,7.0, 8.0, 9.0 );

141

Indexing components in aggregate types Subscripts: rgba, xyzw, stpq (work exactly the same)

float red = color.r; float v_y = velocity.y;but also float red = color.x; float v_y = velocity.g;

With 0-base index: float red = color[0]; float m22 = M[1][1]; // second row and column

// of matrix M

142

SwizzlingYou can select the elements of the aggregate type:vec4 rgba_color( 1.0, 1.0, 0.0, 1.0 );vec3 rgb_color = rgba_color.rgb;vec3 bgr_color = rgba_color.bgr;vec3 luma = rgba_color.ggg;

143

Arrays Similar to Cfloat lut[5] = float[5]( 1.0, 1.42, 1.73, 2.0, 2.23 );

Size can be checked with “length()”for( int i = 0; i < lut.length(); i++ ) {

lut[i] *= 2;}

144

139 140

141 142

143 144

Page 27: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics Michaelmas Term 2021/2022

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafal Mantiuk

Storage qualifiers const – read-only, fixed at compile time in – input to the shader out – output from the shader uniform – parameter passed from the application (Java),

constant for the drawn geometry buffer – shared with the application shared – shared with local work group (compute

shaders only)

Example: const float pi=3.14;

145

Shader inputs and outputs

Vertex shader

Fragmentshader

out vec3 frag_normal

in vec3 frag_normal

Vertex attribute interpolation

in vec3 position

in vec3 normal

out vec3 colour

ArrayBuffer (vertices)

ArrayBuffer (normals)glGetAttribLocationglBindBufferglVertexAttribPointerglEnableVertexAttribArray

FrameBuffer (pixels)[optional]glBindFragDataLocationorlayout(location=?) in GLSL

146

GLSL Operators Arithmetic: + - ++ --

Multiplication: vec3 * vec3 – element-wise mat4 * vec4 – matrix multiplication (with a column vector)

Bitwise (integer): <<, >>, &, |, ^ Logical (bool): &&, ||, ^^ Assignment: float a=0;a += 2.0; // Equivalent to a = a + 2.0

See the quick reference guide at: https://www.opengl.org/documentation/glsl/

147

GLSL Math Trigonometric:

radians( deg ), degrees( rad ), sin, cos, tan, asin, acos, atan, sinh, cosh, tanh, asinh, acosh, atanh

Exponential: pow, exp, log, exp2, log2, sqrt, inversesqrt

Common functions: abs, round, floor, ceil, min, max, clamp, …

And many more

See the quick reference guide at: https://www.opengl.org/documentation/glsl/

148

GLSL flow controlif( bool ) {// true

} else {// false

}

switch( int_value ) {case n:// statementsbreak;

case m:// statementsbreak;

default:

} for( int i = 0; i<10; i++ ) {

...}

while( n < 10 ) {...}

do {...} while ( n < 10 )

149

Simple OpenGL application - flow Initialize rendering window & OpenGL

context Send the geometry (vertices, triangles,

normals) to the GPU

Load and compile Shaders

Initialize OpenGL

Set up inputs

Draw a frame

Clear the screen buffer

Set the model-view-projection matrix Render geometry

Flip the screen buffers

Free resources

150

145 146

147 148

149 150

Page 28: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics Michaelmas Term 2021/2022

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafal Mantiuk

Rendering geometry To render a single object with OpenGL1. glUseProgram() – to activate vertex & fragment shaders2. glVertexAttribPointer() – to indicate which Buffers with vertices and normal should be input to fragment shader3. glUniform*() – to set uniforms (parameters of the fragment/vertex shader) 4. glBindTexture() – to bind the texture5. glBindVertexArray() – to bind the vertex array6. glDrawElements() – to queue drawing the geometry7. Unbind all objects OpenGL API is designed around the idea of a state-machine –

set the state & queue drawing command151

Textures

(Most important) OpenGL texture types

1Ds0 1

2Ds

t

0 10

1

s

t

p

3D

Texture can have any size but the sizes that are powers of two (POT, 2n) may give better performance.

CUBE_MAP Used for environment mapping

0

0

11

1

0

Texel

153

Texture mapping 1. Define your texture

function (image) T(u,v) (u,v) are texture

coordinates

154

0 10

1

Texture mapping 2. Define the correspondence

between the vertices on the 3D object and the texture coordinates

155

Texture mapping 3. When rendering, for every surface point compute

texture coordinates. Use the texture function to get texture value. Use as color or reflectance.

156

151 152

153 154

155 156

Page 29: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics Michaelmas Term 2021/2022

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafal Mantiuk

Sampling

Up-samplingMore pixels than texelsValues need to be interpolated

Down-samplingFewer pixels than texelsValues need to be averaged over an area of the texture (usually using a mipmap)

Texturev

u

157

Nearest neighbor vs. bilinear interpolation (upsampling)

A B

C DX

Interpolate first along x-axis between AB and CD, then along y-axis between the interpolated points.

158

A B

C DX

Nea

rest

nei

ghbo

ur

Bilin

ear

inte

rpol

atio

n

Pick the nearest texel: D

Texel

159

Texture mapping examples

nearest-neighbour

bilinear

u

v

160

Up-sampling

nearest-neighbour

blocky artefacts

bilinear

blurry artefacts

u

v

if one pixel in the texture map covers several pixels in the final image, you get visible artefacts

only practical way to prevent this is to ensure that texture map is of sufficiently high resolution that it does not happen

161

Down-sampling

if the pixel covers quite a large area of the texture, then it will be necessary to average the texture across that area, not just take a sample in the middle of the area

Mipmap Textures are often stored at multiple

resolutions as a mipmap Each level of the pyramid is half the size of

the lower level Mipmap resolution is always power-of-two

(1024, 512, 256, 128, ...)

It provides pre-filtered texture (area-averaged) when screen pixels are larger than the full resolution texels

Mipmap requires just an additional 1/3 of the original texture size to store

OpenGL can generate a mipmap with glGenerateMipmap(GL_TEXTURE_2D)

162

This image is an illustration showing only 1/3 increase in storeage. Mipmaps are stored differently in the GPU memory.

157 158

159 160

161 162

Page 30: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics Michaelmas Term 2021/2022

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafal Mantiuk

Down-samplingwithout area averaging with area averaging

163

Texture tiling Repetitive patterns can be represented as texture tiles. The texture folds over, so that

T(u=1.1, v=0) = T(u=0.1, v=0)

164

Gimp and other drawing software often offer plugins for creating tiled textures

Multi-surface UV maps A single texture is often used for multiple surfaces and

objects

Example from: http://awshub.com/blog/blog/2011/11/01/hi-poly-vs-low-poly/

165

Bump (normal) mapping Special kind of texture that

modifies surface normal Surface normal is a vector

that is perpendicular to a surface

The surface is still flat but shading appears as on an uneven surface

Easily done in fragment shaders

166

Displacement mapping Texture that modifies surface Better results than bump

mapping since the surface is not flat

Requires geometry shaders

167

Environment mapping To show environment

reflected by an object

168

163 164

165 166

167 168

Page 31: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics Michaelmas Term 2021/2022

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafal Mantiuk

Environment mapping Environment cube Each face captures

environment in that direction

169

Texture objects in OpenGL

170

Texture parameters//Setup filtering, i.e. how OpenGL will interpolate the pixels when scaling up or downglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);

//Setup wrap mode, i.e. how OpenGL will handle pixels outside of the expected rangeglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

How to interpolate in

2D

How to interpolate between mipmap

levels

171

Raster buffers (colour, depth, stencil)

Render buffers in OpenGL

GL_FRONT GL_BACKColour:

Depth:

GL_FRONT_LEFT GL_FRONT_RIGHT

GL_BACK_LEFT GL_BACK_RIGHT

DEPTH

Stencil: STENCIL

In stereo:

Four components:RGBA

Typically 8 bits per component

To block rendering selected pixelsSingle component, usually 8 bits.

To resolve occlusions (see Z-buffer algorithm)Single component, usually >8 bits

173

Double buffering To avoid flicker, tearing Use two buffers (rasters):

Front buffer – what is shown on the screen Back buffer – not shown, GPU draws into that buffer

When drawing is finished, swap front- and back-buffers

Front buffer – displayBack buffer - draw

1st buffer2nd buffer

time

174

169 170

171 172

173 174

Page 32: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics Michaelmas Term 2021/2022

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafal Mantiuk

Triple buffering Do not wait for swapping to start drawing the next

frame

Shortcomings More memory needed Higher delay between drawing and displaying a frame

Front buffer – displayBack buffer - draw

1st buffer2nd buffer

time

Front buffer – displayBack buffer - draw

time

3rd buffer

Double buffering

Get rid of these gaps

Triple buffering

175

Vertical Synchronization: V-Sync Pixels are copied from colour buffer to monitor row-by-row If front & back buffer are swapped during this process:

Upper part of the screen contains previous frame Lower part of the screen contains current frame Result: tearing artefact

Solution: When V-Sync is enabled glwfSwapInterval(1);

glSwapBuffers() waits until the last row of pixels is copied to the display.

176

No V-Sync vs. V-Sync

177

No

V-Sy

ncV-

Sync

FreeSync (AMD) & G-Sync (Nvidia) Adaptive sync

Graphics card controls timing of the frames on the display Can save power for 30fps video of when the screen is static Can reduce lag for real-time graphics

178

Vision, colour and colour spaces

179 180

The workings of the human visual system to understand the requirements of displays (resolution,

quantisation and colour) we need to know how the human eye works...

The lens of the eye forms an image of the world on the retina: the back surface of the eye

Inverted vision experiment

175 176

177 178

179 180

Page 33: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics Michaelmas Term 2021/2022

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafal Mantiuk

181

Structure of the human eye the retina is an array of

light detection cells the fovea is the high

resolution area of the retina

the optic nerve takes signals from the retina to the visual cortex in the brain

cornea and lens focus the light on the retina

pupil shrinks and expands to control the amount of light

See Animagraffs web page for an animated visualizationhttps://animagraffs.com/human-eye/

Retina, cones and rods 2 classes of photoreceptors

Cones are responsible for day-light vision and colour perception Three types of cones: sensitive

to short, medium and longwavelengths

Rods are responsible for night vison

182

Fovea, distribution of photoreceptors

183

the fovea is a densely packed region in the centre of the macula contains the highest density of cones provides the highest resolution vision

Electromagneticspectrum Visible light

Electromagnetic waves of wavelength in the range 380nm to 730nm

Earth’s atmosphere lets through a lot of light in this wavelength band

Higher in energy than thermal infrared, so heat does not interfere with vision

184

Colour There is no physical definition of colour – colour is the

result of our perception

For emissive displays / objects

colour = perception( spectral_emission )

For reflective displays / objects

colour = perception( illumination * reflectance )

185

Reflectance Most of the light we see is reflected from objects These objects absorb a certain part of the light spectrum

Spectral reflectance of ceramic tiles

Why not red?

186

181 182

183 184

185 186

Page 34: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics Michaelmas Term 2021/2022

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafal Mantiuk

Reflected light

Reflected light = illumination × reflectance

The same object may appear to have different color under different illumination.

187

)()()( RIL

Colour vision Cones are the

photreceptors responsible for color vision Only daylight, we see no

colors when there is not enough light

Three types of cones S – sensitive to short

wavelengths M – sensitive to medium

wavelengths L – sensitive to long

wavelengths

Sensitivity curves – probability that a photon of that wavelengths will be absorbed by a photoreceptor. S,M and L curves are normalized in this plot.

188

Perceived light cone response = sum( sensitivity × reflected light )

Although there is an infinite number of wavelengths, we have only three photoreceptor types to sense differences between light spectra

730

380

)()( dLSR SS

Formally

189 Index S for S-cones

Metamers Even if two light spectra are different, they may appear to

have the same colour The light spectra that appear to have the same colour are

called metamers Example:

*

*

= [L1, M1, S1]

= [L2, M2, S2]

=

190

Practical application of metamerism Displays do not emit the same light spectra as real-world

objects Yet, the colours on a display look almost identical

On the display

In real world

*

*

=

= [L1, M1, S1]

= [L2, M2, S2]

191

Observation Any colour can be matched

using three linear independent reference colours

May require “negative” contribution to test colour

Matching curves describe the value for matching mono-chromatic spectral colours of equal intensity With respect to a certain

set of primary colours

192

Tristimulus Colour Representation

187 188

189 190

191 192

Page 35: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics Michaelmas Term 2021/2022

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafal Mantiuk

Standard Colour Space CIE-XYZ CIE Experiments [Guild and Wright, 1931]

Colour matching experiments Group ~12 people with normal colour vision 2 degree visual field (fovea only) Basis for CIE XYZ 1931 colour matching functions

CIE 2006 XYZ Derived from LMS color matching functions by Stockman & Sharpe S-cone response differs the most from CIE 1931

CIE-XYZ Colour Space Goals

Abstract from concrete primaries used in experiment All matching functions are positive Primary „Y” is roughly proportionally to light intensity (luminance)

193

Standard Colour Space CIE-XYZ Standardized imaginary primaries

CIE XYZ (1931) Could match all physically realizable

colour stimuli Y is roughly equivalent to luminance

Shape similar to luminous efficiency curve

Monochromatic spectral coloursform a curve in 3D XYZ-space

Cone sensitivity curves can be obtained by a linear transformation of CIE XYZ

194

CIE chromaticity diagram chromaticity values are defined in terms of x, y, z

ignores luminance can be plotted as a 2D function

pure colours (single wavelength)lie along the outer curve

all other colours are a mix ofpure colours and hence lieinside the curve

points outside the curve do notexist as colours

195

x XX Y Z

y YX Y Z

z ZX Y Z

x y z

, , 1

FvDFH Fig 13.24Colour plate 2

580nm

600nm

700nm

560nm

540nm

520nm

500nm

490nm

510nm

480nm

460nm 410n

m

Achromatic/chromatic vision mechanisms

196

Light spectra

Achromatic/chromatic vision mechanisms

197

Light spectra

Sensitivity of the achromatic mechanism

Luminance does NOT explain the brightness of light! [Koenderink et al. Vision Research 2016]

Achromatic/chromatic vision mechanisms

198

Light spectra

193 194

195 196

197 198

Page 36: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics Michaelmas Term 2021/2022

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafal Mantiuk

Achromatic/chromatic vision mechanisms

199

Light spectra

Achromatic/chromatic vision mechanisms

200

Light spectra

Rods

Cao et al. (2008). Vision Research, 48(26), 2586–92.

Luminous efficiency function (weighting)

Light spectrum (radiance)

Luminance Luminance – measure of light weighted by the response

of the achromatic mechanism. Units: cd/m2

Luminance

201

𝐿 = 𝑘𝐿 𝜆 𝑉 𝜆 𝑑𝜆 𝑘 = 1683.002 All physically possible and visible

colours form a solid in XYZ space Each display device can reproduce a

subspace of that space A chromacity diagram is a slice

taken from a 3D solid in XYZ space Colour Gamut – the solid in a

colour space Usually defined in XYZ to be device-

independent

202

Visible vs. displayable colours

HDR cameras/formats/displays attempt capture/represent/reproduce (almost) all visible colours They represent scene colours and

therefore we often call this representation scene-referred

SDR cameras/formats/devices attempt to capture/represent/reproduce only colours of a standard sRGB colour gamut, mimicking the capabilities of CRTs monitors They represent display colours and

therefore we often call this representation display-referred

203

Standard vs. High Dynamic Range From rendering to display

204

199 200

201 202

203 204

Page 37: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics Michaelmas Term 2021/2022

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafal Mantiuk

From rendering to display

205

From rendering to display

206

Display encoding for SDR:gamma correction Gamma correction is often used to encode luminance or

tri-stimulus color values (RGB) in imaging systems (displays, printers, cameras, etc.)

LumaDigital signal (0-1)

(relative) LuminancePhysical signal

Gamma (usually =2.2)

Gain

Inverse:

Colour: the same equation applied to red, green and blue colour channels.

207

V = a 𝑉V = 1𝑎 𝑉

Why is gamma needed?

Gamma-corrected pixel values give a scale of brightness levels that is more perceptually uniform

At least 12 bits (instead of 8) would be needed to encode each color channel without gamma correction

And accidentally it was also the response of the CRT gun

<- Pixel value (luma)<- Luminance

208

Luma – gray-scale pixel value Luma - pixel brightness in gamma corrected units𝐿 = 0.2126𝑅 + 0.7152𝐺 + 0.0722𝐵

𝑅 , 𝐺 and 𝐵 are gamma-corrected colour values Prime symbol denotes gamma corrected Used in image/video coding

Note that relative luminance if often approximated with𝐿 = 0.2126𝑅 + 0.7152𝐺 + 0.0722𝐵= 0.2126(𝑅 ) +0.7152(𝐺 ) +0.0722(𝐵 ) 𝑅, 𝐺, and 𝐵 are linear colour values Luma and luminace are different quantities despite similar

formulas

209

Standards for display encodingDisplay type Colour space EOTF Bit depth

Standard Dynamic Range ITU-R 709 2.2 gamma / sRGB 8 to 10

High Dynamic Range ITU-R 2020 ITU-R 2100 (PQ/HLG) 10 to 12

210

Colour spaceWhat is the colour of “pure” red,

green and blue

Electro-Optical Transfer FunctionHow to efficiently encode each primary

colour

205 206

207 208

209 210

Page 38: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics Michaelmas Term 2021/2022

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafal Mantiuk

How to transform between RGB colour spaces?

From ITU-R 709 RGB to XYZ:𝑋𝑌𝑍 = 0.4124 0.3576 0.18050.2126 0.7152 0.07220.0193 0.1192 0.9505 𝑅𝐺𝐵211

RGBITU-R 709

RGBITU-R 2020XYZ

SDR HDRDevice-independent

Relative XYZ of the red primary

Relative XYZ of the green

primary

Relative XYZ of the blue

primary

How to transform between RGB colour spaces? From ITU-R 709 RGB to ITU-R 2020 RGB:𝑅𝐺𝐵 = 𝑀 𝑀 𝑅𝐺𝐵 From ITU-R 2020 RGB to ITU-R 709 RGB:𝑅𝐺𝐵 = 𝑀 𝑀 𝑅𝐺𝐵 Where:𝑀 = 0.4124 0.3576 0.18050.2126 0.7152 0.07220.0193 0.1192 0.9505 and 𝑀 = 𝑀𝑀 = 0.6370 0.1446 0.16890.2627 0.6780 0.05930.0000 0.0281 1.0610 and 𝑀 = 𝑀

212

Representing colour We need a mechanism which allows us to represent

colour in the computer by some set of numbers A) preferably a small set of numbers which can be quantised to

a fairly small number of bits each Linear and gamma corrected RGB, sRGB

B) a set of numbers that are easy to interpret Munsell’s artists’ scheme HSV, HLS

C) a set of numbers in a 3D space so that the (Euclidean) distance in that space corresponds to approximately perceptually uniform colour differences CIE Lab, CIE Luv

213

RGB spaces Most display devices that output light mix red, green and

blue lights to make colour televisions, CRT monitors, LCD screens

RGB colour space Can be linear (RGB) or display-encoded (R’G’B’) Can be scene-referred (HDR) or display-referred (SDR)

There are multiple RGB colour spaces ITU-R 709 (sRGB), ITU-R 2020, Adobe RGB, DCI-P3

Each using different primary colours

And different OETFs (gamma, PQ, etc.)

Nominally, RGB space is a cube

214

RGB in CIE XYZ space Linear RGB colour values can

be transformed into CIE XYZ by matrix multiplication because it is a rigid

transformationthe colour gamut in CIE XYZ is a rotate and skewed cube

Transformation into Yxy is non-linear (non-rigid) colour gamut is more

complicated

215

RG

B ga

mut

in

XYZ

colo

ur s

pace

RG

B ga

mut

in Y

xyco

lour

spa

ce

CMY space printers make colour by mixing coloured inks the important difference between inks (CMY) and lights

(RGB) is that, while lights emit light, inks absorb light cyan absorbs red, reflects blue and green magenta absorbs green, reflects red and blue yellow absorbs blue, reflects green and red

CMY is, at its simplest, the inverse of RGB CMY space is nominally a cube

216

211 212

213 214

215 216

Page 39: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics Michaelmas Term 2021/2022

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafal Mantiuk

217

CMYK space

in real printing we use black (key) as well as CMY

why use black? inks are not perfect absorbers mixing C + M + Y gives a muddy

grey, not black lots of text is printed in black:

trying to align C, M and Y perfectly for black text would be a nightmare

218

Munsell’s colour classification system three axes

hue the dominant colour value bright colours/dark colours chroma vivid colours/dull colours

can represent this as a 3D graph

219

Munsell’s colour classification system any two adjacent colours are a standard “perceptual”

distance apart worked out by testing it on people a highly irregular space

e.g. vivid yellow is much brighter than vivid blue

invented by Albert H. Munsell, an American artist, in 1905 in an attempt to systematically classify colours220

Colour spaces for user-interfaces RGB and CMY are based on the physical devices which

produce the coloured output RGB and CMY are difficult for humans to use for

selecting colours Munsell’s colour system is much more intuitive:

hue — what is the principal colour? value — how light or dark is it? chroma — how vivid or dull is it?

computer interface designers have developed basic transformations of RGB which resemble Munsell’s human-friendly system

221

HSV: hue saturation value

three axes, as with Munsell hue and value have same meaning the term “saturation” replaces

the term “chroma”

designed by Alvy Ray Smith in 1978

algorithm to convert HSV to RGBand back can be found in Foley et al., Figs 13.33 and 13.34 222

HLS: hue lightness saturationa simple variation of HSV

hue and saturation have same meaning

the term “lightness” replaces the term “value”

designed to address the complaint that HSV has all pure colours having the same lightness/value as white designed by Metrick in 1979 algorithm to convert HLS to RGB

and back can be found in Foley et al., Figs 13.36 and 13.37

217 218

219 220

221 222

Page 40: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics Michaelmas Term 2021/2022

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafal Mantiuk

Perceptually uniformity MacAdam ellipses & visually indistinguishable colours

223

In CIE xy chromatic coordinates In CIE u’v’ chromatic coordinates

CIE L*u*v* and u’v’

224

sRGB in CIE L*u*v*

Approximately perceptually uniform u’v’ chromacity

CIE LUV

Hue and chroma

Lightness

Chromacitycoordinates

Colours less distinguishable

when dark

CIE L*a*b* colour space Another approximately perceptually

uniform colour space

Chroma and hue

225

Trichromatic values of the

white point, e.g.

226

Lab space this visualization shows

those colours in Lab space which a human can perceive

again we see that human perception of colour is not uniform perception of colour

diminishes at the white and black ends of the L axis

the maximum perceivable chroma differs for different hues

Recap: Linear and display-encoded colour Linear colour spaces

Examples: CIE XYZ, LMS cone responses, linear RGB Typically floating point numbers Directly related to the measurements of light (radiance and luminance) Perceptually non-uniform Transformation between linear colour spaces can be expressed as a

matrix multiplication

Display-encoded and non-linear colour spaces Examples: display-encoded (gamma-corrected, gamma-encoded) RGB,

HVS, HLS, PQ-encoded RGB Typically integers, 8-12 bits per colour channel Intended for efficient encoding, easier interpretation of colour,

perceptual uniformity

227

Colour - references Chapters „Light” and „Colour” in

Shirley, P. & Marschner, S., Fundamentals of Computer Graphics

Textbook on colour appearance Fairchild, M. D. (2005). Color Appearance Models (second.). John

Wiley & Sons.

228

223 224

225 226

227 228

Page 41: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics Michaelmas Term 2021/2022

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafal Mantiuk

Tone-mapping problem

luminance range [cd/m2]

conventional display

simultaneouslyhuman visionadapted

Tone mapping

229

Why do we need tone mapping? To reduce dynamic range To customize the look (colour grading) To simulate human vision (for example night vision) To simulate a camera (for example motion blur) To adapt displayed images to a display and viewing

conditions To make rendered images look more realistic To map from scene- to display-referred colours

Different tone mapping operators achieve different combination of these goals

230

From scene- to display-referred colours The primary purpose of tone mapping is to transform an

image from scene-referred to display-referred colours

231

scene-referred, linear, float

Tone mapping and display encoding Tone mapping is often combined with display encoding

Display encoding can model the display and account for Display contrast (dynamic range), brightness and ambient light

levels

232

Rendered HDR image

Tone mappingDisplay encoding(inverse display

model)

SDR raster buffer

Different for SDR and HDR displays

display-referred, linear, float

display-referred, display-encoded, int

Basic tone-mapping and display coding The simplest form of tone-mapping is the

exposure/brightness adjustment:𝑅 = 𝑅𝐿 R for red, the same for green and blue No contrast compression, only for a moderate dynamic range

The simplest form of display coding is the “gamma”𝑅 = (𝑅 ) For SDR displays only

Prime (‘) denotes a gamma-corrected value Typically 𝛾=2.2

233

Display-referred relative red value [0;1]

Scene-referred

Scene-referred luminance of white

sRGB textures and display coding OpenGL offers sRGB textures to automate

RGB to/from sRGB conversion sRGB textures store data in gamma-corrected space sRGB colour values are converted to (linear) RGB colour

values on texture look-up (and filtering) Inverse display coding

RGB to sRGB conversion when writing to sRGB texture with glEnable(GL_FRAMEBUFFER_SRGB) Forward display coding

234

229 230

231 232

233 234

Page 42: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics Michaelmas Term 2021/2022

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafal Mantiuk

Tone-curve

Image histogram

The „best” tone-mapping is the

one which does not do anything, i.e. slope of the tone-mapping curves is equal

to 1.

235

Tone-curve

But in practice contrast (slope) must be limited due to display

limitations.

236

Tone-curve

Global tone-mapping is a compromise

between clipping and contrast compression.

237

Sigmoidal tone-curves Very common in

digital cameras Mimic the response

of analog film Analog film has been

engineered over manyyears to produce good tone-reproduction

Fast to compute

238

Sigmoidal tone mapping Simple formula for a sigmoidal tone-curve:𝑅′(𝑥, 𝑦) = 𝑅(𝑥, 𝑦)𝐿𝑎 + 𝑅(𝑥, 𝑦)where 𝐿 is the geometric mean (or mean of logarithms):𝐿 = 𝑒𝑥𝑝 1𝑁 ln (𝐿 𝑥, 𝑦 )( , )and 𝐿 𝑥, 𝑦 is the luminance of the pixel 𝑥, 𝑦 .

239

Sigmoidal tone mapping example

a=0.25

a=1

a=4

b=0.5 b=1 b=2240

235 236

237 238

239 240

Page 43: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics Michaelmas Term 2021/2022

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafal Mantiuk

Glare Illusion

“Alan Wake” © Remedy Entertainment

241

Glare Illusion

PaintingPhotography

Computer GraphicsHDR rendering in games

242

Scattering of the light in the eye

From: Sekuler, R., and Blake, R. Perception, second ed. McGraw- Hill, New York, 1990

243

Point Spread Function of the eye What portion of

the light is scattered towards a certain visual angle

To simulate: construct a

digital filter convolve the

image with that filter

Green – daytime (photopic)Red – night time (scotopic)

From: Spencer, G. et al. 1995. Proc. of SIGGRAPH. (1995)

244

Selective application of glare A) Glare applied to the

entire image𝐼 = 𝐼 ∗ 𝐺 Reduces image

contrast and sharpness

B) Glare applied only to the clipped pixels𝐼 = 𝐼 + 𝐼 ∗ 𝐺 − 𝐼where 𝐼 = 𝐼 𝑓𝑜𝑟 𝐼 > 10 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒Better image quality

Glare kernel (PSF)

245

Selective application of glare

Original image

A) Glare applied to the entire image

B) Glare applied to clipped pixels only

246

241 242

243 244

245 246

Page 44: Introduction to Graphics Part2 - cl.cam.ac.uk

Introduction to Graphics Michaelmas Term 2021/2022

©1996–2021 Neil A. Dodgson, Peter Robinson & Rafal Mantiuk

Glare (or bloom) in games Convolution with large, non-separable filters is too slow The effect is approximated by a combination of Gaussian

filters Each filter with different “sigma”

The effect is meant to look good, not be be accurate model of light scattering

Some games simulatea camera rather than the eye

247

References: Tone-mapping Tone-mapping

REINHARD, E., HEIDRICH, W., DEBEVEC, P., PATTANAIK, S., WARD, G., ANDMYSZKOWSKI, K. 2010. High Dynamic Range Imaging: Acquisition, Display, and Image-Based Lighting. Morgan Kaufmann.

MANTIUK, R.K., MYSZKOWSKI, K., AND SEIDEL, H. 2015. High Dynamic Range Imaging. In: Wiley Encyclopedia of Electrical and Electronics Engineering. John Wiley & Sons, Inc., Hoboken, NJ, USA, 1–42. http://www.cl.cam.ac.uk/~rkm38/pdfs/mantiuk15hdri.pdf (Chapter 5)

248

247 248