Texture Mapping Modeling an Orange
Total Page:16
File Type:pdf, Size:1020Kb
Texture Mapping Modeling an Orange q Problem: Model an orange (the fruit) q Start with an orange-colored sphere – Too smooth q Replace sphere with a more complex shape – Takes too many polygons to model all the dimples q Soluon: retain the simple model, but add detail as a part of the rendering process. 1 Modeling an orange q Surface texture: we want to model the surface features of the object not just the overall shape q Soluon: take an image of a real orange’s surface, scan it, and “paste” onto a simple geometric model q The texture image is used to alter the color of each point on the surface q This creates the ILLUSION of a more complex shape q This is efficient since it does not involve increasing the geometric complexity of our model Recall: Graphics Rendering Pipeline Application Geometry Rasterizer 3D 2D input CPU GPU output scene image 2 Rendering Pipeline – Rasterizer Application Geometry Rasterizer Image CPU GPU output q The rasterizer stage does per-pixel operaons: – Turn geometry into visible pixels on screen – Add textures – Resolve visibility (Z-buffer) From Geometry to Display 3 Types of Texture Mapping q Texture Mapping – Uses images to fill inside of polygons q Bump Mapping – Alter surface normals q Environment Mapping – Use the environment as texture Texture Mapping - Fundamentals q A texture is simply an image with coordinates (s,t) q Each part of the surface maps to some part of the texture q The texture value may be used to set or modify the pixel value t (1,1) (199.68, 171.52) Interpolated (0.78,0.67) s (0,0) 256x256 4 2D Texture Mapping q How to map surface coordinates to texture coordinates? 1. Set the coordinates manually for each vertex (1,1) v1 t1 t v 0 t2 t 0 (0,0) v s 2 2. Automacally compute the coordinates – Use an algorithm that sets the texture coordinates q What about the surface interior? Linear interpolaon? Manually Specifying the Coordinates q Specify the texture coordinates at each vertex: 5 Manually Specifying the Coordinates q Can choose alternate texture coordinates Manually Specifying the Coordinates q Can choose alternate texture coordinates 6 Hands-On Session The OpenGL tutor programs § Go to the class website, click on the Links secBon § Download and compile the OpenGL tutors § Run the texture tutor § Play with the parameters of glTexCoord § Enlarge / shrink the texture map and observe the changes in the texture of the screen object § Play with the parameters of glVertex § Change the shape of the screen object and observe the changes in its appearance q By default, the texture is projected orthogonal on the xy plane, on a square 1x1 in dimensions – and repeats. 7 Naïve Texture Mapping Rasterizer q For each pixel: – Interpolate s & t along edges and across spans t – Look up nearest texel in texture map s – Color pixel according to texel Texture map color (possibly modulated by lighBng calculaons) Triangle in world space Interpolang Coordinates (x3, y3), (s3, t3) y y y y ⎛ y − y ⎞ ⎛ y − y ⎞ ⎛ − 1 ⎞ ⎛ − 1 ⎞ 2 2 sR = ⎜1− ⎟s1 + ⎜ ⎟s3 sL = ⎜1− ⎟s2 + ⎜ ⎟s3 ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ y3 − y1 y3 − y1 ⎝ y3 − y2 ⎠ ⎝ y3 − y2 ⎠ ⎝ ⎠ ⎝ ⎠ ⎛ x − x ⎞ ⎛ x − x ⎞ s 1 L s L s = ⎜ − ⎟ L + ⎜ ⎟ R ⎝ xR − xL ⎠ ⎝ xR − xL ⎠ (x2, y2), (s2, t2) (x1, y1), (s1, t1) 8 InterpolaIon – What Goes Wrong? q Texture mapping is done in screen space as the polygon is rasterized texture source what we get what we want Check out hbp://graphics.lcs.mit.edu/classes/6.837/F98/Lecture21/Slide06.html What arBfacts do you see? Recall: PerspecIve ProjecIon (xw, yw, zw) World Image point " xw yw % I = $− ,− ,1' # zw zw & y Screen Camera f=1 z x 9 Visualizing the Problem • NoBce that uniform steps on the image plane do not correspond to uniform steps along the edge. q PerspecBve correct interpolaon: “translate step size in image plane correctly to step size on object” Linear Interpolaon in Screen Space " % Interpolation (α) x1 x2 x1 p(α) = p1 +α(p2 − p1) = +α$ − ' in screen space z1 # z2 z1 & ! $ ! $ (! $ ! $+ x x1 x2 x1 x1 + β(x2 − x1) Interpolation (β) # & = # &+ β *# &−# &- p(β) = z z * z z - z1 + β(z2 − z1) in world 3-space " % "# 1 %& )"# 2 %& "# 1 %&, 10 Perspecve Correct Interpolaon We need a mapping from α values to β values: x " x x % x + β(x − x ) 1 +α$ 2 − 1 ' = 1 2 1 z1 # z2 z1 & z1 + β(z2 − z1) Solve for β in terms of α: αz β = 1 z2 +α(z1 − z2 ) At this point in the pipeline (after projection) we no longer have z, but we do have w1= 1/z1 and w2 = 1/z2, so: 1 α w αw β = 1 = 2 " % 1 1 1 w1 +α (w2 − w1) +α$ − ' w2 # w1 w2 & Perspecve Correct Interpolaon αw β = 2 w1 +α (w2 − w1) We can now use this expression for β to interpolate texture indices (s, t), over our 3-space texture. This is accomplished substituting our solution for β given α into the parameter interpolation: s = s1 + β(s2 − s1) αw2 s = s1 + (s2 − s1) w1 +α (w2 − w1) s1w1 +α(s2w2 − s1w1) s = s1 + w1 +α (w2 − w1) 11 Perspecve-Correct Texturing q Demo at hbp://groups.csail.mit.edu/graphics/classes/6.837/F98/Lecture21/Slide14.html Texture and Aliasing q Textures are subject to aliasing: – A polygon point maps into a texture image, essenBally sampling the texture at a point – If a textured object is far away, one screen pixel may map to many texture pixels. The problem is how to combine them Texture map Polygon far from the viewer in perspective projection Rasterized and textured 12 Texture Aliasing q Occurs when screen resoluBon differs from texture resoluBon q Magnificaon aliasing – Screen resoluBon finer than texture resoluBon – MulBple pixels per texel q Minificaon aliasing – Screen resoluBon coarser than texture resoluBon – MulBple texels per pixel MagnificaIon Texels Pixels on screen q One texel maps to mulBple pixels 13 MinificaIon Color? Texels Pixels on screen q MulBple texels map to one pixel MagnificaIon – Nearest Filtering Texels Pixels on screen q Choose the texel nearest the pixel’s center 14 MinificaIon – Nearest Filtering Pixels on screen q Choose the texel nearest the pixel’s center q Expensive to compute which texels every pixel covers Nearest Filtering Example @bottom of screen 15 MagnificaIon – Bilinear Filtering R=247 R=255 G=237 G=204 B=141 B=102 R=253 R=214 G=230 R=102 B=145 G=193 G=102 B=110 B=51 Texels Pixels on screen q Average for the 2x2 texels surrounding a given pixel MinificaIon – Bilinear Filtering R=234 R=252 G=189 G=219 B=0 B=96 R=0 G=0 B=0 R=155 R=135 G=132 G=119 B=30 B=23 Pixels on screen q Average for the 2x2 texels surrounding a given pixel 16 Bilinear vs. Nearest Filtering Bilinear Nearest What is Going On? q Texture Filtering so far: – For a given texture coordinate, pick the nearest texel or average the nearest 4 texels q Problem – For distant areas of the surface, texture space area covered is larger than 4 texels across q Soluon: – Use scaled-down texture versions – Also known as MIP-maps, or MIP levels 17 MIP-Mapping (Pre-Filtering) q MIP = Multum in Parvo (many things in a small place) q A MIP-map is a low resoluBon version of a texture n n q For a texture of 2 x 2 pixels, compute n-1 textures Use on near objects Use on middle objects Use on far objects GeneraIng MIP-maps q Generang a MIP-map from a texture is easy q For each texel in level i, average the values of the 2x2 corresponding texels in level i-1 q If a texture requires n bytes of storage, how much storage will a MIP-map require? R G Answer: 4n/3 R G R G R G B B B B 18 Visualizing MIP-Maps q Pyramid of progressively lower- average resoluBon images, filtered down 2x2 texels 32x32 64x64 from original q Each level of the MIP-map 128x128 represents a pre-blurred version of mulBple texels 256x256 – A texel at level d represents 2d original texels q Which level of mip-map to use? – Index into MIP-map with 3 coordinates: s, t, d (depth) Using MIP-maps q Based on distance from camera 19 Using MIP-Maps q Use the resoluBon that maintains pixel to texel rao close to 1 q Red box represents the pixel area q Green box represents nearest texels q Nearest filtering in each MIP-map level yields a white pixel q Linear filtering on a lower level MIP-map yields a shade of grey Using MIP-Maps q When rendering: – Figure out how large (or small) a projected pixel is (in texels of the original map) – Find the level of the MIP map in which texels average approximately that many original texels – Interpolate the value of the four nearest texels (bilinear interpola-on) 20 Using MIP-Maps q Even beer: – Likely, the coverage of the pixel will fall somewhere between the coverage of texels in two adjacent levels of the MIP map – Find the pixel’s value in each of the two textures using two bilinear interpolaons – Using a third interpolaon, find a value in between these two values, based on the coverage of the pixel versus each of the MIP-map levels – This is called trilinear filtering Trilinear Filtering using MIP-maps R=155 R=229 R=147 R=58 G=132 G=208 G=114 G=0 B=30 B=119 B=117 B=0 R=178 R=233 R=106 R=66 G=179 G=227 G=80 G=0 B=90 B=143 B=74 B=0 Screen Lower Res.