Bump Mapping
Total Page:16
File Type:pdf, Size:1020Kb
CSc 155 Lecture Note Slides Bump Mapping Dr. John Clevenger Computer Science Dept. CSUS CSc 155 Lecture Note Slides Bump Mapping Overview • Limitations of Texture Mapping • Normal Perturbation Functions • Obtaining Bump Functions . Explicit functions . Height Fields . Normal Maps • Lighting & Tangent Space • Normalization Cube Maps John Clevenger CSc Dept, CSUS 2 CSc 155 Lecture Note Slides Bump Mapping Limitations Of Texture Mapping • Texture mapping “paints” surfaces o Texture image is typically fixed • Some characteristics are difficult to texture o “Roughness”, “Wrinkles” • Some characteristics might change over time • Texture illumination direction is fixed o Texture looks “wrong” if scene lighting is different John Clevenger CSc Dept, CSUS 3 CSc 155 Lecture Note Slides Bump Mapping Bumps: Perturbed Normals Surface normals on a real “bump” Surface normals on a flat polygon Modified (“perturbed”) normals John Clevenger CSc Dept, CSUS 4 CSc 155 Lecture Note Slides Bump Mapping Bump Functions Original parametric surface Bump function u 2.0 0 1 1.0 8π b(u) sin(8 u) 1.1 Modified “bumpy” surface John Clevenger CSc Dept, CSUS 5 CSc 155 Lecture Note Slides Bump Mapping Computing Perturbed Normals • “New surface points” are implied by N and B(u) Implied “bumpy surface” P(u) P(u) Nˆ *b(u) Nˆ u P(u) John Clevenger CSc Dept, CSUS 6 CSc 155 Lecture Note Slides Bump Mapping Perturbed Normals (cont.) • Approximating the normal at new surface point P(u) P(u) Nˆ *b(u) Bump function b(u) N N T P(u) T d bu T slope of b(u) ˆ du N T P(u) A good approximation for the new normal is db(u) N Nˆ [Blinn] du John Clevenger CSc Dept, CSUS 7 CSc 155 Lecture Note Slides Bump Mapping Perturbed Normals (cont.) • Relationship of bump function slope to modified normals slope = 0 N 2 N1 N 3 positive slope negative slope Bump function b(u) N1 N 2 N 3 John Clevenger CSc Dept, CSUS 8 CSc 155 Lecture Note Slides Bump Mapping Perturbed Normals (cont.) • Similar steps apply for 3D surfaces: P(u,v) P(u,v) T slope in udirection Surface implied by u Bump function b(u,v) P(u,v) B slope inv direction N B v v N T B u T P(u,v) P(u,v) P(u,v) Nˆ b(u,v) Vectors in local (“tangent”) space John Clevenger CSc Dept, CSUS 9 CSc 155 Lecture Note Slides Bump Mapping Perturbed Normals (cont.) • Approximating N : ˆ P (u,v) P(u,v) N b(u,v) T N u u u P(u,v) B P(u,v) P(u,v) Nˆ b(u,v) B T v v v B Nˆ v N T B P(u,v) u T b(u,v) b(u,v) N Nˆ Nˆ T B Nˆ v u Bump function Vector in B Bump function Vector in T slope in v direction slope in u direction John Clevenger CSc Dept, CSUS 10 CSc 155 Lecture Note Slides Bump Mapping Say What Again? • In (somewhat) plain English: o Given a parametric surface P(u,v), o the existing normal N at (u,v) can be perturbed in each of two directions u and v o by adding small changes Δu and Δv respectively o where Δu and Δv are derived from the slope of a bump function, b(u,v). o The new normal N’ is then used in the lighting and shading equations. John Clevenger CSc Dept, CSUS 11 CSc 155 Lecture Note Slides Bump Mapping Example Bump-Mapped Objects John Clevenger CSc Dept, CSUS 12 CSc 155 Lecture Note Slides Bump Mapping Obtaining Bump Functions • John Clevenger CSc Dept, CSUS 13 CSc 155 Lecture Note Slides Bump Mapping Bump Function Frag. Shader /** This fragment shader generates a sinusoidal pattern defined by a bump function * f(x,y) = a*sin(bx)*sin(by). The partial derivatives of this function are used to * offset the true normal; the input texture coordinate values s&t are used for x and y. * Based on an example in Interactive Computer Graphics, 5th Ed., by Ed Angel. */ #version 330 in vec3 L ; //vector to Light in vec3 V ; //vector to Viewpoint (eye) in vec2 texCoord ; //fragment texture coordinate out vec3 fragColor ; //fragment output color void main() { float a = 0.5; //wave height float b = 10.0; //wave frequency vec3 N = vec3 (0.0, 0.0, 1.0); //local space normal //get the x,y values from the texture float x = texCoord.s; float y = texCoord.t; //modify X & Y components of the normal using the partial derivatives of the function N.x = a*(b*cos(b*x))*sin(b*y); //partial derivative with respect to sin(bx) N.y = a*sin(b*x)*(b*cos(b*y)); //partial derivative with respect to sin(by) N = normalize(N); vec3 LL = normalize(L); //insure the light vector is normalized float Kd = max ( dot(N,LL), 0.0); //compute simple Lambertian reflection term vec3 materialColor = vec3( 0.2, 0.2, 0.9); //assume a mostly-blue material //output a color based on the hard-coded material, reduced by Lambertian term fragColor = Kd * materialColor; } John Clevenger CSc Dept, CSUS 14 Run CSc 155 Lecture Note Slides Bump Mapping Height Fields • Consider a monochrome image where pixel intensity represents “height” . Low values (black) = low height . High values (white) = high height “Tilted” View “Edge-on” View “Height Field” Image John Clevenger CSc Dept, CSUS 15 CSc 155 Lecture Note Slides Bump Mapping Height Fields (cont.) Original Height Field Image Expanded Portion John Clevenger CSc Dept, CSUS 16 CSc 155 Lecture Note Slides Bump Mapping Forward Differences As Slope • We can use the difference between adjacent pixels as an approximation of the “rate of change”… . “rate of change” ≈ slope Pixel value: 0 43 86 129 172 215 250 255 250 215 172 129 … Forward Diffs: -43 -43 -43 -43 -43 -35 -5 5 35 43 43 … John Clevenger CSc Dept, CSUS 17 CSc 155 Lecture Note Slides Bump Mapping Height Field Example A bump map “height field” texture file A checker-board textured bump-mapped cylinder John Clevenger CSc Dept, CSUS 18 CSc 155 Lecture Note Slides Bump Mapping Normal Mapping • Basic bump-mapping goal: alter the normal • Alternative: replace normals o Need a source of new normals… • Normals (when normalized) are unit length o hence, (x*x)+(y*y)+(z*z) = 1, and therefore -1 <= x,y,z <= 1 • New normals can be stored in 3 bytes (e.g. in a color image): r = (Nx+1)/2 g = (Ny+1)/2 b = (Nz+1)/2 John Clevenger CSc Dept, CSUS 19 CSc 155 Lecture Note Slides Bump Mapping Encoding Normals In RGB • A true unit normal is perpendicular to the surface (has value [0 0 1]) o Need to represent ∆x/∆y “deviations” (positive or negative) • RGB values can range from 0..255 o Choose “midrange” R & G values to represent ∆x = ∆y= 0 RGB Nˆ 0 0 1 Z As ints: 128 128 255 Y 0 0 1 As floats: 0.5 0.5 1.0 X ∆x=0 ∆y=0 z John Clevenger CSc Dept, CSUS 20 CSc 155 Lecture Note Slides Bump Mapping Normal Mapping Example A Normal Map image file… Diffusely lit textured torus Normal-mapped torus Image credit: Paul Baker, www.paulsprojects.net Run John Clevenger CSc Dept, CSUS 21 CSc 155 Lecture Note Slides Bump Mapping Lighting Calculations N H L V • Problem: vectors frequently not in same “space” o E.g. Light position is (typically) in WORLD coordinates • Solution: transform vectors to common local or tangent space o John Clevenger Mapping is different for each vertex CSc Dept, CSUS 22 CSc 155 Lecture Note Slides Bump Mapping Tangent Space N TBN Matrix B L Object Space Model Matrix TBN Matrix World Space Matrix T ModelView View Matrix Tangent Space Eye Space Needed: Light vector in TBN Matrix Tangent Space, for each vertex Proj Matrix Tx Ty Tz 0 B B B 0 TBN x y z Clip Space N x N y N z 0 0 0 0 1 Example: objSpaceLightPosition = inverseModelMatrix * worldLightPosition; objSpaceLightVector = objSpaceLightPosition - objSpaceVertPosition; John Clevenger vertTanSpaceLightVector = TBNobjSpace * objSpaceLightVector; CSc Dept, CSUS 23 CSc 155 Lecture Note Slides Bump Mapping Storing TBN values • Must be computed/saved per vertex Vertex3D ... - normal : Vector3D - tangent : Vector3D - binormal : Vector3D - tangentSpaceLight : Vector3D ... John Clevenger CSc Dept, CSUS 24 CSc 155 Lecture Note Slides Bump Mapping Torus With TBN Values • Sweep a ring of vertices about Y T Outer Radius Y B N T X B Z Precision N X Ring 0 T Inner Radius Precision Inner Radius Outer Radius Front View Y Top View X John Clevenger CSc Dept, CSUS 25 CSc 155 Lecture Note Slides Bump Mapping Saving Torus TBN Values /** This class defines a torus constructed as previously shown, but augmented with TBN values*/ public class Torus extends Shape3D { ... private void initTorus(double innerRadius, double outerRadius, int precision) { for(int i=0; i<precision+1; i++) { //...code here as before to compute the torus ring 0 vertices... //set ring 0 coordinates for texture mapping vertices[i].setS(0.0f); vertices[i].setT((float)i/precision; //set the tangent, binormal, and normal vectors for the vertex vertices[i].setTangent(0.0f, 0.0f, -1.0f); //"into screen”, for right-handed Vector3D binormal = new Vector3D(0.0, -1.0, 0.0); binormal = binormal.mult(rotateZ); vertices[i].setBinormal(binormal); Vector3D normal = vertices[i].getBinormal().cross(vertices[i].getTangent()); vertices[i].setNormal(normal); }//end for each vertex in first ring ... for(int ring=1; ring<precision+1; ring++) { //for each new ring ..