Computer Graphics Course Computer Graphics Course 2006 2006 OpenGL OpenGL Texture Mapping Texture Mapping
Based on the slides from the University of Virgina CG course and Angel’s “Interactive Computer Graphics”, Addison-Wisley 2005
Computer Graphics Course Computer Graphics Course 2006 2006 - - PowerPoint PPT Presentation
Computer Graphics Course Computer Graphics Course 2006 2006 OpenGL OpenGL Texture Mapping Texture Mapping Based on the slides from the University of Virgina CG course and Angels Interactive Computer Graphics, Addison-Wisley 2005
Based on the slides from the University of Virgina CG course and Angel’s “Interactive Computer Graphics”, Addison-Wisley 2005
Texture Mapping Texture Mapping
Texture map is an image, two-
dimensional array of color values ( color values (texels texels) )
Texels are specified by texture are specified by texture’ ’s (u,v) space s (u,v) space
At each screen pixel, texel texel can be used to substitute a can be used to substitute a polygon polygon’ ’s surface property (color) s surface property (color)
We must map (u,v) space to polygon’ ’s (s, t) space s (s, t) space
U V S T
Texture Mapping Texture Mapping
(u,v) to (s,t) mapping can be explicitly set at vertices by storing vertices by storing texture coordinates texture coordinates with each with each vertex vertex
How do we compute (u,v) to (s,t) mapping for points in between points in between
Watch for aliasing
Watch for many to one mappings
Watch for perspective foreshortening effects and linear interpolation linear interpolation
Texture coordinates Texture coordinates
To tell OpenGL how to map texture, it is necessary to tell it a texture coordinate for each vertex 3-D vertex coordinates 2-D texture coordinates
OpenGL textures OpenGL textures
OpenGL textures have dimensions (x and y [and z]) are powers of 2 512 256 256 256 64 512 If the image you want to use as a texture isn’t sized like this, you can either stretch it appropriately before using it as an OpenGL texture,
320 240 512 256 512 256
Texture coordinate values Texture coordinate values
512 256 256 256 64 512 All OpenGL textures span the range [0,1] in both x and y 512 256
0.0 1.0 0.0 1.0 0.0 1.0 0.0 1.0 0.0 1.0 1.0 0.0 0.0 1.0 0.0 1.0 0.625 0.9375
320 240
Unnecessary Limitation Unnecessary Limitation
Extensions
GLEW
Example Texture Map Example Texture Map
glTexCoord2d(1,1); glVertex3d (s, s, s); glTexCoord2d(0, 0); glVertex3d (-s, -s, -s);
Example Texture Map Example Texture Map
glTexCoord2d(5, 5); glVertex3d (s, s, s); glTexCoord2d(1, 1); glVertex3d (s, s, s); Repeating textures vs. Clamped textures
Texture Coordinates Texture Coordinates
Every polygon can have object coordinates and texture coordinates texture coordinates
Object coordinates describe where polygon vertices are on the screen are on the screen
Texture coordinates describe texel texel coordinates of coordinates of each vertex (usually 0 each vertex (usually 0 -
> 1)
Texture coordinates are interpolated along vertex-
vertex edges
glTexCoord{1234}{sifd}(TYPE coords coords) )
Why 14 coords? s, t, r, and q (for homogeneous coordinates, an advanced topic)
Texture objects Texture objects
Since version 1.2, OpenGL has supported the idea of texture objects This allows multiple, independent textures to be used One texture at a time is active As usual we start with glEnable(GL_TEXTURE_2D); Texture objects are created, deleted and each is given a name (an unsigned integer identifier) 1 2 3
1-D & 3-D textures are possible
Create Texture Objects Create Texture Objects
glGenTextures(1, &texture[texture_num]); &texture[texture_num]);
First argument tells GL how many Texture Objects to create Objects to create
Second argument is a pointer to the place where OpenGL will store the names (unsigned integers) OpenGL will store the names (unsigned integers)
texture[ ] is of type GLuint GLuint
Specify which texture Specify which texture
Tell OpenGL that you are going to define the specifics of the Texture Object it created specifics of the Texture Object it created
glBindTexture(GL_TEXTURE_2D, texture[texture_num]); texture[texture_num]);
Textures can be 1D and 3D as well
Begin defining texture Begin defining texture
glTexParameter() ()
Sets various parameters that control how a texture is treated as it as it’ ’s applied to a fragment or stored in a texture object s applied to a fragment or stored in a texture object
// scale linearly when image bigger than texture glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE _MAG_FILTER,GL_LINEAR); _MAG_FILTER,GL_LINEAR);
// scale linearly when image smaller than texture glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE _MIN_FILTER,GL_LINEAR); _MIN_FILTER,GL_LINEAR);
Wrapping Mode Wrapping Mode
Clamping: if Clamping: if s,t > 1 s,t > 1 use 1, if use 1, if s,t <0 s,t <0 use 0 use 0 Wrapping: use Wrapping: use s,t s,t modulo 1 modulo 1
glTexParameteri glTexParameteri( GL_TEXTURE_2D, ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP ) GL_TEXTURE_WRAP_S, GL_CLAMP ) glTexParameteri glTexParameteri( GL_TEXTURE_2D, ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ) GL_TEXTURE_WRAP_T, GL_REPEAT ) texture s t GL_CLAMP wrapping GL_REPEAT wrapping
Assign image data Assign image data
glTexImage2D();
GL_TEXTURE_2D GL_TEXTURE_2D (2D Texture) (2D Texture) (level of detail 0) (level of detail 0) 3 3 (3 components, RGB) (3 components, RGB) image1 image1-
>sizeX sizeX (size) (size) image1 image1-
>sizeY sizeY (size) (size) (no border pixel) (no border pixel) GL_RGB GL_RGB (RGB color order) (RGB color order) GL_UNSIGNED_BYTE GL_UNSIGNED_BYTE (unsigned byte data) (unsigned byte data) image1 image1-
>data (pointer to the data)) (pointer to the data))
glTexImage2D glTexImage2D – – Arg Arg 1 1
GLenum target target
GL_TEXTURE_{1|2|3}D
That’ ’s what you will use for the exercise s what you will use for the exercise
GL_TEXTURE_RECTANGLE_ARB
Extensions
That’ ’s what you will use for the fun at your home machine s what you will use for the fun at your home machine
GL_PROXY_TEXTURE_2D
Provides queries for texture resources
Proceed with hypothetical texture use (GL won’ ’t apply the texture) t apply the texture)
After query, call GLGetTexLevelParamter GLGetTexLevelParamter to verify presence of to verify presence of required system components required system components
Doesn’ ’t check possibility of multiple texture interference t check possibility of multiple texture interference
glTexImage2D glTexImage2D – – Arg Arg 2 2
GLint level level
Used for Level of Detail (LOD)
LOD stores multiple versions of texture that can be used at runtime (set of sizes) used at runtime (set of sizes)
Runtime algorithms select appropriate version of texture texture
Pixel size of polygon used to select best texture
Eliminates need for error-
prone filtering algorithms
glTexImage2D glTexImage2D – – Arg Arg 3 3
GLint internalFormat internalFormat
GL defines 38 symbolic constants that describe which of R, G, B, and A are used in internal representation of G, B, and A are used in internal representation of texels texels
GL_RGB, GL_RGBA
Provides control over things texture can do
High bit depth alpha blending
High bit depth intensity mapping
General purpose RGB
GL doesn’ ’t guarantee all options are available on given t guarantee all options are available on given hardware hardware
glTexImage2D glTexImage2D – – Args Args 4 4-
6
GLsizei width width
GLsizei height height
Dimensions of texture image
Must be 2m
m + 2b (b=0 or 1 depending on border)
+ 2b (b=0 or 1 depending on border)
min, 64 x 64
GLint border border
Width of border (1 or 0)
Border allows linear blending between overlapping textures
Useful when manually tiling textures
glTexImage2D glTexImage2D – – Args Args 7 & 8 7 & 8
GLenum format format
Describe how texture data is stored in input array
GL_RGB, GL_RGBA, GL_BLUE… …
GLenum type type
Data size of array components
GL_SHORT, GL_BYTE, GL_INT… …
glTexImage2D glTexImage2D – – Arg Arg 9 9
Const GLvoid GLvoid * *texels texels
Pointer to data describing texture map
Apply texture Apply texture
Before defining geometry
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture[0]); texture[0]);
glTexEnvf(GL_TEXTURE_ENV, , GL_TEXTURE_ENV_MODE, GL_REPLACE); GL_TEXTURE_ENV_MODE, GL_REPLACE);
GL_TEXTURE_ENV is the only possible value value
glTexEnv glTexEnv() ()
GL_TEXTURE_ENV_MODE GL_TEXTURE_ENV_MODE GL_DECAL GL_DECAL (alpha blends texture with
(alpha blends texture with poly color) poly color)
GL_REPLACE GL_REPLACE (straight up
(straight up replacement) replacement)
GL_MODULATE GL_MODULATE (texture application
(texture application is a function of poly lighting) is a function of poly lighting)
GL_BLEND GL_BLEND (texture controls blending
(texture controls blending with another color) with another color)
If GL_BLEND selected, second call to If GL_BLEND selected, second call to glTexEnv glTexEnv() must specify () must specify GL_TEXTURE_ENV_COLOR GL_TEXTURE_ENV_COLOR 4 4-
float array for R,G,B,A blend
First argument to function is always GL_TEXTURE_ENV
gluScaleImage gluScaleImage() ()
Alters the size of an image to meet the 2m
m size
size requirement of OpenGL requirement of OpenGL
Scaling performed by linear and box filtering
glCopyTexImage2D() glCopyTexImage2D()
Use current frame buffer contents as texture
Copy frame buffer to named texture location
glTexSubImage2D() glTexSubImage2D()
Replace a region of current working texture with a smaller texture a smaller texture
SubImage need not adhere to 2 need not adhere to 2m
m size limitation
size limitation
This is how you add data from your system’ ’s s camera to GL environment camera to GL environment
glCopyTexSubImage2D
Frame buffer cut and paste possible too
Filtering Filtering
OpenGL tries to pick best mipmap mipmap level level
Question: Which Which texel texel corresponds to a particular corresponds to a particular pixel? pixel?
GL_NEAREST (Point Sampling)
Pick the texel texel with center nearest pixel with center nearest pixel
GL_LINEAR (Bilinear Sampling)
Weighted average of 2x2 closest texels texels
GL_NEAREST_MIPMAP_LINEAR
Average nearest texels texels from two from two mipmap mipmap levels levels
GL_LINEAR_MIPMAP_LINEAR (Trilinear Trilinear) )
Average two averaged texels texels from two from two mipmaps mipmaps
Texture Mapping Texture Mapping
More details
Mip mapping mapping
Perspective correction hint
Environment mapping
Different data layouts
…
Practical advice
Glance at the existing source code (Red Book is a good place to start) good place to start)