41
Introduction to 3D Introduction to 3D Graphics Graphics Lecture 2: Mathematics Lecture 2: Mathematics of the Simple Camera of the Simple Camera Anthony Steed Anthony Steed University College London University College London

Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

Embed Size (px)

Citation preview

Page 1: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

Introduction to 3D GraphicsIntroduction to 3D Graphics

Lecture 2: Mathematics of Lecture 2: Mathematics of the Simple Camerathe Simple Camera

Anthony SteedAnthony Steed

University College LondonUniversity College London

Page 2: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

OverviewOverview

Basic MathsBasic Maths– PointsPoints– VectorsVectors

Simple CameraSimple Camera– Scenes with spheresScenes with spheres– COP on +zCOP on +z

Local illuminationLocal illumination– Ambient Ambient – DiffuseDiffuse– SpecularSpecular

Page 3: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

OverviewOverview

Basic MathsBasic Maths– PointsPoints– VectorsVectors

Simple CameraSimple Camera– Scenes with spheresScenes with spheres– COP on +z COP on +z

Local illuminationLocal illumination– Ambient Ambient – DiffuseDiffuse– SpecularSpecular

Page 4: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

Basic MathsBasic Maths

In computer graphics we need In computer graphics we need mathematics both for describing our mathematics both for describing our scenes and also for performing scenes and also for performing operations on it, such as projecting operations on it, such as projecting and transforming it.and transforming it.

Coordinate systems (right- and left-Coordinate systems (right- and left-handed), serves as a reference point.handed), serves as a reference point.

3 axis labelled x, y, z at right angles. 3 axis labelled x, y, z at right angles.

Page 5: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

Co-ordinate SystemsCo-ordinate Systems

X

Y

Z

Right-Handed System(Z comes out of the screen)

X

Y

Z

Left-Handed System(Z goes in to the screen)

Page 6: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

Points, P (x, y, z)Points, P (x, y, z)

Gives us a position in relation to Gives us a position in relation to the origin of our coordinate systemthe origin of our coordinate system

Page 7: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

Vectors, V (x, y, z)Vectors, V (x, y, z)

Is a Is a direction direction in 3D spacein 3D space

Points != VectorsPoints != Vectors– Point – Point = VectorPoint – Point = Vector– Vector+Vector = VectorVector+Vector = Vector– Point + Vector = PointPoint + Vector = Point– Point + Point = ?Point + Point = ?

Page 8: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

Vectors, V (x, y, z)Vectors, V (x, y, z)

v

w

v + w

Vector additionsum v + w

v2v

(-1)v (1/2)V

Scalar multiplication of vectors (they remain parallel)

v

w

Vector differencev - w = v + (-w)

v

w

v - w

x

y

Vector OP

P

O

Page 9: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

Vectors Vectors VV

Length (modulus) of a vector Length (modulus) of a vector VV (x, (x, y, z)y, z)– ||VV| = | =

A unit vectorA unit vector–

222 zyx

||V of modulus

Vvector ^

V

VV

Page 10: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

Dot ProductDot Product

a · b = |a| |b| cosa · b = |a| |b| coscoscos = a · b/ |a| |b| = a · b/ |a| |b|

a · b = xa · b = xaa ·x ·xbb + y + yaa ·y ·ybb + z + zaa ·z ·zbb

what happens when the vectors are unitwhat happens when the vectors are unit if dot product == 0 or == 1?if dot product == 0 or == 1? This is purely a scalar number not a This is purely a scalar number not a

vectorvector

Page 11: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

Cross ProductCross Product

The result is not a scalar but a vector The result is not a scalar but a vector which is normal to the plane of the which is normal to the plane of the other 2other 2

direction is found using the determinantdirection is found using the determinant

– i(yi(yvvzzuu -z -zvvyyuu), -j(x), -j(xvvzzuu - z - zvvxxuu), k(x), k(xvvyyuu - y - yvvxxuu))

size is a x b = |a||b|sinsize is a x b = |a||b|sin cross product of vector with it self is nullcross product of vector with it self is null

uuu

vvv

zyx

zyx

kji

uuu

vvv

zyx

zyx

kji

uuu

vvv

zyx

zyx

kji

uuu

vvv

zyx

zyx

kji

Page 12: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

Parametric equation of a Parametric equation of a line (ray)line (ray)

Given two points P0 = (x0, y0, z0) and P1 = (x1, y1, z1) the line passing through them can be expressed as:

P(t) = P0 + t(P1 - P0)x(t) = x0 + t(x1 - x0)

y(t) = y0 + t(y1 - y0)

z(t) = z0 + t(z1 - z0)

=

With - < t <

Page 13: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

Equation of a sphereEquation of a sphere

a

bchypotenuse

a2 + b2 = c2

P

xp

yp

(0, 0)

r

x2 + y2 = r2

Pythagoras Pythagoras Theorem:Theorem:

Given a circle Given a circle through the origin through the origin with radius r, then with radius r, then for any point P on for any point P on it we have:it we have:

Page 14: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

Equation of a sphereEquation of a sphere

If the circle is not centered on the origin:

(0, 0)

P

xp

yp

r

xc

yc ab

a

ba2 + b2 = r2

We still have

buta = xp- xc

b = yp- yc

So for the general case (x- xc)2 + (y- yc)

2 = r2

(xp,yp)

(xc,yc)

Page 15: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

Equation of a sphereEquation of a sphere

Pythagoras theorem generalises to 3D givinga2 + b2 + c2 = d2 Based on that we can easily prove that the general equation of a sphere is:

(x- xc)2 + (y- yc)

2 + (z- zc)2 = r2

and at origin: x2 + y2 + z2 = r2

Page 16: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

OverviewOverview

Basic MathsBasic Maths– PointsPoints– VectorsVectors

Simple CameraSimple Camera– Scenes with spheresScenes with spheres– COP on +zCOP on +z

Local illuminationLocal illumination– Ambient Ambient – DiffuseDiffuse– SpecularSpecular

Page 17: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

Simple Camera Simple Camera (Cross Section)(Cross Section)

Z -Z

Yd

COP

ymax

ymin

Page 18: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

View From the CameraView From the Camera

(xmin, ymin)

(xmax, ymax)

Page 19: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

Forming the RaysForming the Rays

Map screen pixels (M by N window) Map screen pixels (M by N window) to points in camera view planeto points in camera view plane

(0,0)

(M-1, N-1)

(xmin, ymin)

(xmax, ymax)

Page 20: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

Forming the RaysForming the Rays

Consider pixel i,jConsider pixel i,j It corresponds to a rectangle It corresponds to a rectangle

width = (xmax-xmin)/Mwidth = (xmax-xmin)/M

height = (ymax-ymin)/Nheight = (ymax-ymin)/N

Our ray goes through the center of the Our ray goes through the center of the pixelpixel

Thus the ray goes through the pointThus the ray goes through the point

(xmin + width*(i+0.5), ymin + height*(j+0.5), 0.0)(xmin + width*(i+0.5), ymin + height*(j+0.5), 0.0)

Page 21: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

Forming the RaysForming the Rays

Thus the ray from the COP Thus the ray from the COP through pixel i,j is defined bythrough pixel i,j is defined by

p(t) = (x(t), y(t), z(t)) = p(t) = (x(t), y(t), z(t)) =

((t*(xmin + width*(i+0.5)),t*(xmin + width*(i+0.5)),

t*(ymin + height*(j+0.5)),t*(ymin + height*(j+0.5)),

t*d-d)t*d-d)

Page 22: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

Ray CastingRay Casting

Intersection of Sphere and line Intersection of Sphere and line (sphere at origin)(sphere at origin)

Substitute the ray equation in the Substitute the ray equation in the sphere equation and solve!sphere equation and solve!

Get an equation in t of the formGet an equation in t of the form

AtAt2 2 + 2Bt + C = 0+ 2Bt + C = 0

Page 23: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

Ray CastingRay Casting

If bIf b2 2 – AC < 0 then the ray doesn’t intersect the – AC < 0 then the ray doesn’t intersect the sphere. sphere.

If bIf b2 2 -AC = 0 the ray graze (is tangent to the -AC = 0 the ray graze (is tangent to the sphere)sphere)

If bIf b2 2 – AC > 0 then there are two roots given by – AC > 0 then there are two roots given by

t = (-b t = (-b (b(b22 – AC))/A – AC))/A

chose the highest value one (the one closest chose the highest value one (the one closest to the COP)to the COP)

Page 24: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

Ray CastingRay Casting

Intersection of Sphere and line Intersection of Sphere and line (general case)(general case)– Sphere is centred at (a,b,c)Sphere is centred at (a,b,c)– Translate the start of the ray by (-a,-Translate the start of the ray by (-a,-

b,-c)b,-c)– Proceed as beforeProceed as before

Page 25: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

The Image - Detection The Image - Detection

Page 26: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

OverviewOverview

Basic MathsBasic Maths– PointsPoints– VectorsVectors

Simple CameraSimple Camera– Scenes with spheresScenes with spheres– COP on +zCOP on +z

Local illuminationLocal illumination– Ambient Ambient – DiffuseDiffuse– SpecularSpecular

Page 27: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

Ambient LightAmbient Light

Approximation to global illuminationApproximation to global illumination– Each object is illuminated to a certain Each object is illuminated to a certain

extent by “stray” lightextent by “stray” light– Constant across a whole objectConstant across a whole object

Often used simply to make sure Often used simply to make sure everything is lit, just in case it isn’t everything is lit, just in case it isn’t struck by light direct from a light struck by light direct from a light sourcesource

Page 28: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

Ambient LightAmbient Light

Ambient light usually set for whole Ambient light usually set for whole scene (Iscene (Iaa))

Each object reflects only a Each object reflects only a proportion of that (kproportion of that (kaa) )

So far thenSo far then Ir = kaIa

Page 29: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

Lighting Equation #1Lighting Equation #1

But we use RGB soBut we use RGB so

Ir, red = ka,redIa,red

Ir,green = ka,greenIa,green

Ir,blue = ka,blueIa,blue

Page 30: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

The Image - Ambient The Image - Ambient

Page 31: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

Lambert’s LawLambert’s Law

Reflected intensity is proportional to cos Reflected intensity is proportional to cos L is the direction to the lightL is the direction to the light N is the surface normalN is the surface normal

L

N

Page 32: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

Diffuse LightDiffuse Light

The normalised intensity of the The normalised intensity of the light incident on the surface due to light incident on the surface due to a ray from a light sourcea ray from a light source

The light reflected due to The light reflected due to Lambert’s lawLambert’s law

The proportion of light reflected The proportion of light reflected rather than absorbed (krather than absorbed (kdd))

Page 33: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

Lighting Equation #2Lighting Equation #2

Ambient and diffuse componentsAmbient and diffuse components Again kAgain kdd is wavelength dependent is wavelength dependent

and we work with kand we work with kd,red d,red kkd,green d,green and kand kd, d,

blueblue

Ir = kaIa + kdIi (n.l)

Page 34: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

Multiple Lights?Multiple Lights?

Add the diffuse termsAdd the diffuse terms

IIi,ji,j is the incoming intensity of light is the incoming intensity of light jj

lljj is the vector to light j is the vector to light j

Ir = kaIa + kdIi,j (n.l j)j =1

m

Page 35: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

The Image - Diffuse The Image - Diffuse

Page 36: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

Perfect SpecularityPerfect Specularity

Would almost never see the Would almost never see the specular highlightspecular highlight

Page 37: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

Imperfect Specularity Imperfect Specularity (Phong)(Phong)

E is the direction E is the direction to the eyeto the eye

N is the normalN is the normal L is the direction L is the direction

to the lightto the light H bisects E and LH bisects E and L

EN H

L

surface

Page 38: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

Specular ComponentSpecular Component

m is the power of the lightm is the power of the light– High m implies smaller specular High m implies smaller specular

highlighthighlight– Low m makes the highlight more Low m makes the highlight more

blurredblurred

ksIi (h.n)m

Page 39: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

Lighting Equation #3Lighting Equation #3

Ambient, diffuse&specular componentsAmbient, diffuse&specular components Again if there are multiple lights there is Again if there are multiple lights there is

a sum of the specular and diffuse a sum of the specular and diffuse components for each lightcomponents for each light

(This is the time to worry about clamping values to 0,1 (This is the time to worry about clamping values to 0,1 required for monitor display)required for monitor display)

Ir = kaIa + Ii (kd (n.l) + ks(h.n)m )

Page 40: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

The Image - SpecularThe Image - Specular

Page 41: Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera Anthony Steed University College London

ConclusionsConclusions

We can now draw imagesWe can now draw images– Forming rays from the cameraForming rays from the camera– Intersecting those rays with objects in the Intersecting those rays with objects in the

scenescene– Colouring the pixelsColouring the pixels

Immediate work requiredImmediate work required– More interesting scenesMore interesting scenes– A useful cameraA useful camera

At the moment we must move the objects in front At the moment we must move the objects in front of the camera to be able to see themof the camera to be able to see them