texture mapping. example mappings mapping techniques consider the problem of rendering a sphere in...

52
Texture Mapping

Upload: baldwin-miller

Post on 12-Jan-2016

231 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Texture Mapping

Page 2: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Example Mappings

Page 3: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Mapping Techniques

Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere But the color changes rapidly With the local shading model, so far, the only place to

specify color is at the vertices To get color details, would need thousands of polygons for

a simple shape Same things goes for an orange: simple shape but

complex details Solution: Mapping techniques use simple geometry

modified by a mapping of some type

Page 4: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Modeling an Orange Consider the problem of modeling an orange

(the fruit) Start with an orange-colored sphere

Too simple Replace sphere with a more complex shape

Does not capture surface characteristics (small dimples)

Takes too many polygons to model all the dimples

Page 5: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Modeling an Orange (2)

Take a picture of a real orange, scan it, and “paste” onto simple geometric model This process is known as texture mapping

Still might not be sufficient because resulting surface will be smooth Need to change local shape Bump mapping

Page 6: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

texturesThe concept is very simple!

Page 7: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Texture Mapping

Texture mapping associates the color of a point with the color in an image: the texture Each point on the sphere gets the color of mapped pixel of

the texture

Question to address: Which point of the texture do we use for a given point on the surface?

Establish a mapping from surface points to image points Different mappings are common for different shapes We will, first, look at triangles (polygons)

Page 8: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Basic Mapping

The texture lives in a 2D space Parameterize points in the texture with 2

coordinates: (s,t) These are just what we would call (x,y) if we were

talking about an image, but we wish to avoid confusion with the world (x,y,z)

Define the mapping from (x,y,z) in world space to (s,t) in texture space To find the color in the texture, take an (x,y,z) point

on the surface, map it into texture space, and use it to look up the color of the texture

Page 9: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Texture Mapping

parametric coordinates

texture coordinates

world coordinateswindow coordinates

Page 10: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Mapping Functions

Basic problem is how to find the maps Consider mapping from a point a surface to

texture coordinates need functions

s = s (x, y, z) t = t (x, y, z)

Such functions are difficult to find in

general

s

t

(x,y,z)

Page 11: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Polygon Texture Interpolation

For polygons, specify (s,t) coordinates at vertices s = s (x, y, z) t = t (x, y, z)

Linearly interpolate the mapping for other points in world space Straight lines in world

space go to straight lines in texture space

Texture maps

t

Triangle in world space

Page 12: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Barycentric Coordinates

0 0 1 1 2 2P c P c P c P

0 1 2 1c c c

0 2 01

1 0 2 0

1 0 02

1 0 2 0

0 1 2

( ) ( )

( ) ( )

( ) ( )

( ) ( )

1

P P P Pc

P P P P

P P P Pc

P P P P

c c c

This can be used for checking if P isIn the triangle. It is also useful when Computing the texture coordinates andOther linear interpolations (normal).

P is in the triangle if c1 > 0, c2 >0, andc1+c2 < 1

Page 13: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Set Texture Coordinates for Curved Surfaces

Assume a curved parametric surface S = {x(u, v), y(u, v), z(u, v)} approximated by a polygonal mesh

It’s easy to set appropriate texture coordinates for mesh vertices u = a → s = 0 and u = b → s = 1 v = c → t = 0 and v = d → t = 1

Page 14: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Cylindrical Surfaces

Parametric form

Mapping function

cos

sin

x r

y r

z h

[ , ] , 0,1 0,1a b a b

a

b a

a

b a

h h

s

h hth h

Page 15: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Spherical Surfaces

Parametric form

Linear Mapping

Triangular mapping

cos cos

cos sin

sin

x r

y r

z r

Page 16: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Texture Mapping and Pipelines 2D texture is stored in texture memory as an n×m array of

pixel elements (texels) Rasterization converts primitives to fragments (pixels to be

displayed) Texture mapping is applied to fragments in the rasterization

stage The color of a fragment is determined by looking up in the

textures.

Geometric Processing

FragmentProcessing Display

TexturesPixel

Page 17: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Textures

Two-dimensional texture pattern T(s, t) It is stored in texture memory as an n*m array of

texture elements (texels) Due to the nature of rendering process, which works

on a pixel-to-pixel base, we are more interested in the inverse map from screen coordinates to the texture coordinates Requires us to go from screen coordinates to texture

coordinates We are interested in mapping the pixel domains (areas) to

areas in the texture space

Page 18: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Computing Pixel Color in Texture mapping Associate texture with polygon Map pixel onto polygon and then into texture map Use weighted average of covered texture to compute color.

Page 19: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Basic Strategy of OpenGL Texture Mapping

Three steps to applying a texture1. specify the texture

read or generate image assign to texture enable texturing

2. specify texture parameters wrapping, filtering

3. assign texture coordinates to vertices Proper mapping function is left to application

Page 20: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Texture Example

The texture (below) is a 256 x 256 image that has been mapped to a rectangular polygon which is viewed in perspective

Page 21: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

2D Texturing Enable 2D Texturing:

glEnable(GL_TEXTURE_2D) Texture mapping is disabled by default

Texture objects store texture data and keep it readily available for usage. Many texture objects can be generated.

Generate identifiers for texture objects first GLuint texids[n]; glGenTextures(n, texids)

n: the number of texture objects identifiers to generate texids: an array of unsigned integers to hold texture object identifiers

Bind a texture object as the current texture glBindTexture(target, identifier)

Target: can be GL_TEXTURE_1D, GL_TEXTURE_2D, or GL_TEXTURE 3D Identifier: a texture object identifier

Following texture commands refer to the bound texture, e.g. glTexImage2D()

Page 22: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Define Image as a Texture Load image into a 2D Texture Map

glTexImage2D(GL_TEXTURE_2D, level, internalFormat, width, height, border, format, type, texels) level: level of the texture resolutions. For now = 0 internalFormat: number of color components used for

the texture. (integer value from 1 - 4, 3 for RGB) width, height: size of the texture map (should be

power of 2) border: with (1) or without (0) border format: format of the texture data, e.g. GL_RGB type: data type of the texture data, e.g. GL_BYTE textels: pointer to the actual texture image data

Page 23: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

1D and 3D Textures 1D textures can be considered as 2D textures with a

height equal to 1. It is often used for drawing color stripes glTexImage1D(GL_TEXTURE_1D, level,

components, width, border, format, type, texels)

3D textures can be considered as a stack of 2D textures. It is often used in visualization applications, e.g. medical imaging. glTexImage3D(GL_TEXTURE_3D, level,

components, width, height, depth, border, format, type, texels)

Page 24: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

3D Texturing Examples

Page 25: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Texture Parameters

OpenGL has a variety of parameters that determine how texture is applied Wrapping parameters determine what happens if

s and t are outside the (0,1) range Filter modes allow us to use area averaging

instead of point samples Mipmapping allows us to use textures at multiple

resolutions Environment parameters determine how texture

mapping interacts with shading

Page 26: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Wrapping Mode

Clamping: if s,t > 1 use 1, if s,t <0 use 0Wrapping: use s,t modulo 1

glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP )

glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT )

texture

s

t

GL_CLAMPwrapping

GL_REPEATwrapping

Page 27: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Texture Functions You can specify how the texture-map colors are used to modify the pixel

colors glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE, mode); mode values:

GL_REPLACE: replace pixel color with texture color GL_DECAL: replace pixel color with texture color for GL_RGB texture GL_BLEND: C = Cf(1-Ct) + CcCt,

Cf is the pixel color, Ct is the texture color, and Cc is some constant color

GL_MODULATE: C = CfCt More on OpenGL programming guide

Example: Texture is applied after lighting, so how do you adjust the texture’s brightness? Make the polygon white and light it normally Use glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,

GL_MODULATE) Then, texture color is multiplied by surface (fragment) color and appears lighted

Page 28: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Every point on a surface should have a texture coordinate (s, t) in texture mapping

We often specify texture coordinates to polygon vertices and interpolate texture coordinates with the polygon.

glTexCoord*() specified at each vertex

s

t1, 1

0, 1

0, 0 1, 0

(s, t) = (0.2, 0.8)

(0.4, 0.2)

(0.8, 0.4)

A

B C

a

bc

Texture Space Object Space

Mapping a Texture

Page 29: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Typical Code

glBegin(GL_POLYGON);glColor3f(r0, g0, b0); //if no shading usedglNormal3f(u0, v0, w0); // if shading usedglTexCoord2f(s0, t0);glVertex3f(x0, y0, z0);glColor3f(r1, g1, b1);glNormal3f(u1, v1, w1);glTexCoord2f(s1, t1);glVertex3f(x1, y1, z1);

.

.glEnd();

Note that we can use vertex arrays to increase efficiency.See the example posted on the web site.

Page 30: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Texture Filtering A pixel may be mapped to a small portion of a texel or a collection of texels from the

texture map. How to determine the color of the pixel? Magnification: when a pixel mapped to a small portion of a texel

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, type); type: GL_NEAREST or GL_LINEAR

Minification: when a pixel mapped to many texels glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, type);

type: GL_NEAREST, GL_LINEAR, GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_LINEAR, …

Page 31: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Texture Aliasing

Rasterized and texturedPolygon to pixels Texture map

Without filtering Using Mipmaping

Page 32: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Mipmaps

Use different resolution of texture image for rendering different objects Level 0: original texture map Level 1: half size in width and height

Define mipmaps glTexImage2D(

GL_TEXTURE_2D, level, GL_RGB, …); Where level = 0, 1, 2, ..

Automatically generate mipmaps gluBuild2DMipmaps(GL_TEXTURE_2D,

GL_RGB, width, height, format, type, texels);

For near objects

For far objects

For middle objects

Page 33: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Mipmap Filters Mipmap minification filters

GL_LINEAR_MIPMAP_NEAREST: use the nearest mipmap closest to the polygon resolution, and use linear filtering

GL_LINEAR_MIPMAP_LINEAR: use linear interpolation between the two mipmaps closest to the polygon resolution, and use GL_LINEAR filtering in each mipmap

Example Code: gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, 64, 64,

GL_RGB, GL_UNSIGNED_BYTE, texImage); glTexParameteri(GL_TEXTURE_2D,

GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,

GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);

Page 34: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Other Texture Stuff Texture must be in fast memory - it is accessed for

every pixel drawn If you exceed it, performance will degrade horribly There are functions for managing texture memory Skilled artists can pack textures for different objects

into one map Texture memory is typically limited, so a range of

functions are available to manage it Specifying texture coordinates can be annoying, so

there are functions to automate it Sometimes you want to apply multiple textures to the

same point: Multitexturing is now in some hardware

Page 35: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Texture Matrix

Normally, the texture coordinates given at vertices are interpolated and directly used to index the texture

The texture matrix applies a homogeneous transform to the texture coordinates before indexing the texture

What use is this?

Page 36: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Animating Texture

Loading a texture onto the graphics card is very expensive

But once there, the texture matrix can be used to “transform” the texture For example, changing the translation can select different

parts of the texture

If the texture matrix is changed from frame to frame, the texture will appear to move on the object

This is particularly useful for things like flame, or swirling vortices, or pulsing entrances, …

Page 37: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Example

glMatrixMode(GL_TEXTURE);

glLoadIdentity();

// Scale the texture matrix

glScalef(scale, scale, scale);

// Rotate the UVs around the center of the texture

glTranslatef(0.5f, 0.5f, 0.0f);

glRotatef(angle, 0.0f, 0.0f, 1.0f);

glTranslatef(-0.5f, -0.5f, 0.0f);

glMatrixMode(GL_MODELVIEW);Demo from www.gametutorial.com

Page 38: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Procedural Texture Mapping

Instead of looking up an image, pass the texture coordinates to a function that computes the texture value on the fly Renderman, the Pixar rendering language, does this Available in a limited form with vertex shaders on current

generation hardware

Advantages: Near-infinite resolution with small storage cost Idea works for many other things

Has the disadvantage of being slow in many cases

Page 39: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Other Types of Mapping Environment mapping looks up incoming illumination in a map

Simulates reflections from shiny surfaces Bump-mapping computes an offset to the normal vector at each

rendered pixel No need to put bumps in geometry, but silhouette looks wrong

Displacement mapping adds an offset to the surface at each point Like putting bumps on geometry, but simpler to model

All are available in software renderers like RenderMan compliant renderers

All these are becoming available in hardware

Page 40: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Bump MappingTextures can be used to alter the surface normal of an object. This does not change the actual shape of the surface -- we are only shading it as if it were a different shape! This technique is called bump mapping. The texture map is treated as a single-valued height function. The value of the function is not actually used, just its partial derivatives. The partial derivatives tell how to alter the true surface normal at each point on the surface to make the object appear as if it were deformed by the height function.

Since the actual shape of the object does not change, the silhouette edge of the object will not change. Bump Mapping also assumes that the Illumination model is applied at every pixel (as in Phong Shading or ray tracing).

Sphere w/Diffuse Texture

Swirly Bump Map

Sphere w/Diffuse Texture & Bump Map

Page 41: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Bump Map Examples

Cylinder w/Diffuse Texture Map

Bump Map

Cylinder w/Texture Map & Bump Map

Page 42: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

One More Bump Map Example

Notice that the shadow boundaries remain unchanged.

Page 43: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Displacement Mapping

We use the texture map to actually move the surface point. This is called displacement mapping. How is this fundamentally different than bump mapping?

The geometry must be displaced before visibility is determined.

Page 44: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Environment Maps

We use the direction of the reflected ray to index a texture map.

We can simulate reflections. This approach is not completely accurate. It assumes that all reflected rays begin from the same point, and that all objects in the scene are the same distance from that point.

Page 45: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Environment Mapping

Page 46: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Environment Mapping Environment mapping produces

reflections of its environment on shiny objects

Texture is transferred in the direction of the reflected ray from the environment map onto the object

In principle, trace a ray from eye to a point P on the object surface, determine the reflection ray, and then trace the reflection ray until it hits the surrounding sphere or cube. The color from environment map at this hit-point will be placed at point P

Reflected ray: R=2(N·V)N-V. R is used to index the environment map.

Object

ViewerReflected ray

Environment Map

Page 47: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Environment Mapping The environment map may take one of several forms:

Cubic mapping: map resides on 6 faces of a cube Spherical mapping: map resides on a sphere surrounding the

object The map should contain a view of the world with the point of

interest on the object as the eye We can’t store a separate map for each point, so one map is

used with the eye at the center of the object Introduces distortions in the reflection, but the eye doesn’t notice Distortions are minimized for a small object in a large room

The object will not reflect itself The mapping can be computed at each pixel, or only at the

vertices

Page 48: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Sphere Mapping

Implemented in hardware Single texture map Problems:

Highly non-uniform sampling Highly non-linear mapping

Page 49: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Cubic Mapping

The map resides on the surfaces of a cube around the object Typically, align the faces of the cube with the coordinate axes

To generate the map: For each face of the cube, render the world from the center of the

object with the cube face as the image plane Rendering can be arbitrarily complex (it’s off-line)

Or, take 6 photos of a real environment with a camera in the object’s position Actually, take many more photos from different places the object might be Warp them to approximate map for all intermediate points

Remember Terminator 2? http://developer.nvidia.com/object/cube_map_ogl_tutorial.html

Page 50: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Cubic Map Example

Page 51: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

Indexing Cubic Maps Assume you have R and the

cube’s faces are aligned with the coordinate axes, and have texture coordinates in [0,1]x[0,1] How do you decide which

face to use? How do you decide which

texture coordinates to use? What is the problem using

cubic maps when texture coordinates are only computed at vertices?

Page 52: Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere

OpenGL Implementation We can use automatically generated texture coordinates in

OpenGL// Build the environment as a texture object// Automatically generate the texture coordinatesglTexGenf(GL_S, GL_TEXTURE_GEN_MODE,

GL_SPHERE_MAP);glTexGenf(GL_T, GL_TEXTURE_GEN_MODE,

GL_SPHERE_MAP);glEnable(GL_TEXTURE_GEN_S);glEnable(GL_TEXTURE_GEN_T);// Bind the environment texture…// Draw object…

Example