200 likes | 427 Views
CS 3388: Texture Mapping. [Hill § 8.5]. Enemy Territory: Quake Wars . Per-vertex colour not enough. In real objects, large changes to shape less frequent than large changes in colour. colour change: red vs white. shape change: almost flat!.
E N D
CS 3388: Texture Mapping [Hill §8.5] Enemy Territory: Quake Wars
Per-vertex colour not enough... • In real objects, large changes to shape less frequent than large changes in colour colour change: red vs white shape change: almost flat! Do we build this wall out of 1,000 vertices of different colours? Or ... only 4 vertices + “brick texture”!
Texture Mapping • Instead of interpolating solid colour... • Interpolate (x,y) location in bitmap and then sample current(x,y)to get colour! thousands of vertices dozens of vertices + texture vs.
Bitmap + Mapping = Texture Mapping! texture (bitmap) texture coordinates (per-vertex) + = texture mapped mesh very few vertices very complex colour!
Re-use Vertices with New Texture! texture 1 texture 2 mesh 1 mesh 2
Texture Coordinates (texcoords) 3D positions 2D texcoords • Each vertex has position (x,y,z) in space and position (u,v) in texture • Colour at (x,y,z)is texture[u,v] (u0,v0) (x0,y0,z0) (x2,y2,z2) (x1,y1,z1) (u1,v1) (u2,v2) sample the texture at (u,v)
Texture Coordinates (texcoords) 3D positions 2D texcoords • Texture coordinates t= (u,v)2 [0,1]2 • note textbook/opengl call them (s,t), but in general this is called “u-v mapping” in 3D modeling software, so we call them (u,v) t0 = (0,0) p0 p2 t1= (0,1) p1 t2= (1,1)
Texture Coordinates (texcoords) 3D positions 2D texcoords • Given p on triangle what’s corresponding t? • If (®,¯,°)is barycentriccoord of p, coord then (®,¯,°) is alsobarycentriccoord of t! t0 = (0,0) p0 p t p2 t1= (0,1) p1 t2= (1,1)
[Hill §8.5.2] Perspective Correction • The (®,¯,°)must be barycentriccoord of the 3D point, not 2D screen coordinate! • All modern hardware supports perspective correct texture mapping interpolate (u,v) based on 2Dscreen coords interpolate (u,v)based on 3D eye coords halfway mark in ... screen coords 3D coords linear non-linear
OpenGL Texture Mapping (pre-3.0) • See texture_demo.cpp documentation
Texture Sampling (“Filtering”) Texture pixel = “texel” Q: what should colour of this pixel be? ... and what about this pixel? avg = 1 avg¼ 0.6 A: integrated colour (average colour) over corresponding area in texture But how do we integrate texture for each pixel?? (slow? complex? or is there a fast hacky trick?)
Hack Solution: Mip Mapping • Pre-compute low-frequency versions of the texture (a “mipmap”) 128£128 64£64 get avg of 64 samples here by taking just 1 sample here! glGenerateMipmap(GL_TEXTURE_2D); 16£16 32£32
How to Visualize Mip-Map Sampling • Manually load solid colour texture at each mipmap level! sampling of fake colour-coded mipmaps (yellow=128£128 white = 1£1) sampling of actual texture mipmaps minification magnification
OpenGL Texture Filtering • Two cases to configure • magnification: what should OpenGL do when a screen pixel maps to region smaller than a texel? • minification: what if screen pixel maps to region much larger than a texel? • Configure mode of bound texture glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,mag_mode); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,min_mode); // where mag_mode = GL_NEAREST or GL_LINEAR // where min_mode= GL_NEAREST or GL_LINEAR or GL_LINEAR_MIPMAP_LINEAR
OpenGL Texture Wrapping • Some textures repeat over and over... 2004 George Bush campaign ad
OpenGL Texture Wrapping • Some textures repeat over and over... • If interpolated (u,v)is outside texmap range [0,1]2, where should we sample? • GL_CLAMP_TO_EDGE: clamp u and v to range [0,1] • GL_REPEAT: take fractional part of u and v glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,wrap_mode); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,wrap_mode); // where wrap_mode = GL_CLAMP_TO_EDGE or GL_REPEAT clamped repeated once repeated four times 6£6 texture
Anisotropic Sampling/Filtering • Normal mip-maps are isotropic: they blur same amount in each direction. • Pixel may project onto highly stretched region in texture • Anisotropic filtering uses “rip-maps” to sample more in u than in v etc.
Anisotropic Sampling/Filtering • Avoids oblique textures looking blurred
Anisotropic Sampling/Filtering • Higher-detail samples in one dimension