Create Presentation
Download Presentation

Download Presentation
## CM30075: Computer Graphics bath.ac.uk/~maspmh/

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -

**CM30075: Computer Graphicswww.bath.ac.uk/~maspmh/**Peter Hall pmh@cs.bath.ac.uk**L01: about this course**Aim: to teach the elements of Computer Graphics Traditional 3D photorealism Modern use of images, and non-photorealism Method: traditional forms lectures personal reading personal practical work Assumptions: knowledge of analytic mathematics vectors and matrices integration and differentiation**Reading**• Lecture Slides • are NOT intended to replace text book reading. • Standard texts • Watt, 3D Computer Graphics, Addison Wesley. • Foley et al, 3D Computer Graphics, Addison Wesley. • Watt and Watt, Advanced Computer Graphics, Addison Wesley. • For the interested. • SIGGRAPH proceedings (published as journal special issue; Transactions on Graphics) • Eurographics proceedings • IEEE Transactions on Visualization and Computer Graphics • Computer Graphics Forum**Practical**• Aim: You are to write a simple ray-casting / ray-tracing program. • The practical work is broken into three stages • ray-caster with self-shading • cast-shadows • full ray-tracer. • You should spend no more than 15 hours on this component; • including time to prepare the documents needed for your assessment.**Assessment**Assessment will cover all material given in lectures, in assigned reading and in practical work. 75% Sat ExaminationQuestions usually comprise 4 parts: a) Basic knowledge (3rd class) b) Moderate knowledge, basic understanding (2.2nd class) c) Good knowledge / moderate understanding (2.2nd class) d) Good understanding – can solve new problems (1st class) 25% practical work Already explained**camera**object point (pixel) focus window Computer Graphics Basics Traditional: What colour is this pixel?**Make 3D models**transform models into place illuminate models project models clip invisible parts raterization display Rendering Pipeline The rendering pipeline shows the flow of information uses and the processes needed to synthesise an image. In fact, there are many rendering pipelines. The order of processes can change depending, for example, on whether rendering time or rendering quality is more important. These different pipelines differ only in details – the general flow of information from 3D model to 2D image is always the same.**target image**source image point (pixel) Modern approaches What colour is this pixel?**L02: B-rep basics**• The B-rep modelling scheme is introduced as just one way to build objects. points points and lines points, lines, and polygons**B-rep basics**Brep = Boundary Representation Start with a set of M points p = { (x, y, z)i : i = 1…M } Make a set of N lines from points L = { (i, j)k : k = 1…N } Make a set of m polygons from lines B = { (k1, k2, …, kn}l : l = 1…m } A model is three-dimensional (3D), if the points are 3D (as above).**points**points and lines points, lines, and polygons Different informationsupports different rendering • points only dots (used in Chemistry, also in modern Point based Rendering) • linesWire-frame rendering, good for quick tests in animation, say • polygons shaded surfaces**Practical Issues**!! ALWAYS INDEX POINTS !! polygon table point table line table • avoids repeated points, so more efficient • avoids numerical error when animating**Different tables can be used**point table triangle table Triangles are the most common polygon, because triangles are always flat. But triangles are expensive – many of them; so other polygons used for modelling are often decomposed into triangles for rendering.**anti-clockwise**clockwise A Minor Complication !! POINT ORDER MATTERS !! The ordering gives the polygon a “front” and “back”. eh! which side is which?**The normal of a triangular polygon**The normal of a polygon is used in lighting calculations (and in other calculations too). Suppose a triangle has points, p, q, r, each point in 3D. The normal direction is n = (p-q) x (q-r) where x is the vector cross product. Exercise: Show that reversing the order of points also reverses the direction of the normal.**build a table of trianglesfrom these points**1 Exercise: The points are randomly numbered. From the view given,point 4 is at the back of the cube,point 5 is the nearest corner. Build a table of trianglesin which vertices are consistentlyordered clockwise,when each face of the cube is viewedfrom the outside. 2 6 4 5 7 3 8**B-rep basics: summary**• B-rep = boundary representation • Models objects with points, lines, polygons • Points are ordered around a polygon • Best to index into tables • The form of tables dictates rendering algorithms Exercise: Build a 3D cube from well ordered triangles, render it as a wire-frame. See L03 for projection methods.**L03: Cameras and Projection**• Camera models introduced. • Points are projected, so that objects models can be rendered as wire-frames.**object**window image focus ray optical axis The Linear Camera • In a linear camera • rays of light travel in straight lines from a object • the camera captures all rays passing through a single focus • intersect a planar window to make the image • the normal from the plane to the focus is the optical axis**object**image window focus The Linear Camera • Two variants exist: • the “physical” model – shown in the previous slidehas the focus between the object and the window, in which the image is inverted • the “mathematical model” – which we use, is shown belowhas the window between the object and focus leaving the image the right way up**Basic Perspective Projection**Uses similar triangles to compute the height of the image object image focus**3D is almost as easy as 2D**In the canonical camera, focal length (f) is taken as 1.**Homogeneous Points**Make projection easy and convenient 3D point (x, y, z) written as (x,y,z,1) homogeneous point (x, y, z, a) maps to real point 1/a (x, y, z) The homogeneous points p = (x,y,z,1) and q = (sx, sy, sz, s) differ only by a scale factor s; this makes them equivalent in homogenous space. Notice (x,y,z) and (sx,sy,sz) are two points on the same straight line passing through the origin. This makes them equivalent – they represent the same line! In homogeneous space, lines and points are dual concepts!**A little homogeneous geometry**the set of all rays project normally onto the window to make a pattern of “spokes” object point a scaled version of the image straight line (a ray of light) image point a scaled version of the object window focus: at the origin a particular line – the optical axis – passes normally through the window and through the focus. This line has the image of the focus – a vanishing point.**Projection with a matrix**Using homogeneous coordinates, projection can be written as a matrix we do need to divide by homogeneous depth, a, after this Compare this to with f = 1.**The camera as a matrix**• Using a matrix for projection is very convenient in many ways. • It means we can model the camera as a matrix, C, say.Now projection of a homogenous point p is justq = pCand we know that the homogeneous image point q is just a scale factor away from being correct – and all we need do is scale it by its depth (last element). • It means we can move the camera about in space just by pre-multiplying by a matrix transformq = pMC • It means we can change the internals of the camera (focal length, aspect ratio, etc) by post-multiplying by a matrixq = pMCK • We can do all of this at once! just set A = MCK, now A is a linear cameraq = pA**Setting**using rotates then translates the camera before projection. Notice this is equivalent to applying M-1 to the point. Some Examples**Another example**To change focal length, set now post-multiply**Define a new linear projective camera in a new place and**with a new focal length It is easy to use this camera… Of course, you can set M and K as you please – not just rotate and translate and new focal length! Exercise: See notes for an exercise All at once**Rendering with a Projection Matrix**If we had a model built just of points (no lines, planes etc) then we could make a simple images using this simple technique: • Project all points using the projection matrix • Keep all points that lie within the window bounds • Connect points in the picture that are connected in 3D This produces a “wire frame” picture – easy, and fast! If all pixels inside a projected triangle can be identified,then they too can be coloured. This is the basis of scan-conversion.**L04: Ray-Casting**• Lines are intersected with planar polygons. • The result used as a basis for ray-casting. • Now objects can be rendered to look solid.**Ray-casting algorithm**For each pixel Cast a ray from the focus through the pixel Compute all intersections with all polygons Find the nearest polygon Colour pixel with polygon colour Actually, the polygon colour is modified to create the effect of shading, as on a sphere.**Ray Casting Basics**• One ray per pixel, cast into the scene • Look for nearest intersection • Colour pixel accordingly scene of objects camera pixel focus window Expensive part: computing the intersection of a ray with a polygon**Line / Polygon intersection**n c an infinitely wide plane an infinitely long line x(s) r p compute scale factor Given the scale factor, the intersection is easy to get**Point Inside Triangle?**outside inside p2 p1 x p3 some “turns” are anti-clockwise, others are clockwise all three “turns” are anti-clockwise direction of turn: parallel direction of turn, sij > 0:**The frame buffer is in the computer.**We imagine casting rays thru’ pixel centres i j The window is in “space”; where are pixel centres in space? A practical problem**Solution: recall the camera as a matrix, C = MPK**• y = xC = ((xM)P)K • external parameters M locates the camera in spaceM is a 4x4 matrix, (eg rotation) notice M maps a model point in space use the inverse of M to move camera! • a projection P maps 3D points, to the windowP is rank degenerate can be (4x3) but usually is (4x4) • internal parameters, maps window points to frame-buffertransform confined to the plane a matrix K (a 3x3 will do!)**The internal parameters transform the pixels centres from**“frame buffer” coordinates to “window coordinates”. There is no single “answer” – here I’ve mapped a rectangular frame buffer to a window which is square, so pixels get stretched into rectangles – but you may keep pixels square. It is very common (almost universal) to “flip” coordinates so pixel (j,i) maps to location (x,y); and notices row (j) increase going DOWN - take care when designing K! y i K j x K-1 point is at (x,y) pixel is typically at (j,i)**The external parameters transforms the canonical camera**in “camera space” to the desired camera in “world space”. M M-1**Ray-casting takes a long time**Why does ray-casting take so long? Because every ray is compared to every polygon. Is this necessary? What are bounding spheres? Homework question: what is a BSP tree?**L05: Simple Reflection**• Point light sources are introduced. • Lambertian and Specular reflection, so objects can be shaded.**Point light sources**• A point light source is, er, a point (x,y,z) that emits light. • Actually the point light can be at infinity (think of the sun), in which case (x,y,z) is its direction. Most point lights sources are often at infinity in computer graphics.This makes shading/reflection efficient to compute because the direction of the light is the same for every intersection point.**Basic Reflection Model**light direction surface normal mirror direction Mirror reflection - specular reflection – Phong reflection Diffuse refection – Lambertian reflection**Diffuse/Lambertian Reflection**A beam of light spreads over an area, and is reflected equally in all directions. So here we’ve not drawn any reflected direction over anarrow area over a wide area these beams are of equal width The energy in the light is spread out more over the wider area. So, the area will appear less bright.**w**h q q w The light energy in the beam of width w is spread over a length h = w/cos(q). Energy density in beam: I/w Energy density on ground: I/h = Icos(q)/w h = w/cos(q) Functional analogy: the sun warms the earth more near the equator than the pole