420 likes | 635 Views
Introduction to 3D Graphics Lecture 2: Mathematics of the Simple Camera. Anthony Steed University College London. Overview. Basic Maths Points Vectors Simple Camera Scenes with spheres COP on +z Local illumination Ambient Diffuse Specular. Overview. Basic Maths Points Vectors
E N D
Introduction to 3D GraphicsLecture 2: Mathematics of the Simple Camera Anthony Steed University College London
Overview • Basic Maths • Points • Vectors • Simple Camera • Scenes with spheres • COP on +z • Local illumination • Ambient • Diffuse • Specular
Overview • Basic Maths • Points • Vectors • Simple Camera • Scenes with spheres • COP on +z • Local illumination • Ambient • Diffuse • Specular
Basic Maths • In computer graphics we need mathematics both for describing our scenes and also for performing operations on it, such as projecting and transforming it. • Coordinate systems (right- and left-handed), serves as a reference point. • 3 axis labelled x, y, z at right angles.
Co-ordinate Systems Y Y X X Z Z Left-Handed System (Z goes in to the screen) Right-Handed System (Z comes out of the screen)
Points, P (x, y, z) • Gives us a position in relation to the origin of our coordinate system
Vectors, V (x, y, z) • Is a direction in 3D space • Points != Vectors • Point – Point = Vector • Vector+Vector = Vector • Point + Vector = Point • Point + Point = ?
y x Vector addition sum v + w w v v + w Vectors, V (x, y, z) v 2v (1/2)V (-1)v Scalar multiplication of vectors (they remain parallel) w P v - w v v w O Vector difference v - w = v + (-w) Vector OP
Vectors V • Length (modulus) of a vector V (x, y, z) • |V| = • A unit vector
Dot Product • a · b = |a| |b| cos cos = a · b/ |a| |b| • a · b = xa ·xb + ya ·yb + za ·zb • what happens when the vectors are unit • if dot product == 0 or == 1? • This is purely a scalar number not a vector
Cross Product • The result is not a scalar but a vector which is normal to the plane of the other 2 • direction is found using the determinant • i(yvzu -zvyu), -j(xvzu - zvxu), k(xvyu - yvxu) • size is a x b = |a||b|sin • cross product of vector with it self is null
x(t) = x0 + t(x1 -x0) y(t) = y0 + t(y1 -y0) z(t) = z0 + t(z1 -z0) Parametric equation of a 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) = With - < t <
hypotenuse c b a P r yp xp (0, 0) Equation of a sphere • Pythagoras Theorem: • Given a circle through the origin with radius r, then for any point P on it we have: a2 + b2 = c2 x2 + y2 = r2
(x-xc)2 + (y-yc)2 = r2 So for the general case Equation of a sphere • If the circle is not centered on the origin: We still have yp P (xp,yp) a2 + b2 = r2 r b b but yc a (xc,yc) a = xp-xc b = yp-yc xp xc (0, 0) a
Equation of a sphere • Pythagoras theorem generalises to 3D giving Based on that we can easily a2 + b2 + c2 = d2 prove that the general equation of a sphere is: (x-xc)2 + (y-yc)2 + (z-zc)2 = r2 x2 + y2 + z2 = r2 and at origin:
Overview • Basic Maths • Points • Vectors • Simple Camera • Scenes with spheres • COP on +z • Local illumination • Ambient • Diffuse • Specular
Simple Camera (Cross Section) Y d ymax Z -Z COP ymin
View From the Camera (xmax, ymax) (xmin, ymin)
Forming the Rays • Map screen pixels (M by N window) to points in camera view plane (xmax, ymax) (M-1, N-1) (0,0) (xmin, ymin)
Forming the Rays • Consider pixel i,j • It corresponds to a rectangle width = (xmax-xmin)/M height = (ymax-ymin)/N • Our ray goes through the center of the pixel • Thus the ray goes through the point (xmin + width*(i+0.5), ymin + height*(j+0.5), 0.0)
Forming the Rays • Thus the ray from the COP through pixel i,j is defined by p(t) = (x(t), y(t), z(t)) = (t*(xmin + width*(i+0.5)), t*(ymin + height*(j+0.5)), t*d-d)
Ray Casting • Intersection of Sphere and line (sphere at origin) • Substitute the ray equation in the sphere equation and solve! • Get an equation in t of the form At2 + 2Bt + C = 0
Ray Casting If b2 – AC < 0 then the ray doesn’t intersect the sphere. If b2 -AC = 0 the ray graze (is tangent to the sphere) If b2 – AC > 0 then there are two roots given by t = (-b (b2 – AC))/A chose the highest value one (the one closest to the COP)
Ray Casting • Intersection of Sphere and line (general case) • Sphere is centred at (a,b,c) • Translate the start of the ray by (-a,-b,-c) • Proceed as before
Overview • Basic Maths • Points • Vectors • Simple Camera • Scenes with spheres • COP on +z • Local illumination • Ambient • Diffuse • Specular
Ambient Light • Approximation to global illumination • Each object is illuminated to a certain extent by “stray” light • Constant across a whole object • Often used simply to make sure everything is lit, just in case it isn’t struck by light direct from a light source
Ambient Light • Ambient light usually set for whole scene (Ia) • Each object reflects only a proportion of that (ka) • So far then Ir = kaIa
Lighting Equation #1 But we use RGB so Ir, red = ka,redIa,red Ir,green = ka,greenIa,green Ir,blue = ka,blueIa,blue
Lambert’s Law • Reflected intensity is proportional to cos • L is the direction to the light • N is the surface normal
Diffuse Light • The normalised intensity of the light incident on the surface due to a ray from a light source • The light reflected due to Lambert’s law • The proportion of light reflected rather than absorbed (kd)
Lighting Equation #2 • Ambient and diffuse components • Again kd is wavelength dependent and we work with kd,red kd,green and kd, blue Ir = kaIa + kdIi (n.l)
Multiple Lights? • Add the diffuse terms • Ii,j is the incoming intensity of light j • lj is the vector to light j m Ir = kaIa + kdIi,j (n.l j) j =1
Perfect Specularity • Would almost never see the specular highlight
Imperfect Specularity (Phong) • E is the direction to the eye • N is the normal • L is the direction to the light • H bisects E and L
Specular Component • m is the power of the light • High m implies smaller specular highlight • Low m makes the highlight more blurred ksIi (h.n)m
Lighting Equation #3 • Ambient, diffuse&specular components • Again if there are multiple lights there is a sum of the specular and diffuse components for each light (This is the time to worry about clamping values to 0,1 required for monitor display) Ir = kaIa + Ii (kd (n.l) + ks(h.n)m )
Conclusions • We can now draw images • Forming rays from the camera • Intersecting those rays with objects in the scene • Colouring the pixels • Immediate work required • More interesting scenes • A useful camera • At the moment we must move the objects in front of the camera to be able to see them