Texture target
Texture environment
Texture coordinate
Texture parameter
(filters, wrap)
Texture object
Texture transformation
TexGen
Multitexture
Applications
Light map
3D textures
Projective texture
Environment map
Specular map
Fall 2013 Revised 2
Specify (create) the texture
Set texture parameters :
Indicate how the texture is to be applied to each pixel
Enable texture mapping
Draw the scene, supplying both texture and geometric coordinates
Works only in RGB mode
Fall 2013 Revised 3
glTexImage1D() glTexImage2D() glTexImage3D()
Fall 2013 Revised 4
glTexImage2D
(GLenum target, GLint level, GLint components,
GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid * pixels )
target
:
GL_TEXTURE_2D
level : LOD number (0: base image) components
: number of color components
(1|2|3|4)
Format : pixel data format
Border: 0 or 1
Width & height
2 m + 2(border bit) w and h can be different
There are new extensions that removes this restriction
Fall 2013 Revised 5
In GL version 1.1 or greater, null pointer. pixels may be a
In this case texture memory is allocated to accommodate a texture of width height width
. You can then download and height subtextures to initialize this texture memory.
The image is undefined if the user tries to apply an uninitialized portion of the texture image to a primitive.
Fall 2013 Revised 6
glTexEnv{if}{v}(GLenum target,
GLenum pname, TYPEparam);
Setting how textures are to be interpreted:
Target : GL_TEXTURE_ENV
Pname: GL_TEXTURE_ENV_MODE
Param: modes
(DECAL|REPLACE|MODULATE|BLEND|ADD|…)
Fall 2013 Revised 7
GL_REPLACE
GL_MODULATE (default)
GL_DECAL
GL_BLEND
New environment modes:
GL_ADD: C v
= C f
+ C t
GL_COMBINE (ARB, see here )
Fall 2013 Revised 8
Color of polygon affects the display of texture
Tree: (r,g,b,a) a cutout
Polygon: (1,0,0)
= 0
Fall 2013 Revised 9
Use with: glTexEnvfv
(GL_TEXTURE_ENV,
GL_TEXTURE_ENV_COLOR, colorPtr)
Fall 2013 Revised
C c
: texture environment color
10
Appearance solely determined by texture
Tree: (r,g,b,a) a cutout
Polygon: (1,0,0)
= 0
Fall 2013 Revised
FOR TEXTURE
CUT-OUTS
11
Tree: (r,g,b,a) a cutout
Polygon: (1,0,0)
= 0
Cp: replace
RGB: DECAL=REPLACE
Fall 2013 Revised 12
To show fragment color, use
GL_MODULATE
Apply specular color AFTER texture mapping:
glLightModeli
(GL_LIGHT_MODEL_COLOR_CONTROL,
GL_SEPARATE_SPECULAR_COLOR);
GL_SEPARATE_SPECULAR_COLOR
See FAQ 21.040
Fall 2013 Revised
GL_SINGLE_COLOR
13
Texture coordinate:
Associate texture location (in texture space) with vertices in the polygon glTexCoord2i (s, t); glVertex2i (x, y);
Order cannot be reversed!
[Think of TexCoord as state assignment]
Fall 2013 Revised 14
Most quadric primitives have default setting for texture coordinates
To turn on the default setting:
gluQuadricTexture
(qobj, GL_TRUE)
Fall 2013 Revised 15
Fall 2013 Revised 16
Each fragment got its texture coordinates from interpolation
Then via table lookup , obtain its (interpolated) color values.
Fall 2013 Revised 17
(1,1)
(0,.75) linear
(1,0)
Polygon (in screen
space) and texture coordinates
Interpolate (s,t) and lookup (r,g,b,a) in the texture map
(0,.75)
Filters nearest
(1,1) rasterization
Fall 2013 Revised
Texture map (4x4) (1,0)
18
1
Minification
Magnification
Texels
Pixels
Nature of problem:
Mismatch between texels and pixels
Pixels
Fall 2013 Revised 19
Magnification
GL_NEAREST
GL_LINEAR
Nearest: in Manhattan distance to the center of the pixel
Chooses the mipmap that most closely matches the size of the pixel being textured
Minification
GL_NEAREST,
GL_LINEAR,
GL_NEAREST_MIPMAP_NEAREST
GL_LINEAR_MIPMAP_NEAREST
GL_NEAREST_MIPMAP_LINEAR
GL_LINEAR_MIPMAP_LINEAR
Chooses the two mipmaps that most closely match the size of the pixel being textured
Fall 2013 Revised 20
GL_LINEAR
Interpolate the texels
More time consuming but more accurate
GL_NEAREST
Snap to the nearest texel
Fall 2013 Revised 21
without mipmap
Fall 2013 Revised with mipmap
23
Mipmapping was invented by Lance
Williams in 1983 and is described in his paper Pyramidal parametrics .
The most popular method of anti-aliasing for textures
‘Mip’ stands for “ multum in parvo ” = “many things in a small place”
The texture is downsampled to a quarter of the original area.
Level 3 d = the Level Of Detail (LOD)
Level 2
If a pixel covers
2 2
2 2 texels , go to level 2 and get a texel .
2 1
2 1 , level 1
Level 1
2 m-1
2 n-1
Level 0
2 m
m, n
2 n
∈
Fall 2013 Revised
+
Level 0 Texture 24
glTexParameteri( GL_TEXTURE_2D,
GL_TEXTURE_MIN_FILTER, GL_LINEAR,
GL_NEAREST, GL_LINEAR_MIPMAP_LINEAR,
GL_LINEAR_MIPMAP_NEAREST,
GL_NEAREST_MIPMAP_LINEAR,
GL_NEAREST_MIPMAP_NEAREST );
Remarks:
Mipmap selection is done per pixel
Using not-yet-ready mipmap disables the texture mapping function.
Fall 2013 Revised 26
gluBuild2DMipmaps
Storage overhead
1
1
4
1
4
2
1
1
1
4
4
3
Fall 2013 Revised 27
When the texture coordinates are outside
[0,1] glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_WRAP_S,param); glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_WRAP_T,param); param: GL_CLAMP|GL_REPEAT
Fall 2013 Revised 28
GL_CLAMP
use the border texel
Diagonal: corner texel
GL_REPEAT
repeat the texels in
[0,1]
Fall 2013 Revised 29
A texture object stores texture data and makes it readily available
You can now control many textures and go back to textures that have been previously loaded into your texture resources
Using texture objects is usually the fastest way to apply textures, resulting in big performance gains
it is almost always much faster to bind (reuse) an existing texture object than it is to reload a texture image using glTexImage*D()
Fall 2013 Revised 30
glGenTextures
Allocate n textures (integer identifiers) glBindTexture
Bind the current texture to specified identifier glDeleteTextures
Free the allocated texture identifiers
Reverse of glGenTextures
Fall 2013 Revised 31
Fall 2013 Revised checker.c
32
texbind.c
Once created, a named texture may be re-bound to the target of the matching dimensionality as often as needed. It is usually much faster to use glBindTexture to bind an existing named texture to one of the texture targets than it is to reload the texture image using glTexImage1D, glTexImage2D, or glTexImage3D.
33
Texture properties saved in texture objects:
Minification and magnification filters, wrapping modes, border color and texture priority
When a texture object is bound again, one may edit the contents of the bound texture object. Any commands that change the above properties will change the currently bound texture as well as the current texture state.
Texture environment, texgen, etc. are NOT stored in texture objects
These settings need to be repeated after texture binding.
Fall 2013 Revised 34
Features
Build mipmap
Load and bind 2D textures
Load image data (image loader)
Handle transparent image (cut-out texture)
TEXTURE ID IS
RETURNED TO YOU
BY PNG LOADER
Important API
id = pngBind(filename, mipmap, trans, info, wrapst, minfilter, magfilter)
pngSetStencil(red, green, blue) pngSetStandardOrientation(1)
OpenGL: origin (0,0) is at lower left corner pngLoad(filename, mipmap, trans, info)
Fall 2013 Revised 35
Texture coordinates are multiplied by a 4 by 4 matrix before any texture mapping occurs.
Texture animation: using this, you can make the texture slide over the surface, rotate around it, stretch and shrink, …
All matrix operations apply: Push/Pop/Mult/ …
Fall 2013 Revised 36
smoke cloud explode water
Fall 2013 Revised fire 37
Automatic
ture Coordinate
eration void glTexGen{ifd}{v}(GLenum coord, GLenum
pname, TYPEparam);
Coord: GL_S, GL_T, GL_R, GL_Q
Pname: GL_TEXTURE_GEN_MODE
Type: GL_OBJECT_LINEAR, GL_EYE_LINEAR, or
GL_SPHERE_MAP
Used when the tex coords are too cumbersome to specify, or will be changed with time.
Texture environment, texgen, etc. are NOT stored in texture objects.
Fall 2013 Revised 38
Continuous color gradation
(0,1/32,2/32, … , 31/32)
Fall 2013 Revised
Discrete white lines
(0,0,0,1,0,0,0,1, … )
39
for a vertex with object coordinates (x o
,y o
,z o
,w o
), generated coordinate = p
1 x o
+ p
2 y o
+ p
3 z o
+ p
4 w
Meaning: when p
1
, p o
2 p
3 coord corresponds to signed distance
, are normalized, this
Similarly, EYE_LINEAR applies to eye coordinates
(0,0,-1,0) (x o
,y o
,z o
,w o
)
Initially, all texture generation functions are set to GL_EYE_LINEAR and are disabled. Both s plane equations are (1, 0, 0, 0), both t plane equations are (0, 1, 0, 0), and all r and q plane equations are (0, 0, 0, 0).
Fall 2013 Revised 40
ref
Computing this distance in eye coordinates is a little tricky, since the plane and vertex must first be transformed to eye coordinates. if M is the modelview matrix at the time glTexGenfv( GLX,
GL EYE PLANE, plane ) is called , then the transformed vertex V’ is V’ = M V, let’s call V’ = ( x e
, y e
, z e
, w e
). The plane must also be transformed to get a new plane
A’x +
(
B’y + C’z + D’w = 0 We get ( A’,B’,C’,D’ ) =
A,B,C,D )M − 1 and M is the modelview matrix when glTexGen is invoked . The texture coordinate is then computed as
A’x e
+ B’y e
+ C’z e
+ D’w e
.
Fall 2013 Revised 41
a
x e y e z w e e
b
M c
x o y o z o w o d
, M
: a modelview b c d matrix
M
1 tex coord
a
b
c
d
x e y e z e w e
Fall 2013 Revised 42
Another mode of
TexGen, SphereMap , will be explain in the
“Environment Map” note.
Fall 2013 Revised 43
What this is about …
Extension Wrangler ( GLEW )
Fall 2013 Revised 44
Include <gl/glew.h> before <gl/glut.h>
Remember to add glewInit()
After the first glutWindow is created
Version of glew library
Sometimes you’ll see this error message when you run the program
It is because the static library (in the EXE) and the
DLL files are not the same version.
Recompiling the application solves the problem
Fall 2013 Revised 47
ref
(First) ARB-approved extension (1998)
Support up to 32 texture units*
Texture matrix and texgen commands affect the active texture environment
* glGetIntegerv (GL_MAX_TEXTURE_UNITS, &units);
My platform has only 4! OpenGL 3.1 requires minimum of 16 texture units
48
OpenGL's multitexture support requires that every texture unit be fully functional and maintain state that is independent of any other texture units.
Each texture unit has its own texture coordinate generation state, texture matrix state, texture enable state, and texture environment state.
However, each texture unit within an OpenGL context shares the same set of texture objects.
Fall 2013 Revised 49
Fall 2013 Revised 50
When the GL_ARB_multitexture extension is supported, glTexGen set the texture generation parameters for the currently active texture unit, selected with glActiveTexture.
TexUnit0: texture2D;(s,t) given
TexUnit1: texture1D; texGen
51 Fall 2013 Revised
A better way of doing it: Render-to-texture (RTT) with framebuffer object (FBO)
CopyTexSubImage2D
Replace a rectangular portion of 2D texture with pixels from the current READ_BUFFER
This involves a readback from GPU to CPU.
Can you think of a way to move the dynamic texture around?!
Fall 2013 Revised 52
Replace part of the original image as new subimage
Width and size of image need not be 2 n
E.g., dynamic texture glTexSubImage2D (target, level, xoffset, yoffset, width, height, format, type, pixels)
Fall 2013 Revised 53
Similar to glCopyPixels void glCopyTexImage2D(target, level, internalformat, x, y, width, height, border);
defines a two-dimensional texture image with pixels from the current GL_READ_BUFFER.
void glCopyTexSubImage2D(target, level, xoffset, yoffset, x, y, width, height); doc
replaces a rectangular portion of a two-dimensional texture image with pixels from the current GL_READ_BUFFER
(rather than from main memory, as is the case for glTexSubImage2D).
void glGetTexImage(target, level, format, type, void
*pixels );
Get the content in the texture target into the array
Fall 2013 Revised 54
Fall 2013 Revised 55
Fall 2013 Revised 56
Quake ( 雷神之錘 , id software) was the first computer game to use light map
Fall 2013 Revised 57
Fall 2013 Revised 58
Spherical specular map obtained on a highly tesselated sphere`
Apply this spheremap as “specular texture” on the teapot
Fall 2013 Revised 59
Fall 2013 Revised 60
Fall 2013 Revised 61