460 likes | 579 Views
The Limits of Geometric Modeling. Modeling surfaces with complex surface finishes is too complex if we simply try to do it with uniform colored polygons! For example: a book’s cover painting of Mona Lisa. Textured Map Scene. Modeling an Orange. Start with an orange-colored sphere
E N D
The Limits of Geometric Modeling • Modeling surfaces with complex surface finishes is too complex if we simply try to do it with uniform colored polygons! • For example: • a book’s cover • painting of Mona Lisa
Modeling an Orange • 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
Modeling an Orange • Take a picture of a real orange, scan it, and “paste” onto simple geometric model • This process is texture mapping • Still might not be sufficient because resulting surface will be smooth • Need to change local shape • Bump mapping
Three Types of Mapping • Texture Mapping • Uses images to fill polygons • Environmental (reflection mapping) • Uses a picture of the environment for texture maps • Allows simulation of highly specular surfaces • Bump mapping • Emulates altering normal vectors during the rendering process
Texture Mapping geometric model texture mapped
Texture Mapping • Map 2D texture (any digitized image) onto clipped, visible parts of a surface.
Texture Mapping Examples All based on same 2D checkerboard texture
Texture Mapping Image may come in different formats • But can be read and stored as a 2D array of RGB values
Texture Image Assume we have an NxN image • OpenGL requires N to be a power of 2 • Typically, images are indexed using upper left is the origin N Individual RGB pixel values of the texture image are called texels N
Texture Space • Rather than thinking of the image as an array, we will think of it as a function T that maps a point (s,t) to an RGB value 0 <= s, t <= 1 • s,t : texture coordinates • Given any (s,t) in [0,1], the texture image defines the value of T(s,t)
Texture Space T(s,t)=Im[round((1-t)(N-1))][round(s(N-1))] Texel (0,0) (1,1) t (0,0) s NxN Image Im Texture space: (s,t) coordinates
Wrapping • We wish to wrap this 2D texture image on to a 2D surface • But, the surface resides in 3D
Is it Simple? • Although the idea is simple – map an image to a surface – there are 3 or 4 coordinate systems involved 2D image 2D surface residing in 3D
Texture Mapping parametriccoordinates texture coordinates screen coordinates world coordinates
Mapping Functions • Basic problem is how to find the maps • Consider mapping from texture coordinates to a point a surface • Appear to need three functions • x = Fx(s,t) • y = Fy(s,t) • z = Fz(s,t) • But we really want to go the other way (x,y,z) t s
Inverse Mapping • We really want to go from model to texture • Given a point on an model, we want to know to which point in the texture it corresponds • Need a map of the form s = Fs(x,y,z) t = Ft(x,y,z) • Although these functions exist conceptually, finding them might not be easy (or even possible) in practice
Parameterized Surfaces • We first compute a 2D parameterization of the surface. • Associate each point (x, y, z) in world coordinates with a (u,v) in parameter coordinates • Recall the sphere parameterization we saw earlier • Then, we will have functions • fx(u,v) = x, • fy(u,v) = y, • fz(u,v) = z, and their inverses as well. • Mapping (u,v) -> (s,t) is easier!
Cylindrical Mapping parametric cylinder maps rectangle in (u,v) parameter space to cylinder of radius r and height h in world coordinates (x, y, z) s = u t = v maps from texture space to parameter space
OpenGL Texture Mapping • At this point texture mapping is rather abstract and we will see how OpenGL does it.
Where Does Mapping Take Place? • At the end of the rendering pipeline • At rasterization stage we need the texture value • How do we match points on a surface with texels?
OpenGL Texture Mapping • OpenGL handles this by: • Forcing programmer to define “texture coordinates” for each vertex! • Texture coordinates are “part of state” • Thus, we never define the inverse mapping functions explicitly!
OpenGL Texture Mapping • We define texture coordinates (s,t) for vertices • OpenGL interpolates texture coordinates for intermediate points during rasterization! • We will see more details about this part.
OpenGL Texture Mapping • The process involves a large variety of options! • We will concentrate on 2D texture mapping (3D is also available)
OpenGL Texture Mapping • First thing to do: enable texturing • glEnable(GL_TEXTURE_2D) • Input texture into an array of RGB (or RGBA) values • 3 bytes per pixel • Stored row by row from upperleft to lowerright • Image dimensions should be powers of 2 • See example code for reading a PPM file into an array.
glTexImage2D • Process image into OpenGL internal texture format glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 512, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, image); Image dimensions Texels are in RGB Pointer to image data (name of image array) Internally use RGB Use highest level resolution Type of R, G and B No border
glTexCoord2f • Assign texture coordinates to each vertex • glTexCoord2f(s, t) • Part of state • Texture coordinates for interior points of a polygon is found by interpolation
Example glBegin(GL_POLYGON); glNormal3f(0.0,0.0,1.0); glColor3fv(ground_color); glTexCoord2f(0.0,0.0); glVertex3f(0.0,0.0,0.0); glTexCoord2f(0.0,1.0); glVertex3f(0.0,200.0,0.0); glTexCoord2f(1.0,1.0); glVertex3f(200.0,200.0,0.0); glTexCoord2f(1.0,0.0); glVertex3f(200.0,0.0,0.0); glEnd(); (200,200) (1,1) (0,0) (0,0) polygon Texture space (s,t)
Example (200,400) glBegin(GL_POLYGON); glNormal3f(0.0,0.0,1.0); glColor3fv(ground_color); glTexCoord2f(0.0,0.0); glVertex3f(0.0,0.0,0.0); glTexCoord2f(0.0,1.0); glVertex3f(0.0,400.0,0.0); glTexCoord2f(1.0,1.0); glVertex3f(200.0,400.0,0.0); glTexCoord2f(1.0,0.0); glVertex3f(200.0,0.0,0.0); glEnd(); (1,1) (0,0) Texture space (s,t) (0,0) polygon
GL_REPEAT, GL_CLAMP • How to interpret (s,t) outside of [0,1]? • Repeat texture (GL_REPEAT) Or • Clamp values less than 0 to 0, clamp values greater than 1 to 1 (GL_CLAMP) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
GL_REPEAT glBegin(GL_POLYGON); glNormal3f(0.0,0.0,1.0); glColor3fv(ground_color); glTexCoord2f(0.0,0.0); glVertex3f(0.0,0.0,0.0); glTexCoord2f(0.0,2.0); glVertex3f(0.0,200.0,0.0); glTexCoord2f(2.0,2.0); glVertex3f(200.0,200.0,0.0); glTexCoord2f(2.0,0.0); glVertex3f(200.0,0.0,0.0); glEnd(); (200,200) (1,1) (0,0) (0,0) polygon Texture space (s,t)
Some more detail… • Mapping of a texture to a surface takes place during rasterization • Hence, it is not really applied to the surface but to a pixel, that is the projection of a small region of the surface
Each pixel corresponds to a small region on a surface and a small region of texture space. Texture map t Surface of object s Pixel on projection plane
Depending on the surface, viewer, texture coordinates • Each texel may cover multiple pixels (MAGNIFICATION) • Or, each pixel may cover multiple texels • (MINIFICATION)
GL_NEAREST or GL_LINEAR What does OpenGL do? • Simplest/Fastest: Point sampling Pick center of each pixel (maps to a single point on surface, and in texture space) and use the texture value at that point. Visible aliasing errors: center point does not give information on the rest of the pixel glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
GL_NEAREST or GL_LINEAR (2) Smoother results: Average texels Use average of a group of texels around the point sample to obtain the value of the texture OpenGL uses 2x2 array of neighboring texels glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
GL_MODULATE or GL_DECAL How does texture interact with shading? GL_DECAL: color of pixel=color of texture GL_MODULATE: color of pixel= product of the color of pixel (without the texture) and the color of texture glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
Perspective Correction Perspective projection does not preserve affine combinations, that is Projection followed by wrapping (using affine combination in 2D) IS NOT EQUAL TO wrapping (using affine combination in 3D)followed by projection You can ask OpenGL to employ a better interpolation scheme by: glHint(GL_PERSPECTIVE_CORRECTION, GL_NICEST);