CS考试辅导 COSC363

4 Texture Mapping
R. Mukundan Department of Computer Science and Software Engineering University of Canterbury, .

Basic Texture Mapping

Copyright By PowCoder代写 加微信 powcoder

Basic texture mapping refers to the process of applying an image or a set of images to an object or a primitive.
 Adds colour based surface features to polygons  Makes objects and scenes appear more realistic  Brings 3D objects to life!

Texture Mapping Basic Shapes: Examples

Texture Mapping Models: Examples

Advanced Applications
 Environment Mapping: Simulates reflections in an object that suggest the “world” surrounding that object.
 Billboarding: View oriented texture mapped polygons commonly used in place of models of trees.
 Bump Mapping: Simulates surface displacements without modifying the geometry, to create the appearance of bumps and wrinkles.

Colour image
3 bytes per pixel Pixel depth: 24bpp GL_RGB

Colour image + alpha
4 bytes per pixel Pixel depth: 32bpp

Image Types Used for Texture Mapping

Texture Mapping: Step 1
Generate texture Ids (also referred to as texture names).
 A texture Id is an unsigned integer value (or values) obtained
by calling the function glGenTextures.
 The texture Ids are then used in the function
glBindTexture to specify the texture in use. Example: 1 Texture
Gluint texId;
glGenTextures(1, &texId); glBindTexture(GL_TEXTURE_2D, texId); …
Gluint texId[3];
glGenTextures(3, texId); glBindTexture(GL_TEXTURE_2D, texId[0]); …
glBindTexture(GL_TEXTURE_2D, texId[1]); …
glBindTexture(GL_TEXTURE_2D, texId[2]); …
Example: 3 Textures

Texture Mapping: Step 2
Load image data:
glTexImage2D (GL_TEXTURE_2D, 0,
n, //No. of colour components(1, 3, 4) wid, //Image width, a power of 2
hgt, //Image height, a power of 2
0, //Border
format, //GL_LUMINANCE,GL_RGB or GL_RGBA type, //GL_UNSIGNED_BYTE
imgData // Pointer to image data
We require a function that extracts pixel data from an image file.
Examples of image sizes: 256×256, 1024×512, 512×64
Please use low resolution images (1024×1024 or lower).
4K and 8K resolutions are required only for UHD display devices.

Loading Textures in TGA format
loadTGA(“Scene.tga”);
glTexImage2D(…)
#include “loadTGA.h”
Gluint texId;
glGenTextures(1, &texId); glBindTexture(GL_TEXTURE_2D, texId); loadTGA(“Scene.tga”);
Note: TGA Images must be stored in uncompressed format.
When exporting/saving a file, do not select any form of compression

Loading Textures in BMP format
loadBMP(“Earth.bmp”);
glTexImage2D(…)
#include “loadBMP.h”
Gluint texId;
glGenTextures(1, &texId); glBindTexture(GL_TEXTURE_2D, texId); loadBMP(“Earth.bmp”);
Note: BMP Images must be stored in Windows bitmap format.

Texture Image Types
 Bitmap (.bmp) :
 24 bits per pixel, Windows bitmap.
 Use loadBMP() function included in loadBMP.h (Lab 03)
 Targa (.tga):
 24 bits per pixel, Uncompressed.
 Use loadTGA() function included in loadTGA.h (Lab 03)
 Other formats (.PNG, .JPG etc)
 Option 1: Convert to .bmp or .tga using an image editor (e.g.,
 Option 2: Use image loaders available in image libraries
Eg., http://openil.sourceforge.net/tuts/tut_10/index.htm COSC363

Texture Mapping: Step 3
Set texture sampling parameters:
 Minification and magnification filters (discussed later)  Wrapping mode.
#include “loadTGA.h”
Gluint texId;
glGenTextures(1, &texId);
glBindTexture(GL_TEXTURE_2D, texId);
loadTGA(“Scene.tga”);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

Texture Mapping: Step 4
Enable texturing and assign texture coordinates to vertices.
 Texture coordinates (s, t) are defined in the image space with the origin at the bottom-left corner of the image, and a value 1 at image extremities, independent of image size.
 The user specifies the image region to be mapped to a primitive by associating a pair of texture coordinates with each vertex.

Texture Coordinates
glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, texId);
glBegin(GL_QUADS); glTexCoord2f(0., 0.); glVertex3f(x1, y1, z1); glTexCoord2f(1., 0.); glVertex3f(x2, y2, z2); glTexCoord2f(1., 1.); glVertex3f(x3, y3, z3); glTexCoord2f(0., 1.); glVertex3f(x4, y4, z4);
( x4, y4, z4)
( x1, y1, z1)
( x3, y3, z3)
( x2, y2, z2)

(-20, 0, 5)
(20, 20, 8)
(10, -20, 1)
glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, texId); …
glBegin(GL_TRIANGLES);
glTexCoord2f(0.0, 0.5); glVertex3f(-20, 0, 5); glTexCoord2f(1.0, 0.0); glVertex3f(10, -20, 1); glTexCoord2f(0.5, 1.0); glVertex3f(20, 20, 8);

Texture Coordinates
(6, 6, 4) (5, 4, 4)
 Texture coordinate is a vertex attribute, similar to colour (glColor3f) and normal (glNormal3f).
glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, texId); …
glBegin(GL_TRIANGLES);
glTexCoord2f(0.0, 0.5); glVertex3f(6, 6, 4); glTexCoord2f(1.0, 0.0); glVertex3f(0, 8, 3); glTexCoord2f(0.5, 1.0); glVertex3f(0, 0, 5);
glVertex3f(5, 4, 4); glVertex3f(5, 0, 4); glVertex3f(7, 0, 5);
 An attribute specified for a vertex will remain in effect for subsequent vertices till it is redefined or disabled.

Texture Coordinates
 It is often required to “crop” an image (map only a part of the image) to preserve aspect ratio
glBegin(GL_QUADS); glTexCoord2f(0., 0.2); glVertex3f(x1, y1, z1); glTexCoord2f(1., 0.2); glVertex3f(x2, y2, z2); glTexCoord2f(1., 0.7); glVertex3f(x3, y3, z3); glTexCoord2f(0., 0.7); glVertex3f(x4, y4, z4);

Texture Atlas
 A texture atlas combines a set of images mapped to different parts of a model into a single texture.

Texture Mapping: Step 5
Texture environment parameters
 GL_REPLACE: Texture colour replaces the fragment’s colour
 GL_MODULATE: Texture colour is multiplied by fragment’s colour
Texture Color
Primitive’s Color
GL_REPLACE
GL_MODULATE
#include “loadTGA.h”
Gluint texId;
glGenTextures(1, &texId);
glBindTexture(GL_TEXTURE_2D, texId);
loadTGA(“Scene.tga”);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

Sphere Colour RED
Texture Colour Modulation
GL_REPLACE
Texture replaces the colours on the sphere including lighting effects
GL_MODULATE
Texture colour is modulated with colours on the sphere including lighting effects
GL_MODULATE
If the texture colour and lighting effects
are to be displayed correctly, the surface colour should be set
to white. 20
Sphere Colour RED
Sphere Colour

Texture Colour Modulation
 Using only a single texture, multiple shades of that texture may be produced on a surface by modifying only the material colour
of the surface.
Environment Parameter: GL_MODULATE

Texture Tiling
If the wrap parameter for a texture axis is set to GL_CLAMP, then the coordinate value is clamped to the range [0, 1].
(E.g., a texture coordinate value 1.3 is treated as 1). glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
0 0 Texture 1 COSC363
( x4, y4, z4) (0, 1)
( x1, y1, z1) 11.3
( x3, y3, z3) (3, 1)
(3, 0) ( x2, y2, z2)

Texture Tiling
 Texture coordinates assigned to a vertex can have values greater than 1. Such values can be used for tiling.
 If the wrap parameter for a texture axis is set to GL_REPEAT, then the integer part of the texture coordinate along that axis is ignored. (eg. A value 1.3 is treated as 0.3). This results in the tiling of the image along that axis. [Default]
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
( x4, y4, z4) (0, 1)
( x1, y1, z1) 11.3
( x3, y3, z3) (3, 1)
(3, 0) ( x2, y2, z2)
00 Texture 1 COSC363

Seamlessly Tileable Textures
Texture Coords
(0, 2) (4, 2)
(0, 0) Quad
https://www.textures.com/

Auto Texturing
 Quadric Surfaces have two-parameter representations. The two parameters can be independently mapped to texture coordinates (s, t)
 For example a sphere is given by angles (, ), where −, −/2/2
 For any point (x,y,z) on the sphere, we can have the mapping: (x,y,z) → (, ) → (s, t)
 Auto texturing quadric surfaces: gluQuadricTexture (q, GL_TRUE);
 For generating a texture mapped sphere, use gluSphere() function (not glutSolidSphere())
 A cube generated using glutSolidCube()cannot be texture

Texture Sampling
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_NEAREST) GL_LINEAR
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_NEAREST GL_LINEAR
 The texture has a fixed size, but the projected size of the polygon on the screen may vary based on distance from camera.
 Texture parameters determine how a texture is sampled to obtain a colour value at each pixel of a polygon. Commonly used filter parameters are:
 GL_NEAREST  GL_LINEAR
The pixel value of a texture is often called a “texel”.

GL_NEAREST
Texture Magnification
A small texture mapped to a large polygon
GL_NEAREST: The pixel gets the colour of the texel value nearest to the centre of the pixel.
GL_LINEAR: The pixel gets the weighted average of four texel values closest to the centre of the pixel.

Texture Minification
Texture Polygon
A large texture mapped to a small polygon
GL_NEAREST: The pixel gets the colour of the texel value nearest to the centre of the pixel.
GL_LINEAR: The pixel gets the weighted average of four texel values closest to the centre of the pixel.

Texture Minification
Thin lines often disappear when a texture is mapped to a region containing fewer pixels.
Both GL_NEAREST and GL_LINEAR settings produce similar images

Texture Mipmaps
 MIP = Multum In Parvo = “Much in a small place”
 A mipmap is a set of prefiltered versions of the same image
at different scales (resolutions)
 The problem of disappearing lines when a texture is mapped to a small region can be solved by using a mipmap.
 Mipmapping requires additional processing, and 33% extra texture storage space.

Texture Mipmaps
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_LINEAR)
glTexImage2D(GL_TEXTURE_2D, 0, 3, 64,64, 0, GL_RGB, GL_UNSIGNED_BYTE, img1)
glTexImage2D(GL_TEXTURE_2D, 1, 3, 32,32, 0, GL_RGB, GL_UNSIGNED_BYTE, img2)
glTexImage2D(GL_TEXTURE_2D, 2, 3, 16,16, 0, GL_RGB, GL_UNSIGNED_BYTE, img3)
glTexImage2D(GL_TEXTURE_2D, 6, 3, 1,1, 0, GL_RGB, GL_UNSIGNED_BYTE, img7)

Texturing and Lighting
 Lighting computation is a per-vertex operation, whereas texturing is done later at the fragment processing stage.
 If GL_REPLACE is used as the texturing environment (See slide 11), the colour values got from lighting computation would be replaced with texture colours.
 In order to see the variation of diffuse reflections from the surface, the texture values must be modulated with the already computed fragment colour (GL_MODULATE)
 Modulation will reduce the effect of specular highlights. To get a strong specular highlight on a textured surface, select the following light model:
glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);

 The surrounding environment is displayed as textures on the faces of a large cube, and the cube is rendered centered around the view point.
 Try to minimise perspective distortions by
 Adjusting the focal length (“near” value in gluPerspective)
and the field of view (“fov” value in gluPerspective)
 Adjusting the size of the cube used for texture mapping
 Not moving the camera very close to the four sides of the cube
Skybox Textures

Sky Box Textures

Billboarding
 Billboarding is a technique that changes the orientation of texture mapped quads in a 3D environment based on view direction.
 When a texture is mapped to a quad, only those pixels belonging to the object are rendered. The background of the texture is removed using alpha texturing (next slide)

• Requires an RGBA image.
• Background pixels have alpha value 0.
• Foreground pixels have alpha value 1.
• By enabling alpha testing, we can
selectively map only those pixels where the alpha value greater than zero.
Alpha Texturing
glEnable(GL_TEXTURE_2D); glEnable(GL_ALPHA_TEST); glAlphaFunc(GL_GREATER,0); glBindTexture(GL_TEXTURE_2D, texId); drawBillboard(); glDisable(GL_TEXTURE_2D); glDisable(GL_ALPHA_TEST);

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com