程序代写 CM0304 Graphics I Graphics Hardware I.1 Graphics Systems

CM0304 Graphics I Graphics Hardware I.1 Graphics Systems

CMT107 Visual Computing

Copyright By PowCoder代写 加微信 powcoder

V.1 Texture Mapping

Xianfang Sun

School of Computer Science & Informatics
Cardiff University

➢ Texture mapping

• Texture coordinates

• Aliasing effects and MIP mapping

➢ Bump mapping

➢ Displacement mapping

➢ Light maps

➢ Shadow maps

➢ Texture Mapping in OpenGL

15:15 2CMT107 Visual Computing

From Shading to Texture

15:15 3CMT107 Visual Computing

15:15 4CMT107 Visual Computing

➢ Visual appearance of objects can be enhanced by textures
➢ The concept is simple

Texture Coordinates

15:15 5CMT107 Visual Computing

➢ For each vertex specify texture coordinates (s,t)[0,1]2

• Canonical position of pixel in texture for vertex
• For each point p on the 3D polygon, corresponding

texture coordinates (s,t) are required
→ Bilinearly interpolate texture coordinates in 3D

➢ Texture coordinates for point on quad
p = sa + te

e = b + s(c – b)

p = sa + tb + st(c – b)

→ Solve for (s,t) (assuming (0,0)
is texture coordinate of P1)

Alias Effects

15:15 6CMT107 Visual Computing

➢ One major problem of texture: alias effects
• Caused by undersampling; results in unreal artefacts

Anti-aliasing

15:19 7CMT107 Visual Computing

➢ Similar to untextured images use anti-aliasing technique
➢ Most successful approach: supersampling

• Compute picture at a higher resolution
• Average the supersamples to find pixel colour
• This blurs boundaries, but leaves coherent areas of

colour unchanged
• Works well for polygons, but requires a lot of

computations and does not work for line drawings
➢ Other approaches: convolution filtering

(see image processing)

MIP Mapping

15:19 8CMT107 Visual Computing

➢ Popular technique of precomputing / prefiltering to address
alias effects (MIP = multum in parvo; much in little)

➢ Basic idea: construct a pyramid of images for different
texture sizes (prefiltered and resampled)
• Pick texture image suitable for size or interpolate

between texture images

Generalising Texture Mapping

15:19 9CMT107 Visual Computing

➢ So far: texture is a label (colour) for each pixel
➢ Can use it to modify other things

• E.g. use it for illumination to adjust material properties
(all light types or only some of them)

Material Texture as label Texture as material

Bump Mapping

15:19 10CMT107 Visual Computing

➢ Texture can be used to alter surface normals of an object
• Does not change shape, but illumination computation
• Changes in texture (partial derivatives) tell how to

change the “height” of the normals

Bump Map Example

15:19 11CMT107 Visual Computing

Sphere w/Texture Bump Map Bumpy Sphere

➢ As we do not change the shape, the silhouette does
not change
• Use only for small bumps
• Requires illumination computation for each pixel (Phong

shading, ray tracing, . . . )

Another Bump Map Example

15:19 12CMT107 Visual Computing

Displacement Mapping

15:19 13CMT107 Visual Computing

➢ Use texture map to move surface points

Light Maps

15:19 14CMT107 Visual Computing

➢ In Quake texture and light maps are used
• Light map contains precomputed illumination at low

resolution
• Multiply light map with texture map at run-time

(and cache it)

Only Texture Map Texture and Light Map

Shadow Maps

15:19 15CMT107 Visual Computing

➢ Generate shadows using texture maps
• Render scene from the viewpoint of each light source

and only keep depth buffer values in shadow buffers
• When shading each pixel (illumination computation per

− Compute vector L from visible point to light source

(needed for illumination computation)
− Compute the length of L
− Compare this length with corresponding value in the

shadow buffers
− If the shadow buffer value is less, then the point is in

the shadow and we can ignore the light source

Shadow Map Example

15:19 16CMT107 Visual Computing

Texture Mapping in OpenGL

15:19 17CMT107 Visual Computing

➢ Use Texture and TextureIO to apply a texture
1. Create a texture object using TextureIO

• TextureIO.newTexture(File, boolean);
2. Indicate how the texture is to be applied to each pixel

• Texture.setTexParameteri (…)
3. Draw the scene, supplying both texture and geometric

coordinates; send the coordinates to vertex shader, and
send texture sampler to fragment shader
• Texture.getImageTexCoords().top() …

Texture Mapping in OpenGL

15:19 18CMT107 Visual Computing

➢ Using OpenGL Core functions to apply a texture
1. Create a texture object and specify a texture for that object

• glGenTextures(…)

• glBindTexture(…)

• glTexImage2D(…)
2. Indicate how the texture is to be applied to each pixel

• glTexParameteri(…)
3. Enable texture mapping

• glEnable(GL_TEXTURE_2D)
4. Draw the scene, supplying both texture and geometric

coordinates; send the coordinates to vertex shader, and
send texture sampler to fragment shader

➢ Step 0: Read in texture image

Texture Object

15:19 19CMT107 Visual Computing

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

➢ Generate identifiers for texture objects first
int 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

➢ Specify texture image
glTexImage2D(target, level, internalFormat,

width, height, border, format, type, data);

Texture Object Example Code

15:19 20CMT107 Visual Computing

int texids[] = new int[1];

ByteBuffer texImg = readImage(“textures/Day.png”);

glGenTextures(1, texids, 0);

glBindTexture(GL_TEXTURE_2D, texids[0]);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,

texWidth, texHeight,0, GL_BGR,

GL_UNSIGNED_BYTE, texImg);

Texture Parameters

15:19 21CMT107 Visual Computing

➢ 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
• Environment parameters determine how texture

mapping interacts with shading
• Mipmapping allows us to use textures at multiple

resolutions
➢ OpenGL Command

glTexParameterf(target, pname, param);
• target: Specifies the target texture
• pname: Specifies the symbolic name of a single-valued texture parameter
• param: Specifies the value of pname.

Wrapping Modes

15:19 22CMT107 Visual Computing

➢ Repeat: use s,t modulo 1

➢ Clamp: if s,t > 1 use 1, if s,t <0 use 0 • glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ) • glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) • GL_CLAMP_TO_BORDER, GL_MIRRORED_REPEAT… GL_CLAMP_TO_EDGE Texture Filtering 15:19 23CMT107 Visual Computing ➢ 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 ➢ 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, … Shading and Texture Interaction 15:19 24CMT107 Visual Computing • You can specify how the texture-map colors are used to modify the pixel colors by setting environment parameters in old version of OpenGL glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE, mode); mode values: • GL_REPLACE: replace pixel color with texture color • 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 (Default) • More on OpenGL programming guide • In the shader version of OpenGL, the interaction should be implemented in the fragment shader. Assign Texture Coordinates 15:19 25CMT107 Visual Computing ➢ 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 ➢Texture.getImageTexCoords() can be used to retrieve texture coordinates (s, t) = (0.2, 0.8) (0.4, 0.2) (0.8, 0.4) Texture Space Object Space Typical Code in Main Program 15:19 26CMT107 Visual Computing // Set the texture to be used texture = TextureIO.newTexture(new File("WelshDragon.jpg"), false); } catch (IOException ex) { Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex); // Set texture coordinates float[] texCoord ={…}; FloatBuffer textures = FloatBuffer.wrap(texCoord); gl.glGenBuffers(…); gl.glBindBuffer(…); gl.glBufferData(…); gl.glBufferSubData(…); // Send texture coordinates to vertex shader vTexCoord = gl.glGetAttribLocation( program, "vTexCoord" ); gl.glEnableVertexAttribArray( vTexCoord ); gl.glVertexAttribPointer( vTexCoord, 2, GL_FLOAT, false, 0, offsetSize); // Set the fragment shader texture sampler variable gl.glUniform1i( gl.glGetUniformLocation(program, "tex"), 0 ); Vertex Shader 15:19 27CMT107 Visual Computing #version 330 core layout(location = 0) in vec4 vPosition; layout(location = 1) in vec3 vColour; layout(location = 2) in vec2 vTexCoord; out vec4 color; out vec2 texCoord; uniform mat4 ModelView; uniform mat4 Projection; void main() gl_Position = Projection * ModelView * vPosition; texCoord = vTexCoord; color.rgb = vColour; color.a = 1.0; Fragment Shader 15:19 28CMT107 Visual Computing #version 330 core in vec4 color; in vec2 texCoord; out vec4 fColor; uniform sampler2D tex; void main() fColor = color* texture( tex, texCoord ); More details in the Labs... 15:19 29CMT107 Visual Computing ➢ Describe the principle of texture maps. What are texture coordinates and how are they related to 3D coordinates? ➢ What options do exist to generalise texture maps? For what other effects are they useful and what are the advantages and disadvantages of these techniques? ➢ How to program texture mapping in OpenGL? 程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com